Passed
Push — develop ( 55880a...608a34 )
by Mathieu
07:50
created

DBCollectionTest::testBuildFromSql()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 12
rs 10
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 testBuildFromSql()
89
    {
90
        $this->setupData();
91
92
        $sql = "SELECT * FROM categories WHERE id>:id";
93
        $sqlParams = ['id' => 0];
94
95
        $retVal = CategoriesList::buildFromSql($sql, $sqlParams);
96
        
97
        $this->assertInstanceOf(\Suricate\DBCollection::class, $retVal);
98
        $this->assertSame(2, $retVal->count());
99
        $this->assertInstanceOf(Category::class, $retVal[0]);
100
    }
101
102
    public function testLoadAll()
103
    {
104
        $this->setupData();
105
        $retVal = CategoriesList::loadAll();
106
107
        $this->assertInstanceOf(\Suricate\DBCollection::class, $retVal);
108
        $this->assertSame(2, $retVal->count());
109
    }
110
111
    protected function getDatabase()
112
    {
113
        $database = new \Suricate\Database();
114
        $database->configure([
115
            'type' => 'sqlite',
116
            'file' => '/tmp/test.db',
117
        ]);
118
119
        return $database;
120
    }
121
122
    protected function getDBCollection()
123
    {
124
        $dbLink = $this->getDatabase();
125
        // Inject database handler
126
        $testDBCollection = new \Suricate\DBCollection();
127
128
129
        $reflector = new ReflectionClass(get_class($testDBCollection));
130
        $property = $reflector->getProperty('dbLink');
131
        $property->setAccessible(true);
132
        $property->setValue($testDBCollection, $dbLink);
133
134
        self::mockProperty($testDBCollection, 'tableName', 'categories');
135
        self::mockProperty($testDBCollection, 'itemsType', Category::class);
136
        self::mockProperty($testDBCollection, 'parentIdField', 'parent_id');
137
        self::mockProperty($testDBCollection, 'parentType', Category::class);
138
        
139
        return $testDBCollection;
140
    }
141
142
    protected function setupData()
143
    {
144
        $pdo = new PDO('sqlite:/tmp/test.db');
145
        $pdo->exec("DROP TABLE IF EXISTS `users`");
146
        $pdo->exec("DROP TABLE IF EXISTS `categories`");
147
        $pdo->exec("CREATE TABLE `users` (`id` INTEGER PRIMARY KEY,`category_id` INTEGER, `name` varchar(50) DEFAULT NULL,`date_added` datetime NOT NULL)");
148
        $pdo->exec("CREATE TABLE `categories` (`id` INTEGER PRIMARY KEY, `name` varchar(50) DEFAULT NULL, `parent_id` INTEGER DEFAULT NULL)");
149
        
150
        $stmt = $pdo->prepare("INSERT INTO `users` (name, category_id, date_added) VALUES (:name, :categoryid, :date)");
151
        $values = [
152
            ['John', 100, '2019-01-10 00:00:00'],
153
            ['Paul', 100, '2019-01-11 00:00:00'],
154
            ['Robert', 101, '2019-01-12 00:00:00']
155
        ];
156
        foreach ($values as $value) {
157
            $stmt->execute(['name' => $value[0], 'categoryid' => $value[1], 'date' => $value[2]]);
158
        }
159
160
        $stmt = $pdo->prepare("INSERT INTO `categories` (id, name) VALUES (:id, :name)");
161
        $values = [
162
            [100, 'Admin'],
163
            [101, 'Employee']
164
        ];
165
        foreach ($values as $value) {
166
            $stmt->execute(['id' => $value[0], 'name' => $value[1]]);
167
        }
168
    }
169
170
    public static function mockProperty($object, string $propertyName, $value)
171
    {
172
        $reflectionClass = new \ReflectionClass($object);
173
174
        $property = $reflectionClass->getProperty($propertyName);
175
        $property->setAccessible(true);
176
        $property->setValue($object, $value);
177
        $property->setAccessible(false);
178
    }
179
}