Completed
Push — master ( 4fd1db...6347d5 )
by Adam
02:08
created

DatabaseTest::connectToLiveDB()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace DBAL\Tests;
3
4
use PHPUnit\Framework\TestCase;
5
use DBAL\Database;
6
7
class DatabaseTest extends TestCase{
8
    public static $db;
9
    
10
    /**
11
     * @covers \DBAL\Database::connectToServer
12
     * @covers \DBAL\Database::isConnected
13
     */
14
    public function setUp(){
15
        $this->connectToLiveDB();
16
        if(!self::$db->isConnected()){
17
            $this->markTestSkipped(
18
                'No local database connection is available'
19
            );
20
        }
21
        else{
22
            self::$db->query('DROP TABLE IF EXISTS `test_table`;
23
CREATE TABLE `test_table` (
24
    `id` int(11) NOT NULL AUTO_INCREMENT,
25
    `name` varchar(255) NOT NULL,
26
    `text_field` text NOT NULL,
27
    `number_field` int(11) NOT NULL,
28
    PRIMARY KEY (`id`)
29
);
30
31
INSERT INTO `test_table` (`id`, `name`, `text_field`, `number_field`) VALUES
32
(1, "My Name", "Hello World", 256),
33
(2, "Inigo Montoya", "You killed my father, prepare to die", 320);');
34
        }
35
    }
36
    
37
    public static function tearDownAfterClass(){
38
        self::$db = null;
39
    }
40
    
41
    /**
42
     * @covers \DBAL\Database::isConnected
43
     */
44
    public function testConnect(){
45
        $this->assertTrue(self::$db->isConnected());
46
    }
47
    
48
    /**
49
     * @covers \DBAL\Database::isConnected
50
     */
51
    public function testConnectFailure(){
52
        $db = new Database('localhost', 'wrong_username', 'incorrect_password', 'non_existent_db');
53
        $this->assertFalse($db->isConnected());
54
        $this->connectToLiveDB();
55
    }
56
    
57
    /**
58
     * @covers \DBAL\Database::query
59
     */
60
    public function testQuery(){
61
        // Insert a couple of test vales
62
        self::$db->insert('test_table', array('name' => 'My Name', 'text_field' => 'Hello World', 'number_field' => rand(1, 1000)));
63
        self::$db->insert('test_table', array('name' => 'Inigo Montoya', 'text_field' => 'You killed my father, prepare to die', 'number_field' => rand(1, 1000)));
64
        $query = self::$db->query("SELECT * FROM `test_table` WHERE `id` = ?", array(1));
65
        $this->assertArrayHasKey(0, $query);
66
        $this->assertCount(1, $query);
67
    }
68
    
69
    /**
70
     * @covers \DBAL\Database::select
71
     * @covers \DBAL\Database::selectAll
72
     */
73
    public function testSelect(){
74
       $simpleSelect = self::$db->select('test_table', array('id' => array('>', 1)), '*', array('id' => 'ASC'));
75
       $this->assertArrayHasKey('name', $simpleSelect);
76
    }
77
    
78
    /**
79
     * @covers \DBAL\Database::selectAll
80
     */
81
    public function testSelectAll(){
82
        $selectAll = self::$db->selectAll('test_table');
83
        $this->assertGreaterThan(1, self::$db->numRows());
84
        $this->assertArrayHasKey('id', $selectAll[0]);
85
    }
86
    
87
    /**
88
     * @covers \DBAL\Database::selectAll
89
     */
90
    public function testSelectFailure(){
91
        $this->assertFalse(self::$db->selectAll('test_table', array('id' => 100)));
92
        $this->assertFalse(self::$db->selectAll('unknown_table'));
93
    }
94
    
95
    /**
96
     * @covers \DBAL\Database::insert
97
     * @covers \DBAL\Database::numRows
98
     */
99
    public function testInsert(){
100
        $this->assertTrue(self::$db->insert('test_table', array('name' => 'Third User', 'text_field' => 'Helloooooo', 'number_field' => rand(1, 1000))));
101
    }
102
    
103
    /**
104
     * @covers \DBAL\Database::insert
105
     * @covers \DBAL\Database::numRows
106
     */
107
    public function testInsertFailure(){
108
        $this->assertFalse(self::$db->insert('test_table', array('id' => 3, 'name' => 'Third User', 'text_field' => NULL, 'number_field' => rand(1, 1000))));
109
    }
110
    
111
    /**
112
     * @covers \DBAL\Database::update
113
     * @covers \DBAL\Database::numRows
114
     */
115
    public function testUpdate(){
116
        $this->assertTrue(self::$db->update('test_table', array('text_field' => 'Altered text', 'number_field' => rand(1, 1000)), array('id' => 1)));
117
    }
118
    
119
    /**
120
     * @covers \DBAL\Database::update
121
     * @covers \DBAL\Database::numRows
122
     */
123
    public function testUpdateFailure(){
124
        $this->assertFalse(self::$db->update('test_table', array('number_field' => 256), array('id' => 1)));
125
    }
126
    
127
    /**
128
     * @covers \DBAL\Database::delete
129
     * @covers \DBAL\Database::numRows
130
     */
131
    public function testDelete(){
132
        $this->assertTrue(self::$db->delete('test_table', array('id' => array('>=', 2))));
133
    }
134
    
135
    /**
136
     * @covers \DBAL\Database::delete
137
     * @covers \DBAL\Database::numRows
138
     */
139
    public function testDeleteFailure(){
140
        $this->assertFalse(self::$db->delete('test_table', array('id' => 3)));
141
    }
142
    
143
    /**
144
     * @covers \DBAL\Database::count
145
     */    
146
    public function testCount(){
147
        $this->assertEquals(2, self::$db->count('test_table'));
148
    }
149
    
150
    public function testFulltextIndex(){
151
        $this->markTestIncomplete(
152
          'This test has not been implemented yet.'
153
        );
154
    }
155
    
156
    /**
157
     * @covers \DBAL\Database::lastInsertID
158
     */
159
    public function testLastInsertID(){
160
        $this->testInsert();
161
        $this->assertEquals(3, self::$db->lastInsertID());
162
    }
163
    
164
    /**
165
     * @covers \DBAL\Database::setCaching
166
     */
167
    public function testSetCaching(){
168
        $this->markTestIncomplete(
169
          'This test has not been implemented yet.'
170
        );
171
    }
172
    
173
    protected function connectToLiveDB(){
174
        self::$db = new Database($GLOBALS['HOSTNAME'], $GLOBALS['USERNAME'], $GLOBALS['PASSWORD'], $GLOBALS['DATABASE'], false, false, true, $GLOBALS['DRIVER']);
175
    }
176
}
177