From 64db426e32dcc2acff514bce3dae5031c61dd61f Mon Sep 17 00:00:00 2001 From: Winus Van heumen Date: Thu, 17 May 2018 07:46:34 +0200 Subject: [PATCH 1/3] Added function to 'lazyload' the repository --- Entity/ClientManager.php | 15 +++++++++++++-- Entity/TokenManager.php | 15 +++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/Entity/ClientManager.php b/Entity/ClientManager.php index aef9cef4..d8177d48 100644 --- a/Entity/ClientManager.php +++ b/Entity/ClientManager.php @@ -37,7 +37,6 @@ class ClientManager extends BaseClientManager public function __construct(ObjectManager $em, $class) { $this->em = $em; - $this->repository = $em->getRepository($class); $this->class = $class; } @@ -54,7 +53,7 @@ public function getClass() */ public function findClientBy(array $criteria) { - return $this->repository->findOneBy($criteria); + return $this->getRepository()->findOneBy($criteria); } /** @@ -74,4 +73,16 @@ public function deleteClient(ClientInterface $client) $this->em->remove($client); $this->em->flush(); } + + /** + * @return EntityRepository + */ + private function getRepository() + { + if(is_null($this->repository)) + { + $this->repository = $this->em->getRepository($this->class); + } + return $this->repository; + } } diff --git a/Entity/TokenManager.php b/Entity/TokenManager.php index a17a934a..8bcaa6d6 100644 --- a/Entity/TokenManager.php +++ b/Entity/TokenManager.php @@ -37,7 +37,6 @@ class TokenManager extends BaseTokenManager public function __construct(ObjectManager $em, $class) { $this->em = $em; - $this->repository = $em->getRepository($class); $this->class = $class; } @@ -54,7 +53,7 @@ public function getClass() */ public function findTokenBy(array $criteria) { - return $this->repository->findOneBy($criteria); + return $this->getRepository()->findOneBy($criteria); } /** @@ -88,4 +87,16 @@ public function deleteExpired() return $qb->getQuery()->execute(); } + + /** + * @return EntityRepository + */ + private function getRepository() + { + if(is_null($this->repository)) + { + $this->repository = $this->em->getRepository($this->class); + } + return $this->repository; + } } From a6fb14b93295a4a8895c0448d81cf42b13ea0303 Mon Sep 17 00:00:00 2001 From: Winus Van heumen Date: Thu, 17 May 2018 08:35:46 +0200 Subject: [PATCH 2/3] Missed a repository call --- Entity/TokenManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Entity/TokenManager.php b/Entity/TokenManager.php index 8bcaa6d6..9cdd1b88 100644 --- a/Entity/TokenManager.php +++ b/Entity/TokenManager.php @@ -79,7 +79,7 @@ public function deleteToken(TokenInterface $token) */ public function deleteExpired() { - $qb = $this->repository->createQueryBuilder('t'); + $qb = $this->getRepository()->createQueryBuilder('t'); $qb ->delete() ->where('t.expiresAt < ?1') From 22eed572fd841b9c036b86455b5d94c5d8f84eda Mon Sep 17 00:00:00 2001 From: Winus Van heumen Date: Thu, 17 May 2018 09:21:14 +0200 Subject: [PATCH 3/3] Altered tests --- Tests/Entity/ClientManagerTest.php | 15 ++++++++------- Tests/Entity/TokenManagerTest.php | 24 ++++++++++++++++-------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/Tests/Entity/ClientManagerTest.php b/Tests/Entity/ClientManagerTest.php index 744c5df5..cc8e6eed 100644 --- a/Tests/Entity/ClientManagerTest.php +++ b/Tests/Entity/ClientManagerTest.php @@ -46,12 +46,6 @@ public function setUp() ; $this->className = 'RandomClassName'. \random_bytes(5); - $this->entityManager - ->expects($this->once()) - ->method('getRepository') - ->with($this->className) - ->willReturn($this->repository) - ; $this->instance = new ClientManager($this->entityManager, $this->className); @@ -61,7 +55,6 @@ public function setUp() public function testConstructWillSetParameters() { $this->assertAttributeSame($this->entityManager, 'em', $this->instance); - $this->assertAttributeSame($this->repository, 'repository', $this->instance); $this->assertAttributeSame($this->className, 'class', $this->instance); } @@ -84,6 +77,14 @@ public function testFindClientBy() ->willReturn($randomResult) ; + + $this->entityManager + ->expects($this->once()) + ->method('getRepository') + ->with($this->className) + ->willReturn($this->repository) + ; + $this->assertSame($randomResult, $this->instance->findClientBy($criteria)); } diff --git a/Tests/Entity/TokenManagerTest.php b/Tests/Entity/TokenManagerTest.php index bbbaea5c..b729389d 100644 --- a/Tests/Entity/TokenManagerTest.php +++ b/Tests/Entity/TokenManagerTest.php @@ -60,20 +60,12 @@ public function setUp() ->getMock() ; - $this->entityManager - ->expects($this->once()) - ->method('getRepository') - ->with($this->className) - ->willReturn($this->repository) - ; - $this->instance = new TokenManager($this->entityManager, $this->className); } public function testConstructWillSetParameters() { $this->assertAttributeSame($this->entityManager, 'em', $this->instance); - $this->assertAttributeSame($this->repository, 'repository', $this->instance); $this->assertAttributeSame($this->className, 'class', $this->instance); } @@ -116,6 +108,14 @@ public function testFindTokenBy() ->willReturn($randomResult) ; + + $this->entityManager + ->expects($this->once()) + ->method('getRepository') + ->with($this->className) + ->willReturn($this->repository) + ; + $this->assertSame($randomResult, $this->instance->findTokenBy($criteria)); } @@ -223,6 +223,14 @@ public function testDeleteExpired() ->willReturn($randomResult) ; + + $this->entityManager + ->expects($this->once()) + ->method('getRepository') + ->with($this->className) + ->willReturn($this->repository) + ; + $this->assertSame($randomResult, $this->instance->deleteExpired()); } }