Skip to content

Commit aaec300

Browse files
author
Rafal Ksiazek
committed
Testing
1 parent 3c5a18c commit aaec300

File tree

8 files changed

+325
-42
lines changed

8 files changed

+325
-42
lines changed

module/Application/config/service.config.php

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
'factories' => [
1616
'Application\Logger' => \Application\Library\Logger\Factory\LoggerFactory::class,
1717
\Application\Library\QueryFilter\QueryFilter::class => \Application\Library\QueryFilter\SLFactory\QueryFilterSLFactory::class,
18+
\Application\Library\QueryFilter\Command\Repository\CommandCollection::class => \Application\Library\QueryFilter\Command\Repository\SLFactory\CommandCollectionSLFactory::class,
1819
],
1920
]
2021
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Application\Library\QueryFilter\Command\Repository\SLFactory;
4+
5+
use Application\Library\QueryFilter\Command;
6+
use Zend\ServiceManager\FactoryInterface;
7+
use Zend\ServiceManager\ServiceLocatorInterface;
8+
9+
class CommandCollectionSLFactory implements FactoryInterface
10+
{
11+
/**
12+
* Create service
13+
*
14+
* @param ServiceLocatorInterface $serviceLocator
15+
*
16+
* @return Command\Repository\CommandCollection
17+
*/
18+
public function createService(ServiceLocatorInterface $serviceLocator)
19+
{
20+
return new Command\Repository\CommandCollection(
21+
[
22+
// condition
23+
new Command\Repository\BetweenCommand(),
24+
new Command\Repository\StartsWithCommand(),
25+
new Command\Repository\EndsWithCommand(),
26+
new Command\Repository\MinCommand(),
27+
new Command\Repository\MaxCommand(),
28+
new Command\Repository\EqualCommand(),
29+
new Command\Repository\InArrayCommand(),
30+
// special
31+
new Command\Repository\FieldsCommand(),
32+
new Command\Repository\SortCommand(),
33+
new Command\Repository\LimitCommand(),
34+
new Command\Repository\OffsetCommand(),
35+
]
36+
);
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace AuthTest\Service\Storage;
4+
5+
use Auth\Service\Storage\DbStorage;
6+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
7+
use User\Entity\UserEntity;
8+
use User\Repository\UserRepositoryInterface;
9+
use UserTest\Entity\Provider\UserEntityProvider;
10+
use Zend\Authentication\Storage\Session;
11+
12+
class DbStorageTest extends \PHPUnit_Framework_TestCase
13+
{
14+
/**
15+
* @var DbStorage
16+
*/
17+
private $testedObject;
18+
19+
/**
20+
* @var MockObject
21+
*/
22+
private $userRepositoryMock;
23+
24+
public function setUp()
25+
{
26+
$this->userRepositoryMock = $this->getMockBuilder(UserRepositoryInterface::class)
27+
->disableOriginalConstructor()
28+
->getMock();
29+
30+
$this->testedObject = new DbStorage($this->userRepositoryMock);
31+
}
32+
33+
public function testIsEmpty()
34+
{
35+
$result = $this->testedObject->isEmpty();
36+
37+
$this->assertTrue($result);
38+
}
39+
40+
public function testWriteAndReadAndClear()
41+
{
42+
$userEntity = UserEntityProvider::createEntityWithRandomData();
43+
44+
$this->testedObject->write($userEntity->getId());
45+
46+
$this->userRepositoryMock->expects($this->once())
47+
->method('find')
48+
->with($userEntity->getId())
49+
->will($this->returnValue($userEntity));
50+
51+
$result = $this->testedObject->read();
52+
53+
$this->assertSame($userEntity, $result);
54+
55+
$this->testedObject->clear();
56+
57+
$result = $this->testedObject->read();
58+
59+
$this->assertSame(null, $result);
60+
}
61+
62+
public function testSetGetStorage()
63+
{
64+
$identity = mt_rand(1, 100);
65+
66+
$sessionStorage = new Session();
67+
$sessionStorage->write($identity);
68+
69+
$this->testedObject->setStorage($sessionStorage);
70+
71+
$result = $this->testedObject->getStorage();
72+
73+
$this->assertInstanceOf(Session::class, $result);
74+
$this->assertSame($identity, $result->read());
75+
}
76+
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace AuthTest\Service\Storage\SLFactory;
4+
5+
use Auth\Service\Storage\DbStorage;
6+
use Auth\Service\Storage\SLFactory\DbStorageSLFactory;
7+
use Test\Bootstrap;
8+
9+
class DbStorageSLFactoryTest extends \PHPUnit_Framework_TestCase
10+
{
11+
/**
12+
* @var DbStorageSLFactory
13+
*/
14+
private $testedObj;
15+
16+
public function setUp()
17+
{
18+
$this->testedObj = new DbStorageSLFactory();
19+
}
20+
21+
public function testCreateService()
22+
{
23+
$result = $this->testedObj->createService(Bootstrap::getServiceManager());
24+
25+
$this->assertInstanceOf(DbStorage::class, $result);
26+
}
27+
}

module/Library/src/Library/Repository/BookRepository.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,22 @@ public function findByQueryFilter(
8686
QueryFilter $queryFilter,
8787
CommandCollection $criteriaCommands,
8888
$hydrationMode = Query::HYDRATE_OBJECT
89+
) {
90+
$qb = $this->provideQueryBuilderToFindByQueryFilter($queryFilter, $criteriaCommands);
91+
92+
return $qb->getQuery()->getResult($hydrationMode);
93+
}
94+
95+
/**
96+
* @param QueryFilter $queryFilter
97+
* @param CommandCollection $criteriaCommands
98+
*
99+
* @return \Doctrine\ORM\QueryBuilder
100+
* @throws \Application\Library\QueryFilter\Exception\UnsupportedTypeException
101+
*/
102+
private function provideQueryBuilderToFindByQueryFilter(
103+
QueryFilter $queryFilter,
104+
CommandCollection $criteriaCommands
89105
) {
90106
$i = 0;
91107
$alias = 'b';
@@ -109,7 +125,7 @@ public function findByQueryFilter(
109125
));
110126
}
111127

112-
return $qb->getQuery()->getResult($hydrationMode);
128+
return $qb;
113129
}
114130

115131
/**

module/Library/src/Library/Service/Book/Factory/FilterResultsServiceFactory.php

+2-19
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Library\Service\Book\Factory;
44

5-
use Application\Library\QueryFilter\Command;
5+
use Application\Library\QueryFilter\Command\Repository;
66
use Library\Repository\BookRepository;
77
use Library\Service\Book\FilterResultsService;
88
use Zend\ServiceManager\FactoryInterface;
@@ -24,24 +24,7 @@ public function createService(ServiceLocatorInterface $serviceLocator)
2424
* @var $bookRepository BookRepository
2525
*/
2626
$bookRepository = $serviceLocator->get(BookRepository::class);
27-
28-
$commandCollection = new Command\Repository\CommandCollection(
29-
[
30-
// condition
31-
new Command\Repository\BetweenCommand(),
32-
new Command\Repository\StartsWithCommand(),
33-
new Command\Repository\EndsWithCommand(),
34-
new Command\Repository\MinCommand(),
35-
new Command\Repository\MaxCommand(),
36-
new Command\Repository\EqualCommand(),
37-
new Command\Repository\InArrayCommand(),
38-
// special
39-
new Command\Repository\FieldsCommand(),
40-
new Command\Repository\SortCommand(),
41-
new Command\Repository\LimitCommand(),
42-
new Command\Repository\OffsetCommand(),
43-
]
44-
);
27+
$commandCollection = $serviceLocator->get(Repository\CommandCollection::class);
4528

4629
return new FilterResultsService($bookRepository, $commandCollection);
4730
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
namespace LibraryTest\Repository;
4+
5+
use Doctrine\ORM\AbstractQuery;
6+
use Doctrine\ORM\EntityManager;
7+
use Doctrine\ORM\Mapping\ClassMetadata;
8+
use Doctrine\ORM\Query;
9+
use Doctrine\ORM\QueryBuilder;
10+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
11+
12+
abstract class AbstractRepositoryTestCase extends \PHPUnit_Framework_TestCase
13+
{
14+
/**
15+
* @var MockObject
16+
*/
17+
protected $entityManagerMock;
18+
19+
/**
20+
* @var MockObject
21+
*/
22+
protected $classMetadataMock;
23+
24+
/**
25+
* @var MockObject
26+
*/
27+
protected $queryBuilderMock;
28+
29+
/**
30+
* @var MockObject
31+
*/
32+
protected $queryMock;
33+
34+
public function setUp()
35+
{
36+
$this->entityManagerMock = $this->getMockBuilder(EntityManager::class)
37+
->disableOriginalConstructor()
38+
->setMethods(['persist', 'flush', 'remove', 'getClassMetadata', 'createQueryBuilder'])
39+
->getMock();
40+
41+
$this->classMetadataMock = $this->getMockBuilder(ClassMetadata::class)
42+
->disableOriginalConstructor()
43+
->getMock();
44+
45+
$this->queryBuilderMock = $this->getMockBuilder(QueryBuilder::class)
46+
->setMethods(['__construct', 'getQuery'])
47+
->setConstructorArgs([$this->entityManagerMock])
48+
->getMock();
49+
50+
$this->queryMock = $this->getMockBuilder(AbstractQuery::class)
51+
->setMethods(['getResult'])
52+
->disableOriginalConstructor()
53+
->getMockForAbstractClass();
54+
55+
$this->queryBuilderMock->expects($this->any())
56+
->method('getQuery')
57+
->will($this->returnValue($this->queryMock));
58+
59+
$this->entityManagerMock->expects($this->any())
60+
->method('createQueryBuilder')
61+
->will($this->returnValue($this->queryBuilderMock));
62+
63+
$this->entityManagerMock->expects($this->any())
64+
->method('getClassMetadata')
65+
->will($this->returnValue($this->classMetadataMock));
66+
}
67+
68+
/**
69+
* @param QueryBuilder $qb
70+
*
71+
* @return string
72+
*/
73+
public function getRawSQLFromORMQueryBuilder(QueryBuilder $qb)
74+
{
75+
$sql = $qb->getDQL();
76+
$parameters = $qb->getParameters();
77+
78+
/** @var Query\Parameter $param */
79+
foreach ($parameters as $param) {
80+
if (is_array($param->getValue()) && !empty($param->getValue())) {
81+
$sql = str_replace(':' . $param->getName(), "'" . implode("', '", $param->getValue()) . "'", $sql);
82+
} else {
83+
$sql = str_replace(':' . $param->getName(), "'" . $param->getValue() . "'", $sql);
84+
}
85+
}
86+
87+
return $sql;
88+
}
89+
90+
}

0 commit comments

Comments
 (0)