|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Elchroy\PotatoORM\PotatoQuery; |
|
4
|
|
|
use Mockery as m; |
|
5
|
|
|
|
|
6
|
|
|
class PotatoQueryTest extends \PHPUnit_Framework_TestCase |
|
7
|
|
|
{ |
|
8
|
|
|
public $mockConnector; |
|
9
|
|
|
public $mockStatement; |
|
10
|
|
|
private $mockQuery; |
|
11
|
|
|
|
|
12
|
|
|
public function setUp() |
|
13
|
|
|
{ |
|
14
|
|
|
$this->mockConnector = m::mock('Elchroy\PotatoORM\PotatoConnector'); |
|
15
|
|
|
$this->mockConnection = m::mock('PDO'); |
|
16
|
|
|
$this->mockConnector->shouldReceive('setConnection')->andReturn($this->mockConnection); |
|
17
|
|
|
$this->mockConnector->connection = $this->mockConnection; |
|
|
|
|
|
|
18
|
|
|
$this->mockStatement = m::mock('PDOStatement'); |
|
19
|
|
|
$this->mockQuery = new PotatoQuery($this->mockConnector); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
View Code Duplication |
public function testGetFrom() |
|
|
|
|
|
|
23
|
|
|
{ |
|
24
|
|
|
$sql = 'SELECT name, price FROM orders'; |
|
25
|
|
|
$this->mockConnection->shouldReceive('prepare')->once()->with($sql)->andReturn($this->mockStatement); |
|
26
|
|
|
$this->mockStatement->shouldReceive('execute'); |
|
27
|
|
|
$this->mockStatement->shouldReceive('fetchAll')->with(\PDO::FETCH_CLASS)->andReturn(new stdClass()); |
|
28
|
|
|
|
|
29
|
|
|
$result = $this->mockQuery->getFrom('orders', 'name, price'); |
|
30
|
|
|
$this->assertInstanceOf('stdClass', $result); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @expectedException Elchroy\PotatoORMExceptions\FaultyExecutionException |
|
35
|
|
|
* |
|
36
|
|
|
* @expectedExceptionMessage There was a problem with excecuting this query. |
|
37
|
|
|
*/ |
|
38
|
|
|
public function testGetFromThrowsExceptionForWrongExecution() |
|
39
|
|
|
{ |
|
40
|
|
|
$sql = 'SELECT name, price FROM orders'; |
|
41
|
|
|
$this->mockConnection->shouldReceive('prepare')->once()->with($sql)->andReturn($this->mockStatement); |
|
42
|
|
|
$this->mockStatement->shouldReceive('execute')->andThrow('Elchroy\PotatoORMExceptions\FaultyExecutionException', 'There was a problem with excecuting this query.'); |
|
43
|
|
|
$result = $this->mockQuery->getFrom('orders', 'name, price'); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @expectedException Elchroy\PotatoORMExceptions\NoRecordException |
|
48
|
|
|
* |
|
49
|
|
|
* @expectedExceptionMessage The table (orders) is empty. |
|
50
|
|
|
*/ |
|
51
|
|
View Code Duplication |
public function testGetFromThrowsExceptionForNoRecordFound() |
|
|
|
|
|
|
52
|
|
|
{ |
|
53
|
|
|
$sql = 'SELECT name, price FROM orders'; |
|
54
|
|
|
$this->mockConnection->shouldReceive('prepare')->once()->with($sql)->andReturn($this->mockStatement); |
|
55
|
|
|
$this->mockStatement->shouldReceive('execute'); |
|
56
|
|
|
$this->mockStatement->shouldReceive('fetchAll')->with(\PDO::FETCH_CLASS)->andThrow('Elchroy\PotatoORMExceptions\NoRecordException', 'The table (orders) is empty.'); |
|
57
|
|
|
// $this->mockStatement->shouldReceive('fetchObject')->with('orders')->andReturn(true); |
|
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
$result = $this->mockQuery->getFrom('orders', 'name, price'); |
|
60
|
|
|
$this->assertInstanceOf('stdClass', $result); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @expectedException Elchroy\PotatoORMExceptions\FaultyOrNoTableException |
|
65
|
|
|
* |
|
66
|
|
|
* @expectedExceptionMessage There seems to be a problem. Please confirm if the 'people' table exists in the database. |
|
67
|
|
|
*/ |
|
68
|
|
|
public function testGetFromThrowsException() |
|
69
|
|
|
{ |
|
70
|
|
|
$sql = 'SELECT name, price FROM people'; |
|
71
|
|
|
$this->mockConnection->shouldReceive('prepare')->once()->with($sql)->andReturn(false); |
|
72
|
|
|
$result = $this->mockQuery->getFrom('people', 'name, price'); |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @expectedException Elchroy\PotatoORMExceptions\FaultyOrNoTableException |
|
77
|
|
|
* |
|
78
|
|
|
* @expectedExceptionMessage There seems to be a problem. Please confirm if the 'people' table exists in the database. |
|
79
|
|
|
*/ |
|
80
|
|
View Code Duplication |
public function testGetOneThrowsException() |
|
|
|
|
|
|
81
|
|
|
{ |
|
82
|
|
|
$sql = 'SELECT * FROM people WHERE id = :id '; |
|
83
|
|
|
$this->mockConnection->shouldReceive('prepare')->once()->with($sql)->andReturn(false); |
|
84
|
|
|
|
|
85
|
|
|
$result = $this->mockQuery->getOne('people', 43); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
View Code Duplication |
public function testGetOne() |
|
|
|
|
|
|
89
|
|
|
{ |
|
90
|
|
|
$sql = 'SELECT * FROM people WHERE id = :id '; |
|
91
|
|
|
$this->mockConnection->shouldReceive('prepare')->once()->with($sql)->andReturn($this->mockStatement); |
|
92
|
|
|
$this->mockStatement->shouldReceive('bindParam')->once()->with(':id', 43); |
|
93
|
|
|
$this->mockStatement->shouldReceive('execute'); |
|
94
|
|
|
$this->mockStatement->shouldReceive('fetchObject')->with('people')->andReturn(new stdClass()); |
|
95
|
|
|
|
|
96
|
|
|
$result = $this->mockQuery->getOne('people', 43); |
|
97
|
|
|
$this->assertInstanceOf('stdClass', $result); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function testStoreInWorks() |
|
101
|
|
|
{ |
|
102
|
|
|
$data = ['name' => 'Diamane', 'rooms' => 400]; |
|
103
|
|
|
$sql = 'INSERT INTO hotels (name, rooms) VALUES (?, ?)'; |
|
104
|
|
|
$this->mockConnection->shouldReceive('prepare')->once()->with($sql)->andReturn($this->mockStatement); |
|
105
|
|
|
$this->mockStatement->shouldReceive('bindParam')->once()->with(1, 'Diamane'); |
|
106
|
|
|
$this->mockStatement->shouldReceive('bindParam')->once()->with(2, 400); |
|
107
|
|
|
$this->mockStatement->shouldReceive('execute')->andReturn(true); |
|
108
|
|
|
|
|
109
|
|
|
$result = $this->mockQuery->storeIn('hotels', $data); |
|
|
|
|
|
|
110
|
|
|
$this->assertEquals(true, $result); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @expectedException Elchroy\PotatoORMExceptions\FaultyOrNoTableException |
|
115
|
|
|
* |
|
116
|
|
|
* @expectedExceptionMessage There seems to be a problem. Please confirm if the 'people' table exists in the database. |
|
117
|
|
|
*/ |
|
118
|
|
|
public function testStoreInThrowsException() |
|
119
|
|
|
{ |
|
120
|
|
|
$data = ['name' => 'Diamane', 'rooms' => 400]; |
|
121
|
|
|
$sql = 'INSERT INTO people (name, rooms) VALUES (?, ?)'; |
|
122
|
|
|
$this->mockConnection->shouldReceive('prepare')->once()->with($sql)->andReturn(false); |
|
123
|
|
|
|
|
124
|
|
|
$result = $this->mockQuery->storeIn('people', $data); |
|
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function testGetColumnsWorks() |
|
128
|
|
|
{ |
|
129
|
|
|
$data = ['name' => 'Diamane', 'rooms' => 400]; |
|
130
|
|
|
$columnsString = '(name, rooms)'; |
|
131
|
|
|
$result = $this->mockQuery->getColumns($data); |
|
132
|
|
|
$this->assertEquals($columnsString, $result); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function testCountWorks() |
|
136
|
|
|
{ |
|
137
|
|
|
$data = ['name' => 'Diamane', 'rooms' => 400]; |
|
138
|
|
|
$this->assertEquals(2, count($data)); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
View Code Duplication |
public function testDeleteWorks() |
|
|
|
|
|
|
142
|
|
|
{ |
|
143
|
|
|
$sql = 'DELETE FROM people WHERE id = :id '; |
|
144
|
|
|
$this->mockConnection->shouldReceive('prepare')->with($sql)->andReturn($this->mockStatement); |
|
145
|
|
|
$this->mockStatement->shouldReceive('bindParam')->with(':id', 43); |
|
146
|
|
|
$this->mockStatement->shouldReceive('execute')->andReturn(true); |
|
147
|
|
|
|
|
148
|
|
|
$result = $this->mockQuery->deleteFrom('people', 43); |
|
149
|
|
|
$this->assertEquals(true, $result); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @expectedException Elchroy\PotatoORMExceptions\FaultyOrNoTableException |
|
154
|
|
|
* |
|
155
|
|
|
* @expectedExceptionMessage There seems to be a problem. Please confirm if the 'people' table exists in the database. |
|
156
|
|
|
*/ |
|
157
|
|
View Code Duplication |
public function testDeleteThrowsException() |
|
|
|
|
|
|
158
|
|
|
{ |
|
159
|
|
|
$sql = 'DELETE FROM people WHERE id = :id '; |
|
160
|
|
|
$this->mockConnection->shouldReceive('prepare')->with($sql)->andReturn(false); |
|
161
|
|
|
|
|
162
|
|
|
$result = $this->mockQuery->deleteFrom('people', 43); |
|
|
|
|
|
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @expectedException Elchroy\PotatoORMExceptions\NoRecordException |
|
167
|
|
|
* |
|
168
|
|
|
* @expectedExceptionMessage Record 43 : Not found found in this table people. |
|
169
|
|
|
*/ |
|
170
|
|
View Code Duplication |
public function testGetOneThrowsExceptionForNoRecord() |
|
|
|
|
|
|
171
|
|
|
{ |
|
172
|
|
|
$sql = 'SELECT * FROM people WHERE id = :id '; |
|
173
|
|
|
$this->mockConnection->shouldReceive('prepare')->once()->with($sql)->andReturn($this->mockStatement); |
|
174
|
|
|
$this->mockStatement->shouldReceive('bindParam')->once()->with(':id', 43); |
|
175
|
|
|
$this->mockStatement->shouldReceive('execute'); |
|
176
|
|
|
$this->mockStatement->shouldReceive('fetchObject')->with('people')->andThrow('Elchroy\PotatoORMExceptions\NoRecordException', 'Record 43 : Not found found in this table people.'); |
|
177
|
|
|
|
|
178
|
|
|
$result = $this->mockQuery->getOne('people', 43); |
|
|
|
|
|
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @expectedException Elchroy\PotatoORMExceptions\NoRecordException |
|
183
|
|
|
* |
|
184
|
|
|
* @expectedExceptionMessage Record 4 : Not found found in this table (states). |
|
185
|
|
|
*/ |
|
186
|
|
|
public function testNoRecordExceptionWithId() |
|
187
|
|
|
{ |
|
188
|
|
|
$this->mockQuery->throwNoRecordException('states', 4); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @expectedException Elchroy\PotatoORMExceptions\NoRecordException |
|
193
|
|
|
* |
|
194
|
|
|
* @expectedExceptionMessage The table (states) is empty. |
|
195
|
|
|
*/ |
|
196
|
|
|
public function testNoRecordExceptionWithoutId() |
|
197
|
|
|
{ |
|
198
|
|
|
$this->mockQuery->throwNoRecordException('states'); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
public function testUpdateFunctionworks() |
|
202
|
|
|
{ |
|
203
|
|
|
$sql = 'UPDATE hotels SET name = :name_val, location = :location_val WHERE id = :id_val'; |
|
204
|
|
|
$this->mockConnection->shouldReceive('prepare')->with($sql)->andReturn($this->mockStatement); |
|
205
|
|
|
$this->mockStatement->shouldReceive('bindValue')->with(':name_val', 'Diamane'); |
|
206
|
|
|
$this->mockStatement->shouldReceive('bindValue')->with(':location_val', 'CapeTown, S-Africa'); |
|
207
|
|
|
$this->mockStatement->shouldReceive('bindValue')->with(':id_val', 32); |
|
208
|
|
|
$this->mockStatement->shouldReceive('execute')->andReturn(true); |
|
209
|
|
|
|
|
210
|
|
|
$result = $this->mockQuery->updateAt('hotels', ['id' => 32, 'name' => 'Diamane', 'location' => 'CapeTown, S-Africa']); |
|
211
|
|
|
$this->assertEquals(true, $result); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* @expectedException Elchroy\PotatoORMExceptions\FaultyOrNoTableException |
|
216
|
|
|
* |
|
217
|
|
|
* @expectedExceptionMessage There seems to be a problem. Please confirm if the 'people' table exists in the database. |
|
218
|
|
|
*/ |
|
219
|
|
|
public function testUpdateFunctionThrowsException() |
|
220
|
|
|
{ |
|
221
|
|
|
$sql = 'UPDATE people SET name = :name_val, location = :location_val WHERE id = :id_val'; |
|
222
|
|
|
$this->mockConnection->shouldReceive('prepare')->with($sql)->andReturn(false); |
|
223
|
|
|
|
|
224
|
|
|
$result = $this->mockQuery->updateAt('people', ['id' => 32, 'name' => 'Diamane', 'location' => 'CapeTown, S-Africa']); |
|
|
|
|
|
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
public function testputQuesMarksWorks() |
|
228
|
|
|
{ |
|
229
|
|
|
$result = $this->mockQuery->putQuesMarks(3); |
|
230
|
|
|
$this->assertEquals('?, ?, ?', $result); |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: