3 require_once 'PHPUnit/Framework.php';
4 require_once ISSO
. '/Api.php';
10 * @copyright Copyright (c)2005 - 2008, Blue Static
14 class ApiTest
extends PHPUnit_Framework_TestCase
19 public function setUp()
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();
26 CREATE TABLE pre_apitest
30 avarchar varchar(200) not null,
32 abool boolean not null,
33 aint integer not null,
34 afloat float not null,
35 autoset varchar(200) not null,
41 public function tearDown()
43 $this->db
->query("DROP TABLE pre_apitest");
44 $this->fixture
= null
;
47 public function testFailInsert()
51 $this->fixture
->insert();
52 $this->fail('exception expected');
54 catch (ApiException
$e)
57 foreach ($e->getExceptions() AS $exc)
59 if ($exc instanceof FieldException
)
64 $this->assertEquals(sizeof($e->getExceptions()), $i);
68 public function testInsert()
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);
78 $this->fixture
->insert();
82 $this->fail('unexpected exception');
86 public function testMissingFields()
88 $this->fixture
->set('atext', 'moocow');
91 $this->fixture
->insert();
92 $this->fail('exception expected');
94 catch (ApiException
$e)
98 public function testCondition()
100 $this->fixture
->set('id', 4);
101 $this->fixture
->set('atext', 'foo');
103 $this->fixture
->setCondition();
104 $this->assertEquals('id = 4', $this->fixture
->T_getCondition());
106 $this->fixture
->setCondition(array('atext'));
107 $this->assertEquals("atext = 'foo'", $this->fixture
->T_getCondition());
109 $this->fixture
->setCondition('aint = 3');
110 $this->assertEquals('aint = 3', $this->fixture
->T_getCondition());
114 $this->fixture
->setCondition(array('__noexist__'));
115 $this->fail('exception expected');
121 public function testActionMethods()
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');
127 $this->fixture
= $mock;
132 public function testZeroValidation()
134 $this->fixture
->set('aint', 0);
137 $this->fixture
->insert();
138 $this->fail('exception expected');
143 $this->fixture
= new TestApiFixture();
144 $this->fixture
->set('aint', 4);
145 $this->fixture
->set('avarchar', 'foo');
148 $this->fixture
->insert();
152 $this->fail('unexpected exception');
156 public function testEmptyValidation()
158 $this->fixture
->set('avarchar', '');
161 $this->fixture
->insert();
162 $this->fail('exception expected');
167 $this->fixture
= new TestApiFixture();
168 $this->fixture
->set('aint', 3);
169 $this->fixture
->set('avarchar', 'foo');
172 $this->fixture
->insert();
176 $this->fail('unexpected exception');
180 public function testUniqueValidation()
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();
188 $this->fixture
= new TestApiFixture();
189 $this->fixture
->set('afloat', 5);
190 $this->fixture
->set('id', 1);
193 $this->fixture
->insert();
194 $this->fail('exception expected');
199 $this->fixture
= new TestApiFixture();
200 $this->fixture
->set('afloat', 6);
201 $this->fixture
->set('id', 16);
202 $this->fixture
->set('avarchar', 'foo');
205 $this->fixture
->insert();
209 $this->fail('unexpected exception');
213 public function testFetch()
215 $this->fixture
->set('id', 1);
216 $this->assertFalse($this->fixture
->fetch());
220 $this->fixture
->set('id', 1);
221 $this->assertTrue($this->fixture
->fetch());
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);
231 public function testRemove()
233 $this->fixture
->set('id', 1);
234 $this->fixture
->set('aint', 4);
235 $this->fixture
->set('avarchar', 'foo');
236 $this->fixture
->insert();
238 $this->fixture
= new TestApiFixture();
239 $this->fixture
->set('id', 1);
243 $this->fixture
->remove();
247 $this->fail('unexpected exception');
250 $this->assertEquals(1, $this->fixture
->record
['id']);
251 $this->assertEquals(4, $this->fixture
->record
['aint']);
253 $this->fixture
= new TestApiFixture();
254 $this->fixture
->set('id', 1);
255 $this->assertFalse($this->fixture
->fetch());
258 public function testUpdate()
260 $this->fixture
->set('id', 1);
261 $this->fixture
->set('aint', 4);
262 $this->fixture
->set('avarchar', 'foo');
263 $this->fixture
->insert();
265 $this->fixture
= new TestApiFixture();
266 $this->fixture
->set('id', 1);
267 $this->fixture
->set('aint', 6);
270 $this->fixture
->update();
274 $es = $e->getExceptions();
275 $this->fail('unexpected exception: ' . print_r($es[0]->getMessage()));
278 $this->fixture
= new TestApiFixture();
279 $this->fixture
->set('id', 1);
280 $this->fixture
->fetch();
282 $this->assertEquals(6, $this->fixture
->record
['aint']);
285 public function testAutoSet()
287 $this->fixture
->set('id', 1);
288 $this->fixture
->set('avarchar', 'foo');
289 $this->fixture
->insert();
291 $this->fixture
= new TestApiFixture();
292 $this->fixture
->set('id', 1);
293 $this->fixture
->fetch();
295 $this->assertEquals('this is auto', $this->fixture
->record
['autoset']);
298 public function testFieldException()
302 throw new FieldException('this is an error', 'field');
306 $this->assertEquals('this is an error', $e->getMessage());
307 $this->assertEquals('field', $e->getField());
312 class TestApiFixture
extends BSApi
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
)
325 protected $table = 'apitest';
327 protected $prefix = 'pre_';
329 public function T_getCondition()
331 return $this->condition
;
334 protected function validate_aint()
336 $this->_verifyIsNotZero('aint');
339 protected function validate_avarchar()
341 $this->_verifyIsNotEmpty('avarchar');
344 protected function validate_afloat()
346 $this->_verifyIsNotUnique('afloat');
349 protected function set_autoset()
351 $this->set('autoset', 'this is auto');