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..b0783b7 100644 --- a/composer.json +++ b/composer.json @@ -19,8 +19,7 @@ ], "require": { "php" : ">=5.4", - "ocramius/generated-hydrator": "1.1.0", - "symfony/console": "~2.7" + "ocramius/generated-hydrator": "1.1.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 0bcbdd0..7d7b1cc 100644 --- a/src/Repository/RepositoryAbstract.php +++ b/src/Repository/RepositoryAbstract.php @@ -7,32 +7,9 @@ abstract class RepositoryAbstract { - /** - * @var object StorageORMInterface - */ - protected $storage; - - /** - * @param StorageORMInterface $storage - * @param string $repository - * @return RepositoryAbstract - */ - public function setStorage(StorageORMInterface $storage, $repository) - { - $storage->setRepository($repository); - - $this->storage = $storage->getRepository(); + 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/RepositoryAbstractTest.php b/tests/unit/Repository/RepositoryAbstractTest.php index 9fa572a..e4b5fec 100644 --- a/tests/unit/Repository/RepositoryAbstractTest.php +++ b/tests/unit/Repository/RepositoryAbstractTest.php @@ -2,6 +2,8 @@ use WilliamEspindola\Field\Repository\RepositoryAbstract; +use WilliamEspindola\Field\Storage\ORM\StorageORMInterface; + class RepositoryAbstractTest extends PHPUnit_Framework_TestCase { public function testSetAndGetStorageWithValidDataShouldWork() @@ -28,7 +30,6 @@ public function testSetAndGetStorageWithValidDataShouldWork() $repository->getStorage() ); } - public function testFindAllShouldReturnAnArrayObject() { $storageAbstract = $this->getMock('sdtClass', ['findAll']); @@ -56,5 +57,36 @@ public function testFindAllShouldReturnAnArrayObject() } } -class MockRepository extends RepositoryAbstract {} +class MockRepository extends RepositoryAbstract { + /** + * @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; + } +} diff --git a/tests/unit/Storage/ORM/RespectRelationalTest.php b/tests/unit/Storage/ORM/RespectRelationalTest.php index 6834abf..d7f10fd 100644 --- a/tests/unit/Storage/ORM/RespectRelationalTest.php +++ b/tests/unit/Storage/ORM/RespectRelationalTest.php @@ -2,10 +2,14 @@ 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(); @@ -61,30 +65,38 @@ public function testSetRepositoryWithInvalidValueShouldThrowAndException() public function testSetRepositoryWithAValidNamespaceShouldWork() { $instance = new RespectRelational($this->mapper); - $stubClass = $this->getMock('MyNamespace\MyClass'); $instance->setRepository('MyNamespace\MyClass'); - $this->assertEquals( - 'myclass', + $this->assertInstanceOf( + 'Respect\Data\Collections\Collection', PHPUnit_Framework_Assert::readAttribute($instance, 'repository'), - 'The attribute repository is not instance of the string class name: myclass' + 'The attribute repository is not instance of the string class name: Respect\Data\Collections\Collection' ); } - /** - * @depends testSetRepositoryWithAValidNamespaceShouldWork - */ - public function testSetRepositoryWithValidStringShouldWork() + public function testGetRepositoryShouldReturnMockedInstance() { - $instance = new RespectRelational($this->mapper); + $conn = $this->getMock( + 'PDO', + array('lastInsertId'), + array('sqlite::memory:') + ); + $conn->exec('CREATE TABLE mytable(id INTEGER PRIMARY KEY)'); + $conn->expects($this->any()) + ->method('lastInsertId') + ->will($this->throwException(new \PDOException)); + + + $mapper = new Mapper($conn); + $instance = new RespectRelational($mapper); $instance->setRepository('mytable'); - $this->assertEquals( - 'mytable', - PHPUnit_Framework_Assert::readAttribute($instance, 'repository'), - 'The attribute repository is not instance of the string table name: mytable' + $this->assertInstanceOf( + 'Respect\Data\Collections\Collection', + $instance->getRepository(), + 'The attribute repository is not instance of the string class name: Respect\Data\Collections\Collection' ); } -} \ No newline at end of file +}