]> src.bluestatic.org Git - isso.git/blob - UnitTest/DatabaseTestAbstract.php
Cleaing up error reporting and error triggering in Api.php
[isso.git] / UnitTest / DatabaseTestAbstract.php
1 <?php
2
3 require_once 'PHPUnit/Framework.php';
4
5 abstract class DatabaseTestAbstract extends PHPUnit_Framework_TestCase
6 {
7 public function setUp()
8 {
9 throw new Exception('You must impelement setUp() yourself!');
10 }
11
12 public function tearDown()
13 {
14 throw new Exception('You must implement tearDown() yourself!');
15 }
16
17 public abstract function testConnect();
18
19 public function testInsertSelect()
20 {
21 try
22 {
23 $this->fixture->query("INSERT INTO test (textstuff) VALUES ('moo'), ('foo'), ('hey'), ('foo'), ('boo'), ('poo')");
24 }
25 catch (BSDbException $e)
26 {
27 $this->fail('unexpected exception');
28 }
29
30 try
31 {
32 $res = $this->fixture->query("SELECT * FROM test WHERE textstuff = 'foo'");
33
34 $row = $res->fetchArray();
35 $this->assertNotNull($row['textstuff']);
36
37 $row = $res->fetchArray();
38 $this->assertNotNull($row['textstuff']);
39
40 $row = $res->fetchArray();
41 $this->assertNull($row['textstuff']);
42 }
43 catch (BSDbException $e)
44 {
45 $this->fail('unexpected exception');
46 }
47 }
48
49 public function testEscapeString()
50 {
51 $this->assertEquals("Robert\'s castle", $this->fixture->escapeString("Robert's castle"));
52 }
53
54 public function testFetchObject()
55 {
56 try
57 {
58 $this->fixture->query("INSERT INTO test (textstuff) VALUES ('foo')");
59 }
60 catch (BSDbException $e)
61 {
62 $this->fail('unexpected exception');
63 }
64
65 try
66 {
67 $res = $this->fixture->query("SELECT * FROM test");
68 $obj = $res->fetchObject();
69
70 $this->assertNotNull($obj);
71 $this->assertEquals('foo', $obj->textstuff);
72 }
73 catch (BSDbException $e)
74 {
75 $this->fail('unexpected exception');
76 }
77 }
78
79 public function testQueryFirst()
80 {
81 try
82 {
83 $this->fixture->query("INSERT INTO test (textstuff) VALUES ('abc'), ('123'), ('def')");
84 }
85 catch (BSDbException $e)
86 {
87 $this->fail('unexpected exception');
88 }
89
90 try
91 {
92 $res = $this->fixture->queryFirst("SELECT * FROM test ORDER BY id DESC");
93 $this->assertEquals('def', $res['textstuff']);
94 }
95 catch (BSDbException $e)
96 {
97 $this->fail('unexpected exception');
98 }
99 }
100
101 public function testInsertId()
102 {
103 try
104 {
105 $this->fixture->query("INSERT INTO test (textstuff) VALUES ('abc')");
106 $this->assertEquals(1, $this->fixture->insertId('test', 'textstuff'));
107
108 $this->fixture->query("INSERT INTO test (textstuff) VALUES ('123')");
109 $this->assertEquals(2, $this->fixture->insertId('test', 'textstuff'));
110 }
111 catch (BSDbException $e)
112 {
113 $this->fail('unexpected exception');
114 }
115 }
116
117 public function testNumRows()
118 {
119 try
120 {
121 $this->fixture->query("INSERT INTO test (textstuff) VALUES ('123'), ('456'), ('789')");
122
123 $res = $this->fixture->query("SELECT * FROM test");
124 $this->assertEquals(3, $res->size());
125
126 $res = $this->fixture->query("SELECT * FROM test WHERE textstuff = '--invalid value--'");
127 $this->assertEquals(0, $res->size());
128 }
129 catch (BSDbException $e)
130 {
131 $this->fail('unexpected exception');
132 }
133 }
134
135 public function testAffectedRows()
136 {
137 try
138 {
139 $this->fixture->query("INSERT INTO test (textstuff) VALUES ('123'), ('456'), ('123')");
140
141 $res = $this->fixture->query("UPDATE test SET textstuff = 'abc' WHERE textstuff = '123'");
142 $this->assertEquals(2, $this->fixture->affectedRows());
143
144 $res = $this->fixture->query("SELECT * FROM test WHERE textstuff = 'abc'");
145 $this->assertEquals(2, $res->size());
146 }
147 catch (BSDbException $e)
148 {
149 $this->fail('unexpected exception');
150 }
151 }
152
153 public function testInvalidQuery()
154 {
155 try
156 {
157 $this->fixture->query("SELECT * FROM foobar");
158 $this->fail('exception expected');
159 }
160 catch (BSDbException $e)
161 {
162 $this->assertEquals('SELECT * FROM foobar', $e->getQuery());
163 }
164 }
165
166 public function testTransactions()
167 {
168 $this->fixture->begin();
169 $this->fixture->query("INSERT INTO test (textstuff) VALUES ('foo')");
170 $this->fixture->commit();
171
172 $res = $this->fixture->query("SELECT * FROM test WHERE textstuff = 'foo'");
173 $this->assertEquals(1, $res->size());
174
175 $this->fixture->begin();
176 $this->fixture->query("UPDATE test SET textstuff = 'abc'");
177 $this->fixture->rollback();
178
179 $res = $this->fixture->query("SELECT * FROM test WHERE textstuff = 'abc'");
180 $this->assertEquals(0, $res->size());
181
182 $res = $this->fixture->query("SELECT * FROM test WHERE textstuff = 'foo'");
183 $this->assertEquals(1, $res->size());
184 }
185 }
186
187 ?>