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 testGetSetLazyLoad() |
36
|
|
|
{ |
37
|
|
|
$testCollection = new \Suricate\DBCollection(); |
38
|
|
|
$this->assertFalse($testCollection->getLazyLoad()); |
39
|
|
|
$retVal = $testCollection->setLazyLoad(true); |
40
|
|
|
$this->assertInstanceOf(\Suricate\DBCollection::class, $retVal); |
41
|
|
|
$this->assertTrue($testCollection->getLazyLoad()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testLoadFromSql() |
45
|
|
|
{ |
46
|
|
|
$this->setupData(); |
47
|
|
|
$testDBCollection = $this->getDBCollection(); |
48
|
|
|
|
49
|
|
|
$sql = "SELECT * FROM categories WHERE id>:id"; |
50
|
|
|
$sqlParams = ['id' => 0]; |
51
|
|
|
|
52
|
|
|
$retVal = $testDBCollection->loadFromSql($sql, $sqlParams); |
53
|
|
|
|
54
|
|
|
$this->assertInstanceOf(\Suricate\DBCollection::class, $retVal); |
55
|
|
|
$this->assertSame(2, $testDBCollection->count()); |
56
|
|
|
$this->assertInstanceOf(Category::class, $testDBCollection[0]); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testBuildFromSql() |
60
|
|
|
{ |
61
|
|
|
$this->setupData(); |
62
|
|
|
|
63
|
|
|
$sql = "SELECT * FROM categories WHERE id>:id"; |
64
|
|
|
$sqlParams = ['id' => 0]; |
65
|
|
|
|
66
|
|
|
$retVal = CategoriesList::buildFromSql($sql, $sqlParams); |
67
|
|
|
|
68
|
|
|
$this->assertInstanceOf(\Suricate\DBCollection::class, $retVal); |
69
|
|
|
$this->assertSame(2, $retVal->count()); |
70
|
|
|
$this->assertInstanceOf(Category::class, $retVal[0]); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testLoadAll() |
74
|
|
|
{ |
75
|
|
|
$this->setupData(); |
76
|
|
|
$retVal = CategoriesList::loadAll(); |
77
|
|
|
|
78
|
|
|
$this->assertInstanceOf(\Suricate\DBCollection::class, $retVal); |
79
|
|
|
$this->assertSame(2, $retVal->count()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
protected function getDatabase() |
83
|
|
|
{ |
84
|
|
|
$database = new \Suricate\Database(); |
85
|
|
|
$database->configure([ |
86
|
|
|
'type' => 'sqlite', |
87
|
|
|
'file' => '/tmp/test.db', |
88
|
|
|
]); |
89
|
|
|
|
90
|
|
|
return $database; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
protected function getDBCollection() |
94
|
|
|
{ |
95
|
|
|
$dbLink = $this->getDatabase(); |
96
|
|
|
// Inject database handler |
97
|
|
|
$testDBCollection = new \Suricate\DBCollection(); |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
$reflector = new ReflectionClass(get_class($testDBCollection)); |
101
|
|
|
$property = $reflector->getProperty('dbLink'); |
102
|
|
|
$property->setAccessible(true); |
103
|
|
|
$property->setValue($testDBCollection, $dbLink); |
104
|
|
|
|
105
|
|
|
self::mockProperty($testDBCollection, 'tableName', 'categories'); |
106
|
|
|
self::mockProperty($testDBCollection, 'itemsType', Category::class); |
107
|
|
|
|
108
|
|
|
return $testDBCollection; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
protected function setupData() |
112
|
|
|
{ |
113
|
|
|
$pdo = new PDO('sqlite:/tmp/test.db'); |
114
|
|
|
$pdo->exec("DROP TABLE IF EXISTS `users`"); |
115
|
|
|
$pdo->exec("DROP TABLE IF EXISTS `categories`"); |
116
|
|
|
$pdo->exec("CREATE TABLE `users` (`id` INTEGER PRIMARY KEY,`category_id` INTEGER, `name` varchar(50) DEFAULT NULL,`date_added` datetime NOT NULL)"); |
117
|
|
|
$pdo->exec("CREATE TABLE `categories` (`id` INTEGER PRIMARY KEY, `name` varchar(50) DEFAULT NULL, `parent_id` INTEGER DEFAULT NULL)"); |
118
|
|
|
|
119
|
|
|
$stmt = $pdo->prepare("INSERT INTO `users` (name, category_id, date_added) VALUES (:name, :categoryid, :date)"); |
120
|
|
|
$values = [ |
121
|
|
|
['John', 100, '2019-01-10 00:00:00'], |
122
|
|
|
['Paul', 100, '2019-01-11 00:00:00'], |
123
|
|
|
['Robert', 101, '2019-01-12 00:00:00'] |
124
|
|
|
]; |
125
|
|
|
foreach ($values as $value) { |
126
|
|
|
$stmt->execute(['name' => $value[0], 'categoryid' => $value[1], 'date' => $value[2]]); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$stmt = $pdo->prepare("INSERT INTO `categories` (id, name) VALUES (:id, :name)"); |
130
|
|
|
$values = [ |
131
|
|
|
[100, 'Admin'], |
132
|
|
|
[101, 'Employee'] |
133
|
|
|
]; |
134
|
|
|
foreach ($values as $value) { |
135
|
|
|
$stmt->execute(['id' => $value[0], 'name' => $value[1]]); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public static function mockProperty($object, string $propertyName, $value) |
140
|
|
|
{ |
141
|
|
|
$reflectionClass = new \ReflectionClass($object); |
142
|
|
|
|
143
|
|
|
$property = $reflectionClass->getProperty($propertyName); |
144
|
|
|
$property->setAccessible(true); |
145
|
|
|
$property->setValue($object, $value); |
146
|
|
|
$property->setAccessible(false); |
147
|
|
|
} |
148
|
|
|
} |