objectmanager test
authorMichael Wallner <mike@php.net>
Tue, 22 Sep 2015 08:53:45 +0000 (10:53 +0200)
committerMichael Wallner <mike@php.net>
Tue, 22 Sep 2015 08:53:45 +0000 (10:53 +0200)
tests/lib/pq/Mapper/ObjectManagerTest.php

index a73ddd48bd989da33e2b49499d5a742768d04a76..9c6844f4bb4897e8475a07f0be16413fb3bffd97 100644 (file)
@@ -4,7 +4,9 @@ namespace pq\Mapper;
 
 require_once __DIR__."/../../../setup.inc";
 
+use BadMethodCallException;
 use pq\Connection;
+use pq\Gateway\Row;
 use pq\Gateway\Table;
 use pq\Mapper\ObjectManager;
 use QueryLogger;
@@ -16,206 +18,125 @@ class ObjectManagerTest extends \PHPUnit_Framework_TestCase
         * @var Connection
         */
        protected $conn;
+
        /**
-        * @var ObjectManager
+        * @var Mapper
         */
-       protected $objectManager;
+       protected $mapper;
 
        /**
-        * Sets up the fixture, for example, opens a network connection.
-        * This method is called before a test is executed.
+        * @var ObjectManager
         */
+       protected $objectManager;
+
        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);
+               $this->mapper = new Mapper;
+               $mapping = TestModel::mapAs($this->mapper);
                $mapping->getGateway()->getQueryExecutor()->attach(new QueryLogger());
-               $this->objectManager = new ObjectManager($mapping);
+               $this->objectManager = $mapping->getObjects();
        }
 
-       /**
-        * 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_TEARDOWN_SQL);
        }
 
-       public function testBasic() {
+       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.'
-               );
+       function testGetObject() {
+               $row = $this->objectManager->getMap()->getGateway()->find(["id="=>1])->current();
+               $row_id = $this->objectManager->rowId($row);
+               $this->objectManager->createObject($row);
+               $this->assertTrue($this->objectManager->hasObject($row_id));
+               $this->assertInstanceof(TestModel::class, $this->objectManager->getObject($row));
        }
 
        /**
-        * @covers pq\Mapper\ObjectManager::hasObject
-        * @todo   Implement testHasObject().
+        * @expectedException BadMethodCallException
         */
-       public function testHasObject() {
-               // Remove the following lines when you implement this test.
-               $this->markTestIncomplete(
-                       'This test has not been implemented yet.'
-               );
+       function testGetObjectException() {
+               $row = $this->objectManager->getMap()->getGateway()->find(["id="=>1])->current();
+               $this->objectManager->getObject($row);
        }
 
-       /**
-        * @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.'
-               );
+       function testReset() {
+               $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::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.'
-               );
+       function testResetObject() {
+               $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->resetObject($row);
+               $this->assertFalse($this->objectManager->hasObject($row_id));
        }
 
-       /**
-        * @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.'
-               );
+       function testFalseRowId() {
+               $this->assertFalse($this->objectManager->rowId(new Row($this->objectManager->getMap()->getGateway())));
        }
 
-       /**
-        * @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.'
-               );
+       function testExtractRowId() {
+               $row = $this->objectManager->getMap()->getGateway()->find(["id="=>1])->current();
+               $row_id = $this->objectManager->rowId($row);
+               $object = $this->objectManager->getMap()->map($row);
+               $this->assertEquals($row_id, $this->objectManager->extractRowId($object));
        }
 
-       /**
-        * @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.'
+       function testSerializeRowIdScalar() {
+               $this->assertEquals(
+                       $this->objectManager->serializeRowId(["id" => 1]),
+                       $this->objectManager->serializeRowId($this->objectManager->serializeRowId(["id"=>1]))
                );
        }
 
-       /**
-        * @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.'
-               );
+       function testSerializeRowIdNull() {
+               $this->assertEquals("null", $this->objectManager->serializeRowId(null));
+               $this->assertFalse($this->objectManager->serializeRowId(null, true));
+               $this->assertFalse($this->objectManager->serializeRowId(["id"=>null], true));
        }
 
-       /**
-        * @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.'
-               );
+       function testGetRow() {
+               $row = $this->objectManager->getMap()->getGateway()->find(["id="=>1])->current();
+               $obj = $this->objectManager->getMap()->map($row);
+               $this->assertSame($row, $this->objectManager->getRow($obj));
        }
 
-       /**
-        * @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.'
-               );
+       function testAsRow() {
+               $row = $this->objectManager->getMap()->getGateway()->find(["id="=>1])->current();
+               $obj = $this->objectManager->getMap()->map($row);
+               $this->assertSame($row, $this->objectManager->asRow($obj));
        }
 
-       /**
-        * @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.'
-               );
+       function testHasRow() {
+               $row = $this->objectManager->getMap()->getGateway()->find(["id="=>1])->current();
+               $obj = $this->objectManager->getMap()->map($row);
+               $this->assertSame($row, $this->objectManager->asRow($obj));
+               $this->assertTrue($this->objectManager->hasRow($this->objectManager->objectId($obj)));
+               $this->objectManager->resetRow($obj);
+               $this->assertFalse($this->objectManager->hasRow($this->objectManager->objectId($obj)));
+               $this->assertNotSame($row, $this->objectManager->asRow($obj));
        }
 
        /**
-        * @covers pq\Mapper\ObjectManager::asRow
-        * @todo   Implement testAsRow().
+        * @expectedException \BadMethodCallException
         */
-       public function testAsRow() {
-               // Remove the following lines when you implement this test.
-               $this->markTestIncomplete(
-                       'This test has not been implemented yet.'
-               );
+       function testGetRowException() {
+               $this->objectManager->getRow(new \stdClass);
        }
-
 }