QueryTest::testEntity()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Fwk\Db;
4
5
/**
6
 */
7
class QueryTest extends \PHPUnit_Framework_TestCase
8
{
9
    /**
10
     * @var Query
11
     */
12
    protected $object;
13
14
    /**
15
     * Sets up the fixture, for example, opens a network connection.
16
     * This method is called before a test is executed.
17
     */
18
    protected function setUp()
19
    {
20
        $this->object = new Query;
21
    }
22
23
    /**
24
     * Tears down the fixture, for example, closes a network connection.
25
     * This method is called after a test is executed.
26
     */
27
    protected function tearDown()
28
    {
29
        unset($this->object);
30
    }
31
32
    /**
33
     */
34
    public function testSelect()
35
    {
36
        $this->object->select('column1, column2');
37
        $this->assertEquals('column1, column2', $this->object['select']);
38
        $this->assertEquals(Query::TYPE_SELECT, $this->object->getType());
39
    }
40
41
    /**
42
     */
43
    public function testFrom()
44
    {
45
        $this->object->from('table alias');
46
        $this->assertEquals('table alias', $this->object['from']);
47
        $this->object->from('table', 'alias');
48
        $this->assertEquals('table alias', $this->object['from']);
49
    }
50
51
    /**
52
     */
53
    public function testFetchMode()
54
    {
55
        $this->object->setFetchMode(\PDO::FETCH_ASSOC);
56
        $this->assertEquals(\PDO::FETCH_ASSOC, $this->object->getFetchMode());
57
    }
58
59
    /**
60
     */
61
    public function testDelete()
62
    {
63
        $this->object->delete('table');
64
        $this->assertEquals('table', $this->object['delete']);
65
        $this->assertEquals(Query::TYPE_DELETE, $this->object->getType());
66
    }
67
68
    /**
69
     */
70
    public function testInsert()
71
    {
72
        $this->object->insert('table');
73
        $this->assertEquals('table', $this->object['insert']);
74
        $this->assertEquals(Query::TYPE_INSERT, $this->object->getType());
75
    }
76
77
    /**
78
     */
79
    public function testUpdate()
80
    {
81
        $this->object->update('table');
82
        $this->assertEquals('table', $this->object['update']);
83
        $this->assertEquals(Query::TYPE_UPDATE, $this->object->getType());
84
    }
85
86
    /**
87
     */
88
    public function testWhere()
89
    {
90
        $this->object->where('condition = 1');
91
        $this->assertEquals('condition = 1', $this->object['where']);
92
    }
93
94
    /**
95
     */
96
    public function testAndWhere()
97
    {
98
        $this->assertEquals(null, $this->object['wheres']);
99
        $this->object->andWhere('condition = 1');
100
        $this->assertTrue(is_array($this->object['wheres']));
101
        $this->assertEquals(1, count($this->object['wheres']));
102
    }
103
104
    /**
105
     */
106
    public function testOrWhere()
107
    {
108
        $this->assertEquals(null, $this->object['wheres']);
109
        $this->object->orWhere('condition = 1');
110
        $this->assertTrue(is_array($this->object['wheres']));
111
        $this->assertEquals(1, count($this->object['wheres']));
112
    }
113
114
    /**
115
     */
116
    public function testLimit()
117
    {
118
        $this->object->limit(100);
119
        $this->assertTrue(is_array($this->object['limit']));
120
        $this->assertArrayHasKey('max', $this->object['limit']);
121
        $this->assertArrayHasKey('first', $this->object['limit']);
122
        $this->assertEquals(100, $this->object['limit']['max']);
123
        $this->object->limit(100, 50);
124
        $this->assertEquals(50, $this->object['limit']['first']);
125
    }
126
127
    /**
128
     */
129
    public function testGroupBy()
130
    {
131
        $this->object->groupBy('field');
132
        $this->assertEquals('field', $this->object['groupBy']);
133
    }
134
135
    /**
136
     */
137
    public function testOrderBy()
138
    {
139
        $this->object->orderBy('column', true);
140
        $this->assertTrue(is_array($this->object['orderBy']));
141
        $this->assertEquals(array(
142
            'column' => 'column',
143
            'order' => true
144
        ), $this->object['orderBy']);
145
    }
146
147
    /**
148
     */
149
    public function testFactory()
150
    {
151
        $this->assertInstanceOf('\Fwk\Db\Query', Query::factory());
152
    }
153
154
    /**
155
     */
156
    public function testSet()
157
    {
158
        $this->assertNull($this->object['values']);
159
        $this->object->set('key', 'value');
160
        $this->assertTrue(is_array($this->object['values']));
161
        $this->assertEquals(1, count($this->object['values']));
162
        $this->object->set('key2', 'value');
163
        $this->assertEquals(2, count($this->object['values']));
164
    }
165
166
    /**
167
     */
168
    public function testValues()
169
    {
170
        $this->assertNull($this->object['values']);
171
        $this->object->values(array('key' => 'value', 'key2' => 'value'));
172
        $this->assertTrue(is_array($this->object['values']));
173
        $this->assertEquals(2, count($this->object['values']));
174
        $this->object->set('key3', 'value');
175
        $this->assertEquals(3, count($this->object['values']));
176
    }
177
178
    /**
179
     */
180
    public function testJoin()
181
    {
182
        $this->assertNull($this->object['joins']);
183
        $this->object->join('joinTable', 'id');
184
        $this->assertTrue(is_array($this->object['joins']));
185
        $this->assertEquals(1, count($this->object['joins']));
186
    }
187
188
    /**
189
     */
190
    public function testEntity()
191
    {
192
        $this->object->entity('\stdClass');
193
        $this->assertEquals('\stdClass', $this->object['entity']);
194
    }
195
}
196