c0ff003b204df80f8bd613d3946c077d1fd2b259
[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 testUpdate() {
75 $rowset = $this->table->find();
76 $rowset->apply(function($row) {
77 $row->data = "updated";
78 });
79 $rowset->update();
80 $rowset = $this->table->find();
81 $rowset->apply(function($row) {
82 $this->assertSame("updated", $row->data->get());
83 });
84 }
85
86 public function testDelete() {
87 $this->table->find()->delete();
88 $this->assertCount(0, $this->table->find());
89 }
90
91 public function testJsonSerialize() {
92 $json = sprintf('[{"id":"1","created":"%s","counter":"-1","number":"-1.1","data":"yesterday"}'
93 .',{"id":"2","created":"%s","counter":"0","number":"0","data":"today"}'
94 .',{"id":"3","created":"%s","counter":"1","number":"1.1","data":"tomorrow"}]',
95 date("Y-m-d H:i:s", strtotime("yesterday")),
96 date("Y-m-d H:i:s", strtotime("today")),
97 date("Y-m-d H:i:s", strtotime("tomorrow"))
98 );
99 $this->assertJsonStringEqualsJsonString($json, json_encode($this->table->find()));
100 }
101
102 public function testIterator() {
103 $counter = 0;
104 foreach ($this->table->find() as $index => $row) {
105 $this->assertSame($counter++, $index);
106 $this->assertInstanceOf("\\pq\\Gateway\\Row", $row);
107 }
108 }
109
110 public function testSeekEx() {
111 $this->setExpectedException("\\OutOfBoundsException", "Invalid seek position (3)");
112 $this->table->find()->seek(3);
113 }
114
115 public function testSeek() {
116 $rowset = $this->table->find();
117 for ($i = count($rowset); $i > 0; --$i) {
118 $this->assertEquals($i, $rowset->seek($i-1)->current()->id->get());
119 }
120 }
121
122 public function testCount() {
123 $this->assertCount(3, $this->table->find());
124 }
125
126 public function testGetRows() {
127 $rowset = $this->table->find();
128 $rows = $rowset->getRows();
129 $rowset2 = $rowset->filter(function($row) { return true; });
130 $this->assertEquals($rows, $rowset2->getRows());
131 $rowset3 = $rowset->filter(function($row) { return false; });
132 $this->assertCount(0, $rowset3);
133 $this->assertSame(array(), $rowset3->getRows());
134 $this->assertCount(1, $rowset->filter(function($row) { return $row->id->get() == 1; }));
135 }
136 }