Repository navigation
Expand file tree
/
Copy pathTest.php
More file actions
146 lines (120 loc) · 2.87 KB
/
Copy pathTest.php
File metadata and controls
146 lines (120 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
namespace SOB;
/**
* Abstract interface class for tests to implement
*/
abstract class Test
{
/**
* Don't do work in the constructor
*/
final public function __construct()
{
}
/**
* Close the database connection
*/
abstract public function closeConnection() : void;
public function dbSupported(\SOB\Configuration $config) : bool
{
return true;
}
/**
* class must delete one record with id=$id
*
* @return true if deleted
*/
abstract public function delete(int $id) : bool;
/**
* override to flush buffers between tests if needed
*/
public function flush() : void
{
}
/**
* Get the lines in the schema to load into the database
*
* @return array<string> sql to insert into database
*/
public function getSchemaLines(\SOB\Configuration $config) : array
{
$connection = $config->getPDOConnectionString();
$schema = 'schema';
if ('RedBean' === $config->getNamespace())
{
$schema = 'schema-pkid';
}
$schema .= '.sql';
$file = __DIR__ . '/../northwind/' . $config->getDriver() . '/' . $schema;
if (file_exists($file))
{
$schema = $file;
}
else
{
$file = __DIR__ . '/../northwind/' . $schema;
}
if (\str_contains($connection, 'sqlite') && ! \str_contains($connection, 'memory'))
{
\fclose(\fopen($config->getNamespace() . '.sqlite', 'w'));
}
$lines = \file($file);
return $lines;
}
/**
* Initialize the orm
*
* @param array<string> $lines sql to import into schema
*/
abstract public function init(\SOB\Configuration $config, array $lines, \SOB\BaseLine $runTimer) : static;
/**
* class must insert one record with id=$id
*
* @return int $id inserted
*/
abstract public function insert(int $id) : int;
/**
* @param array<string> $lines of sql to import into schema
*/
public function loadSchema(array $lines, callable $callback, \SOB\BaseLine $runTimer) : static
{
$runTimer->pause();
$sql = '';
foreach ($lines as $line)
{
// Ignoring comments from the SQL script
if (\str_starts_with((string)$line, '--') || '' == $line)
{
continue;
}
$sql .= $line;
if (\str_ends_with(\trim((string)$line), ';'))
{
\call_user_func($callback, $sql);
$sql = '';
}
} // end foreach
$runTimer->resume();
return $this;
}
/**
* class must read and return one record with id=$id or null on no matching record
*/
abstract public function read(int $id) : ?object;
/**
* class must test one record with id=$id
*/
public function testUpdate(object $employee, string $expected) : bool
{
if ($employee->last_name != $expected)
{
\print_r($employee);
throw new \Exception("Employee update failed. Expected: {$expected}, Actual: {$employee->last_name}");
}
return true;
}
/**
* class must update one record with id=$id to have $to in the data
*/
abstract public function update(int $id, int $to) : bool;
}