storage test
authorMichael Wallner <mike@php.net>
Tue, 22 Sep 2015 15:16:05 +0000 (17:16 +0200)
committerMichael Wallner <mike@php.net>
Tue, 22 Sep 2015 15:16:05 +0000 (17:16 +0200)
lib/pq/Gateway/Cell.php
lib/pq/Mapper/MapInterface.php
lib/pq/Mapper/Property/All.php
tests/lib/pq/Mapper/MapTest.php
tests/lib/pq/Mapper/MapperTest.php
tests/lib/pq/Mapper/StorageTest.php [new file with mode: 0644]

index ab2a46fceac95a3a97056fe78bdc4679c20ca2da..3667854430f1848fe844040457c4aff3e20d93fa 100644 (file)
@@ -50,8 +50,10 @@ class Cell extends Expressible implements \ArrayAccess
                if ($data instanceof Cell) {
                        $data = $data->get();
                }
-               parent::set($data);
-               $this->dirty = true;
+               if ($this->data !== $data) {
+                       parent::set($data);
+                       $this->dirty = true;
+               }
                return $this;
        }
        
index 8c950568f365d82fb32b6af8ef5bdf3505dede82..bf238f1cccbb42b68069c39624e145bb00e7a80b 100644 (file)
@@ -14,6 +14,12 @@ interface MapInterface
         */
        function getClass();
 
+       /**
+        * Get the object manager
+        * @return ObjectManager
+        */
+       function getObjects();
+
        /**
         * The the underlying table gateway
         * @return Table
index 6eb49457645c246efb91c41d63b4a2e170e52b46..02855fd279a678c5a76ebe536991a4c76d20f3c7 100644 (file)
@@ -44,13 +44,13 @@ class All implements RefPropertyInterface
         * @return callable deferred callback
         */
        function write($object, Row $rowToUpdate) {
-               $property = $this->findRefProperty($object);
-               $map = $this->mapper->mapOf($this->refClass);
                if (($refs = $this->extract($object))) {
+                       $property = $this->findRefProperty($object);
                        foreach ($refs as $ref) {
                                $property->assign($ref, $object);
                        }
-                       return function() use($map, $refs) {
+                       return function() use($refs) {
+                               $map = $this->mapper->mapOf($this->refClass);
                                foreach ($refs as $ref) {
                                        $map->unmap($ref);
                                }
index 3ecff32b2b5647e8e436359bec51f9764fdbafa7..852c82edfa37f7dbc74d187ffc1f35a4d73c1487 100644 (file)
@@ -2,16 +2,16 @@
 
 namespace pq\Mapper;
 
+use PHPUnit_Framework_TestCase;
 use pq\Connection;
 use pq\Gateway\Table;
+use QueryLogger;
 use RefTestModel;
-use stdClass;
 use TestModel;
-use UnexpectedValueException;
 
 require_once __DIR__."/../../../setup.inc";
 
-class MapTest extends \PHPUnit_Framework_TestCase
+class MapTest extends PHPUnit_Framework_TestCase
 {
        /**
         * @var Connection
@@ -33,7 +33,9 @@ class MapTest extends \PHPUnit_Framework_TestCase
                $this->conn->exec(PQ_TEST_SETUP_SQL);
                Table::$defaultConnection = $this->conn;
                $this->mapper = new Mapper;
-               $this->map = TestModel::mapAs($this->mapper);
+               $this->map = $this->mapper->mapOf(TestModel::class);
+               $this->map->getGateway()->getQueryExecutor()->attach(new QueryLogger());
+               $this->mapper->mapOf(RefTestModel::class)->getGateway()->getQueryExecutor()->attach(new QueryLogger());
        }
 
        protected function tearDown() {
@@ -56,7 +58,7 @@ class MapTest extends \PHPUnit_Framework_TestCase
        function testUnmapRef() {
                $obj = new \TestModel;
                $obj->ref1 = $obj->ref2 = [
-                       new \RefTestModel
+                       new RefTestModel
                ];
                $this->map->unmap($obj);
        }
index fa9a4c79f5a09736c54cb7c4277383541de0e38a..46120661b6759820947d1a523f09a0f2db5406d6 100644 (file)
@@ -4,6 +4,7 @@ namespace pq\Mapper;
 
 use pq\Connection;
 use pq\Gateway\Table;
+use QueryLogger;
 use RefTestModel;
 use stdClass;
 use TestModel;
@@ -34,6 +35,7 @@ class MapperTest extends \PHPUnit_Framework_TestCase
                Table::$defaultConnection = $this->conn;
                $this->mapper = new Mapper;
                $this->map = TestModel::mapAs($this->mapper);
+               $this->map->getGateway()->getQueryExecutor()->attach(new QueryLogger());
        }
 
        protected function tearDown() {
diff --git a/tests/lib/pq/Mapper/StorageTest.php b/tests/lib/pq/Mapper/StorageTest.php
new file mode 100644 (file)
index 0000000..fc63f18
--- /dev/null
@@ -0,0 +1,71 @@
+<?php
+
+namespace pq\Mapper;
+
+use PHPUnit_Framework_TestCase;
+use pq\Connection;
+use pq\Gateway\Table;
+use QueryLogger;
+use RefTestModel;
+use TestModel;
+
+require_once __DIR__."/../../../setup.inc";
+
+class StorageTest extends PHPUnit_Framework_TestCase
+{
+       /**
+        * @var Connection
+        */
+       protected $conn;
+
+       /**
+        * @var Mapper
+        */
+       protected $mapper;
+
+       /**
+        * @var Storage
+        */
+       protected $storage;
+
+       protected function setUp() {
+               $this->conn = new Connection(PQ_TEST_DSN);
+               $this->conn->exec(PQ_TEST_SETUP_SQL);
+               Table::$defaultConnection = $this->conn;
+               $this->mapper = new Mapper;
+               $this->mapper->mapOf(TestModel::class)->getGateway()->getQueryExecutor()->attach(new QueryLogger());
+               $this->mapper->mapOf(RefTestModel::class)->getGateway()->getQueryExecutor()->attach(new QueryLogger());
+               $this->storage = $this->mapper->createStorage(TestModel::class);
+       }
+
+       protected function tearDown() {
+               $this->conn->exec(PQ_TEST_TEARDOWN_SQL);
+       }
+
+       function testFind() {
+               $objects = $this->storage->find();
+               for ($i = 0; $i < count($objects); ++$i) {
+                       $this->assertSame($i+1, $objects[$i]->id);
+               }
+       }
+
+       function testSave() {
+               $test = new TestModel;
+               $test->ref1 = $test->ref2 = [
+                       new RefTestModel
+               ];
+               $this->storage->save($test);
+
+               $this->mapper->mapOf(TestModel::class)->getObjects()->reset();
+               $this->mapper->mapOf(RefTestModel::class)->getObjects()->reset();
+
+               $this->assertEquals([$test], $this->storage->find(["id="=>$test->id]));
+       }
+
+       function testDelete() {
+               $obj = current($this->storage->find());
+               $this->storage->delete($obj);
+               $this->mapper->mapOf(TestModel::class)->getObjects()->resetRow($obj);
+               $this->assertCount(0, $this->storage->find(["id="=>$obj->id]));
+       }
+}