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_SETUP_SQL
);
22 Table
::$defaultConnection = $this->conn
;
23 $this->table
= new Table("test");
24 $this->table
->getQueryExecutor()->attach(new \
QueryLogger());
27 protected function tearDown() {
28 $this->conn
->exec(PQ_TEST_TEARDOWN_SQL
);
31 public function test__invoke() {
32 $rowset = $this->table
->find();
33 $this->table
->setRowsetPrototype(null);
34 $result = $this->table
->find();
35 $rowset2 = $rowset($result);
36 $this->assertEquals($rowset, $rowset2);
39 public function testSetRowPrototype() {
40 $prop = new \
ReflectionProperty("\\pq\\Gateway\\Rowset", "row");
41 $prop->setAccessible(true);
42 $prototype = new Rowset($this->table
);
43 $this->assertEquals("\\pq\\Gateway\\Row", $prop->getValue($prototype));
44 $prototype->setRowPrototype(null);
45 $this->assertNull($prop->getValue($prototype));
46 $this->table
->setRowsetPrototype($prototype);
47 $rowset = $this->table
->find();
48 foreach ($rowset as $row) {
49 $this->assertInstanceOf("stdClass", $row);
50 $this->assertObjectHasAttribute("id", $row);
52 $prototype->setRowPrototype(new Row($this->table
));
53 $rowset = $this->table
->find();
54 foreach ($rowset as $index => $row) {
55 $this->assertInstanceOf("\\pq\\Gateway\\Row", $row);
56 $this->assertEquals($index+
1, $row->id
->get());
60 public function testGetTable() {
61 $rowset = new Rowset($this->table
);
62 $this->assertSame($this->table
, $rowset->getTable());
65 public function testCreate() {
66 $rowset = new Rowset($this->table
);
67 $rowset->append(new Row($this->table
));
69 $this->assertCount(1, $rowset);
70 $this->assertCount(4, $this->table
->find());
73 public function testCreateFail() {
74 $this->setExpectedException("\\OutOfBoundsException");
75 $rowset = new Rowset($this->table
);
76 $rowset->append(new Row($this->table
, array("foo" => "bar"), true));
80 public function testUpdate() {
81 $rowset = $this->table
->find();
82 $rowset->apply(function($row) {
83 $row->data
= "updated";
86 $rowset = $this->table
->find();
87 $rowset->apply(function($row) {
88 $this->assertSame("updated", $row->data
->get());
92 public function testUpdateFail() {
93 $this->setExpectedException("pq\\Exception");
94 $rowset = $this->table
->find();
95 $rowset->apply(function($row) {
96 $row->data
= new \pq\Query\
Expr("die");
102 public function testDelete() {
103 $this->table
->find()->delete();
104 $this->assertCount(0, $this->table
->find());
107 public function testDeleteFail() {
108 $this->setExpectedException("Exception");
109 $rowset = new Rowset($this->table
);
110 $rowset->append(new Row($this->table
, array("xx" => 0)))->delete();
113 public function testJsonSerialize() {
114 $json = sprintf('[{"id":"1","created":"%s","counter":"-1","number":"-1.1","data":"yesterday","list":[-1,0,1],"prop":null}'
115 .',{"id":"2","created":"%s","counter":"0","number":"0","data":"today","list":[0,1,2],"prop":null}'
116 .',{"id":"3","created":"%s","counter":"1","number":"1.1","data":"tomorrow","list":[1,2,3],"prop":null}]',
117 new \pq\
DateTime("yesterday"),
118 new \pq\
DateTime("today"),
119 new \pq\
DateTime("tomorrow")
121 $this->assertJsonStringEqualsJsonString($json, json_encode($this->table
->find()));
124 public function testIterator() {
126 foreach ($this->table
->find() as $index => $row) {
127 $this->assertSame($counter++
, $index);
128 $this->assertInstanceOf("\\pq\\Gateway\\Row", $row);
132 public function testSeekEx() {
133 $this->setExpectedException("\\OutOfBoundsException", "Invalid seek position (3)");
134 $this->table
->find()->seek(3);
137 public function testSeek() {
138 $rowset = $this->table
->find();
139 for ($i = count($rowset); $i > 0; --$i) {
140 $this->assertEquals($i, $rowset->seek($i-1)->current()->id
->get());
144 public function testCount() {
145 $this->assertCount(3, $this->table
->find());
148 public function testGetRows() {
149 $rowset = $this->table
->find();
150 $rows = $rowset->getRows();
151 $rowset2 = $rowset->filter(function($row) { return true; });
152 $this->assertEquals($rows, $rowset2->getRows());
153 $rowset3 = $rowset->filter(function($row) { return false; });
154 $this->assertCount(0, $rowset3);
155 $this->assertSame(array(), $rowset3->getRows());
156 $this->assertCount(1, $rowset->filter(function($row) { return $row->id
->get() == 1; }));
159 public function testApplyAppend() {
160 $rowset1 = $this->table
->find(null, null, 1);
161 $rowset2 = $this->table
->find(null, null, 1, 1);
162 $this->assertCount(1, $rowset1);
163 $this->assertCount(1, $rowset2);
164 $rowset2->apply(array($rowset1, "append"));
165 $this->assertCount(1, $rowset2);
166 $this->assertCount(2, $rowset1);