Test Failed
Push — master ( a8e19d...ceb7b3 )
by Adam
08:19
created

DatabaseTest::testFulltextIndex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 8
rs 9.4285
c 2
b 0
f 0
cc 2
eloc 4
nc 2
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
    
9
    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...
10
    
11
    /**
12
     * @covers DBAL\Database::__construct
13
     * @covers DBAL\Database::connectToServer
14
     */
15
    public static function setUpBeforeClass(){
0 ignored issues
show
Coding Style introduced by
setUpBeforeClass 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...
16
        self::$db = new Database($GLOBALS['DB_HOST'], $GLOBALS['DB_USER'], $GLOBALS['DB_PASSWD'], $GLOBALS['DB_DBNAME']);
17
    }
18
    
19
    /**
20
     * @covers DBAL\Database::__destruct
21
     */
22
    public static function tearDownAfterClass(){
23
        self::$db = null;
24
    }
25
26
    /**
27
     * @covers DBAL\Database::__construct
28
     * @covers DBAL\Database::connectToServer
29
     */
30
    public function testConnect(){
31
        if(is_object(self::$db)){
32
            $this->assertObjectHasAttribute('db', self::$db);
33
        }
34
        else{
35
            $this->assertFalse(false);
36
        }
37
    }
38
    
39
    /**
40
     * @covers DBAL\Database
41
     */
42
    public function testConnectFailure(){
43
        if(is_object(self::$db)){
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
44
            
45
        }
46
        else{
47
            $this->assertFalse(false);
48
        }
49
    }
50
    
51
    /**
52
     * @covers DBAL\Database::query
53
     */
54
    public function testQuery(){
55
        if(is_object(self::$db)){
56
            $query = self::$db->query("SELECT * FROM `test_table` WHERE `id` = ?", array(1));
57
            $this->assertArrayHasKey('0', $query);
58
            $this->assertCount(1, $query);
59
        }
60
        else{
61
            $this->assertFalse(false);
62
        }
63
    }
64
    
65
    /**
66
     * @covers DBAL\Database::select
67
     * @covers DBAL\Database::buildSelectQuery
68
     * @covers DBAL\Database::where
69
     * @covers DBAL\Database::orderBy
70
     * @covers DBAL\Database::limit
71
     * @covers DBAL\Database::executeQuery
72
     */
73
    public function testSelect(){
74
        if(is_object(self::$db)){
75
            $simpleSelect = self::$db->select('test_table', array('id' => array('>', 1)), '*', array('id' => 'ASC'));
76
            $this->assertArrayHasKey('name', $simpleSelect);
77
        }
78
        else{
79
            $this->assertFalse(false);
80
        }
81
    }
82
    
83
    /**
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 testSelectAll(){
92
        if(is_object(self::$db)){
93
            $selectAll = self::$db->selectAll('test_table');
94
            $this->assertGreaterThan(1, self::$db->numRows());
95
            $this->assertArrayHasKey('id', $selectAll[0]);
96
        }
97
        else{
98
            $this->assertFalse(false);
99
        }
100
    }
101
    
102
    /**
103
     * @covers DBAL\Database::select
104
     * @covers DBAL\Database::selectAll
105
     * @covers DBAL\Database::buildSelectQuery
106
     * @covers DBAL\Database::where
107
     * @covers DBAL\Database::orderBy
108
     * @covers DBAL\Database::limit
109
     * @covers DBAL\Database::executeQuery
110
     */
111
    public function testSelectFailure(){
112
        if(is_object(self::$db)){
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
113
            
114
        }
115
        else{
116
            $this->assertFalse(false);
117
        }
118
    }
119
    
120
    /**
121
     * @covers DBAL\Database::insert
122
     * @covers DBAL\Database::fields
123
     * @covers DBAL\Database::executeQuery
124
     * @covers DBAL\Database::numRows
125
     */
