00dbbbb6c9c2eb806a877e3177adb1036d05ed54
[m6w6/pq-gateway] / tests / lib / pq / Gateway / RowTest.php
1 <?php
2
3 namespace pq\Gateway;
4
5 include_once __DIR__."/../../../setup.inc";
6
7 class RowTest extends \PHPUnit_Framework_TestCase {
8
9 /**
10 * @var \pq\Connection
11 */
12 protected $conn;
13
14 /**
15 * @var \pq\Gateway\Table
16 */
17 protected $table;
18
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());
27 }
28
29 protected function tearDown() {
30 $this->conn->exec(PQ_TEST_REFTABLE_DROP);
31 $this->conn->exec(PQ_TEST_TABLE_DROP);
32 }
33
34 function testBasic() {
35 $row = new Row($this->table, array("id" => 3), true);
36 $this->assertTrue($row->isDirty());
37 $row->refresh();
38 $this->assertSame(
39 array(
40 "id" => "3",
41 "created" => date("Y-m-d H:i:s", strtotime("tomorrow")),
42 "counter" => "1",
43 "number" => "1.1",
44 "data" => "tomorrow"
45 ),
46 $row->getData()
47 );
48 $this->assertFalse($row->isDirty());
49 }
50
51 function testGetTable() {
52 $row = new Row($this->table);
53 $this->assertSame($this->table, $row->getTable());
54 }
55
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();
60 $row->data = "foo";
61 $row->update();
62 $txn->commit();
63 $this->assertSame("foo", $row->data->get());
64 }
65
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();
70 $row->data = "foo";
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");
76 $row->update();
77 $txn->commit();
78 }
79
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();
84 $row->data = "foo";
85 $row->update();
86 $this->assertEquals("foo", $row->data->get());
87 $this->assertEquals($cnt +1, $row->counter->get());
88 }
89
90 function testOptimisticLockFail() {
91 $this->table->setLock(new Table\OptimisticLock("counter"));
92 $row = $this->table->find(null, null, 1)->current();
93 $cnt = $row->counter->get();
94 $row->data = "foo";
95 executeInConcurrentTransaction(
96 $this->table->getQueryExecutor(),
97 "UPDATE {$this->table->getName()} SET counter = 10 WHERE id=\$1",
98 array($row->id->get()));
99 $this->setExpectedException("\\UnexpectedValueException", "No row updated");
100 $row->update();
101 }
102
103 function testRef() {
104 foreach ($this->table->find() as $row) {
105 foreach ($row->reftest() as $ref) {
106 $this->assertEquals($row->id->get(), $ref->test->id->get());
107 }
108 }
109 }
110 }