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_SETUP_SQL
);
22 Table
::$defaultConnection = $this->conn
;
23 $this->table
= new Table("test");
24 $this->table
->getQueryExecutor()->attach(new \
QueryLogger());
27 protected function tearDown() {
28 $this->conn
->exec(PQ_TEST_TEARDOWN_SQL
);
31 function testBasic() {
32 $row = new Row($this->table
, array("id" => 3), true);
33 $this->assertTrue($row->isDirty());
38 "created" => new \pq\
DateTime("tomorrow"),
45 $this->assertFalse($row->isDirty());
48 function testGetTable() {
49 $row = new Row($this->table
);
50 $this->assertSame($this->table
, $row->getTable());
53 function testPessimisticLock() {
54 $this->table
->setLock(new Table\PessimisticLock
);
55 $txn = $this->table
->getConnection()->startTransaction();
56 $row = $this->table
->find(null, null, 1)->current();
60 $this->assertSame("foo", $row->data
->get());
63 function testPessimisticLockFail() {
64 $this->table
->setLock(new Table\PessimisticLock
);
65 $txn = $this->table
->getConnection()->startTransaction();
66 $row = $this->table
->find(null, null, 1)->current();
68 executeInConcurrentTransaction(
69 $this->table
->getQueryExecutor(),
70 "UPDATE {$this->table->getName()} SET data='bar' WHERE id=\$1",
71 array($row->id
->get()));
72 $this->setExpectedException("\\UnexpectedValueException", "Row has already been modified");
77 function testOptimisticLock() {
78 $this->table
->setLock(new Table\
OptimisticLock("counter"));
79 $row = $this->table
->find(null, null, 1)->current();
80 $cnt = $row->counter
->get();
83 $this->assertEquals("foo", $row->data
->get());
84 $this->assertEquals($cnt +
1, $row->counter
->get());
87 function testOptimisticLockFail() {
88 $this->table
->setLock(new Table\
OptimisticLock("counter"));
89 $row = $this->table
->find(null, null, 1)->current();
91 executeInConcurrentTransaction(
92 $this->table
->getQueryExecutor(),
93 "UPDATE {$this->table->getName()} SET counter = 10 WHERE id=\$1",
94 array($row->id
->get()));
95 $this->setExpectedException("\\UnexpectedValueException", "No row updated");
100 foreach ($this->table
->find() as $row) {
101 foreach ($row->reftest() as $ref) {
102 $this->assertEquals($row->id
->get(), $ref->test
->current()->id
->get());