From 8a790ac1dfe231ce4aa77538837278e7eab9656e Mon Sep 17 00:00:00 2001 From: William Espindola Date: Tue, 22 Sep 2015 16:56:34 -0300 Subject: [PATCH 1/2] Fix - Correct use of abstract methods on RepositoryAbstract close #13 - Remove EntityInterface of RepositoryAbstract class close #19 --- .travis.yml | 2 + composer.json | 3 +- coverage.clover | 764 ++++++++++++++++++ src/Repository/CollectionFieldRepository.php | 27 + src/Repository/CollectionRepository.php | 27 + src/Repository/FieldRepository.php | 27 + src/Repository/OptionRepository.php | 27 + src/Repository/RepositoryAbstract.php | 25 +- src/Storage/ORM/Doctrine.php | 7 +- src/Storage/ORM/RespectRelational.php | 29 +- src/Storage/ORM/StorageORMInterface.php | 7 +- tests/unit/Repository/FiendRepositoryTest.php | 28 +- .../unit/Repository/OptionRepositoryTest.php | 22 - .../Repository/RepositoryAbstractTest.php | 106 ++- .../Storage/ORM/RespectRelationalTest.php | 28 +- 15 files changed, 1007 insertions(+), 122 deletions(-) create mode 100644 coverage.clover delete mode 100644 tests/unit/Repository/OptionRepositoryTest.php diff --git a/.travis.yml b/.travis.yml index 18fc613..372b51e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,6 +17,8 @@ matrix: before_script: - travis_retry composer self-update - travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source + - travis_retry composer require doctrine/orm + - travis_retry composer require respect/relational script: - phpunit --configuration phpunit.xml.dist --coverage-text --coverage-clover=coverage.clover diff --git a/composer.json b/composer.json index c4108e1..d34d3b9 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,8 @@ "require": { "php" : ">=5.4", "ocramius/generated-hydrator": "1.1.0", - "symfony/console": "~2.7" + "symfony/console": "~2.7", + "respect/relational": "^0.8.0" }, "require-dev": { "phpunit/phpunit" : "4.*", diff --git a/coverage.clover b/coverage.clover new file mode 100644 index 0000000..fc94eae --- /dev/null +++ b/coverage.clover @@ -0,0 +1,764 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Repository/CollectionFieldRepository.php b/src/Repository/CollectionFieldRepository.php index c86221b..9ae2bfd 100644 --- a/src/Repository/CollectionFieldRepository.php +++ b/src/Repository/CollectionFieldRepository.php @@ -8,8 +8,35 @@ class CollectionFieldRepository extends RepositoryAbstract implements RepositoryInterface { + /** + * @var object StorageORMInterface + */ + protected $storage; + public function __construct(StorageORMInterface $storage) { $this->setStorage($storage, 'WilliamEspindola\Field\Entity\CollectionField'); } + + /** + * @param StorageORMInterface $storage + * @param string $repository + * @return RepositoryAbstract + */ + public function setStorage(StorageORMInterface $storage, $repository) + { + $storage->setRepository($repository); + + $this->storage = $storage->getStorage(); + + return $this; + } + + /** + * @return StorageInterface + */ + public function getStorage() + { + return $this->storage; + } } \ No newline at end of file diff --git a/src/Repository/CollectionRepository.php b/src/Repository/CollectionRepository.php index 69da266..fb6d1ca 100644 --- a/src/Repository/CollectionRepository.php +++ b/src/Repository/CollectionRepository.php @@ -8,8 +8,35 @@ class CollectionRepository extends RepositoryAbstract implements RepositoryInterface { + /** + * @var object StorageORMInterface + */ + protected $storage; + public function __construct(StorageORMInterface $storage) { $this->setStorage($storage, 'WilliamEspindola\Field\Entity\Collection'); } + + /** + * @param StorageORMInterface $storage + * @param string $repository + * @return RepositoryAbstract + */ + public function setStorage(StorageORMInterface $storage, $repository) + { + $storage->setRepository($repository); + + $this->storage = $storage->getStorage(); + + return $this; + } + + /** + * @return StorageInterface + */ + public function getStorage() + { + return $this->storage; + } } \ No newline at end of file diff --git a/src/Repository/FieldRepository.php b/src/Repository/FieldRepository.php index d068697..80f9b26 100644 --- a/src/Repository/FieldRepository.php +++ b/src/Repository/FieldRepository.php @@ -8,8 +8,35 @@ class FieldRepository extends RepositoryAbstract implements RepositoryInterface { + /** + * @var object StorageORMInterface + */ + protected $storage; + public function __construct(StorageORMInterface $storage) { $this->setStorage($storage, 'WilliamEspindola\Field\Entity\Field'); } + + /** + * @param StorageORMInterface $storage + * @param string $repository + * @return RepositoryAbstract + */ + public function setStorage(StorageORMInterface $storage, $repository) + { + $storage->setRepository($repository); + + $this->storage = $storage->getStorage(); + + return $this; + } + + /** + * @return StorageInterface + */ + public function getStorage() + { + return $this->storage; + } } \ No newline at end of file diff --git a/src/Repository/OptionRepository.php b/src/Repository/OptionRepository.php index 25b42c8..9ed0dbd 100644 --- a/src/Repository/OptionRepository.php +++ b/src/Repository/OptionRepository.php @@ -8,8 +8,35 @@ class OptionRepository extends RepositoryAbstract implements RepositoryInterface { + /** + * @var object StorageORMInterface + */ + protected $storage; + public function __construct(StorageORMInterface $storage) { $this->setStorage($storage, 'WilliamEspindola\Field\Entity\Options'); } + + /** + * @param StorageORMInterface $storage + * @param string $repository + * @return RepositoryAbstract + */ + public function setStorage(StorageORMInterface $storage, $repository) + { + $storage->setRepository($repository); + + $this->storage = $storage->getStorage(); + + return $this; + } + + /** + * @return StorageInterface + */ + public function getStorage() + { + return $this->storage; + } } \ No newline at end of file diff --git a/src/Repository/RepositoryAbstract.php b/src/Repository/RepositoryAbstract.php index 48622ec..7d7b1cc 100644 --- a/src/Repository/RepositoryAbstract.php +++ b/src/Repository/RepositoryAbstract.php @@ -7,30 +7,9 @@ abstract class RepositoryAbstract { - /** - * @var object StorageORMInterface - */ - protected $storage; - - /** - * @param StorageORMInterface $storage - * @param string $repository - * @return RepositoryAbstract - */ - public function setStorage(StorageORMInterface $storage, $repository) - { - $this->storage = $storage->setRepository($repository); + abstract public function setStorage(StorageORMInterface $storage, $repository); - return $this; - } - - /** - * @return StorageInterface - */ - public function getStorage() - { - return $this->storage; - } + abstract public function getStorage(); /** * @return \ArrayObject diff --git a/src/Storage/ORM/Doctrine.php b/src/Storage/ORM/Doctrine.php index 22dc329..b8f60ed 100644 --- a/src/Storage/ORM/Doctrine.php +++ b/src/Storage/ORM/Doctrine.php @@ -58,11 +58,14 @@ public function getRepository() return $this->getMapper()->getRepository($this->repository); } + public function getStorage() + { + return $this; + } + public function setRepository($repository) { $this->repository = $repository; - - return $this; } /** diff --git a/src/Storage/ORM/RespectRelational.php b/src/Storage/ORM/RespectRelational.php index 566b26d..88f5a83 100644 --- a/src/Storage/ORM/RespectRelational.php +++ b/src/Storage/ORM/RespectRelational.php @@ -5,6 +5,7 @@ use Respect\Relational\Mapper; use Respect\Data\Styles; use \InvalidArgumentException as Argument; +use Respect\Data\Collections\Collection; /** * Providers the Respect\Relational\Mapper ORM behavior @@ -15,6 +16,9 @@ */ class RespectRelational implements StorageORMInterface { + /** + * + */ const INVALID_MAPPER_MESSAGE = 'Argument must be Respect\Relational\Mapper'; /** @@ -27,6 +31,9 @@ class RespectRelational implements StorageORMInterface */ protected $repository; + /** + * @param Mapper $mapper + */ public function __construct(Mapper $mapper) { $this->setMapper($mapper); @@ -51,6 +58,10 @@ public function getMapper() return $this->mapper; } + /** + * @param $repository + * @throws \InvalidArgumentException + */ public function setRepository($repository) { if (empty($repository)) @@ -61,14 +72,24 @@ public function setRepository($repository) $repository = strtolower($reflect->getShortName()); } - $this->repository = $repository; + $this->repository = new Collection($repository); + $this->repository->setMapper($this->mapper); + } + /** + * @return $this + */ + public function getStorage() + { return $this; } + /** + * @return string + */ public function getRepository() { - return $this->getMapper()->{$this->repository}; + return $this->repository; } /** @@ -76,7 +97,9 @@ public function getRepository() */ public function findAll() { - return $this->getRepository()->fetchAll(); + $repository = $this->getRepository(); + + return $repository->fetchAll(); } /** diff --git a/src/Storage/ORM/StorageORMInterface.php b/src/Storage/ORM/StorageORMInterface.php index c4df294..44bf82e 100644 --- a/src/Storage/ORM/StorageORMInterface.php +++ b/src/Storage/ORM/StorageORMInterface.php @@ -2,8 +2,6 @@ namespace WilliamEspindola\Field\Storage\ORM; -use WilliamEspindola\Field\Entity\EntityInterface; - /** * Interface StorageORMInterface * @@ -77,4 +75,9 @@ public function getRepository(); * @return mixed */ public function setRepository($repository); + + /** + * @return mixed + */ + public function getStorage(); } \ No newline at end of file diff --git a/tests/unit/Repository/FiendRepositoryTest.php b/tests/unit/Repository/FiendRepositoryTest.php index 1e542d9..1134235 100644 --- a/tests/unit/Repository/FiendRepositoryTest.php +++ b/tests/unit/Repository/FiendRepositoryTest.php @@ -4,19 +4,27 @@ class FieldRepositoryTest extends PHPUnit_Framework_TestCase { - protected function setUp() + public function testFindAllShouldReturnAnArrayObject() { - $this->storage = $this->getMock('\WilliamEspindola\Field\Storage\ORM\StorageORMInterface'); + $storageAbstract = $this->getMock('sdtClass', ['findAll']); - $this->storage->expects($this->any()) - ->method('findAll') - ->will($this->returnValue([])); + $storageAbstract->expects($this->any()) + ->method('findAll') + ->will($this->returnValue((object)([]))); - $this->repository = new FieldRepository($this->storage); - } + $storage = $this->getMock('\WilliamEspindola\Field\Storage\ORM\StorageORMInterface'); - public function testFindAllShouldReturnAnArrayObject() - { - $this->assertInstanceOf('ArrayObject', $this->repository->findAll()); + $storage->expects($this->any()) + ->method('__get') + ->with($this->equalTo('storage')) + ->will($this->returnValue($storageAbstract)); + + $storage->expects($this->any()) + ->method('getRepository') + ->will($this->returnValue($storageAbstract)); + + $repository = new FieldRepository($storage); + + $this->assertInstanceOf('stdClass', $repository->findAll()); } } \ No newline at end of file diff --git a/tests/unit/Repository/OptionRepositoryTest.php b/tests/unit/Repository/OptionRepositoryTest.php deleted file mode 100644 index 808b7d5..0000000 --- a/tests/unit/Repository/OptionRepositoryTest.php +++ /dev/null @@ -1,22 +0,0 @@ -storage = $this->getMock('\WilliamEspindola\Field\Storage\ORM\StorageORMInterface'); - - $this->storage->expects($this->any()) - ->method('findAll') - ->will($this->returnValue([])); - - $this->repository = new OptionRepository($this->storage); - } - - public function testFindAllShouldReturnAnArrayObject() - { - $this->assertInstanceOf('ArrayObject', $this->repository->findAll()); - } -} \ No newline at end of file diff --git a/tests/unit/Repository/RepositoryAbstractTest.php b/tests/unit/Repository/RepositoryAbstractTest.php index 53c22a0..593b650 100644 --- a/tests/unit/Repository/RepositoryAbstractTest.php +++ b/tests/unit/Repository/RepositoryAbstractTest.php @@ -2,63 +2,91 @@ use WilliamEspindola\Field\Repository\RepositoryAbstract; +use WilliamEspindola\Field\Storage\ORM\StorageORMInterface; + class RepositoryAbstractTest extends PHPUnit_Framework_TestCase { - protected function setUp() + public function testSetAndGetStorageWithValidDataShouldWork() { - $this->entity = $this->getMock('\WilliamEspindola\Field\Entity\EntityInterface'); - $this->storage = $this->getMock('\WilliamEspindola\Field\Storage\ORM\StorageORMInterface'); + $storageAbstract = $this->getMock('sdtClass', ['findAll']); + + $storage = $this->getMock('\WilliamEspindola\Field\Storage\ORM\StorageORMInterface'); + + $storage->expects($this->any()) + ->method('__get') + ->with($this->equalTo('storage')) + ->will($this->returnValue($storageAbstract)); + + $storage->expects($this->any()) + ->method('getRepository') + ->will($this->returnValue($storageAbstract)); - $this->storage->expects($this->any()) + $repository = new MockRepository($storage, 'sdtClass'); + + $repository->setStorage($storage, 'sdtClass'); + + $this->assertInstanceOf( + 'sdtClass', + $repository->getStorage() + ); + } + public function testFindAllShouldReturnAnArrayObject() + { + $storageAbstract = $this->getMock('sdtClass', ['findAll']); + + $storageAbstract->expects($this->any()) ->method('findAll') - ->will($this->returnValue([])); + ->will($this->returnValue((object)([]))); + + $storage = $this->getMock('\WilliamEspindola\Field\Storage\ORM\StorageORMInterface'); + + $storage->expects($this->any()) + ->method('__get') + ->with($this->equalTo('storage')) + ->will($this->returnValue($storageAbstract)); - $this->repository = new MockRepository('tableName', $this->entity, $this->storage); + $storage->expects($this->any()) + ->method('getRepository') + ->will($this->returnValue($storageAbstract)); + + $repository = new MockRepository($storage, 'sdtClass'); + + $repository->setStorage($storage, 'sdtClass'); + + $this->assertInstanceOf('stdClass', $repository->findAll()); } +} +class MockRepository extends RepositoryAbstract { /** - * @expectedException InvalidArgumentException - * @expectedExceptionMessage Invalid table name MockRepository. + * @var object StorageORMInterface */ - public function testSetTableNameWidhInvalidDataShuldThrownAndException() - { - $this->repository->setTableName(1); - } + protected $storage; - public function testeSetTableNameWithValidDataShouldWork() + public function __construct(StorageORMInterface $storage) { - $this->repository->setTableName('ola'); - $this->assertEquals('ola', PHPUnit_Framework_Assert::readAttribute($this->repository, 'tableName')); + $this->setStorage($storage, 'WilliamEspindola\Field\Entity\Collection'); } - public function testeGetTableNameWithValidDataShouldWork() + /** + * @param StorageORMInterface $storage + * @param string $repository + * @return RepositoryAbstract + */ + public function setStorage(StorageORMInterface $storage, $repository) { - $this->repository->setTableName('ola'); - $this->assertEquals('ola', $this->repository->getTableName()); - } + $storage->setRepository($repository); - public function testSetAndGetEntityWithValidDataShouldWork() - { - $this->repository->setEntity($this->entity); - $this->assertInstanceOf( - '\WilliamEspindola\Field\Entity\EntityInterface', - $this->repository->getEntity() - ); - } + $this->storage = $storage->getStorage(); - public function testSetAndGetStorageWithValidDataShouldWork() - { - $this->repository->setStorage($this->storage); - $this->assertInstanceOf( - '\WilliamEspindola\Field\Storage\ORM\StorageORMInterface', - $this->repository->getStorage() - ); + return $this; } - public function testFindAllShouldReturnAnArrayObject() + /** + * @return StorageInterface + */ + public function getStorage() { - $this->assertInstanceOf('ArrayObject', $this->repository->findAll()); + return $this->storage; } -} - -class MockRepository extends RepositoryAbstract {} \ No newline at end of file +} \ No newline at end of file diff --git a/tests/unit/Storage/ORM/RespectRelationalTest.php b/tests/unit/Storage/ORM/RespectRelationalTest.php index db0708d..ee45b99 100644 --- a/tests/unit/Storage/ORM/RespectRelationalTest.php +++ b/tests/unit/Storage/ORM/RespectRelationalTest.php @@ -3,11 +3,13 @@ use WilliamEspindola\Field\Storage\ORM\RespectRelational; use Respect\Relational\Mapper; +use Respect\Data\Collections\Collection; class RespectRelationalTest extends \PHPUnit_Framework_TestCase { protected function setUp() { + $this->mapper = $this->getMockBuilder('Respect\Relational\Mapper') ->disableOriginalConstructor() ->getMock(); @@ -60,27 +62,13 @@ public function testSetRepositoryWithInvalidValueShouldThrowAndException() public function testSetRepositoryWithAValidNamespaceShouldWork() { $instance = new RespectRelational($this->mapper); - $stubClass = $this->getMock('MyNamespace\MyClass'); $instance->setRepository('MyNamespace\MyClass'); - $this->assertEquals( - 'myclass', - PHPUnit_Framework_Assert::readAttribute($instance, 'repository'), - 'The attribute repository is not instance of the string class name: myclass' - ); - } - - public function testSetRepositoryWithValidStringShouldWork() - { - $instance = new RespectRelational($this->mapper); - - $instance->setRepository('mytable'); - - $this->assertEquals( - 'mytable', + $this->assertInstanceOf( + 'Respect\Data\Collections\Collection', PHPUnit_Framework_Assert::readAttribute($instance, 'repository'), - 'The attribute repository is not instance of the string table name: mytable' + 'The attribute repository is not instance of the string class name: Respect\Data\Collections\Collection' ); } @@ -102,10 +90,10 @@ public function testGetRepositoryShouldReturnMockedInstance() $instance = new RespectRelational($mapper); $instance->setRepository('mytable'); - $this->assertEquals( - 'mytable', + $this->assertInstanceOf( + 'Respect\Data\Collections\Collection', $instance->getRepository(), - 'is not mytable' + 'The attribute repository is not instance of the string class name: Respect\Data\Collections\Collection' ); } } \ No newline at end of file From 434e4fb958fb8eddf04da0c46a48bb70d30914be Mon Sep 17 00:00:00 2001 From: William Espindola Date: Tue, 22 Sep 2015 16:58:22 -0300 Subject: [PATCH 2/2] remove optional modules --- composer.json | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/composer.json b/composer.json index d34d3b9..b0783b7 100644 --- a/composer.json +++ b/composer.json @@ -19,9 +19,7 @@ ], "require": { "php" : ">=5.4", - "ocramius/generated-hydrator": "1.1.0", - "symfony/console": "~2.7", - "respect/relational": "^0.8.0" + "ocramius/generated-hydrator": "1.1.0" }, "require-dev": { "phpunit/phpunit" : "4.*",