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