-
Notifications
You must be signed in to change notification settings - Fork 4
/
TransactionRollbackTeardownTest.php
51 lines (45 loc) · 1.49 KB
/
TransactionRollbackTeardownTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
class TransactionRollbackTeardownTest extends PHPUnit_Framework_TestCase
{
private static $sharedConnection;
private $connection;
public function setUp()
{
if (self::$sharedConnection === null) {
self::$sharedConnection = new PDO('sqlite::memory:');
self::$sharedConnection->exec('CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR(255)
)');
}
$this->connection = self::$sharedConnection;
$this->connection->beginTransaction();
}
public function teardown()
{
$this->connection->rollback();
}
public function testTableCanBePopulated()
{
$this->connection->exec('INSERT INTO users (name) VALUES ("Giorgio")');
$this->assertEquals(1, $this->howManyUsers());
}
public function testTableRestartsFrom1()
{
$this->assertEquals(0, $this->howManyUsers());
$this->connection->exec('INSERT INTO users (name) VALUES ("Isaac")');
$stmt = $this->connection->query('SELECT name FROM users WHERE id=1');
$result = $stmt->fetch();
$this->assertEquals('Isaac', $result['name']);
}
public function testTableIsEmpty()
{
$this->assertEquals(0, $this->howManyUsers());
}
private function howManyUsers()
{
$stmt = $this->connection->query('SELECT COUNT(*) AS number FROM users');
$result = $stmt->fetch();
return $result['number'];
}
}