1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
require_once 'stubs/Category.php'; |
4
|
|
|
require_once 'stubs/CategoriesList.php'; |
5
|
|
|
|
6
|
|
|
class DBCollectionTest extends \PHPUnit\Framework\TestCase |
7
|
|
|
{ |
8
|
|
|
public function testGetTableName() |
9
|
|
|
{ |
10
|
|
|
$testName = 'categories'; |
11
|
|
|
|
12
|
|
|
$testCollection = new \Suricate\DBCollection(); |
13
|
|
|
self::mockProperty($testCollection, 'tableName', $testName); |
14
|
|
|
$this->assertEquals($testName, $testCollection->getTableName()); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public function testGetItemsType() |
18
|
|
|
{ |
19
|
|
|
$testName = Category::class; |
20
|
|
|
|
21
|
|
|
$testCollection = new \Suricate\DBCollection(); |
22
|
|
|
self::mockProperty($testCollection, 'itemsType', $testName); |
23
|
|
|
$this->assertEquals($testName, $testCollection->getItemsType()); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testGetDBConfig() |
27
|
|
|
{ |
28
|
|
|
$testConfigName = 'my_config'; |
29
|
|
|
|
30
|
|
|
$testCollection = new \Suricate\DBCollection(); |
31
|
|
|
self::mockProperty($testCollection, 'DBConfig', $testConfigName); |
32
|
|
|
$this->assertEquals($testConfigName, $testCollection->getDBConfig()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testGetParentIdField() |
36
|
|
|
{ |
37
|
|
|
$testName = 'parent_id'; |
38
|
|
|
|
39
|
|
|
$testCollection = new \Suricate\DBCollection(); |
40
|
|
|
self::mockProperty($testCollection, 'parentIdField', $testName); |
41
|
|
|
$this->assertEquals($testName, $testCollection->getParentIdField()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testGetParentId() |
45
|
|
|
{ |
46
|
|
|
$testId = 100; |
47
|
|
|
|
48
|
|
|
$testCollection = new \Suricate\DBCollection(); |
49
|
|
|
$this->assertNull($testCollection->getParentId()); |
50
|
|
|
self::mockProperty($testCollection, 'parentId', $testId); |
51
|
|
|
$this->assertSame($testId, $testCollection->getParentId()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testGetParentType() |
55
|
|
|
{ |
56
|
|
|
$testType = Category::class; |
57
|
|
|
|
58
|
|
|
$testCollection = new \Suricate\DBCollection(); |
59
|
|
|
$this->assertSame('', $testCollection->getParentType()); |
60
|
|
|
self::mockProperty($testCollection, 'parentType', $testType); |
61
|
|
|
$this->assertSame($testType, $testCollection->getParentType()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testGetSetLazyLoad() |
65
|
|
|
{ |
66
|
|
|
$testCollection = new \Suricate\DBCollection(); |
67
|
|
|
$this->assertFalse($testCollection->getLazyLoad()); |
68
|
|
|
$retVal = $testCollection->setLazyLoad(true); |
69
|
|
|
$this->assertInstanceOf(\Suricate\DBCollection::class, $retVal); |
70
|
|
|
$this->assertTrue($testCollection->getLazyLoad()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testLoadFromSql() |
74
|
|
|
{ |
75
|
|
|
$this->setupData(); |
76
|
|
|
$testDBCollection = $this->getDBCollection(); |
77
|
|
|
|
78
|
|
|
$sql = "SELECT * FROM categories WHERE id>:id"; |
79
|
|
|
$sqlParams = ['id' => 0]; |
80
|
|
|
|
81
|
|
|
$retVal = $testDBCollection->loadFromSql($sql, $sqlParams); |
82
|
|
|
|
83
|
|
|
$this->assertInstanceOf(\Suricate\DBCollection::class, $retVal); |
84
|
|
|
$this->assertSame(2, $testDBCollection->count()); |
85
|
|
|
$this->assertInstanceOf(Category::class, $testDBCollection[0]); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testLoadAll() |
89
|
|
|
{ |
90
|
|
|
$this->setupData(); |
91
|
|
|
$retVal = CategoriesList::loadAll(); |
92
|
|
|
|
93
|
|
|
$this->assertInstanceOf(\Suricate\DBCollection::class, $retVal); |
94
|
|
|
$this->assertSame(2, $retVal->count()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
protected function getDatabase() |
98
|
|
|
{ |
99
|
|
|
$database = new \Suricate\Database(); |
100
|
|
|
$database->configure([ |
101
|
|
|
'type' => 'sqlite', |
102
|
|
|
'file' => '/tmp/test.db', |
103
|
|
|
]); |
104
|
|
|
|
105
|
|
|
return $database; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
protected function getDBCollection() |
109
|
|
|
{ |
110
|
|
|
$dbLink = $this->getDatabase(); |
111
|
|
|
// Inject database handler |
112
|
|
|
$testDBCollection = new \Suricate\DBCollection(); |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
$reflector = new ReflectionClass(get_class($testDBCollection)); |
116
|
|
|
$property = $reflector->getProperty('dbLink'); |
117
|
|
|
$property->setAccessible(true); |
118
|
|
|
$property->setValue($testDBCollection, $dbLink); |
119
|
|
|
|
120
|
|
|
self::mockProperty($testDBCollection, 'tableName', 'categories'); |
121
|
|
|
self::mockProperty($testDBCollection, 'itemsType', Category::class); |
122
|
|
|
self::mockProperty($testDBCollection, 'parentIdField', 'parent_id'); |
123
|
|
|
self::mockProperty($testDBCollection, 'parentType', Category::class); |
124
|
|
|
|
125
|
|
|
return $testDBCollection; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
protected function setupData() |
129
|
|
|
{ |
130
|
|
|
$pdo = new PDO('sqlite:/tmp/test.db'); |
131
|
|
|
$pdo->exec("DROP TABLE IF EXISTS `users`"); |
132
|
|
|
$pdo->exec("DROP TABLE IF EXISTS `categories`"); |
133
|
|
|
$pdo->exec("CREATE TABLE `users` (`id` INTEGER PRIMARY KEY,`category_id` INTEGER, `name` varchar(50) DEFAULT NULL,`date_added` datetime NOT NULL)"); |
134
|
|
|
$pdo->exec("CREATE TABLE `categories` (`id` INTEGER PRIMARY KEY, `name` varchar(50) DEFAULT NULL, `parent_id` INTEGER DEFAULT NULL)"); |
135
|
|
|
|
136
|
|
|
$stmt = $pdo->prepare("INSERT INTO `users` (name, category_id, date_added) VALUES (:name, :categoryid, :date)"); |
137
|
|
|
$values = [ |
138
|
|
|
['John', 100, '2019-01-10 00:00:00'], |
139
|
|
|
['Paul', 100, '2019-01-11 00:00:00'], |
140
|
|
|
['Robert', 101, '2019-01-12 00:00:00'] |
141
|
|
|
]; |
142
|
|
|
foreach ($values as $value) { |
143
|
|
|
$stmt->execute(['name' => $value[0], 'categoryid' => $value[1], 'date' => $value[2]]); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$stmt = $pdo->prepare("INSERT INTO `categories` (id, name) VALUES (:id, :name)"); |
147
|
|
|
$values = [ |
148
|
|
|
[100, 'Admin'], |
149
|
|
|
[101, 'Employee'] |
150
|
|
|
]; |
151
|
|
|
foreach ($values as $value) { |
152
|
|
|
$stmt->execute(['id' => $value[0], 'name' => $value[1]]); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public static function mockProperty($object, string $propertyName, $value) |
157
|
|
|
{ |
158
|
|
|
$reflectionClass = new \ReflectionClass($object); |
159
|
|
|
|
160
|
|
|
$property = $reflectionClass->getProperty($propertyName); |
161
|
|
|
$property->setAccessible(true); |
162
|
|
|
$property->setValue($object, $value); |
163
|
|
|
$property->setAccessible(false); |
164
|
|
|
} |
165
|
|
|
} |