test fixups
[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("\\pq\\Exception");
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 $json = sprintf('[{"id":"1","created":"%s","counter":"-1","number":"-1.1","data":"yesterday"}'
115 .',{"id":"2","created":"%s","counter":"0","number":"0","data":"today"}'
116 .',{"id":"3","created":"%s","counter":"1","number":"1.1","data":"tomorrow"}]',
117 new \pq\DateTime("yesterday"),
118 new \pq\DateTime("today"),
119 new \pq\DateTime("tomorrow")
120 );
121 $this->assertJsonStringEqualsJsonString($json, json_encode($this->table->find()));
122 }
123
124 public function testIterator() {
125 $counter = 0;
126 foreach ($this->table->find() as $index => $row) {
127 $this->assertSame($counter++, $index);
128 $this->assertInstanceOf("\\pq\\Gateway\\Row", $row);
129 }
130 }
131
132 public function testSeekEx() {
133 $this->setExpectedException("\\OutOfBoundsException", "Invalid seek position (3)");
134 $this->table->find()->seek(3);
135 }
136
137 public function testSeek() {
138 $rowset = $this->table->find();
139 for ($i = count($rowset); $i > 0; --$i) {
140 $this->assertEquals($i, $rowset->seek($i-1)->current()->id->get());
141 }
142 }
143
144 public function testCount() {
145 $this->assertCount(3, $this->table->find());
146 }
147
148 public function testGetRows() {
149 $rowset = $this->table->find();
150 $rows = $rowset->getRows();
151 $rowset2 = $rowset->filter(function($row) { return true; });
152 $this->assertEquals($rows, $rowset2->getRows());
153 $rowset3 = $rowset->filter(function($row) { return false; });
154 $this->assertCount(0, $rowset3);
155 $this->assertSame(array(), $rowset3->getRows());
156 $this->assertCount(1, $rowset->filter(function($row) { return $row->id->get() == 1; }));
157 }
158 }