db = BSApp::LoadModule('DbMySql'); $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, 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) {} } } class TestApiFixture extends BSApi { protected $fields = array( 'id' => array(TYPE_UINT, REQ_AUTO), 'atext' => array(TYPE_STR, REQ_NO), 'avarchar' => array(TYPE_STR, REQ_NO), 'abin' => array(TYPE_BIN, REQ_NO), 'abool' => array(TYPE_BOOL, REQ_YES), 'aint' => array(TYPE_INT, REQ_YES), 'afloat' => array(TYPE_FLOAT, REQ_NO) ); protected $table = 'apitest'; protected $prefix = 'pre_'; public function T_getCondition() { return $this->condition; } } ?>