3 require_once 'PHPUnit/Framework.php';
5 abstract class DatabaseTestAbstract
extends PHPUnit_Framework_TestCase
7 public function setUp()
9 throw new Exception('You must impelement setUp() yourself!');
12 public function tearDown()
14 throw new Exception('You must implement tearDown() yourself!');
17 public abstract function testConnect();
19 public function testInsertSelect()
23 $this->fixture
->query("INSERT INTO test (textstuff) VALUES ('moo'), ('foo'), ('hey'), ('foo'), ('boo'), ('poo')");
25 catch (BSDbException
$e)
27 $this->fail('unexpected exception');
32 $res = $this->fixture
->query("SELECT * FROM test WHERE textstuff = 'foo'");
34 $row = $res->fetchArray();
35 $this->assertNotNull($row['textstuff']);
37 $row = $res->fetchArray();
38 $this->assertNotNull($row['textstuff']);
40 $row = $res->fetchArray();
41 $this->assertNull($row['textstuff']);
43 catch (BSDbException
$e)
45 $this->fail('unexpected exception');
49 public function testEscapeString()
51 $this->assertEquals("Robert\'s castle", $this->fixture
->escapeString("Robert's castle"));
54 public function testFetchObject()
58 $this->fixture
->query("INSERT INTO test (textstuff) VALUES ('foo')");
60 catch (BSDbException
$e)
62 $this->fail('unexpected exception');
67 $res = $this->fixture
->query("SELECT * FROM test");
68 $obj = $res->fetchObject();
70 $this->assertNotNull($obj);
71 $this->assertEquals('foo', $obj->textstuff
);
73 catch (BSDbException
$e)
75 $this->fail('unexpected exception');
79 public function testQueryFirst()
83 $this->fixture
->query("INSERT INTO test (textstuff) VALUES ('abc'), ('123'), ('def')");
85 catch (BSDbException
$e)
87 $this->fail('unexpected exception');
92 $res = $this->fixture
->queryFirst("SELECT * FROM test ORDER BY id DESC");
93 $this->assertEquals('def', $res['textstuff']);
95 catch (BSDbException
$e)
97 $this->fail('unexpected exception');
101 public function testInsertId()
105 $this->fixture
->query("INSERT INTO test (textstuff) VALUES ('abc')");
106 $this->assertEquals(1, $this->fixture
->insertId('test', 'textstuff'));
108 $this->fixture
->query("INSERT INTO test (textstuff) VALUES ('123')");
109 $this->assertEquals(2, $this->fixture
->insertId('test', 'textstuff'));
111 catch (BSDbException
$e)
113 $this->fail('unexpected exception');
117 public function testNumRows()
121 $this->fixture
->query("INSERT INTO test (textstuff) VALUES ('123'), ('456'), ('789')");
123 $res = $this->fixture
->query("SELECT * FROM test");
124 $this->assertEquals(3, $res->size());
126 $res = $this->fixture
->query("SELECT * FROM test WHERE textstuff = '--invalid value--'");
127 $this->assertEquals(0, $res->size());
129 catch (BSDbException
$e)
131 $this->fail('unexpected exception');
135 public function testAffectedRows()
139 $this->fixture
->query("INSERT INTO test (textstuff) VALUES ('123'), ('456'), ('123')");
141 $res = $this->fixture
->query("UPDATE test SET textstuff = 'abc' WHERE textstuff = '123'");
142 $this->assertEquals(2, $this->fixture
->affectedRows());
144 $res = $this->fixture
->query("SELECT * FROM test WHERE textstuff = 'abc'");
145 $this->assertEquals(2, $res->size());
147 catch (BSDbException
$e)
149 $this->fail('unexpected exception');
153 public function testInvalidQuery()
157 $this->fixture
->query("SELECT * FROM foobar");
158 $this->fail('exception expected');
160 catch (BSDbException
$e)
162 $this->assertEquals('SELECT * FROM foobar', $e->getQuery());
166 public function testTransactions()
168 $this->fixture
->begin();
169 $this->fixture
->query("INSERT INTO test (textstuff) VALUES ('foo')");
170 $this->fixture
->commit();
172 $res = $this->fixture
->query("SELECT * FROM test WHERE textstuff = 'foo'");
173 $this->assertEquals(1, $res->size());
175 $this->fixture
->begin();
176 $this->fixture
->query("UPDATE test SET textstuff = 'abc'");
177 $this->fixture
->rollback();
179 $res = $this->fixture
->query("SELECT * FROM test WHERE textstuff = 'abc'");
180 $this->assertEquals(0, $res->size());
182 $res = $this->fixture
->query("SELECT * FROM test WHERE textstuff = 'foo'");
183 $this->assertEquals(1, $res->size());