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