ConnectionTest::testAutoConnect()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
namespace Fwk\Db;
4
use Fwk\Db\Events\BeforeQueryEvent;
5
6
/**
7
 * Test class for EventDispatcher.
8
 */
9
class ConnectionTest extends \PHPUnit_Framework_TestCase
10
{
11
    /**
12
     * @var Connection
13
     */
14
    protected $object;
15
16
    /**
17
     * Sets up the fixture, for example, opens a network connection.
18
     * This method is called before a test is executed.
19
     */
20
    protected function setUp()
21
    {
22
        $this->object = new Connection(array(
23
            'memory'    => true,
24
            'driver'    => 'pdo_sqlite'
25
        ));
26
    }
27
28
    public function testConnect()
29
    {
30
        $this->assertFalse($this->object->isConnected());
31
        $this->assertTrue($this->object->connect());
32
        $this->assertTrue($this->object->isConnected());
33
        $this->assertFalse($this->object->isError());
34
    }
35
    
36
    public function testConnectFail()
37
    {
38
        $this->setExpectedException('Fwk\Db\Exceptions\ConnectionErrorException');
39
        $this->object = new Connection(array(
40
            'driver'    => 'pdo_mysql',
41
            'host'  => 'inexistant.example.com-no',
42
            'user'  => 'testEUH',
43
            'autoConnect' => true
44
        ));
45
    }
46
    
47
    public function testAutoConnect()
48
    {
49
        $this->object = new Connection(array(
50
            'memory'    => true,
51
            'driver'    => 'pdo_sqlite',
52
            'autoConnect' => true
53
        ));
54
        $this->assertTrue($this->object->isConnected());
55
        // coverage
56
        $this->assertEquals(Connection::STATE_CONNECTED, $this->object->getState());
57
    }
58
    
59
    public function testConnectFailErrorState()
60
    {
61
        try {
62
            $this->object = new Connection(array(
63
                'driver' => 'pdo_mysql',
64
                'host'  => 'inexistant.example.com-no',
65
                'user'  => 'testEUH'
66
            ));
67
            $this->object->connect();
68
        } catch(\Exception $e) { }
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
69
        
70
        $this->assertTrue($this->object->isError());
71
    }
72
73
    public function testOptions()
74
    {
75
        $this->assertEquals(array('memory' => true, 'driver' => 'pdo_sqlite'), $this->object->getOptions());
76
        $this->object->setOptions(array(
77
            'testOpt1' => "value1",
78
            'testOpt2' => "value2"
79
        ));
80
81
        $this->assertEquals(array(
82
            'memory'    => true,
83
            'driver'    => 'pdo_sqlite',
84
            'testOpt1' => "value1",
85
            'testOpt2' => "value2"
86
        ), $this->object->getOptions());
87
88
        $this->assertEquals("value2", $this->object->get('testOpt2'));
89
        $this->object->setOptions(array(
90
            'testOpt2' => "merged"
91
        ));
92
        $this->assertEquals("merged", $this->object->get('testOpt2'));
93
        $this->object->set('testOpt1', "override");
94
        $this->assertEquals("override", $this->object->get('testOpt1'));
95
        $this->assertEquals("default", $this->object->get('inexistant', "default"));
96
    }
97
98
    public function testDisconnect()
99
    {
100
        $this->assertTrue($this->object->disconnect());
101
        $this->assertFalse($this->object->isConnected());
102
        $this->assertTrue($this->object->connect());
103
        $this->assertTrue($this->object->isConnected());
104
        $this->assertTrue($this->object->disconnect());
105
        $this->assertFalse($this->object->isConnected());
106
    }
107
108
    public function testTableNotExists()
109
    {
110
        $this->setExpectedException('\Fwk\Db\Exceptions\TableNotFoundException');
111
        $tbl = $this->object->table('nonExistant');
0 ignored issues
show
Unused Code introduced by
$tbl is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
112
    }
113
 
114
    public function testTable()
115
    {
116
        $this->prepareTestTable();
117
        $tbl = $this->object->table('test_table');
118
        $this->assertInstanceOf('Fwk\Db\Table', $tbl);
119
    }
120
121
    public function testNewQueryBridge()
122
    {
123
        $this->assertInstanceOf('\Fwk\Db\QueryBridge', $this->object->newQueryBrige());
124
    }
125
126
    protected function prepareTestTable()
127
    {
128
        $schema = $this->object->getSchema();
129
130
        $myTable = $schema->createTable("test_table");
131
        $myTable->addColumn("id", "integer", array("unsigned" => true));
132
        $myTable->addColumn("username", "string", array("length" => 32));
133
        $myTable->setPrimaryKey(array("id"));
134
        $myTable->addUniqueIndex(array("username"));
135
    }
136
    
137
    public function testTransaction()
138
    {
139
        $this->assertInstanceOf('Fwk\Db\Connection', $this->object->beginTransaction());
140
        $this->assertInstanceOf('Fwk\Db\Connection', $this->object->commit());
141
        $this->assertInstanceOf('Fwk\Db\Connection', $this->object->beginTransaction());
142
        $this->assertInstanceOf('Fwk\Db\Connection', $this->object->rollBack());
143
    }
144
}
145