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