]> git.m6w6.name Git - m6w6/pq-gateway/commitdiff
first test
authorMichael Wallner <mike@php.net>
Mon, 21 Sep 2015 18:51:30 +0000 (20:51 +0200)
committerMichael Wallner <mike@php.net>
Mon, 21 Sep 2015 18:51:30 +0000 (20:51 +0200)
lib/pq/Mapper/ObjectManager.php
tests/lib/pq/Mapper/ObjectManagerTest.php [new file with mode: 0644]
tests/setup.inc

index 0281904c0bea60e94bee7068e44e5ddf9d75f6b9..e56fac282c3c3aea13780bb34c70b3346245465e 100644 (file)
@@ -31,6 +31,14 @@ class ObjectManager
                $this->map = $map;
        }
 
+       /**
+        * Get the map
+        * @return MapInterface
+        */
+       function getMap() {
+               return $this->map;
+       }
+
        /**
         * Reset all managed objects
         */
diff --git a/tests/lib/pq/Mapper/ObjectManagerTest.php b/tests/lib/pq/Mapper/ObjectManagerTest.php
new file mode 100644 (file)
index 0000000..a73ddd4
--- /dev/null
@@ -0,0 +1,221 @@
+<?php
+
+namespace pq\Mapper;
+
+require_once __DIR__."/../../../setup.inc";
+
+use pq\Connection;
+use pq\Gateway\Table;
+use pq\Mapper\ObjectManager;
+use QueryLogger;
+use TestModel;
+
+class ObjectManagerTest extends \PHPUnit_Framework_TestCase
+{
+       /**
+        * @var Connection
+        */
+       protected $conn;
+       /**
+        * @var ObjectManager
+        */
+       protected $objectManager;
+
+       /**
+        * 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 Connection(PQ_TEST_DSN);
+               $this->conn->exec(PQ_TEST_SETUP_SQL);
+               Table::$defaultConnection = $this->conn;
+               $mapper = new Mapper;
+               $mapping = TestModel::mapAs($mapper);
+               $mapping->getGateway()->getQueryExecutor()->attach(new QueryLogger());
+               $this->objectManager = new ObjectManager($mapping);
+       }
+
+       /**
+        * Tears down the fixture, for example, closes a network connection.
+        * This method is called after a test is executed.
+        */
+       protected function tearDown() {
+
+       }
+
+       public function testBasic() {
+               $row = $this->objectManager->getMap()->getGateway()->find(["id="=>1])->current();
+               $row_id = $this->objectManager->rowId($row);
+               $this->assertFalse($this->objectManager->hasObject($row_id));
+               $this->objectManager->createObject($row);
+               $this->assertTrue($this->objectManager->hasObject($row_id));
+               $this->objectManager->reset();
+               $this->assertFalse($this->objectManager->hasObject($row_id));
+       }
+       
+       /**
+        * @covers pq\Mapper\ObjectManager::rowId
+        * @todo   Implement testRowId().
+        */
+       public function testRowId() {
+               // Remove the following lines when you implement this test.
+               $this->markTestIncomplete(
+                       'This test has not been implemented yet.'
+               );
+       }
+
+       /**
+        * @covers pq\Mapper\ObjectManager::objectId
+        * @todo   Implement testObjectId().
+        */
+       public function testObjectId() {
+               // Remove the following lines when you implement this test.
+               $this->markTestIncomplete(
+                       'This test has not been implemented yet.'
+               );
+       }
+
+       /**
+        * @covers pq\Mapper\ObjectManager::extractRowId
+        * @todo   Implement testExtractRowId().
+        */
+       public function testExtractRowId() {
+               // Remove the following lines when you implement this test.
+               $this->markTestIncomplete(
+                       'This test has not been implemented yet.'
+               );
+       }
+
+       /**
+        * @covers pq\Mapper\ObjectManager::serializeRowId
+        * @todo   Implement testSerializeRowId().
+        */
+       public function testSerializeRowId() {
+               // Remove the following lines when you implement this test.
+               $this->markTestIncomplete(
+                       'This test has not been implemented yet.'
+               );
+       }
+
+       /**
+        * @covers pq\Mapper\ObjectManager::hasObject
+        * @todo   Implement testHasObject().
+        */
+       public function testHasObject() {
+               // Remove the following lines when you implement this test.
+               $this->markTestIncomplete(
+                       'This test has not been implemented yet.'
+               );
+       }
+
+       /**
+        * @covers pq\Mapper\ObjectManager::createObject
+        * @todo   Implement testCreateObject().
+        */
+       public function testCreateObject() {
+               // Remove the following lines when you implement this test.
+               $this->markTestIncomplete(
+                       'This test has not been implemented yet.'
+               );
+       }
+
+       /**
+        * @covers pq\Mapper\ObjectManager::resetObject
+        * @todo   Implement testResetObject().
+        */
+       public function testResetObject() {
+               // Remove the following lines when you implement this test.
+               $this->markTestIncomplete(
+                       'This test has not been implemented yet.'
+               );
+       }
+
+       /**
+        * @covers pq\Mapper\ObjectManager::getObject
+        * @todo   Implement testGetObject().
+        */
+       public function testGetObject() {
+               // Remove the following lines when you implement this test.
+               $this->markTestIncomplete(
+                       'This test has not been implemented yet.'
+               );
+       }
+
+       /**
+        * @covers pq\Mapper\ObjectManager::getObjectById
+        * @todo   Implement testGetObjectById().
+        */
+       public function testGetObjectById() {
+               // Remove the following lines when you implement this test.
+               $this->markTestIncomplete(
+                       'This test has not been implemented yet.'
+               );
+       }
+
+       /**
+        * @covers pq\Mapper\ObjectManager::asObject
+        * @todo   Implement testAsObject().
+        */
+       public function testAsObject() {
+               // Remove the following lines when you implement this test.
+               $this->markTestIncomplete(
+                       'This test has not been implemented yet.'
+               );
+       }
+
+       /**
+        * @covers pq\Mapper\ObjectManager::hasRow
+        * @todo   Implement testHasRow().
+        */
+       public function testHasRow() {
+               // Remove the following lines when you implement this test.
+               $this->markTestIncomplete(
+                       'This test has not been implemented yet.'
+               );
+       }
+
+       /**
+        * @covers pq\Mapper\ObjectManager::createRow
+        * @todo   Implement testCreateRow().
+        */
+       public function testCreateRow() {
+               // Remove the following lines when you implement this test.
+               $this->markTestIncomplete(
+                       'This test has not been implemented yet.'
+               );
+       }
+
+       /**
+        * @covers pq\Mapper\ObjectManager::resetRow
+        * @todo   Implement testResetRow().
+        */
+       public function testResetRow() {
+               // Remove the following lines when you implement this test.
+               $this->markTestIncomplete(
+                       'This test has not been implemented yet.'
+               );
+       }
+
+       /**
+        * @covers pq\Mapper\ObjectManager::getRow
+        * @todo   Implement testGetRow().
+        */
+       public function testGetRow() {
+               // Remove the following lines when you implement this test.
+               $this->markTestIncomplete(
+                       'This test has not been implemented yet.'
+               );
+       }
+
+       /**
+        * @covers pq\Mapper\ObjectManager::asRow
+        * @todo   Implement testAsRow().
+        */
+       public function testAsRow() {
+               // Remove the following lines when you implement this test.
+               $this->markTestIncomplete(
+                       'This test has not been implemented yet.'
+               );
+       }
+
+}
index bdee9d3ac2da135ab436f61e7644b14ac4df6296..3a639a5af0f8ca13c65c3450f665b11e3f4b2809 100644 (file)
@@ -1,5 +1,13 @@
 <?php
 
