summaryrefslogtreecommitdiff
path: root/tests/unit/Data/TDbTransactionTest.php
blob: 4cfd294309d91ef8793f852e664a7e7bbf9fb5c0 (plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php

require_once(dirname(__FILE__).'/../phpunit.php');

Prado::using('System.Data.*');

define('TEST_DB_FILE',dirname(__FILE__).'/db/test.db');

/**
 * @package System.Data.PDO
 */
class TDbTransactionTest extends PHPUnit_Framework_TestCase
{
	private $_connection;

	public function setUp()
	{
		@unlink(TEST_DB_FILE);
		$this->_connection=new TDbConnection('sqlite:'.TEST_DB_FILE);
		$this->_connection->Active=true;
		$this->_connection->createCommand('CREATE TABLE foo (id INTEGER NOT NULL PRIMARY KEY, name VARCHAR(8))')->execute();
	}

	public function tearDown()
	{
		$this->_connection=null;
	}

	public function testRollBack()
	{
		$sql='INSERT INTO foo(id,name) VALUES (1,\'my name\')';
		$transaction=$this->_connection->beginTransaction();
		try
		{
			$this->_connection->createCommand($sql)->execute();
			$this->_connection->createCommand($sql)->execute();
			$this->fail('Expected exception not raised');
			$transaction->commit();
		}
		catch(Exception $e)
		{
			$this->assertTrue($transaction->Active);
			$transaction->rollBack();
			$this->assertFalse($transaction->Active);
			$reader=$this->_connection->createCommand('SELECT * FROM foo')->query();
			$this->assertFalse($reader->read());
		}
	}

	public function testCommit()
	{
		$sql1='INSERT INTO foo(id,name) VALUES (1,\'my name\')';
		$sql2='INSERT INTO foo(id,name) VALUES (2,\'my name\')';
		$transaction=$this->_connection->beginTransaction();
		try
		{
			$this->_connection->createCommand($sql1)->execute();
			$this->_connection->createCommand($sql2)->execute();
			$this->assertTrue($transaction->Active);
			$transaction->commit();
			$this->assertFalse($transaction->Active);
		}
		catch(Exception $e)
		{
			$transaction->rollBack();
			$this->fail('Unexpected exception');
		}
		$result=$this->_connection->createCommand('SELECT * FROM foo')->query()->readAll();
		$this->assertEquals(count($result),2);
	}
}

?>