From 24c25957160ced5ff376ca3baa3f5195f7583fa2 Mon Sep 17 00:00:00 2001 From: Guilhem Niot Date: Fri, 14 Sep 2018 16:44:58 +0200 Subject: [PATCH] Update Client Manager --- Entity/ClientManager.php | 19 ++++++++----------- Entity/TokenManager.php | 8 +------- Tests/Entity/ClientManagerTest.php | 2 -- Tests/Entity/TokenManagerTest.php | 25 ------------------------- 4 files changed, 9 insertions(+), 45 deletions(-) diff --git a/Entity/ClientManager.php b/Entity/ClientManager.php index 346c9732..615b7a82 100644 --- a/Entity/ClientManager.php +++ b/Entity/ClientManager.php @@ -25,11 +25,6 @@ class ClientManager extends BaseClientManager */ protected $em; - /** - * @var EntityRepository - */ - protected $repository; - /** * @var string */ @@ -37,12 +32,7 @@ class ClientManager extends BaseClientManager public function __construct(EntityManagerInterface $em, $class) { - // NOTE: bug in Doctrine, hinting EntityRepository|ObjectRepository when only EntityRepository is expected - /** @var EntityRepository $repository */ - $repository = $em->getRepository($class); - $this->em = $em; - $this->repository = $repository; $this->class = $class; } @@ -59,7 +49,7 @@ public function getClass() */ public function findClientBy(array $criteria) { - return $this->repository->findOneBy($criteria); + return $this->getRepository()->findOneBy($criteria); } /** @@ -79,4 +69,11 @@ public function deleteClient(ClientInterface $client) $this->em->remove($client); $this->em->flush(); } + + private function getRepository(): EntityRepository + { + $repository = $this->em->getRepository($this->class); + + return $repository; + } } diff --git a/Entity/TokenManager.php b/Entity/TokenManager.php index a5487d17..e4d46ad0 100644 --- a/Entity/TokenManager.php +++ b/Entity/TokenManager.php @@ -85,15 +85,9 @@ public function deleteExpired() return $qb->getQuery()->execute(); } - /** - * @return EntityRepository - */ - protected function getRepository(): EntityRepository + private function getRepository(): EntityRepository { $repository = $this->em->getRepository($this->class); - if (!($repository instanceof EntityRepository)) { - throw new \RuntimeException('EntityRepository needed'); - } return $repository; } diff --git a/Tests/Entity/ClientManagerTest.php b/Tests/Entity/ClientManagerTest.php index d575d320..86f342ba 100644 --- a/Tests/Entity/ClientManagerTest.php +++ b/Tests/Entity/ClientManagerTest.php @@ -58,7 +58,6 @@ public function setUp() $this->className = 'RandomClassName'.\random_bytes(5); $this->entityManager - ->expects($this->once()) ->method('getRepository') ->with($this->className) ->willReturn($this->repository) @@ -72,7 +71,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); } diff --git a/Tests/Entity/TokenManagerTest.php b/Tests/Entity/TokenManagerTest.php index 05891dd6..f30b0b2e 100644 --- a/Tests/Entity/TokenManagerTest.php +++ b/Tests/Entity/TokenManagerTest.php @@ -227,29 +227,4 @@ public function testDeleteExpired() $this->assertSame($randomResult, $this->instance->deleteExpired()); } - - public function testExceptionWithObjectRepository() - { - $this->repository = $this->getMockBuilder(ObjectRepository::class) - ->disableOriginalConstructor() - ->getMock() - ; - - $this->entityManager = $this->getMockBuilder(EntityManager::class) - ->disableOriginalConstructor() - ->getMock() - ; - - $this->entityManager - ->expects($this->once()) - ->method('getRepository') - ->with($this->className) - ->willReturn($this->repository) - ; - - $this->instance = new TokenManager($this->entityManager, $this->className); - - $this->expectException(\RuntimeException::class); - $this->instance->findTokenBy([]); - } }