Unit test updates
[isso.git] / UnitTest / ApiTest.php
1 <?php
2
3 require_once 'PHPUnit/Framework.php';
4 require_once ISSO . '/Api.php';
5
6 /**
7 * ApiTest
8 *
9 * @author rsesek
10 * @copyright Copyright (c)2002 - 2007, Blue Static
11 * @package ISSO Tests
12 *
13 */
14 class ApiTest extends PHPUnit_Framework_TestCase
15 {
16 private $fixture;
17 private $db;
18
19 public function setUp()
20 {
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();
24
25 $this->db->query("
26 CREATE TABLE pre_apitest
27 (
28 id int not null,
29 atext text not null,
30 avarchar varchar(200) not null,
31 abin blob not null,
32 abool boolean not null,
33 aint integer not null,
34 afloat float not null,
35 PRIMARY KEY (id)
36 )
37 ");
38 }
39
40 public function tearDown()
41 {
42 $this->db->query("DROP TABLE pre_apitest");
43 $this->fixture = null;
44 }
45
46 public function testFailInsert()
47 {
48 try
49 {
50 $this->fixture->insert();
51 $this->fail('exception expected');
52 }
53 catch (ApiException $e)
54 {
55 $i = 0;
56 foreach ($e->getExceptions() AS $exc)
57 {
58 if ($exc instanceof FieldException)
59 {
60 $i++;
61 }
62 }
63 $this->assertEquals(sizeof($e->getExceptions()), $i);
64 }
65 }
66
67 public function testInsert()
68 {
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);
75 try
76 {
77 $this->fixture->insert();
78 }
79 catch (Exception $e)
80 {
81 $this->fail('unexpected exception');
82 }
83 }
84
85 public function testMissingFields()
86 {
87 $this->fixture->set('atext', 'moocow');
88 try
89 {
90 $this->fixture->insert();
91 $this->fail('exception expected');
92 }
93 catch (ApiException $e)
94 {}
95 }
96
97 public function testCondition()
98 {
99 $this->fixture->set('id', 4);
100 $this->fixture->set('atext', 'foo');
101
102 $this->fixture->setCondition();
103 $this->assertEquals('id = 4', $this->fixture->T_getCondition());
104
105 $this->fixture->setCondition(array('atext'));
106 $this->assertEquals("atext = 'foo'", $this->fixture->T_getCondition());
107
108 $this->fixture->setCondition('aint = 3');
109 $this->assertEquals('aint = 3', $this->fixture->T_getCondition());
110
111 try
112 {
113 $this->fixture->setCondition(array('__noexist__'));
114 $this->fail('exception expected');
115 }
116 catch (Exception $e)
117 {}
118 }
119 }
120
121 class TestApiFixture extends BSApi
122 {
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)
131 );
132
133 protected $table = 'apitest';
134
135 protected $prefix = 'pre_';
136
137 public function T_getCondition()
138 {
139 return $this->condition;
140 }
141 }
142
143 ?>