Skip to content

Commit f1205db

Browse files
Merge pull request #23 from williamespindola/fix/write-tests
Pass tests
2 parents a977ed0 + 160b502 commit f1205db

File tree

5 files changed

+65
-102
lines changed

5 files changed

+65
-102
lines changed

src/Repository/RepositoryAbstract.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ abstract class RepositoryAbstract
1919
*/
2020
public function setStorage(StorageORMInterface $storage, $repository)
2121
{
22-
$this->storage = $storage->setRepository($repository);
22+
$storage->setRepository($repository);
23+
24+
$this->storage = $storage->getRepository();
2325

2426
return $this;
2527
}

tests/unit/Repository/FiendRepositoryTest.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,27 @@
44

55
class FieldRepositoryTest extends PHPUnit_Framework_TestCase
66
{
7-
protected function setUp()
7+
public function testFindAllShouldReturnAnArrayObject()
88
{
9-
$this->storage = $this->getMock('\WilliamEspindola\Field\Storage\ORM\StorageORMInterface');
9+
$storageAbstract = $this->getMock('sdtClass', ['findAll']);
1010

11-
$this->storage->expects($this->any())
12-
->method('findAll')
13-
->will($this->returnValue([]));
11+
$storageAbstract->expects($this->any())
12+
->method('findAll')
13+
->will($this->returnValue((object)([])));
1414

15-
$this->repository = new FieldRepository($this->storage);
16-
}
15+
$storage = $this->getMock('\WilliamEspindola\Field\Storage\ORM\StorageORMInterface');
1716

18-
public function testFindAllShouldReturnAnArrayObject()
19-
{
20-
$this->assertInstanceOf('ArrayObject', $this->repository->findAll());
17+
$storage->expects($this->any())
18+
->method('__get')
19+
->with($this->equalTo('storage'))
20+
->will($this->returnValue($storageAbstract));
21+
22+
$storage->expects($this->any())
23+
->method('getRepository')
24+
->will($this->returnValue($storageAbstract));
25+
26+
$repository = new FieldRepository($storage);
27+
28+
$this->assertInstanceOf('stdClass', $repository->findAll());
2129
}
2230
}

tests/unit/Repository/OptionRepositoryTest.php

Lines changed: 0 additions & 22 deletions
This file was deleted.

tests/unit/Repository/RepositoryAbstractTest.php

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,61 +4,57 @@
44

