add support for one-dimensional arrays; type input parameters
[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 "list" => array(1,2,3),
43 "prop" => null
44 ),
45 $row->getData()
46 );
47 $this->assertFalse($row->isDirty());
48 }
49
50 function testGetTable() {
51 $row = new Row($this->table);
52 $this->assertSame($this->table, $row->getTable());
53 }
54
55 function testPessimisticLock() {
56 $this->table->setLock(new Table\PessimisticLock);
57 $txn = $this->table->getConnection()->startTransaction();
58 $row = $this->table->find(null, null, 1)->current();
59 $row->data = "foo";
60 $row->update();
61 $txn->commit();
62 $this->assertSame("foo", $row->data->get());
63 }
64
65 function testPessimisticLockFail() {
66 $this->table->setLock(new Table\PessimisticLock);
67 $txn = $this->table->getConnection()->startTransaction();
68 $row = $this->table->find(null, null, 1)->current();
69 $row->data = "foo";
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");
75 $row->update();
76 $txn->commit();
77 }
78
79 function testOptimisticLock() {
80 $this->table->setLock(new Table\OptimisticLock("counter"));
81 $row = $this->table->find(null, null, 1)->current();
82 $cnt = $row->counter->get();
83 $row->data = "foo";
84 $row->update();
85 $this->assertEquals("foo", $row->data->get());
86 $this->assertEquals($cnt +1, $row->counter->get());
87 }
88
89 function testOptimisticLockFail() {
90 $this->table->setLock(new Table\OptimisticLock("counter"));
91 $row = $this->table->find(null, null, 1)->current();
92 $row->data = "foo";
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");
98 $row->update();
99 }
100
101 function testRef() {
102 foreach ($this->table->find() as $row) {
103 foreach ($row->reftest() as $ref) {
104 $this->assertEquals($row->id->get(), $ref->test->current()->id->get());
105 }
106 }
107 }
108 }