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