diff --git a/tests/mock/RepoEntity.php b/tests/mock/RepoEntity.php new file mode 100644 index 0000000..01bbb45 --- /dev/null +++ b/tests/mock/RepoEntity.php @@ -0,0 +1,40 @@ +value = $value; + } + + + public function getId() + { + return $this->value; + } + + + public function setId($value) + { + $this->value = $value; + } + + + public function setAction($action) + { + $this->value = $action; + } + + + public function getAction() + { + return $this->value; + } +} diff --git a/tests/mock/RepoMapper.php b/tests/mock/RepoMapper.php new file mode 100644 index 0000000..b8fed8f --- /dev/null +++ b/tests/mock/RepoMapper.php @@ -0,0 +1,38 @@ +value = $value; + } + + public function fetch(RepoEntity $entity) + { + $entity->setAction('loaded'); + return $this->value; + } + + public function store(RepoEntity $entity) + { + $entity->setAction('saved'); + return $this->value; + } + + public function remove(RepoEntity $entity) + { + $entity->setAction('deleted'); + return $this->value; + } + + public function exists(RepoEntity $entity) + { + return $this->value; + } +} diff --git a/tests/unit/Palladium/Repository/IdentityTest.php b/tests/unit/Palladium/Repository/IdentityTest.php new file mode 100644 index 0000000..a3e6dd8 --- /dev/null +++ b/tests/unit/Palladium/Repository/IdentityTest.php @@ -0,0 +1,122 @@ + new \Mock\RepoMapper, + ])); + $instance->define(\Mock\RepoEntity::class, \Mock\RepoMapper::class); + + $item = new \Mock\RepoEntity; + $instance->load($item); + + $this->assertSame('loaded', $item->getAction()); + } + + + /** + * @test + */ + public function use_assigned_mapper_for_storing_entity() + { + $instance = new Identity(new \Mock\Factory([ + \Mock\RepoMapper::class => new \Mock\RepoMapper, + ])); + $instance->define(\Mock\RepoEntity::class, \Mock\RepoMapper::class); + + $item = new \Mock\RepoEntity; + $instance->save($item); + + $this->assertSame('saved', $item->getAction()); + } + + + /** + * @test + */ + public function use_assigned_mapper_for_deleting_entity() + { + $instance = new Identity(new \Mock\Factory([ + \Mock\RepoMapper::class => new \Mock\RepoMapper, + ])); + $instance->define(\Mock\RepoEntity::class, \Mock\RepoMapper::class); + + $item = new \Mock\RepoEntity; + $instance->delete($item); + + $this->assertSame('deleted', $item->getAction()); + } + + + /** + * @test + */ + public function use_assigned_mapper_for_checking_whether_entity_exists() + { + $instance = new Identity(new \Mock\Factory([ + \Mock\RepoMapper::class => new \Mock\RepoMapper(true), + ])); + $instance->define(\Mock\RepoEntity::class, \Mock\RepoMapper::class); + + $item = new \Mock\RepoEntity; + $this->assertSame(true, $instance->has($item)); + } + + + /** + * @test + */ + public function deny_definition_of_fake_entity_in_repo() + { + $this->expectException(\RuntimeException::class); + + $instance = new Identity(new \Mock\Factory([])); + $instance->define('FooBar', \Mock\RepoMapper::class); + } + + + /** + * @test + */ + public function deny_definition_of_fake_mapper_in_repo() + { + $this->expectException(\RuntimeException::class); + + $instance = new Identity(new \Mock\Factory([])); + $instance->define(\Mock\RepoEntity::class, 'FooBar'); + } + + + /** + * @test + */ + public function deny_override_with_undefined_mapper() + { + $this->expectException(\RuntimeException::class); + + $instance = new Identity(new \Mock\Factory([ + \Mock\RepoMapper::class => new \Mock\RepoMapper, + ])); + $instance->define(\Mock\RepoEntity::class, \Mock\RepoMapper::class); + + $item = new \Mock\RepoEntity; + $instance->load($item, 'FooBar'); + } +}