3 require_once 'PHPUnit/Framework.php';
4 require_once ISSO
. '/Api.php';
5 require_once ISSO
. '/DbMySql.php';
11 * @copyright Copyright (c)2005 - 2008, Blue Static
15 class ApiTest
extends PHPUnit_Framework_TestCase
20 public function setUp()
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();
27 CREATE TABLE pre_apitest
31 avarchar varchar(200) not null,
33 abool boolean not null,
34 aint integer not null,
35 afloat float not null,
36 autoset varchar(200) not null,
42 public function tearDown()
44 $this->db
->query("DROP TABLE pre_apitest");
45 $this->fixture
= null;
48 public function testFailInsert()
52 $this->fixture
->insert();
53 $this->fail('exception expected');
55 catch (ApiException
$e)
58 foreach ($e->getExceptions() AS $exc)
60 if ($exc instanceof FieldException
)
65 $this->assertEquals(sizeof($e->getExceptions()), $i);
69 public function testInsert()
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);
79 $this->fixture
->insert();
83 $this->fail('unexpected exception');
87 public function testMissingFields()
89 $this->fixture
->set('atext', 'moocow');
92 $this->fixture
->insert();
93 $this->fail('exception expected');
95 catch (ApiException
$e)
99 public function testCondition()
101 $this->fixture
->set('id', 4);
102 $this->fixture
->set('atext', 'foo');
104 $this->fixture
->setCondition();
105 $this->assertEquals('id = 4', $this->fixture
->T_getCondition());
107 $this->fixture
->setCondition(array('atext'));
108 $this->assertEquals("atext = 'foo'", $this->fixture
->T_getCondition());
110 $this->fixture
->setCondition('aint = 3');
111 $this->assertEquals('aint = 3', $this->fixture
->T_getCondition());
115 $this->fixture
->setCondition(array('__noexist__'));
116 $this->fail('exception expected');
122 public function testActionMethods()
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');
128 $this->fixture
= $mock;
133 public function testZeroValidation()
135 $this->fixture
->set('aint', 0);
138 $this->fixture
->insert();
139 $this->fail('exception expected');
144 $this->fixture
= new TestApiFixture();
145 $this->fixture
->set('aint', 4);
146 $this->fixture
->set('avarchar', 'foo');
149 $this->fixture
->insert();
153 $this->fail('unexpected exception');
157 public function testEmptyValidation()
159 $this->fixture
->set('avarchar', '');
162 $this->fixture
->insert();
163 $this->fail('exception expected');
168 $this->fixture
= new TestApiFixture();
169 $this->fixture
->set('aint', 3);
170 $this->fixture
->set('avarchar', 'foo');
173 $this->fixture
->insert();
177 $this->fail('unexpected exception');
181 public function testUniqueValidation()
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();
189 $this->fixture
= new TestApiFixture();
190 $this->fixture
->set('afloat', 5);
191 $this->fixture
->set('id', 1);
194 $this->fixture
->insert();
195 $this->fail('exception expected');
200 $this->fixture
= new TestApiFixture();
201 $this->fixture
->set('afloat', 6);
202 $this->fixture
->set('id', 16);
203 $this->fixture
->set('avarchar', 'foo');
206 $this->fixture
->insert();
210 $this->fail('unexpected exception');
214 public function testFetch()
216 $this->fixture
->set('id', 1);
217 $this->assertFalse($this->fixture
->fetch());
221 $this->fixture
->set('id', 1);
222 $this->assertTrue($this->fixture
->fetch());
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);
232 public function testRemove()
234 $this->fixture
->set('id', 1);
235 $this->fixture
->set('aint', 4);
236 $this->fixture
->set('avarchar', 'foo');
237 $this->fixture
->insert();
239 $this->fixture
= new TestApiFixture();
240 $this->fixture
->set('id', 1);
244 $this->fixture
->remove();
248 $this->fail('unexpected exception');
251 $this->assertEquals(1, $this->fixture
->record
['id']);
252 $this->assertEquals(4, $this->fixture
->record
['aint']);
254 $this->fixture
= new TestApiFixture();
255 $this->fixture
->set('id', 1);
256 $this->assertFalse($this->fixture
->fetch());
259 public function testUpdate()
261 $this->fixture
->set('id', 1);
262 $this->fixture
->set('aint', 4);
263 $this->fixture
->set('avarchar', 'foo');
264 $this->fixture
->insert();
266 $this->fixture
= new TestApiFixture();
267 $this->fixture
->set('id', 1);
268 $this->fixture
->set('aint', 6);
271 $this->fixture
->update();
275 $es = $e->getExceptions();
276 $this->fail('unexpected exception: ' . print_r($es[0]->getMessage()));
279 $this->fixture
= new TestApiFixture();
280 $this->fixture
->set('id', 1);
281 $this->fixture
->fetch();
283 $this->assertEquals(6, $this->fixture
->record
['aint']);
286 public function testAutoSet()
288 $this->fixture
->set('id', 1);
289 $this->fixture
->set('avarchar', 'foo');
290 $this->fixture
->insert();
292 $this->fixture
= new TestApiFixture();
293 $this->fixture
->set('id', 1);
294 $this->fixture
->fetch();
296 $this->assertEquals('this is auto', $this->fixture
->record
['autoset']);
299 public function testFieldException()
303 throw new FieldException('this is an error', 'field');
307 $this->assertEquals('this is an error', $e->getMessage());
308 $this->assertEquals('field', $e->getField());
313 class TestApiFixture
extends BSApi
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
)
326 protected $table = 'apitest';
328 protected $prefix = 'pre_';
330 public function T_getCondition()
332 return $this->condition
;
335 protected function validate_aint()
337 $this->_verifyIsNotZero('aint');
340 protected function validate_avarchar()
342 $this->_verifyIsNotEmpty('avarchar');
345 protected function validate_afloat()
347 $this->_verifyIsNotUnique('afloat');
350 protected function set_autoset()
352 $this->set('autoset', 'this is auto');