autoload; cache; tests;
[m6w6/pq-gateway] / tests / lib / pq / Gateway / RowsetTest.php
1 <?php
2
3 namespace pq\Gateway;
4
5 include_once __DIR__."/../../../setup.inc";
6
7 class RowsetTest 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 }
27
28 protected function tearDown() {
29 $this->conn->exec(PQ_TEST_REFTABLE_DROP);
30 $this->conn->exec(PQ_TEST_TABLE_DROP);
31 }
32
33 public function test__invoke() {
34 $rowset = $this->table->find();
35 $this->table->setRowsetPrototype(null);
36 $result = $this->table->find();
37 $rowset2 = $rowset($result);
38 $this->assertEquals($rowset, $rowset2);
39 }
40
41 public function testSetRowPrototype() {
42 $prop = new \ReflectionProperty("\\pq\\Gateway\\Rowset", "row");
43 $prop->setAccessible(true);
44 $prototype = new Rowset($this->table);
45 $this->assertEquals("\\pq\\Gateway\\Row", $prop->getValue($prototype));
46 $prototype->setRowPrototype(null);
47 $this->assertNull($prop->getValue($prototype));
48 $this->table->setRowsetPrototype($prototype);
49 $rowset = $this->table->find();
50 foreach ($rowset as $row) {
51 $this->assertInstanceOf("stdClass", $row);
52 $this->assertObjectHasAttribute("id", $row);
53 }
54 $prototype->setRowPrototype(new Row($this->table));
55 $rowset = $this->table->find();
56 foreach ($rowset as $index => $row) {
57 $this->assertInstanceOf("\\pq\\Gateway\\Row", $row);
58 $this->assertEquals($index+1, $row->id->get());
59 }
60 }
61
62 public function testGetTable() {
63 $rowset = new Rowset($this->table);
64 $this->assertSame($this->table, $rowset->getTable());
65 }
66
67 public function testCreate() {
68 $rowset = new Rowset($this->table);
69 $rowset->append(new Row($this->table));
70 $rowset->create();
71 $this->assertCount(1, $rowset);
72 $this->assertCount(4, $this->table->find());
73 }
74
75 public function testCreateFail() {
76 $this->setExpectedException("\\pq\\Exception");
77 $rowset = new Rowset($this->table);
78 $rowset->append(new Row($this->table, array("foo" => "bar"), true));
79 $rowset->create();
80 }
81
82 public function testUpdate() {
83 $rowset = $this->table->find();
84 $rowset->apply(function($row) {
85 $row->data = "updated";
86 });
87 $rowset->update();
88 $rowset = $this->table->find();
89 $rowset->apply(function($row) {
90 $this->assertSame("updated", $row->data->get());
91 });
92 }
93
94 public function testUpdateFail() {
95 $this->setExpectedException("pq\\Exception");
96 $rowset = $this->table->find();
97 $rowset->apply(function($row) {
98 $row->data = new \pq\Query\Expr("die");
99 });
100 $rowset->update();
101
102 }
103
104 public function testDelete() {
105 $this->table->find()->delete();
106 $this->assertCount(0, $this->table->find());
107 }
108
109 public function testDeleteFail() {
110 $this->setExpectedException("pq\\Exception");
111 $rowset = new Rowset($this->table);
112 $rowset->append(new Row($this->table, array("xx" => 0)))->delete();
113 }
114
115 public function testJsonSerialize() {
116 $json = sprintf('[{"id":"1","created":"%s","counter":"-1","number":"-1.1","data":"yesterday"}'
117 .',{"id":"2","created":"%s","counter":"0","number":"0","data":"today"}'
118 .',{"id":"3","created":"%s","counter":"1","number":"1.1","data":"tomorrow"}]',
119 date("Y-m-d H:i:s", strtotime("yesterday")),
120 date("Y-m-d H:i:s", strtotime("today")),
121 date("Y-m-d H:i:s", strtotime("tomorrow"))
122 );
123 $this->assertJsonStringEqualsJsonString($json, json_encode($this->table->find()));
124 }
125
126 public function testIterator() {
127 $counter = 0;
128 foreach ($this->table->find() as $index => $row) {
129 $this->assertSame($counter++, $index);
130 $this->assertInstanceOf("\\pq\\Gateway\\Row", $row);
131 }
132 }
133
134 public function testSeekEx() {
135 $this->setExpectedException("\\OutOfBoundsException", "Invalid seek position (3)");
136 $this->table->find()->seek(3);
137 }
138
139 public function testSeek() {
140 $rowset = $this->table->find();
141 for ($i = count($rowset); $i > 0; --$i) {
142 $this->assertEquals($i, $rowset->seek($i-1)->current()->id->get());
143 }
144 }
145
146 public function testCount() {
147 $this->assertCount(3, $this->table->find());
148 }
149
150 public function testGetRows() {
151 $rowset = $this->table->find();
152 $rows = $rowset->getRows();
153 $rowset2 = $rowset->filter(function($row) { return true; });
154 $this->assertEquals($rows, $rowset2->getRows());
155 $rowset3 = $rowset->filter(function($row) { return false; });
156 $this->assertCount(0, $rowset3);
157 $this->assertSame(array(), $rowset3->getRows());
158 $this->assertCount(1, $rowset->filter(function($row) { return $row->id->get() == 1; }));
159 }
160 }