5 include_once __DIR__
."/../../../setup.inc";
7 class RowsetTest
extends \PHPUnit_Framework_TestCase
{
15 * @var \pq\Gateway\Table
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
);
25 $this->table
= new Table(PQ_TEST_TABLE_NAME
, $this->conn
);
28 protected function tearDown() {
29 $this->conn
->exec(PQ_TEST_DROP_TABLE
);
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);
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);
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());
61 public function testGetTable() {
62 $rowset = new Rowset($this->table
);
63 $this->assertSame($this->table
, $rowset->getTable());
66 public function testCreate() {
67 $rowset = new Rowset($this->table
);
68 $rowset->append(new Row($this->table
));
70 $this->assertCount(1, $rowset);
71 $this->assertCount(4, $this->table
->find());
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));
81 public function testUpdate() {
82 $rowset = $this->table
->find();
83 $rowset->apply(function($row) {
84 $row->data
= "updated";
87 $rowset = $this->table
->find();
88 $rowset->apply(function($row) {
89 $this->assertSame("updated", $row->data
->get());
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");
103 public function testDelete() {
104 $this->table
->find()->delete();
105 $this->assertCount(0, $this->table
->find());
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();
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"))
122 $this->assertJsonStringEqualsJsonString($json, json_encode($this->table
->find()));
125 public function testIterator() {
127 foreach ($this->table
->find() as $index => $row) {
128 $this->assertSame($counter++
, $index);
129 $this->assertInstanceOf("\\pq\\Gateway\\Row", $row);
133 public function testSeekEx() {
134 $this->setExpectedException("\\OutOfBoundsException", "Invalid seek position (3)");
135 $this->table
->find()->seek(3);
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());
145 public function testCount() {
146 $this->assertCount(3, $this->table
->find());
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; }));