126
    public function testInsert(){
127
        if(is_object(self::$db)){
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
128
            
129
        }
130
        else{
131
            $this->assertFalse(false);
132
        }
133
    }
134
    
135
    /**
136
     * @covers DBAL\Database::insert
137
     * @covers DBAL\Database::fields
138
     * @covers DBAL\Database::executeQuery
139
     * @covers DBAL\Database::numRows
140
     */
141
    public function tsetInsertFailure(){
142
        if(is_object(self::$db)){
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
143
            
144
        }
145
        else{
146
            $this->assertFalse(false);
147
        }
148
    }
149
    
150
    /**
151
     * @covers DBAL\Database::update
152
     * @covers DBAL\Database::fields
153
     * @covers DBAL\Database::where
154
     * @covers DBAL\Database::limit
155
     * @covers DBAL\Database::executeQuery
156
     * @covers DBAL\Database::numRows
157
     */
158
    public function testUpdate(){
159
        if(is_object(self::$db)){
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
160
            
161
        }
162
        else{
163
            $this->assertFalse(false);
164
        }
165
    }
166
    
167
    /**
168
     * @covers DBAL\Database::update
169
     * @covers DBAL\Database::fields
170
     * @covers DBAL\Database::where
171
     * @covers DBAL\Database::limit
172
     * @covers DBAL\Database::executeQuery
173
     * @covers DBAL\Database::numRows
174
     */
175
    public function testUpdateFailure(){
176
        if(is_object(self::$db)){
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
177
            
178
        }
179
        else{
180
            $this->assertFalse(false);
181
        }
182
    }
183
    
184
    /**
185
     * @covers DBAL\Database::delete
186
     * @covers DBAL\Database::where
187
     * @covers DBAL\Database::limit
188
     * @covers DBAL\Database::executeQuery
189
     * @covers DBAL\Database::numRows
190
     */
191
    public function testDelete(){
192
        if(is_object(self::$db)){
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
193
            
194
        }
195
        else{
196
            $this->assertFalse(false);
197
        }
198
    }
199
    
200
    /**
201
     * @covers DBAL\Database::delete
202
     * @covers DBAL\Database::where
203
     * @covers DBAL\Database::limit
204
     * @covers DBAL\Database::executeQuery
205
     * @covers DBAL\Database::numRows
206
     */
207
    public function testDeleteFailure(){
208
        if(is_object(self::$db)){
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
209
            
210
        }
211
        else{
212
            $this->assertFalse(false);
213
        }
214
    }
215
    
216
    /**
217
     * @covers DBAL\Database::count
218
     * @covers DBAL\Database::where
219
     * @covers DBAL\Database::executeQuery
220
     */
221
    public function testCount(){
222
        if(is_object(self::$db)){
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
223
            
224
        }
225
        else{
226
            $this->assertFalse(false);
227
        }
228
    }
229
    
230
    /**
231
     * @covers DBAL\Database::fulltextIndex
232
     */
233
    public function testFulltextIndex(){
234
        if(is_object(self::$db)){
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
235
            
236
        }
237
        else{
238
            $this->assertFalse(false);
239
        }
240
    }
241
    
242
    /**
243
     * @covers DBAL\Database::numRows
244
     * @covers DBAL\Database::rowCount
245
     */
246
    public function testNumRows(){
247
        if(is_object(self::$db)){
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
248
            
249
        }
250
        else{
251
            $this->assertFalse(false);
252
        }
253
    }
254
    
255
    /**
256
     * @covers DBAL\Database::lastInsertId
257
     */
258
    public function testLastInsertID(){
259
        if(is_object(self::$db)){
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
260
            
261
        }
262
        else{
263
            $this->assertFalse(false);
264
        }
265
    }
266
    
267
    /**
268
     * @covers DBAL\Database::setCaching
269
     */
270
    public function testCaching(){
271
        if(is_object(self::$db)){
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
272
            
273
        }
274
        else{
275
            $this->assertFalse(false);
276
        }
277
    }
278
}
279