Repository navigation
Expand file tree
/
Copy pathTests.php
More file actions
95 lines (79 loc) · 2.1 KB
/
Copy pathTests.php
File metadata and controls
95 lines (79 loc) · 2.1 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
<?php
namespace SOB\ActiveRecord;
class Tests extends \SOB\Test
{
public function closeConnection() : void
{
// $this->pdo = null;
}
/**
* class must delete one record with id=$id
*/
public function delete(int $id) : bool
{
$employee = \SOB\ActiveRecord\Model\Employee::find(1);
return $employee->delete(); // @phpstan-ignore-line
}
/**
* Initialize the orm
*
* @param array<string> $lines sql to import into schema
*/
public function init(\SOB\Configuration $config, array $lines, \SOB\BaseLine $runTimer) : static
{
if ('sqlite' !== $config->getDriver())
{
$connectionString = $config->getDriver() . '://' . $config->getUser() . ':' . $config->getPassword() . '@' . $config->getHost() . ':' . $config->getPort() . '/' . $config->getDatabase();
}
else
{
$connectionString = $config->getDriver() . '://' . $config->getDatabase();
}
$cfg = \ActiveRecord\Config::instance();
$cfg->set_connections([
'test' => $connectionString,
]);
$cfg->set_default_connection('test');
$callback = [\ActiveRecord\ConnectionManager::get_connection('test'), 'query'];
$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
{
$employee = new \SOB\ActiveRecord\Model\Employee();
$employee->company = "Company {$id}";
$employee->last_name = "Last {$id}";
$employee->first_name = "First {$id}";
$employee->save();
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
{
$employee = \SOB\ActiveRecord\Model\Employee::find($id);
}
catch (\ActiveRecord\Exception\RecordNotFound $e)
{
return 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
{
$employee = \SOB\ActiveRecord\Model\Employee::find($id);
$employee->last_name = "Updated {$to}";
return $employee->save();
}
}