5 include_once __DIR__
."/../../../setup.inc";
7 class RowTest
extends \PHPUnit_Framework_TestCase
{
15 * @var \pq\Gateway\Table
19 protected function setUp() {
20 $this->conn
= new \pq\
Connection(PQ_TEST_DSN
);
21 $this->conn
->exec(PQ_TEST_TABLE_CREATE
);
22 $this->conn
->exec(PQ_TEST_REFTABLE_CREATE
);
23 $this->conn
->exec(PQ_TEST_DATA
);
24 Table
::$defaultConnection = $this->conn
;
25 $this->table
= new Table("test");
26 $this->table
->getQueryExecutor()->attach(new \
QueryLogger());
29 protected function tearDown() {
30 $this->conn
->exec(PQ_TEST_REFTABLE_DROP
);
31 $this->conn
->exec(PQ_TEST_TABLE_DROP
);
34 function testBasic() {
35 $row = new Row($this->table
, array("id" => 3), true);
36 $this->assertTrue($row->isDirty());
41 "created" => date("Y-m-d H:i:s", strtotime("tomorrow")),
48 $this->assertFalse($row->isDirty());
51 function testGetTable() {
52 $row = new Row($this->table
);
53 $this->assertSame($this->table
, $row->getTable());
56 function testPessimisticLock() {
57 $this->table
->setLock(new Table\PessimisticLock
);
58 $txn = $this->table
->getConnection()->startTransaction();
59 $row = $this->table
->find(null, null, 1)->current();
63 $this->assertSame("foo", $row->data
->get());
66 function testPessimisticLockFail() {
67 $this->table
->setLock(new Table\PessimisticLock
);
68 $txn = $this->table
->getConnection()->startTransaction();
69 $row = $this->table
->find(null, null, 1)->current();
71 executeInConcurrentTransaction(
72 $this->table
->getQueryExecutor(),
73 "UPDATE {$this->table->getName()} SET data='bar' WHERE id=\$1",
74 array($row->id
->get()));
75 $this->setExpectedException("\\UnexpectedValueException", "Row has already been modified");
80 function testOptimisticLock() {
81 $this->table
->setLock(new Table\
OptimisticLock("counter"));
82 $row = $this->table
->find(null, null, 1)->current();
83 $cnt = $row->counter
->get();
86 $this->assertEquals("foo", $row->data
->get());
87 $this->assertEquals($cnt +
1, $row->counter
->get());
90 function testOptimisticLockFail() {
91 $this->table
->setLock(new Table\
OptimisticLock("counter"));
92 $row = $this->table
->find(null, null, 1)->current();
94 executeInConcurrentTransaction(
95 $this->table
->getQueryExecutor(),
96 "UPDATE {$this->table->getName()} SET counter = 10 WHERE id=\$1",
97 array($row->id
->get()));
98 $this->setExpectedException("\\UnexpectedValueException", "No row updated");
103 foreach ($this->table
->find() as $row) {
104 foreach ($row->reftest() as $ref) {
105 $this->assertEquals($row->id
->get(), $ref->test
->current()->id
->get());