db = new BSDbMySql(); $this->db->connect(TEST_DB_MYSQL_HOST, TEST_DB_MYSQL_USER, TEST_DB_MYSQL_PASSWORD, TEST_DB_MYSQL_DATABASE); $this->fixture = new TestApiFixture(); $this->db->query(" CREATE TABLE pre_apitest ( id int not null, atext text not null, avarchar varchar(200) not null, abin blob not null, abool boolean not null, aint integer not null, afloat float not null, autoset varchar(200) not null, PRIMARY KEY (id) ) "); } public function tearDown() { $this->db->query("DROP TABLE pre_apitest"); $this->fixture = null; } public function testFailInsert() { try { $this->fixture->insert(); $this->fail('exception expected'); } catch (ApiException $e) { $i = 0; foreach ($e->getExceptions() as $exc) { if ($exc instanceof FieldException) { $i++; } } $this->assertEquals(sizeof($e->getExceptions()), $i); } } public function testInsert() { $this->fixture->set('atext', 'moocow'); $this->fixture->set('avarchar', 'hello'); $this->fixture->set('abin', 'åß∂œ∑†å∂ƒåß∂ƒå∂ƒ'); $this->fixture->set('abool', true); $this->fixture->set('aint', 3); $this->fixture->set('afloat', 2.53); try { $this->fixture->insert(); } catch (Exception $e) { $this->fail('unexpected exception'); } } public function testMissingFields() { $this->fixture->set('atext', 'moocow'); try { $this->fixture->insert(); $this->fail('exception expected'); } catch (ApiException $e) {} } public function testCondition() { $this->fixture->set('id', 4); $this->fixture->set('atext', 'foo'); $this->fixture->setCondition(); $this->assertEquals('id = 4', $this->fixture->T_getCondition()); $this->fixture->setCondition(array('atext')); $this->assertEquals("atext = 'foo'", $this->fixture->T_getCondition()); $this->fixture->setCondition('aint = 3'); $this->assertEquals('aint = 3', $this->fixture->T_getCondition()); try { $this->fixture->setCondition(array('__noexist__')); $this->fail('exception expected'); } catch (Exception $e) {} } public function testActionMethods() { $mock = $this->getMock('TestApiFixture', array('pre_insert', 'post_insert')); $mock->expects($this->once())->method('pre_insert'); $mock->expects($this->once())->method('post_insert'); $this->fixture = $mock; $this->testInsert(); } public function testZeroValidation() { $this->fixture->set('aint', 0); try { $this->fixture->insert(); $this->fail('exception expected'); } catch (Exception $e) {} $this->fixture = new TestApiFixture(); $this->fixture->set('aint', 4); $this->fixture->set('avarchar', 'foo'); try { $this->fixture->insert(); } catch (Exception $e) { $this->fail('unexpected exception'); } } public function testEmptyValidation() { $this->fixture->set('avarchar', ''); try { $this->fixture->insert(); $this->fail('exception expected'); } catch (Exception $e) {} $this->fixture = new TestApiFixture(); $this->fixture->set('aint', 3); $this->fixture->set('avarchar', 'foo'); try { $this->fixture->insert(); } catch (Exception $e) { $this->fail('unexpected exception'); } } public function testUniqueValidation() { $this->fixture = new TestApiFixture(); $this->fixture->set('afloat', 5); $this->fixture->set('id', 1); $this->fixture->set('avarchar', 'foo'); $this->fixture->insert(); $this->fixture = new TestApiFixture(); $this->fixture->set('afloat', 5); $this->fixture->set('id', 1); try { $this->fixture->insert(); $this->fail('exception expected'); } catch (Exception $e) {} $this->fixture = new TestApiFixture(); $this->fixture->set('afloat', 6); $this->fixture->set('id', 16); $this->fixture->set('avarchar', 'foo'); try { $this->fixture->insert(); } catch (Exception $e) { $this->fail('unexpected exception'); } } public function testFetch() { $this->fixture->set('id', 1); $this->assertFalse($this->fixture->fetch()); $this->testInsert(); $this->fixture->set('id', 1); $this->assertTrue($this->fixture->fetch()); $this->assertEquals($this->fixture->record['atext'], 'moocow'); $this->assertEquals($this->fixture->record['avarchar'], 'hello'); $this->assertEquals($this->fixture->record['abin'], 'åß∂œ∑†å∂ƒåß∂ƒå∂ƒ'); $this->assertEquals((bool)$this->fixture->record['abool'], true); $this->assertEquals($this->fixture->record['aint'], 3); $this->assertEquals($this->fixture->record['afloat'], 2.53); } public function testRemove() { $this->fixture->set('id', 1); $this->fixture->set('aint', 4); $this->fixture->set('avarchar', 'foo'); $this->fixture->insert(); $this->fixture = new TestApiFixture(); $this->fixture->set('id', 1); try { $this->fixture->delete(); } catch (Exception $e) { $this->fail('unexpected exception'); } $this->assertEquals(1, $this->fixture->record['id']); $this->assertEquals(4, $this->fixture->record['aint']); $this->fixture = new TestApiFixture(); $this->fixture->set('id', 1); $this->assertFalse($this->fixture->fetch()); } public function testUpdate() { $this->fixture->set('id', 1); $this->fixture->set('aint', 4); $this->fixture->set('avarchar', 'foo'); $this->fixture->insert(); $this->fixture = new TestApiFixture(); $this->fixture->set('id', 1); $this->fixture->set('aint', 6); try { $this->fixture->update(); } catch (Exception $e) { $es = $e->getExceptions(); $this->fail('unexpected exception: ' . print_r($es[0]->getMessage())); } $this->fixture = new TestApiFixture(); $this->fixture->set('id', 1); $this->fixture->fetch(); $this->assertEquals(6, $this->fixture->record['aint']); } public function testAutoSet() { $this->fixture->set('id', 1); $this->fixture->set('avarchar', 'foo'); $this->fixture->insert(); $this->fixture = new TestApiFixture(); $this->fixture->set('id', 1); $this->fixture->fetch(); $this->assertEquals('this is auto', $this->fixture->record['autoset']); } public function testFieldException() { try { throw new FieldException('this is an error', 'field'); } catch (Exception $e) { $this->assertEquals('this is an error', $e->getMessage()); $this->assertEquals('field', $e->getField()); } } } class TestApiFixture extends BSApi { protected $fields = array( 'id' => array(TYPE_UINT, REQ_AUTO), 'atext' => array(TYPE_STR, REQ_NO), 'avarchar' => array(TYPE_STR, REQ_YES), 'abin' => array(TYPE_BIN, REQ_NO), 'abool' => array(TYPE_BOOL, REQ_NO), 'aint' => array(TYPE_INT, REQ_NO), 'afloat' => array(TYPE_FLOAT, REQ_NO), 'autoset' => array(TYPE_STR, REQ_SET) ); protected $table = 'apitest'; protected $prefix = 'pre_'; public function T_getCondition() { return $this->condition; } protected function validate_aint() { $this->_verifyIsNotZero('aint'); } protected function validate_avarchar() { $this->_verifyIsNotEmpty('avarchar'); } protected function validate_afloat() { $this->_verifyIsNotUnique('afloat'); } protected function set_autoset() { $this->set('autoset', 'this is auto'); } } ?>