Repository navigation
Expand file tree
/
Copy pathTests.php
More file actions
112 lines (95 loc) · 2.62 KB
/
Copy pathTests.php
File metadata and controls
112 lines (95 loc) · 2.62 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
<?php
namespace SOB\Cake;
class Tests extends \SOB\Test
{
private $connection; // @phpstan-ignore-line
public function closeConnection() : void
{
$this->connection = null;
}
/**
* class must delete one record with id=$id
*/
public function delete(int $id) : bool
{
$employeeTable = new \SOB\Cake\Table\Employee();
$employee = $employeeTable->get($id);
$employeeTable->delete($employee);
return true;
}
/**
* Initialize the orm
*
* @param array<string> $lines sql to import into schema
*/
public function init(\SOB\Configuration $config, array $lines, \SOB\BaseLine $runTimer) : static
{
$driver = \ucfirst(\strtolower($config->getDriver()));
if ('Pgsql' === $driver)
{
$driver = 'Postgres';
}
$driver = '\\Cake\\Database\\Driver\\' . $driver;
$run = \SOB\Cake\RunManager::get();
\Cake\Datasource\ConnectionManager::setConfig($run, [
'className' => 'Cake\Database\Connection',
'driver' => $driver,
'persistent' => false,
'host' => $config->getHost(),
'port' => $config->getPort(),
'username' => $config->getUser(),
'password' => $config->getPassword(),
'database' => $config->getDatabase(),
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => false,
]);
$this->connection = \Cake\Datasource\ConnectionManager::get($run);
$callback = [$this->connection, 'execute'];
$this->loadSchema($lines, $callback, $runTimer);
return $this;
}
/**
* class must insert one record with id=$id
*
* @return int $id inserted
*/
public function insert(int $id) : int
{
$employeeTable = new \SOB\Cake\Table\Employee();
$employee = $employeeTable->newEmptyEntity();
$employee->employee_id = $id; // @phpstan-ignore-line
$employee->company = "Company {$id}"; // @phpstan-ignore-line
$employee->last_name = "Last {$id}"; // @phpstan-ignore-line
$employee->first_name = "First {$id}"; // @phpstan-ignore-line
$employeeTable->save($employee);
return $employee->employee_id;
}
/**
* class must read and return one record with id=$id or null on no matching record
*/
public function read(int $id) : ?object
{
try
{
$employeeTable = new \SOB\Cake\Table\Employee();
$employee = $employeeTable->get($id);
}
catch (\Exception $e)
{
$employee = null;
}
return $employee;
}
/**
* class must update one record with id=$id to have $to in the data
*/
public function update(int $id, int $to) : bool
{
$employeeTable = new \SOB\Cake\Table\Employee();
$employee = $employeeTable->get($id);
$employee->last_name = "Updated {$to}"; // @phpstan-ignore-line
$employeeTable->save($employee);
return true;
}
}