diff --git a/src/Repository/RepositoryAbstract.php b/src/Repository/RepositoryAbstract.php index 48622ec..0bcbdd0 100644 --- a/src/Repository/RepositoryAbstract.php +++ b/src/Repository/RepositoryAbstract.php @@ -19,7 +19,9 @@ abstract class RepositoryAbstract */ public function setStorage(StorageORMInterface $storage, $repository) { - $this->storage = $storage->setRepository($repository); + $storage->setRepository($repository); + + $this->storage = $storage->getRepository(); return $this; } 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..9fa572a 100644 --- a/tests/unit/Repository/RepositoryAbstractTest.php +++ b/tests/unit/Repository/RepositoryAbstractTest.php @@ -4,61 +4,57 @@ 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']); - $this->storage->expects($this->any()) - ->method('findAll') - ->will($this->returnValue([])); + $storage = $this->getMock('\WilliamEspindola\Field\Storage\ORM\StorageORMInterface'); - $this->repository = new MockRepository('tableName', $this->entity, $this->storage); - } + $storage->expects($this->any()) + ->method('__get') + ->with($this->equalTo('storage')) + ->will($this->returnValue($storageAbstract)); - /** - * @expectedException InvalidArgumentException - * @expectedExceptionMessage Invalid table name MockRepository. - */ - public function testSetTableNameWidhInvalidDataShuldThrownAndException() - { - $this->repository->setTableName(1); - } + $storage->expects($this->any()) + ->method('getRepository') + ->will($this->returnValue($storageAbstract)); - public function testeSetTableNameWithValidDataShouldWork() - { - $this->repository->setTableName('ola'); - $this->assertEquals('ola', PHPUnit_Framework_Assert::readAttribute($this->repository, 'tableName')); - } + $repository = new MockRepository($storage, 'sdtClass'); - public function testeGetTableNameWithValidDataShouldWork() - { - $this->repository->setTableName('ola'); - $this->assertEquals('ola', $this->repository->getTableName()); - } + $repository->setStorage($storage, 'sdtClass'); - public function testSetAndGetEntityWithValidDataShouldWork() - { - $this->repository->setEntity($this->entity); $this->assertInstanceOf( - '\WilliamEspindola\Field\Entity\EntityInterface', - $this->repository->getEntity() - ); - } - - public function testSetAndGetStorageWithValidDataShouldWork() - { - $this->repository->setStorage($this->storage); - $this->assertInstanceOf( - '\WilliamEspindola\Field\Storage\ORM\StorageORMInterface', - $this->repository->getStorage() + 'sdtClass', + $repository->getStorage() ); } public function testFindAllShouldReturnAnArrayObject() { - $this->assertInstanceOf('ArrayObject', $this->repository->findAll()); + $storageAbstract = $this->getMock('sdtClass', ['findAll']); + + $storageAbstract->expects($this->any()) + ->method('findAll') + ->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)); + + $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 {} \ No newline at end of file +class MockRepository extends RepositoryAbstract {} + diff --git a/tests/unit/Storage/ORM/RespectRelationalTest.php b/tests/unit/Storage/ORM/RespectRelationalTest.php index db0708d..6834abf 100644 --- a/tests/unit/Storage/ORM/RespectRelationalTest.php +++ b/tests/unit/Storage/ORM/RespectRelationalTest.php @@ -2,8 +2,6 @@ use WilliamEspindola\Field\Storage\ORM\RespectRelational; -use Respect\Relational\Mapper; - class RespectRelationalTest extends \PHPUnit_Framework_TestCase { protected function setUp() @@ -57,6 +55,9 @@ public function testSetRepositoryWithInvalidValueShouldThrowAndException() $instance->setRepository(''); } + /** + * @depends testSetRepositoryWithInvalidValueShouldThrowAndException + */ public function testSetRepositoryWithAValidNamespaceShouldWork() { $instance = new RespectRelational($this->mapper); @@ -71,6 +72,9 @@ public function testSetRepositoryWithAValidNamespaceShouldWork() ); } + /** + * @depends testSetRepositoryWithAValidNamespaceShouldWork + */ public function testSetRepositoryWithValidStringShouldWork() { $instance = new RespectRelational($this->mapper); @@ -83,29 +87,4 @@ public function testSetRepositoryWithValidStringShouldWork() 'The attribute repository is not instance of the string table name: mytable' ); } - - public function testGetRepositoryShouldReturnMockedInstance() - { - $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', - $instance->getRepository(), - 'is not mytable' - ); - } } \ No newline at end of file