Test Failed
Push — master ( c5b8d7...35e052 )
by Adam
01:59
created

DatabaseTest::testSelect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 4
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
namespace DBAL\Tests;
3
4
use DBAL\Database;
5
use PHPUnit\Framework\TestCase;
6
7
class DatabaseTest extends TestCase{
8
    public static $db;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $db. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
9
    
10
    /**
11
     * @covers DBAL\Database::__construct
12
     * @covers DBAL\Database::connectToServer
13
     */
14
    public function setUp(){
0 ignored issues
show
Coding Style introduced by
setUp uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
15
        self::$db = new Database('localhost', $GLOBALS['DB_USER'], $GLOBALS['DB_PASSWD'], $GLOBALS['DB_DBNAME']);
16
        if(!self::$db->isConnected()){
17
             $this->markTestSkipped(
18
                'No local database connection is available'
19
            );
20
        }
21
    }
22
    
23
    /**
24
     * @covers DBAL\Database::__destruct
25
     */
26
    public static function tearDownAfterClass(){
27
        self::$db = null;
28
    }
29
30
    /**
31
     * @covers DBAL\Database::__construct
32
     * @covers DBAL\Database::connectToServer
33
     */
34
    public function testConnect(){
35
        $this->assertTrue(self::$db->isConnected());
36
    }
37
    
38
    /**
39
     * @covers DBAL\Database
40
     */
41
    public function testConnectFailure(){
42
        $db = new Database('localhost', 'wrong_username', 'incorrect_password', 'non_existent_db');
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $db. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
43
        $this->assertFalse($db->isConnected());
44
    }
45
    
46
    /**
47
     * @covers DBAL\Database::query
48
     */
49
    public function testQuery(){
50
        $query = self::$db->query("SELECT * FROM `test_table` WHERE `id` = ?", array(1));
51
        $this->assertArrayHasKey('0', $query);
52
        $this->assertCount(1, $query);
53
    }
54
    
55
    /**
56
     * @covers DBAL\Database::select
57
     * @covers DBAL\Database::buildSelectQuery
58
     * @covers DBAL\Database::where
59
     * @covers DBAL\Database::orderBy
60
     * @covers DBAL\Database::limit
61
     * @covers DBAL\Database::executeQuery
62
     */
63
    public function testSelect(){
64
        $simpleSelect = self::$db->select('test_table', array('id' => array('>', 1)), '*', array('id' => 'ASC'));
65
        $this->assertArrayHasKey('name', $simpleSelect);
66
    }
67
    
68
    /**
69
     * @covers DBAL\Database::selectAll
70
     * @covers DBAL\Database::buildSelectQuery
71
     * @covers DBAL\Database::where
72
     * @covers DBAL\Database::orderBy
73
     * @covers DBAL\Database::limit
74
     * @covers DBAL\Database::executeQuery
75
     */
76
    public function testSelectAll(){
77
        $selectAll = self::$db->selectAll('test_table');
78
        $this->assertGreaterThan(1, self::$db->numRows());
79
        $this->assertArrayHasKey('id', $selectAll[0]);
80
    }
81
    
82
    /**
83
     * @covers DBAL\Database::select
84
     * @covers DBAL\Database::selectAll
85
     * @covers DBAL\Database::buildSelectQuery
86
     * @covers DBAL\Database::where
87
     * @covers DBAL\Database::orderBy
88
     * @covers DBAL\Database::limit
89
     * @covers DBAL\Database::executeQuery
90
     */
91
    public function testSelectFailure(){
92
        $this->assertFalse(self::$db->selectAll('test_table', array('id' => 100)));
93
        $this->assertFalse(self::$db->selectAll('unknown_table'));
94
    }
95
    
96
    /**
97
     * @covers DBAL\Database::insert
98
     * @covers DBAL\Database::fields
99
     * @covers DBAL\Database::executeQuery
100
     * @covers DBAL\Database::numRows
101
     */
102
    public function testInsert(){
103
        $this->assertTrue(self::$db->insert('test_table', array('id' => 3, 'name' => 'Third User', 'text_field' => 'Helloooooo', 'number_field' => rand(1, 1000))));
104
    }
105
    
106
    /**
107
     * @covers DBAL\Database::insert
108
     * @covers DBAL\Database::fields
109
     * @covers DBAL\Database::executeQuery
110
     * @covers DBAL\Database::numRows
111
     */
112
    public function tsetInsertFailure(){
113
        $this->assertTrue(self::$db->insert('test_table', array('name' => 'Third User', 'text_field' => NULL, 'number_field' => rand(1, 1000))));
114
    }
115
    
116
    /**
117
     * @covers DBAL\Database::update
118
     * @covers DBAL\Database::fields
119
     * @covers DBAL\Database::where
120
     * @covers DBAL\Database::limit
121
     * @covers DBAL\Database::executeQuery
122
     * @covers DBAL\Database::numRows
123
     */
124
    public function testUpdate(){
125
        $this->assertTrue(self::$db->update('test_table', array('text_field' => 'Altered text', 'number_field' => rand(1, 1000)), array('id' => 3)));
126
    }
127
    
128
    /**
129
     * @covers DBAL\Database::update
130
     * @covers DBAL\Database::fields
131
     * @covers DBAL\Database::where
132
     * @covers DBAL\Database::limit
133
     * @covers DBAL\Database::executeQuery
134
     * @covers DBAL\Database::numRows
135
     */
136
    public function testUpdateFailure(){
137
        $this->assertFalse(self::$db->update('test_table', array('number_field' => 256), array('id' => 1)));
138
    }
139
    
140
    /**
141
     * @covers DBAL\Database::delete
142
     * @covers DBAL\Database::where
143
     * @covers DBAL\Database::limit
144
     * @covers DBAL\Database::executeQuery
145
     * @covers DBAL\Database::numRows
146
     */
147
    public function testDelete(){
148
        $this->assertTrue(self::$db->delete('test_table', array('id' => 3)));
149
    }
150
    
151
    /**
152
     * @covers DBAL\Database::delete
153
     * @covers DBAL\Database::where
154
     * @covers DBAL\Database::limit
155
     * @covers DBAL\Database::executeQuery
156
     * @covers DBAL\Database::numRows
157
     */
158
    public function testDeleteFailure(){
159
        $this->assertFalse(self::$db->delete('test_table', array('id' => 3)));
160
    }
161
    
162
    /**
163
     * @covers DBAL\Database::count
164
     * @covers DBAL\Database::where
165
     * @covers DBAL\Database::executeQuery
166
     */
167
    public function testCount(){
168
        $this->assertEquals(2, self::$db->count('test_table'));
169
    }
170
    
171
    /**
172
     * @covers DBAL\Database::fulltextIndex
173
     */
174
    public function testFulltextIndex(){
175
        $this->markTestIncomplete(
176
          'This test has not been implemented yet.'
177
        );
178
    }
179
    
180
    /**
181
     * @covers DBAL\Database::lastInsertId
182
     */
183
    public function testLastInsertID(){
184
        $this->testInsert();
185
        $this->assertEquals(3, self::$db->lastInsertID());
186
    }
187
    
188
    /**
189
     * @covers DBAL\Database::setCaching
190
     */
191
    public function testSetCaching(){
192
        $this->markTestIncomplete(
193
          'This test has not been implemented yet.'
194
        );
195
    }
196
}
197