1d339639c27d5cda471ce5a68589750b9a3e1b23
[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_SETUP_SQL);
22 Table::$defaultConnection = $this->conn;
23 $this->table = new Table("test");
24 $this->table->getQueryExecutor()->attach(new \QueryLogger());
25 }
26
27 protected function tearDown() {
28 $this->conn->exec(PQ_TEST_TEARDOWN_SQL);
29 }
30
31 function testBasic() {
32 $row = new Row($this->table, array("id" => 3), true);
33 $this->assertTrue($row->isDirty());
34 $row->refresh();
35 $this->assertEquals(
36 array(
37 "id" => "3",
38 "created" => new \pq\DateTime("tomorrow"),
39 "counter" => "1",
40 "number" => "1.1",
41 "data" => "tomorrow"
42 ),
43 $row->getData()
44 );
45 $this->assertFalse($row->isDirty());
46 }
47
48 function testGetTable() {
49 $row = new Row($this->table);
50 $this->assertSame($this->table, $row->getTable());
51 }
52
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();
57 $row->data = "foo";
58 $row->update();
59 $txn->commit();
60 $this->assertSame("foo", $row->data->get());
61 }
62
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();
67 $row->data = "foo";
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");
73 $row->update();
74 $txn->commit();
75 }
76
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();
81 $row->data = "foo";
82 $row->update();
83 $this->assertEquals("foo", $row->data->get());
84 $this->assertEquals($cnt +1, $row->counter->get());
85 }
86
87 function testOptimisticLockFail() {
88 $this->table->setLock(new Table\OptimisticLock("counter"));
89 $row = $this->table->find(null, null, 1)->current();
90 $row->data = "foo";
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");
96 $row->update();
97 }
98
99 function testRef() {
100 foreach ($this->table->find() as $row) {
101 foreach ($row->reftest() as $ref) {
102 $this->assertEquals($row->id->get(), $ref->test->current()->id->get());
103 }
104 }
105 }
106 }