Repository navigation
Expand file tree
/
Copy pathTests.php
More file actions
137 lines (114 loc) · 2.77 KB
/
Copy pathTests.php
File metadata and controls
137 lines (114 loc) · 2.77 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
<?php
namespace SOB\Composite;
/**
* This won't work because the attribute hard codes the DB connection to the table definition. Unbelievable shit!
*/
class Tests extends \SOB\Test
{
private Doctrine\DBAL\Connection $connection;
public function closeConnection() : void
{
$this->pdo = null;
}
/**
* class must delete one record with id=$id
*/
public function delete(int $id) : bool
{
$table = new \SOB\Composite\Record\EmployeeTable();
$employee = $table->findOne($id);
if (! $employee)
{
return false;
}
$table->delete($employee);
return true;
}
/**
* Initialize Responsibilities:
*
* * Initialize the orm
* * open the database
* * initialize the database schema
*/
public function init(\SOB\Configuration $config) : static
{
$connectionParams = [
'dbname' => $config->getDatabase(),
'user' => $config->getUser(),
'password' => $config->getPassword(),
'host' => $config->getHost(),
'driver' => $config->getDriver(),
];
$this->connection = \Doctrine\DBAL\DriverManager::getConnection($connectionParams);
if (\str_contains($config->getDriver(), 'sqlite'))
{
$lines = \file(__DIR__ . '/../../northwind/northwind-schema.sqlite');
\fclose(\fopen($config->getNamespace() . '.sqlite', 'w'));
}
else
{
$lines = \file(__DIR__ . '/../../northwind/northwind-schema.sql');
}
$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), ';'))
{
$this->connection->executeStatement($sql);
$sql = '';
}
} // end foreach
return $this;
}
/**
* class must insert one record with id=$id
*
* @return int $id inserted
*/
public function insert(int $id) : int
{
$employee = new \SOB\Composite\Record\Employee();
$employee->id = $id;
$employee->company = "Company {$id}";
$employee->last_name = "Last {$id}";
$employee->first_name = "First {$id}";
$table = new \SOB\Composite\Record\EmployeeTable();
$table->save($employee);
return $id;
}
/**
* class must read and return one record with id=$id or null on no matching record
*/
public function read(int $id) : ?object
{
$table = new \SOB\Composite\Record\EmployeeTable();
$employee = $table->findOne($id);
if (! $employee)
{
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
{
$table = new \SOB\Composite\Record\EmployeeTable();
$employee = $table->findOne($id);
if (! $employee)
{
return false;
}
$employee->last_name = "Updated {$to}";
$table->save($employee);
return true;
}
}