From: Michael Wallner Date: Tue, 22 Sep 2015 15:16:05 +0000 (+0200) Subject: storage test X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fpq-gateway;a=commitdiff_plain;h=0a6fac6b260383a6abea76804410273c7b8b630b storage test --- diff --git a/lib/pq/Gateway/Cell.php b/lib/pq/Gateway/Cell.php index ab2a46f..3667854 100644 --- a/lib/pq/Gateway/Cell.php +++ b/lib/pq/Gateway/Cell.php @@ -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; } diff --git a/lib/pq/Mapper/MapInterface.php b/lib/pq/Mapper/MapInterface.php index 8c95056..bf238f1 100644 --- a/lib/pq/Mapper/MapInterface.php +++ b/lib/pq/Mapper/MapInterface.php @@ -14,6 +14,12 @@ interface MapInterface */ function getClass(); + /** + * Get the object manager + * @return ObjectManager + */ + function getObjects(); + /** * The the underlying table gateway * @return Table diff --git a/lib/pq/Mapper/Property/All.php b/lib/pq/Mapper/Property/All.php index 6eb4945..02855fd 100644 --- a/lib/pq/Mapper/Property/All.php +++ b/lib/pq/Mapper/Property/All.php @@ -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); } diff --git a/tests/lib/pq/Mapper/MapTest.php b/tests/lib/pq/Mapper/MapTest.php index 3ecff32..852c82e 100644 --- a/tests/lib/pq/Mapper/MapTest.php +++ b/tests/lib/pq/Mapper/MapTest.php @@ -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); } diff --git a/tests/lib/pq/Mapper/MapperTest.php b/tests/lib/pq/Mapper/MapperTest.php index fa9a4c7..4612066 100644 --- a/tests/lib/pq/Mapper/MapperTest.php +++ b/tests/lib/pq/Mapper/MapperTest.php @@ -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 index 0000000..fc63f18 --- /dev/null +++ b/tests/lib/pq/Mapper/StorageTest.php @@ -0,0 +1,71 @@ +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])); + } +}