Test Failed
Push — master ( 919319...c5b8d7 )
by Adam
02:03
created

DatabaseTest   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 195
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 15
Bugs 0 Features 0
Metric Value
wmc 20
lcom 1
cbo 4
dl 0
loc 195
rs 10
c 15
b 0
f 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
A getConnection() 0 4 1
A getDataSet() 0 3 1
A setUp() 0 4 1
A tearDownAfterClass() 0 3 1
A testConnect() 0 4 1
A testConnectFailure() 0 4 1
A testQuery() 0 5 1
A testSelect() 0 4 1
A testSelectAll() 0 5 1
A testSelectFailure() 0 4 1
A testInsert() 0 3 1
A tsetInsertFailure() 0 3 1
A testUpdate() 0 3 1
A testUpdateFailure() 0 3 1
A testDelete() 0 3 1
A testDeleteFailure() 0 3 1
A testCount() 0 3 1
A testFulltextIndex() 0 3 1
A testLastInsertID() 0 4 1
A testCaching() 0 3 1
1
<?php
2
namespace DBAL\Tests;
3
4
use DBAL\Database;
5
use PHPUnit\Framework\TestCase;
6
use PHPUnit\DbUnit\TestCaseTrait;
7
8
class DatabaseTest extends TestCase{
9
    
10
    use TestCaseTrait;
11
    
12
    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...
13
    
14
    public function getConnection(){
15
        $pdo = new \PDO('sqlite::memory:');
16
        return $this->createDefaultDBConnection($pdo, ':memory:');
17
    }
18
19
    public function getDataSet(){
20
        return $this->createXMLDataSet(dirname(__FILE__).'/dataset/test_table.xml');
21
    }
22
    
23
    /**
24
     * @covers DBAL\Database::__construct
25
     * @covers DBAL\Database::connectToServer
26
     */
27
    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...
28
        //$this->getConnection()->createDataSet(['test_table']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
85% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
29
        self::$db = new Database('localhost', $GLOBALS['DB_USER'], $GLOBALS['DB_PASSWD'], $GLOBALS['DB_DBNAME']);
30
    }
31
    
32
    /**
33
     * @covers DBAL\Database::__destruct
34
     */
35
    public static function tearDownAfterClass(){
36
        self::$db = null;
37
    }
38
39
    /**
40
     * @covers DBAL\Database::__construct
41
     * @covers DBAL\Database::connectToServer
42
     */
43
    public function testConnect(){
44
        $this->getConnection()->createDataSet(['test_table']);
45
        $this->assertTrue(self::$db->isConnected());
46
    }
47
    
48
    /**
49
     * @covers DBAL\Database
50
     */
51
    public function testConnectFailure(){
52
        $db = new Database('http://fakehost', '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...
53
        $this->assertFalse($db->isConnected());
54
    }
55
    
56
    /**
57
     * @covers DBAL\Database::query
58
     */
59
    public function testQuery(){
60
        $query = self::$db->query("SELECT * FROM `test_table` WHERE `id` = ?", array(1));
61
        $this->assertArrayHasKey('0', $query);
62
        $this->assertCount(1, $query);
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
        $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
     * @covers DBAL\Database::buildSelectQuery
81
     * @covers DBAL\Database::where
82
     * @covers DBAL\Database::orderBy
83
     * @covers DBAL\Database::limit
84
     * @covers DBAL\Database::executeQuery
85
     */
86
    public function testSelectAll(){
87
        $selectAll = self::$db->selectAll('test_table');
88
        $this->assertGreaterThan(1, self::$db->numRows());
89
        $this->assertArrayHasKey('id', $selectAll[0]);
90
    }
91
    
92
    /**
93
     * @covers DBAL\Database::select
94
     * @covers DBAL\Database::selectAll
95
     * @covers DBAL\Database::buildSelectQuery
96
     * @covers DBAL\Database::where
97
     * @covers DBAL\Database::orderBy
98
     * @covers DBAL\Database::limit
99
     * @covers DBAL\Database::executeQuery
100
     */
101
    public function testSelectFailure(){
102
        $this->assertFalse(self::$db->selectAll('test_table', array('id' => 100)));
103
        $this->assertFalse(self::$db->selectAll('unknown_table'));
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 testInsert(){
113
        $this->assertTrue(self::$db->insert('test_table', array('id' => 3, 'name' => 'Third User', 'text_field' => 'Helloooooo', 'number_field' => rand(1, 1000))));
114
    }
115
    
116
    /**
117
     * @covers DBAL\Database::insert
118
     * @covers DBAL\Database::fields
119
     * @covers DBAL\Database::executeQuery
120
     * @covers DBAL\Database::numRows
121
     */
122
    public function tsetInsertFailure(){
123
        $this->assertTrue(self::$db->insert('test_table', array('name' => 'Third User', 'text_field' => NULL, 'number_field' => rand(1, 1000))));
124
    }
125
    
126
    /**
127
     * @covers DBAL\Database::update
128
     * @covers DBAL\Database::fields
129
     * @covers DBAL\Database::where
130
     * @covers DBAL\Database::limit
131
     * @covers DBAL\Database::executeQuery
132
     * @covers DBAL\Database::numRows
133
     */
134
    public function testUpdate(){
135
        $this->assertTrue(self::$db->update('test_table', array('text_field' => 'Altered text', 'number_field' => rand(1, 1000)), array('id' => 3)));
136
    }
137
    
138
    /**
139
     * @covers DBAL\Database::update
140
     * @covers DBAL\Database::fields
141
     * @covers DBAL\Database::where
142
     * @covers DBAL\Database::limit
143
     * @covers DBAL\Database::executeQuery
144
     * @covers DBAL\Database::numRows
145
     */
146
    public function testUpdateFailure(){
147
        $this->assertFalse(self::$db->update('test_table', array('number_field' => 256), array('id' => 1)));
148
    }
149
    
150
    /**
151
     * @covers DBAL\Database::delete
152
     * @covers DBAL\Database::where
153
     * @covers DBAL\Database::limit
154
     * @covers DBAL\Database::executeQuery
155
     * @covers DBAL\Database::numRows
156
     */
157
    public function testDelete(){
158
        $this->assertTrue(self::$db->delete('test_table', array('id' => 3)));
159
    }
160
    
161
    /**
162
     * @covers DBAL\Database::delete
163
     * @covers DBAL\Database::where
164
     * @covers DBAL\Database::limit
165
     * @covers DBAL\Database::executeQuery
166
     * @covers DBAL\Database::numRows
167
     */
168
    public function testDeleteFailure(){
169
        $this->assertFalse(self::$db->delete('test_table', array('id' => 3)));
170
    }
171
    
172
    /**
173
     * @covers DBAL\Database::count
174
     * @covers DBAL\Database::where
175
     * @covers DBAL\Database::executeQuery
176
     */
177
    public function testCount(){
178
        $this->assertEquals(2, self::$db->count('test_table'));
179
    }
180
    
181
    /**
182
     * @covers DBAL\Database::fulltextIndex
183
     */
184
    public function testFulltextIndex(){
185
        
186
    }
187
    
188
    /**
189
     * @covers DBAL\Database::lastInsertId
190
     */
191
    public function testLastInsertID(){
192
        $this->testInsert();
193
        $this->assertEquals(3, self::$db->lastInsertID());
194
    }
195
    
196
    /**
197
     * @covers DBAL\Database::setCaching
198
     */
199
    public function testCaching(){
200
        
201
    }
202
}
203