+use pq\Connection;
+use pq\Gateway\Table;
+use pq\Mapper\Map;
+use pq\Mapper\MapInterface;
+use pq\Mapper\Mapper;
+use pq\Query\ExecutorInterface;
+use pq\Query\Writer;
+
 ini_set("date.timezone", "UTC");
 ini_set("error_reporting", E_ALL);
 
@@ -41,14 +49,14 @@ SQL;
 
 require_once __DIR__ . "/../vendor/autoload.php";
 
-function executeInConcurrentTransaction(\pq\Query\ExecutorInterface $exec, $sql, array $params = null) {
+function executeInConcurrentTransaction(ExecutorInterface $exec, $sql, array $params = null) {
        $conn = $exec->getConnection();
-       $exec->setConnection(new pq\Connection(PQ_TEST_DSN));
-       $exec->execute(new \pq\Query\Writer($sql, $params), function(){});
+       $exec->setConnection(new Connection(PQ_TEST_DSN));
+       $exec->execute(new Writer($sql, $params), function(){});
        $exec->setConnection($conn);
 }
 
-class QueryLogger implements \SplObserver
+class QueryLogger implements SplObserver
 {
        protected $fp;
        
@@ -57,7 +65,7 @@ class QueryLogger implements \SplObserver
                        $logfile = __DIR__."/query.log";
                }
                if (!$this->fp = @fopen($logfile, "a")) {
-                       throw new \RuntimeException(error_get_last()["message"]);
+                       throw new RuntimeException(error_get_last()["message"]);
                }
        }
        
@@ -67,7 +75,7 @@ class QueryLogger implements \SplObserver
                }
        }
        
-       function update(\SplSubject $executor) {
+       function update(SplSubject $executor) {
                $result = $executor->getResult();
                if (isset($result)) {
                        fprintf($this->fp, "[%s] R %s\n", 
@@ -81,3 +89,29 @@ class QueryLogger implements \SplObserver
                }
        }
 }
+
+class TestModel implements JsonSerializable {
+       private $id, $created, $counter, $number, $data, $list, $prop;
+
+       function jsonSerialize() {
+               return get_object_vars($this);
+       }
+
+       /**
+        * @param Mapper $mapper
+        * @return MapInterface
+        */
+       static function mapAs(Mapper $mapper) {
+               return new Map(
+                       __CLASS__,
+                       new Table("test"),
+                       $mapper->mapField("id"),
+                       $mapper->mapField("created"),
+                       $mapper->mapField("counter"),
+                       $mapper->mapField("number"),
+                       $mapper->mapField("data"),
+                       $mapper->mapField("list"),
+                       $mapper->mapField("prop")
+               );
+       }
+}