DatabaseTest   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 219
Duplicated Lines 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
eloc 121
dl 0
loc 219
rs 10
c 5
b 1
f 0
wmc 17

15 Methods

Rating   Name   Duplication   Size   Complexity  
A setupData() 0 21 2
A tearDown() 0 4 2
A testFetchObject() 0 12 1
A getDatabase() 0 10 1
A testGetSetConfigs() 0 14 1
A testFetchColumn() 0 8 1
A testFetchAll() 0 21 1
A testLastInsertId() 0 12 1
A testFetch() 0 33 1
A testContructor() 0 10 1
A setUp() 0 3 1
A testGetSetConfig() 0 11 1
A testUnsupportedHandler() 0 9 1
A testConnect() 0 9 1
A testGetColumnCount() 0 5 1
1
<?php
2
3
use PHPUnit\Framework\TestCase;
4
5
/**
6
 * @SuppressWarnings("TooManyPublicMethods")
7
 **/
8
class DatabaseTest extends TestCase
9
{
10
    protected $className = '\Suricate\Database';
11
    protected $tableName = 'users';
12
13
    protected function setupData()
14
    {
15
        $pdo = new PDO('sqlite:/tmp/test.db');
16
        $pdo->exec("DROP TABLE IF EXISTS `" . $this->tableName . "`");
17
        $pdo->exec(
18
            "CREATE TABLE `" .
19
                $this->tableName .
20
                "` (`id` INTEGER PRIMARY KEY,`name` varchar(50) DEFAULT NULL,`date_added` datetime NOT NULL)"
21
        );
22
        $stmt = $pdo->prepare(
23
            "INSERT INTO `" .
24
                $this->tableName .
25
                "` (name, date_added) VALUES (:name, :date)"
26
        );
27
        $values = [
28
            ['John', '2019-01-10 00:00:00'],
29
            ['Paul', '2019-01-11 00:00:00'],
30
            ['Robert', '2019-01-12 00:00:00']
31
        ];
32
        foreach ($values as $value) {
33
            $stmt->execute(['name' => $value[0], 'date' => $value[1]]);
34
        }
35
    }
36
37
    protected function getDatabase()
38
    {
39
        $className = $this->className;
40
        $database = new $className();
41
        $database->configure([
42
            'type' => 'sqlite',
43
            'file' => '/tmp/test.db'
44
        ]);
45
46
        return $database;
47
    }
48
49
    public function setUp(): void
50
    {
51
        $this->setupData();
52
    }
53
54
    public function testContructor()
55
    {
56
        $className = $this->className;
57
        $database = new $className();
58
59
        $this->assertNull($database->getConfig());
60
        $reflection = new \ReflectionClass(get_class($database));
61
        $property = $reflection->getProperty('handler');
62
        $property->setAccessible(true);
63
        $this->assertEquals($property->getValue($database), false);
64
    }
65
66
    public function testGetSetConfig()
67
    {
68
        $className = $this->className;
69
        $configName = "testConfig";
70
71
        $database = new $className();
72
        $this->assertNull($database->getConfig());
73
        $retVar = $database->setConfig($configName);
74
75
        $this->assertInstanceOf($className, $retVar);
76
        $this->assertEquals($configName, $database->getConfig());
77
    }
78
79
    public function testGetSetConfigs()
80
    {
81
        $className = $this->className;
82
        $configs = [
83
            'test1' => ['type' => 'sqlite', 'memory' => true],
84
            'test2' => ['type' => 'mysql']
85
        ];
86
87
        $database = new $className();
88
        $this->assertSame([], $database->getConfigs());
89
        $retVar = $database->setConfigs($configs);
90
91
        $this->assertInstanceOf($className, $retVar);
92
        $this->assertEquals($configs, $database->getConfigs());
93
    }
94
95
    public function testConnect()
96
    {
97
        $database = $this->getDatabase();
98
        $database->query("SELECT * FROM users");
99
        $reflection = new \ReflectionClass(get_class($database));
100
        $property = $reflection->getProperty('handler');
101
        $property->setAccessible(true);
102
103
        $this->assertInstanceOf('\PDO', $property->getValue($database));
104
    }
105
106
    public function testUnsupportedHandler()
107
    {
108
        $className = $this->className;
109
        $database = new $className();
110
        $database->configure([
111
            'type' => 'my-pdo-handler'
112
        ]);
113
        $this->expectException(\Exception::class);
114
        $database->query("SELECT * FROM users");
115
    }
116
117
    public function testFetchAll()
118
    {
119
        $database = $this->getDatabase();
120
        $queryResult = $database->query(
121
            "SELECT * FROM `" . $this->tableName . "`"
122
        );
123
        $this->assertEquals($queryResult->fetchAll(), [
124
            [
125
                'id' => '1',
126
                'name' => 'John',
127
                'date_added' => '2019-01-10 00:00:00'
128
            ],
129
            [
130
                'id' => '2',
131
                'name' => 'Paul',
132
                'date_added' => '2019-01-11 00:00:00'
133
            ],
134
            [
135
                'id' => '3',
136
                'name' => 'Robert',
137
                'date_added' => '2019-01-12 00:00:00'
138
            ]
139
        ]);
140
    }
141
142
    public function testFetch()
143
    {
144
        $database = $this->getDatabase();
145
        $database->query("SELECT * FROM `" . $this->tableName . "`");
146
        // Record 1
147
        $this->assertSame(
148
            [
149
                'id' => '1',
150
                'name' => 'John',
151
                'date_added' => '2019-01-10 00:00:00'
152
            ],
153
            $database->fetch()
154
        );
155
        // Record 2
156
        $this->assertSame(
157
            [
158
                'id' => '2',
159
                'name' => 'Paul',
160
                'date_added' => '2019-01-11 00:00:00'
161
            ],
162
            $database->fetch()
163
        );
164
        // Record 3
165
        $this->assertSame(
166
            [
167
                'id' => '3',
168
                'name' => 'Robert',
169
                'date_added' => '2019-01-12 00:00:00'
170
            ],
171
            $database->fetch()
172
        );
173
        // No more records
174
        $this->assertFalse($database->fetch());
175
    }
176
177
    public function testFetchObject()
178
    {
179
        $database = $this->getDatabase();
180
        $database->query("SELECT * FROM `" . $this->tableName . "`");
181
        $result = $database->fetchObject();
182
        $expected = new \stdClass();
183
        $expected->id = '1';
184
        $expected->name = 'John';
185
        $expected->date_added = '2019-01-10 00:00:00';
186
187
        $this->assertEquals($expected, $result);
188
        $this->assertSame($expected->id, $result->id);
189
    }
190
191
    public function testFetchColumn()
192
    {
193
        $database = $this->getDatabase();
194
        $database->query("SELECT * FROM `" . $this->tableName . "` WHERE id=2");
195
        $this->assertSame('2', $database->fetchColumn());
196
        $database->query("SELECT * FROM `" . $this->tableName . "` WHERE id=2");
197
        $this->assertSame('Paul', $database->fetchColumn(1));
198
        $this->assertFalse($database->fetchColumn(1));
199
    }
200
201
    public function testLastInsertId()
202
    {
203
        $database = $this->getDatabase();
204
        $database->query(
205
            "INSERT INTO `" .
206
                $this->tableName .
207
                "` (name, date_added) VALUES ('Rodrigo', '2019-01-13 00:00:00')"
208
        );
209
        $this->assertSame(
210
            '4',
211
            $database->lastInsertId(),
212
            'Test last inserted id'
213
        );
214
    }
215
216
    public function testGetColumnCount()
217
    {
218
        $database = $this->getDatabase();
219
        $database->query("SELECT * FROM `" . $this->tableName . "`");
220
        $this->assertEquals(3, $database->getColumnCount());
221
    }
222
223
    protected function tearDown(): void
224
    {
225
        if (is_file('/tmp/test.db')) {
226
            unlink('/tmp/test.db');
227
        }
228
    }
229
}
230