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_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());
29 protected function tearDown() {
30 $this->conn
->exec(PQ_TEST_REFTABLE_DROP
);
31 $this->conn
->exec(PQ_TEST_TABLE_DROP
);
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);
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);
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());
63 public function testGetTable() {
64 $rowset = new Rowset($this->table
);
65 $this->assertSame($this->table
, $rowset->getTable());
68 public function testCreate() {
69 $rowset = new Rowset($this->table
);
70 $rowset->append(new Row($this->table
));
72 $this->assertCount(1, $rowset);
73 $this->assertCount(4, $this->table
->find());
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));
83 public function testUpdate() {
84 $rowset = $this->table
->find();
85 $rowset->apply(function($row) {
86 $row->data
= "updated";
89 $rowset = $this->table
->find();
90 $rowset->apply(function($row) {
91 $this->assertSame("updated", $row->data
->get());
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");
105 public function testDelete() {
106 $this->table
->find()->delete();
107 $this->assertCount(0, $this->table
->find());
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();
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"))
124 $this->assertJsonStringEqualsJsonString($json, json_encode($this->table
->find()));
127 public function testIterator() {
129 foreach ($this->table
->find() as $index => $row) {
130 $this->assertSame($counter++
, $index);
131 $this->assertInstanceOf("\\pq\\Gateway\\Row", $row);
135 public function testSeekEx() {
136 $this->setExpectedException("\\OutOfBoundsException", "Invalid seek position (3)");
137 $this->table
->find()->seek(3);
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());
147 public function testCount() {
148 $this->assertCount(3, $this->table
->find());
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; }));