55
class RepositoryAbstractTest extends PHPUnit_Framework_TestCase
66
{
7-
protected function setUp()
7+
public function testSetAndGetStorageWithValidDataShouldWork()
88
{
9-
$this->entity = $this->getMock('\WilliamEspindola\Field\Entity\EntityInterface');
10-
$this->storage = $this->getMock('\WilliamEspindola\Field\Storage\ORM\StorageORMInterface');
9+
$storageAbstract = $this->getMock('sdtClass', ['findAll']);
1110

12-
$this->storage->expects($this->any())
13-
->method('findAll')
14-
->will($this->returnValue([]));
11+
$storage = $this->getMock('\WilliamEspindola\Field\Storage\ORM\StorageORMInterface');
1512

16-
$this->repository = new MockRepository('tableName', $this->entity, $this->storage);
17-
}
13+
$storage->expects($this->any())
14+
->method('__get')
15+
->with($this->equalTo('storage'))
16+
->will($this->returnValue($storageAbstract));
1817

19-
/**
20-
* @expectedException InvalidArgumentException
21-
* @expectedExceptionMessage Invalid table name MockRepository.
22-
*/
23-
public function testSetTableNameWidhInvalidDataShuldThrownAndException()
24-
{
25-
$this->repository->setTableName(1);
26-
}
18+
$storage->expects($this->any())
19+
->method('getRepository')
20+
->will($this->returnValue($storageAbstract));
2721

28-
public function testeSetTableNameWithValidDataShouldWork()
29-
{
30-
$this->repository->setTableName('ola');
31-
$this->assertEquals('ola', PHPUnit_Framework_Assert::readAttribute($this->repository, 'tableName'));
32-
}
22+
$repository = new MockRepository($storage, 'sdtClass');
3323

34-
public function testeGetTableNameWithValidDataShouldWork()
35-
{
36-
$this->repository->setTableName('ola');
37-
$this->assertEquals('ola', $this->repository->getTableName());
38-
}
24+
$repository->setStorage($storage, 'sdtClass');
3925

40-
public function testSetAndGetEntityWithValidDataShouldWork()
41-
{
42-
$this->repository->setEntity($this->entity);
4326
$this->assertInstanceOf(
44-
'\WilliamEspindola\Field\Entity\EntityInterface',
45-
$this->repository->getEntity()
46-
);
47-
}
48-
49-
public function testSetAndGetStorageWithValidDataShouldWork()
50-
{
51-
$this->repository->setStorage($this->storage);
52-
$this->assertInstanceOf(
53-
'\WilliamEspindola\Field\Storage\ORM\StorageORMInterface',
54-
$this->repository->getStorage()
27+
'sdtClass',
28+
$repository->getStorage()
5529
);
5630
}
5731

5832
public function testFindAllShouldReturnAnArrayObject()
5933
{
60-
$this->assertInstanceOf('ArrayObject', $this->repository->findAll());
34+
$storageAbstract = $this->getMock('sdtClass', ['findAll']);
35+
36+
$storageAbstract->expects($this->any())
37+
->method('findAll')
38+
->will($this->returnValue((object)([])));
39+
40+
$storage = $this->getMock('\WilliamEspindola\Field\Storage\ORM\StorageORMInterface');
41+
42+
$storage->expects($this->any())
43+
->method('__get')
44+
->with($this->equalTo('storage'))
45+
->will($this->returnValue($storageAbstract));
46+
47+
$storage->expects($this->any())
48+
->method('getRepository')
49+
->will($this->returnValue($storageAbstract));
50+
51+
$repository = new MockRepository($storage, 'sdtClass');
52+
53+
$repository->setStorage($storage, 'sdtClass');
54+
55+
$this->assertInstanceOf('stdClass', $repository->findAll());
6156
}
6257
}
6358

64-
class MockRepository extends RepositoryAbstract {}
59+
class MockRepository extends RepositoryAbstract {}
60+

tests/unit/Storage/ORM/RespectRelationalTest.php

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
use WilliamEspindola\Field\Storage\ORM\RespectRelational;
44

5-
use Respect\Relational\Mapper;
6-
75
class RespectRelationalTest extends \PHPUnit_Framework_TestCase
86
{
97
protected function setUp()
@@ -57,6 +55,9 @@ public function testSetRepositoryWithInvalidValueShouldThrowAndException()
5755
$instance->setRepository('');
5856
}
5957

58+
/**
59+
* @depends testSetRepositoryWithInvalidValueShouldThrowAndException
60+
*/
6061
public function testSetRepositoryWithAValidNamespaceShouldWork()
6162
{
6263
$instance = new RespectRelational($this->mapper);
@@ -71,6 +72,9 @@ public function testSetRepositoryWithAValidNamespaceShouldWork()
7172
);
7273
}
7374

75+
/**
76+
* @depends testSetRepositoryWithAValidNamespaceShouldWork
77+
*/
7478
public function testSetRepositoryWithValidStringShouldWork()
7579
{
7680
$instance = new RespectRelational($this->mapper);
@@ -83,29 +87,4 @@ public function testSetRepositoryWithValidStringShouldWork()
8387
'The attribute repository is not instance of the string table name: mytable'
8488
);
8589
}
86-
87-
public function testGetRepositoryShouldReturnMockedInstance()
88-
{
89-
$conn = $this->getMock(
90-
'PDO',
91-
array('lastInsertId'),
92-
array('sqlite::memory:')
93-
);
94-
$conn->exec('CREATE TABLE mytable(id INTEGER PRIMARY KEY)');
95-
$conn->expects($this->any())
96-
->method('lastInsertId')
97-
->will($this->throwException(new \PDOException));
98-
99-
100-
$mapper = new Mapper($conn);
101-
102-
$instance = new RespectRelational($mapper);
103-
$instance->setRepository('mytable');
104-
105-
$this->assertEquals(
106-
'mytable',
107-
$instance->getRepository(),
108-
'is not mytable'
109-
);
110-
}
11190
}

0 commit comments

Comments
 (0)