add test
[m6w6/pq-gateway] / tests / lib / pq / Gateway / TableTest.php
diff --git a/tests/lib/pq/Gateway/TableTest.php b/tests/lib/pq/Gateway/TableTest.php
new file mode 100644 (file)
index 0000000..bee7bdd
--- /dev/null
@@ -0,0 +1,124 @@
+<?php
+
+namespace pq\Gateway;
+
+include __DIR__."/../../../setup.inc";
+
+/**
+ * Generated by PHPUnit_SkeletonGenerator 1.2.0 on 2013-03-05 at 16:08:03.
+ */
+class TableTest extends \PHPUnit_Framework_TestCase {
+
+       /**
+        * @var \pq\Connection
+        */
+       protected $conn;
+       
+       /**
+        * @var Table
+        */
+       protected $object;
+
+       /**
+        * Sets up the fixture, for example, opens a network connection.
+        * This method is called before a test is executed.
+        */
+       protected function setUp() {
+               $this->conn = new \pq\Connection(PQ_DSN);
+               $this->conn->exec(PQ_TEST_DROP_TABLE);
+               $this->conn->exec(PQ_TEST_CREATE_TABLE);
+               Table::$defaultConnection = $this->conn;
+               $this->object = new Table(PQ_TEST_TABLE_NAME);
+       }
+
+       /**
+        * Tears down the fixture, for example, closes a network connection.
+        * This method is called after a test is executed.
+        */
+       protected function tearDown() {
+               $this->conn->exec(PQ_TEST_DROP_TABLE);
+       }
+       
+       /**
+        * Creates test data in the test table
+        */
+       protected function createTestData() {
+               $this->conn->exec(PQ_TEST_CREATE_DATA);
+       }
+
+       /**
+        * @covers pq\Gateway\Table::setRowsetPrototype
+        */
+       public function testSetRowsetPrototype() {
+               $prop = new \ReflectionProperty("\\pq\\Gateway\\Table", "rowset");
+               $prop->setAccessible(true);
+               $this->assertEquals("\\pq\\Gateway\\Rowset", $prop->getValue($this->object));
+               $this->object->setRowsetPrototype(null);
+               $this->assertNull($prop->getValue($this->object));
+               $rowset = new \pq\Gateway\Rowset($this->object);
+               $this->object->setRowsetPrototype($rowset);
+               $this->assertSame($rowset, $prop->getValue($this->object));
+       }
+
+       /**
+        * @covers pq\Gateway\Table::getConnection
+        */
+       public function testGetConnection() {
+               $this->assertSame($this->conn, $this->object->getConnection());
+       }
+
+       /**
+        * @covers pq\Gateway\Table::getName
+        */
+       public function testGetName() {
+               $this->assertSame(PQ_TEST_TABLE_NAME, $this->object->getName());
+       }
+
+       /**
+        * @covers pq\Gateway\Table::find
+        */
+       public function testFind() {
+               $rowset = $this->object->find();
+               $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
+               $rowset = $this->object->find(array("id = " => 1));
+               $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
+               $rowset = $this->object->find(array("id = " => 0));
+               $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
+               $rowset = $this->object->find(array(array("id<" => 2), array("id>" => 2)));
+               $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
+       }
+
+       /**
+        * @covers pq\Gateway\Table::create
+        */
+       public function testCreate() {
+               $rowset = $this->object->create(array("id" => new \pq\Query\Expr("DEFAULT")));
+               $this->assertInstanceOf("\\pq\\Gateway\\Rowset", $rowset);
+               $this->assertCount(1, $rowset);
+       }
+
+       /**
+        * @covers pq\Gateway\Table::update
+        */
+       public function testUpdate() {
+               $row = $this->object->create(array())->current();
+               $data = array(
+                       "created" => "2013-03-03 03:03:03",
+                       "counter" => 2,
+                       "number" => 2.2,
+                       "data" => "this is a test",
+               );
+               $row = $this->object->update(array("id = " => $row->id), $data)->current();
+               $data = array("id" => $row->id->get()) + $data;
+               $this->assertSame(array_map(function($v){return strval($v);}, $data), $row->getData());
+       }
+
+       /**
+        * @covers pq\Gateway\Table::delete
+        */
+       public function testDelete() {
+               $this->object->delete(array("id!=" => 0));
+               $this->assertCount(0, $this->object->find());
+       }
+
+}