BasicCrudTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 189
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
cbo 5
dl 0
loc 189
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 9 1
A tearDown() 0 4 1
A testNonSpecialSelectFetchAssoc() 0 16 1
A testNonSpecialSelectFetchClass() 0 17 1
A testSelectWithJoin() 0 63 1
A testInsert() 0 16 1
A testUpdate() 0 22 1
A testDelete() 0 20 1
1
<?php
2
3
namespace Fwk\Db;
4
5
/**
6
 * Test class for Accessor.
7
 * Generated by PHPUnit on 2012-05-27 at 17:46:42.
8
 */
9
class BasicCrudTest extends \PHPUnit_Framework_TestCase
10
{
11
    protected $connection;
12
13
    /**
14
     * Sets up the fixture, for example, opens a network connection.
15
     * This method is called before a test is executed.
16
     */
17
    protected function setUp()
18
    {
19
        $this->connection = new Connection(array(
20
            'memory'    => true,
21
            'driver'    => 'pdo_sqlite'
22
        ));
23
24
        \FwkDbTestUtil::createTestDb($this->connection);
25
    }
26
27
    protected function tearDown()
28
    {
29
        \FwkDbTestUtil::dropTestDb($this->connection);
30
    }
31
32
    public function testNonSpecialSelectFetchAssoc()
33
    {
34
        $query = Query::factory()
35
            ->insert('fwkdb_test_users')
36
            ->set('username', 'joeBar');
37
38
        $this->connection->execute($query);
39
        $this->assertEquals(1, count($this->connection->table('fwkdb_test_users')->finder()->all()));
40
41
        $query = Query::factory()->select()->from('fwkdb_test_users')->setFetchMode(\PDO::FETCH_ASSOC);
42
        $res = $this->connection->execute($query);
43
44
        $this->assertTrue(is_array($res));
45
        $this->assertEquals(1, count($res));
46
        $this->assertTrue(is_array($res[0]));
47
    }
48
49
    public function testNonSpecialSelectFetchClass()
50
    {
51
        $query = Query::factory()
52
            ->insert('fwkdb_test_users')
53
            ->set('username', 'joeBar');
54
55
        $this->connection->execute($query);
56
        $this->assertEquals(1, count($this->connection->table('fwkdb_test_users')->finder()->all()));
57
58
        $query = Query::factory()->select()->from('fwkdb_test_users')->setFetchMode(\PDO::FETCH_CLASS);
59
        $res = $this->connection->execute($query);
60
61
        $this->assertTrue(is_array($res));
62
        $this->assertEquals(1, count($res));
63
        $this->assertTrue(is_object($res[0]));
64
        $this->assertInstanceOf('\stdClass', $res[0]);
65
    }
66
67
    public function testSelectWithJoin()
68
    {
69
        $query = Query::factory()
70
            ->insert('fwkdb_test_users')
71
            ->set('username', 'joeBar');
72
73
        $this->connection->execute($query);
74
75
        $query = Query::factory()
76
            ->insert('fwkdb_test_emails')
77
            ->values(array(
78
                'email' => '[email protected]',
79
                'verified' => true
80
            ));
81
82
        $this->connection->execute($query);
83
84
        $query = Query::factory()
85
            ->insert('fwkdb_test_emails')
86
            ->values(array(
87
                'email' => '[email protected]',
88
                'verified' => false
89
            ));
90
91
        $this->connection->execute($query);
92
93
        $query = Query::factory()
94
            ->insert('fwkdb_test_users_emails')
95
            ->values(array(
96
                'user_id' => 1,
97
                'email_id' => 1
98
            ));
99
100
        $this->connection->execute($query);
101
102
        $query = Query::factory()
103
            ->insert('fwkdb_test_users_emails')
104
            ->values(array(
105
                'user_id' => 1,
106
                'email_id' => 2
107
            ));
108
109
        $this->connection->execute($query);
110
111
        $this->assertEquals(1, count($this->connection->table('fwkdb_test_users')->finder()->all()));
112
        $this->assertEquals(2, count($this->connection->table('fwkdb_test_emails')->finder()->all()));
113
        $this->assertEquals(2, count($this->connection->table('fwkdb_test_users_emails')->finder()->all()));
114
115
        $query = Query::factory()
116
            ->select()
117
            ->from('fwkdb_test_users u')
118
            ->join('fwkdb_test_users_emails ue', 'u.id', 'ue.user_id', Query::JOIN_LEFT, array('skipped' => true))
119
            ->join('fwkdb_test_emails e', 'e.id', 'ue.email_id', Query::JOIN_LEFT, array('column' => 'emails'));
120
121
        $res = $this->connection->execute($query);
122
123
        $this->assertTrue(is_array($res));
124
        $this->assertEquals(1, count($res));
125
        $entity = $res[0];
126
127
        $this->assertTrue(is_object($entity));
128
        $this->assertEquals(2, count($entity->emails));
129
    }
130
131
    /**
132
     */
133
    public function testInsert()
134
    {
135
        $this->assertEquals(0, count($this->connection->table('fwkdb_test_users')->finder()->all()));
136
        $query = Query::factory()
137
                    ->insert('fwkdb_test_users')
138
                    ->set('username', 'joeBar');
139
140
        $this->connection->execute($query);
141
        $this->assertEquals(1, count($this->connection->table('fwkdb_test_users')->finder()->all()));
142
143
        $query = Query::factory()
144
                    ->insert('fwkdb_test_users')
145
                    ->set('username', 'joeBar2');
146
        $this->connection->execute($query);
147
        $this->assertEquals(2, count($this->connection->table('fwkdb_test_users')->finder()->all()));
148
    }
149
150
    /**
151
     */
152
    public function testUpdate()
153
    {
154
        $query = Query::factory()
155
                    ->insert('fwkdb_test_users')
156
                    ->set('username', 'joeBar');
157
158
        $this->connection->execute($query);
159
160
        $one = $this->connection->table('fwkdb_test_users')->finder()->one(1);
161
        $this->assertNotNull($one);
162
163
        $query = Query::factory()
164
                ->update('fwkdb_test_users')
165
                ->set('username', '?')
166
                ->where('id = ?');
167
168
        $this->connection->execute($query, array('joeBarUPDATED', 1));
169
170
        $one = $this->connection->table('fwkdb_test_users')->finder()->one(1);
171
        $this->assertNotNull($one);
172
        $this->assertEquals("joeBarUPDATED", $one->username);
173
    }
174
175
    /**
176
     */
177
    public function testDelete()
178
    {
179
        $query = Query::factory()
180
                    ->insert('fwkdb_test_users')
181
                    ->set('username', 'joeBar');
182
183
        $this->connection->execute($query);
184
185
        $one = $this->connection->table('fwkdb_test_users')->finder()->one(1);
186
        $this->assertNotNull($one);
187
188
        $query = Query::factory()
189
                ->delete('fwkdb_test_users')
190
                ->where('id = ?');
191
192
        $this->connection->execute($query, array(1));
193
194
        $one = $this->connection->table('fwkdb_test_users')->finder()->one(1);
195
        $this->assertNull($one);
196
    }
197
}
198