tests
[m6w6/pq-gateway] / tests / lib / pq / Gateway / RowsetTest.php
diff --git a/tests/lib/pq/Gateway/RowsetTest.php b/tests/lib/pq/Gateway/RowsetTest.php
new file mode 100644 (file)
index 0000000..c0ff003
--- /dev/null
@@ -0,0 +1,136 @@
+<?php
+
+namespace pq\Gateway;
+
+include_once __DIR__."/../../../setup.inc";
+
+class RowsetTest extends \PHPUnit_Framework_TestCase {
+
+       /**
+        * @var \pq\Connection
+        */
+       protected $conn;
+       
+       /**
+        * @var \pq\Gateway\Table
+        */
+       protected $table;
+
+       protected function setUp() {
+               $this->conn = new \pq\Connection(PQ_DSN);
+               $this->conn->exec(PQ_TEST_DROP_TABLE);
+               $this->conn->exec(PQ_TEST_CREATE_TABLE);
+               $this->conn->exec(PQ_TEST_CREATE_DATA);
+               
+               $this->table = new Table(PQ_TEST_TABLE_NAME, $this->conn);
+       }
+
+       protected function tearDown() {
+               $this->conn->exec(PQ_TEST_DROP_TABLE);
+       }
+
+       public function test__invoke() {
+               $rowset = $this->table->find();
+               $this->table->setRowsetPrototype(null);
+               $result = $this->table->find();
+               $rowset2 = $rowset($result);
+               $this->assertEquals($rowset, $rowset2);
+       }
+
+       public function testSetRowPrototype() {
+               $prop = new \ReflectionProperty("\\pq\\Gateway\\Rowset", "row");
+               $prop->setAccessible(true);
+               $prototype = new Rowset($this->table);
+               $this->assertEquals("\\pq\\Gateway\\Row", $prop->getValue($prototype));
+               $prototype->setRowPrototype(null);
+               $this->assertNull($prop->getValue($prototype));
+               $this->table->setRowsetPrototype($prototype);
+               $rowset = $this->table->find();
+               foreach ($rowset as $row) {
+                       $this->assertInstanceOf("stdClass", $row);
+                       $this->assertObjectHasAttribute("id", $row);
+               }
+               $prototype->setRowPrototype(new Row($this->table));
+               $rowset = $this->table->find();
+               foreach ($rowset as $index => $row) {
+                       $this->assertInstanceOf("\\pq\\Gateway\\Row", $row);
+                       $this->assertEquals($index+1, $row->id->get());
+               }
+       }
+
+       public function testGetTable() {
+               $rowset = new Rowset($this->table);
+               $this->assertSame($this->table, $rowset->getTable());
+       }
+
+       public function testCreate() {
+               $rowset = new Rowset($this->table);
+               $rowset->append(new Row($this->table));
+               $rowset->create();
+               $this->assertCount(1, $rowset);
+               $this->assertCount(4, $this->table->find());
+       }
+
+       public function testUpdate() {
+               $rowset = $this->table->find();
+               $rowset->apply(function($row) {
+                       $row->data = "updated";
+               });
+               $rowset->update();
+               $rowset = $this->table->find();
+               $rowset->apply(function($row) {
+                       $this->assertSame("updated", $row->data->get());
+               });
+       }
+
+       public function testDelete() {
+               $this->table->find()->delete();
+               $this->assertCount(0, $this->table->find());
+       }
+
+       public function testJsonSerialize() {
+               $json = sprintf('[{"id":"1","created":"%s","counter":"-1","number":"-1.1","data":"yesterday"}'
+                       .',{"id":"2","created":"%s","counter":"0","number":"0","data":"today"}'
+                       .',{"id":"3","created":"%s","counter":"1","number":"1.1","data":"tomorrow"}]',
+                       date("Y-m-d H:i:s", strtotime("yesterday")),
+                       date("Y-m-d H:i:s", strtotime("today")),
+                       date("Y-m-d H:i:s", strtotime("tomorrow"))
+               );
+               $this->assertJsonStringEqualsJsonString($json, json_encode($this->table->find()));
+       }
+
+       public function testIterator() {
+               $counter = 0;
+               foreach ($this->table->find() as $index => $row) {
+                       $this->assertSame($counter++, $index);
+                       $this->assertInstanceOf("\\pq\\Gateway\\Row", $row);
+               }
+       }
+
+       public function testSeekEx() {
+               $this->setExpectedException("\\OutOfBoundsException", "Invalid seek position (3)");
+               $this->table->find()->seek(3);
+       }
+       
+       public function testSeek() {
+               $rowset = $this->table->find();
+               for ($i = count($rowset); $i > 0; --$i) {
+                       $this->assertEquals($i, $rowset->seek($i-1)->current()->id->get());
+               }
+       }
+
+       public function testCount() {
+               $this->assertCount(3, $this->table->find());
+       }
+
+       public function testGetRows() {
+               $rowset = $this->table->find();
+               $rows = $rowset->getRows();
+               $rowset2 = $rowset->filter(function($row) { return true; });
+               $this->assertEquals($rows, $rowset2->getRows());
+               $rowset3 = $rowset->filter(function($row) { return false; });
+               $this->assertCount(0, $rowset3);
+               $this->assertSame(array(), $rowset3->getRows());
+               $this->assertCount(1, $rowset->filter(function($row) { return $row->id->get() == 1; }));
+       }
+}