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