3 require_once 'PHPUnit/Framework.php';
4 require_once ISSO
. '/Api.php';
10 * @copyright Copyright (c)2002 - 2007, 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,
40 public function tearDown()
42 $this->db
->query("DROP TABLE pre_apitest");
43 $this->fixture
= null
;
46 public function testFailInsert()
50 $this->fixture
->insert();
51 $this->fail('exception expected');
53 catch (ApiException
$e)
56 foreach ($e->getExceptions() AS $exc)
58 if ($exc instanceof FieldException
)
63 $this->assertEquals(sizeof($e->getExceptions()), $i);
67 public function testInsert()
69 $this->fixture
->set('atext', 'moocow');
70 $this->fixture
->set('avarchar', 'hello');
71 $this->fixture
->set('abin', 'åß∂œ∑†å∂ƒåß∂ƒå∂ƒ');
72 $this->fixture
->set('abool', true
);
73 $this->fixture
->set('aint', 3);
74 $this->fixture
->set('afloat', 2.53);
77 $this->fixture
->insert();
81 $this->fail('unexpected exception');
85 public function testMissingFields()
87 $this->fixture
->set('atext', 'moocow');
90 $this->fixture
->insert();
91 $this->fail('exception expected');
93 catch (ApiException
$e)
97 public function testCondition()
99 $this->fixture
->set('id', 4);
100 $this->fixture
->set('atext', 'foo');
102 $this->fixture
->setCondition();
103 $this->assertEquals('id = 4', $this->fixture
->T_getCondition());
105 $this->fixture
->setCondition(array('atext'));
106 $this->assertEquals("atext = 'foo'", $this->fixture
->T_getCondition());
108 $this->fixture
->setCondition('aint = 3');
109 $this->assertEquals('aint = 3', $this->fixture
->T_getCondition());
113 $this->fixture
->setCondition(array('__noexist__'));
114 $this->fail('exception expected');
121 class TestApiFixture
extends BSApi
123 protected $fields = array(
124 'id' => array(TYPE_UINT
, REQ_AUTO
),
125 'atext' => array(TYPE_STR
, REQ_NO
),
126 'avarchar' => array(TYPE_STR
, REQ_NO
),
127 'abin' => array(TYPE_BIN
, REQ_NO
),
128 'abool' => array(TYPE_BOOL
, REQ_YES
),
129 'aint' => array(TYPE_INT
, REQ_YES
),
130 'afloat' => array(TYPE_FLOAT
, REQ_NO
)
133 protected $table = 'apitest';
135 protected $prefix = 'pre_';
137 public function T_getCondition()
139 return $this->condition
;