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"),
42 "list" => array(1,2,3),
47 $this->assertFalse($row->isDirty());
50 function testGetTable() {
51 $row = new Row($this->table
);
52 $this->assertSame($this->table
, $row->getTable());
55 function testPessimisticLock() {
56 $this->table
->attach(new Table\PessimisticLock
);
57 $txn = $this->table
->getConnection()->startTransaction();
58 $row = $this->table
->find(null, null, 1)->current();
62 $this->assertSame("foo", $row->data
->get());
65 function testPessimisticLockFail() {
66 $this->table
->attach(new Table\PessimisticLock
);
67 $txn = $this->table
->getConnection()->startTransaction();
68 $row = $this->table
->find(null, null, 1)->current();
70 executeInConcurrentTransaction(
71 $this->table
->getQueryExecutor(),
72 "UPDATE {$this->table->getName()} SET data='bar' WHERE id=\$1",
73 array($row->id
->get()));
74 $this->setExpectedException("\\UnexpectedValueException", "Row has already been modified");
79 function testOptimisticLock() {
80 $this->table
->attach(new Table\
OptimisticLock("counter"));
81 $row = $this->table
->find(null, null, 1)->current();
82 $cnt = $row->counter
->get();
85 $this->assertEquals("foo", $row->data
->get());
86 $this->assertEquals($cnt +
1, $row->counter
->get());
89 function testOptimisticLockFail() {
90 $this->table
->attach(new Table\
OptimisticLock("counter"));
91 $row = $this->table
->find(null, null, 1)->current();
93 executeInConcurrentTransaction(
94 $this->table
->getQueryExecutor(),
95 "UPDATE {$this->table->getName()} SET counter = 10 WHERE id=\$1",
96 array($row->id
->get()));
97 $this->setExpectedException("\\UnexpectedValueException", "No row updated");
102 foreach ($this->table
->find() as $row) {
103 foreach ($row->allOf("reftest") as $ref) {
104 $this->assertEquals($row->id
->get(), $ref->ofWhich("test")->current()->id
->get());