1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
require_once 'stubs/Category.php'; |
4
|
|
|
require_once 'stubs/CategoriesList.php'; |
5
|
|
|
|
6
|
|
|
class DBCollectionOneManyTest extends \PHPUnit\Framework\TestCase |
7
|
|
|
{ |
8
|
|
|
public function testGetParentIdField() |
9
|
|
|
{ |
10
|
|
|
$testName = 'parent_id'; |
11
|
|
|
|
12
|
|
|
$testCollection = new \Suricate\DBCollectionOneMany(); |
13
|
|
|
self::mockProperty($testCollection, 'parentIdField', $testName); |
14
|
|
|
$this->assertEquals($testName, $testCollection->getParentIdField()); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public function testGetParentId() |
18
|
|
|
{ |
19
|
|
|
$testId = 100; |
20
|
|
|
|
21
|
|
|
$testCollection = new \Suricate\DBCollectionOneMany(); |
22
|
|
|
$this->assertNull($testCollection->getParentId()); |
23
|
|
|
self::mockProperty($testCollection, 'parentId', $testId); |
24
|
|
|
$this->assertSame($testId, $testCollection->getParentId()); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
protected function getDatabase() |
28
|
|
|
{ |
29
|
|
|
$database = new \Suricate\Database(); |
30
|
|
|
$database->configure([ |
31
|
|
|
'type' => 'sqlite', |
32
|
|
|
'file' => '/tmp/test.db', |
33
|
|
|
]); |
34
|
|
|
|
35
|
|
|
return $database; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
protected function getDBCollection() |
39
|
|
|
{ |
40
|
|
|
$dbLink = $this->getDatabase(); |
41
|
|
|
// Inject database handler |
42
|
|
|
$testDBCollection = new \Suricate\DBCollectionOneMany(); |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
$reflector = new ReflectionClass(get_class($testDBCollection)); |
46
|
|
|
$property = $reflector->getProperty('dbLink'); |
47
|
|
|
$property->setAccessible(true); |
48
|
|
|
$property->setValue($testDBCollection, $dbLink); |
49
|
|
|
|
50
|
|
|
self::mockProperty($testDBCollection, 'tableName', 'categories'); |
51
|
|
|
self::mockProperty($testDBCollection, 'itemsType', Category::class); |
52
|
|
|
self::mockProperty($testDBCollection, 'parentIdField', 'parent_id'); |
53
|
|
|
|
54
|
|
|
return $testDBCollection; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function setupData() |
58
|
|
|
{ |
59
|
|
|
$pdo = new PDO('sqlite:/tmp/test.db'); |
60
|
|
|
$pdo->exec("DROP TABLE IF EXISTS `users`"); |
61
|
|
|
$pdo->exec("DROP TABLE IF EXISTS `categories`"); |
62
|
|
|
$pdo->exec("CREATE TABLE `users` (`id` INTEGER PRIMARY KEY,`category_id` INTEGER, `name` varchar(50) DEFAULT NULL,`date_added` datetime NOT NULL)"); |
63
|
|
|
$pdo->exec("CREATE TABLE `categories` (`id` INTEGER PRIMARY KEY, `name` varchar(50) DEFAULT NULL, `parent_id` INTEGER DEFAULT NULL)"); |
64
|
|
|
|
65
|
|
|
$stmt = $pdo->prepare("INSERT INTO `users` (name, category_id, date_added) VALUES (:name, :categoryid, :date)"); |
66
|
|
|
$values = [ |
67
|
|
|
['John', 100, '2019-01-10 00:00:00'], |
68
|
|
|
['Paul', 100, '2019-01-11 00:00:00'], |
69
|
|
|
['Robert', 101, '2019-01-12 00:00:00'] |
70
|
|
|
]; |
71
|
|
|
foreach ($values as $value) { |
72
|
|
|
$stmt->execute(['name' => $value[0], 'categoryid' => $value[1], 'date' => $value[2]]); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$stmt = $pdo->prepare("INSERT INTO `categories` (id, name) VALUES (:id, :name)"); |
76
|
|
|
$values = [ |
77
|
|
|
[100, 'Admin'], |
78
|
|
|
[101, 'Employee'] |
79
|
|
|
]; |
80
|
|
|
foreach ($values as $value) { |
81
|
|
|
$stmt->execute(['id' => $value[0], 'name' => $value[1]]); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public static function mockProperty($object, string $propertyName, $value) |
86
|
|
|
{ |
87
|
|
|
$reflectionClass = new \ReflectionClass($object); |
88
|
|
|
|
89
|
|
|
$property = $reflectionClass->getProperty($propertyName); |
90
|
|
|
$property->setAccessible(true); |
91
|
|
|
$property->setValue($object, $value); |
92
|
|
|
$property->setAccessible(false); |
93
|
|
|
} |
94
|
|
|
} |