Skip to content

Commit

Permalink
Merge pull request #23 from williamespindola/fix/write-tests
Browse files Browse the repository at this point in the history
Pass tests
  • Loading branch information
williamespindola committed Sep 18, 2015
2 parents a977ed0 + 160b502 commit f1205db
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 102 deletions.
4 changes: 3 additions & 1 deletion src/Repository/RepositoryAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
28 changes: 18 additions & 10 deletions tests/unit/Repository/FiendRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
22 changes: 0 additions & 22 deletions tests/unit/Repository/OptionRepositoryTest.php

This file was deleted.

80 changes: 38 additions & 42 deletions tests/unit/Repository/RepositoryAbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
class MockRepository extends RepositoryAbstract {}

33 changes: 6 additions & 27 deletions tests/unit/Storage/ORM/RespectRelationalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

use WilliamEspindola\Field\Storage\ORM\RespectRelational;

use Respect\Relational\Mapper;

class RespectRelationalTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
Expand Down Expand Up @@ -57,6 +55,9 @@ public function testSetRepositoryWithInvalidValueShouldThrowAndException()
$instance->setRepository('');
}

/**
* @depends testSetRepositoryWithInvalidValueShouldThrowAndException
*/
public function testSetRepositoryWithAValidNamespaceShouldWork()
{
$instance = new RespectRelational($this->mapper);
Expand All @@ -71,6 +72,9 @@ public function testSetRepositoryWithAValidNamespaceShouldWork()
);
}

/**
* @depends testSetRepositoryWithAValidNamespaceShouldWork
*/
public function testSetRepositoryWithValidStringShouldWork()
{
$instance = new RespectRelational($this->mapper);
Expand All @@ -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'
);
}
}

0 comments on commit f1205db

Please sign in to comment.