Happy new year!
[isso.git] / UnitTest / ApiTest.php
1 <?php
2
3 require_once 'PHPUnit/Framework.php';
4 require_once ISSO . '/Api.php';
5
6 /**
7 * ApiTest
8 *
9 * @author rsesek
10 * @copyright Copyright (c)2005 - 2008, Blue Static
11 * @package ISSO Tests
12 *
13 */
14 class ApiTest extends PHPUnit_Framework_TestCase
15 {
16 private $fixture;
17 private $db;
18
19 public function setUp()
20 {
21 $this->db = BSApp::LoadModule('DbMySql');
22 $this->db->connect(TEST_DB_MYSQL_HOST, TEST_DB_MYSQL_USER, TEST_DB_MYSQL_PASSWORD, TEST_DB_MYSQL_DATABASE);
23 $this->fixture = new TestApiFixture();
24
25 $this->db->query("
26 CREATE TABLE pre_apitest
27 (
28 id int not null,
29 atext text not null,
30 avarchar varchar(200) not null,
31 abin blob not null,
32 abool boolean not null,
33 aint integer not null,
34 afloat float not null,
35 autoset varchar(200) not null,
36 PRIMARY KEY (id)
37 )
38 ");
39 }
40
41 public function tearDown()
42 {
43 $this->db->query("DROP TABLE pre_apitest");
44 $this->fixture = null;
45 }
46
47 public function testFailInsert()
48 {
49 try
50 {
51 $this->fixture->insert();
52 $this->fail('exception expected');
53 }
54 catch (ApiException $e)
55 {
56 $i = 0;
57 foreach ($e->getExceptions() AS $exc)
58 {
59 if ($exc instanceof FieldException)
60 {
61 $i++;
62 }
63 }
64 $this->assertEquals(sizeof($e->getExceptions()), $i);
65 }
66 }
67
68 public function testInsert()
69 {
70 $this->fixture->set('atext', 'moocow');
71 $this->fixture->set('avarchar', 'hello');
72 $this->fixture->set('abin', 'åß∂œ∑†å∂ƒåß∂ƒå∂ƒ');
73 $this->fixture->set('abool', true);
74 $this->fixture->set('aint', 3);
75 $this->fixture->set('afloat', 2.53);
76 try
77 {
78 $this->fixture->insert();
79 }
80 catch (Exception $e)
81 {
82 $this->fail('unexpected exception');
83 }
84 }
85
86 public function testMissingFields()
87 {
88 $this->fixture->set('atext', 'moocow');
89 try
90 {
91 $this->fixture->insert();
92 $this->fail('exception expected');
93 }
94 catch (ApiException $e)
95 {}
96 }
97
98 public function testCondition()
99 {
100 $this->fixture->set('id', 4);
101 $this->fixture->set('atext', 'foo');
102
103 $this->fixture->setCondition();
104 $this->assertEquals('id = 4', $this->fixture->T_getCondition());
105
106 $this->fixture->setCondition(array('atext'));
107 $this->assertEquals("atext = 'foo'", $this->fixture->T_getCondition());
108
109 $this->fixture->setCondition('aint = 3');
110 $this->assertEquals('aint = 3', $this->fixture->T_getCondition());
111
112 try
113 {
114 $this->fixture->setCondition(array('__noexist__'));
115 $this->fail('exception expected');
116 }
117 catch (Exception $e)
118 {}
119 }
120
121 public function testActionMethods()
122 {
123 $mock = $this->getMock('TestApiFixture', array('pre_insert', 'post_insert'));
124 $mock->expects($this->once())->method('pre_insert');
125 $mock->expects($this->once())->method('post_insert');
126
127 $this->fixture = $mock;
128
129 $this->testInsert();
130 }
131
132 public function testZeroValidation()
133 {
134 $this->fixture->set('aint', 0);
135 try
136 {
137 $this->fixture->insert();
138 $this->fail('exception expected');
139 }
140 catch (Exception $e)
141 {}
142
143 $this->fixture = new TestApiFixture();
144 $this->fixture->set('aint', 4);
145 $this->fixture->set('avarchar', 'foo');
146 try
147 {
148 $this->fixture->insert();
149 }
150 catch (Exception $e)
151 {
152 $this->fail('unexpected exception');
153 }
154 }
155
156 public function testEmptyValidation()
157 {
158 $this->fixture->set('avarchar', '');
159 try
160 {
161 $this->fixture->insert();
162 $this->fail('exception expected');
163 }
164 catch (Exception $e)
165 {}
166
167 $this->fixture = new TestApiFixture();
168 $this->fixture->set('aint', 3);
169 $this->fixture->set('avarchar', 'foo');
170 try
171 {
172 $this->fixture->insert();
173 }
174 catch (Exception $e)
175 {
176 $this->fail('unexpected exception');
177 }
178 }
179
180 public function testUniqueValidation()
181 {
182 $this->fixture = new TestApiFixture();
183 $this->fixture->set('afloat', 5);
184 $this->fixture->set('id', 1);
185 $this->fixture->set('avarchar', 'foo');
186 $this->fixture->insert();
187
188 $this->fixture = new TestApiFixture();
189 $this->fixture->set('afloat', 5);
190 $this->fixture->set('id', 1);
191 try
192 {
193 $this->fixture->insert();
194 $this->fail('exception expected');
195 }
196 catch (Exception $e)
197 {}
198
199 $this->fixture = new TestApiFixture();
200 $this->fixture->set('afloat', 6);
201 $this->fixture->set('id', 16);
202 $this->fixture->set('avarchar', 'foo');
203 try
204 {
205 $this->fixture->insert();
206 }
207 catch (Exception $e)
208 {
209 $this->fail('unexpected exception');
210 }
211 }
212
213 public function testFetch()
214 {
215 $this->fixture->set('id', 1);
216 $this->assertFalse($this->fixture->fetch());
217
218 $this->testInsert();
219
220 $this->fixture->set('id', 1);
221 $this->assertTrue($this->fixture->fetch());
222
223 $this->assertEquals($this->fixture->record['atext'], 'moocow');
224 $this->assertEquals($this->fixture->record['avarchar'], 'hello');
225 $this->assertEquals($this->fixture->record['abin'], 'åß∂œ∑†å∂ƒåß∂ƒå∂ƒ');
226 $this->assertEquals((bool)$this->fixture->record['abool'], true);
227 $this->assertEquals($this->fixture->record['aint'], 3);
228 $this->assertEquals($this->fixture->record['afloat'], 2.53);
229 }
230
231 public function testRemove()
232 {
233 $this->fixture->set('id', 1);
234 $this->fixture->set('aint', 4);
235 $this->fixture->set('avarchar', 'foo');
236 $this->fixture->insert();
237
238 $this->fixture = new TestApiFixture();
239 $this->fixture->set('id', 1);
240
241 try
242 {
243 $this->fixture->remove();
244 }
245 catch (Exception $e)
246 {
247 $this->fail('unexpected exception');
248 }
249
250 $this->assertEquals(1, $this->fixture->record['id']);
251 $this->assertEquals(4, $this->fixture->record['aint']);
252
253 $this->fixture = new TestApiFixture();
254 $this->fixture->set('id', 1);
255 $this->assertFalse($this->fixture->fetch());
256 }
257
258 public function testUpdate()
259 {
260 $this->fixture->set('id', 1);
261 $this->fixture->set('aint', 4);
262 $this->fixture->set('avarchar', 'foo');
263 $this->fixture->insert();
264
265 $this->fixture = new TestApiFixture();
266 $this->fixture->set('id', 1);
267 $this->fixture->set('aint', 6);
268 try
269 {
270 $this->fixture->update();
271 }
272 catch (Exception $e)
273 {
274 $es = $e->getExceptions();
275 $this->fail('unexpected exception: ' . print_r($es[0]->getMessage()));
276 }
277
278 $this->fixture = new TestApiFixture();
279 $this->fixture->set('id', 1);
280 $this->fixture->fetch();
281
282 $this->assertEquals(6, $this->fixture->record['aint']);
283 }
284
285 public function testAutoSet()
286 {
287 $this->fixture->set('id', 1);
288 $this->fixture->set('avarchar', 'foo');
289 $this->fixture->insert();
290
291 $this->fixture = new TestApiFixture();
292 $this->fixture->set('id', 1);
293 $this->fixture->fetch();
294
295 $this->assertEquals('this is auto', $this->fixture->record['autoset']);
296 }
297
298 public function testFieldException()
299 {
300 try
301 {
302 throw new FieldException('this is an error', 'field');
303 }
304 catch (Exception $e)
305 {
306 $this->assertEquals('this is an error', $e->getMessage());
307 $this->assertEquals('field', $e->getField());
308 }
309 }
310 }
311
312 class TestApiFixture extends BSApi
313 {
314 protected $fields = array(
315 'id' => array(TYPE_UINT, REQ_AUTO),
316 'atext' => array(TYPE_STR, REQ_NO),
317 'avarchar' => array(TYPE_STR, REQ_YES),
318 'abin' => array(TYPE_BIN, REQ_NO),
319 'abool' => array(TYPE_BOOL, REQ_NO),
320 'aint' => array(TYPE_INT, REQ_NO),
321 'afloat' => array(TYPE_FLOAT, REQ_NO),
322 'autoset' => array(TYPE_STR, REQ_SET)
323 );
324
325 protected $table = 'apitest';
326
327 protected $prefix = 'pre_';
328
329 public function T_getCondition()
330 {
331 return $this->condition;
332 }
333
334 protected function validate_aint()
335 {
336 $this->_verifyIsNotZero('aint');
337 }
338
339 protected function validate_avarchar()
340 {
341 $this->_verifyIsNotEmpty('avarchar');
342 }
343
344 protected function validate_afloat()
345 {
346 $this->_verifyIsNotUnique('afloat');
347 }
348
349 protected function set_autoset()
350 {
351 $this->set('autoset', 'this is auto');
352 }
353 }
354
355 ?>