From 25b47fa90329612286a443377d9d6216f1a296ad Mon Sep 17 00:00:00 2001 From: Arnaud Bienvenu Date: Fri, 18 Nov 2016 17:06:33 +0100 Subject: [PATCH] Repository for ClientManager and TokenManager is now instanciated on the fly, not in the constructor anymore This avoids a database connection to be established for every request Fixes issue https://github.com/FriendsOfSymfony/FOSOAuthServerBundle/issues/422 --- Entity/ClientManager.php | 13 +------------ Entity/TokenManager.php | 15 ++------------- Tests/Entity/ClientManagerTest.php | 15 +++++++-------- Tests/Entity/TokenManagerTest.php | 22 ++++++++++++++-------- 4 files changed, 24 insertions(+), 41 deletions(-) diff --git a/Entity/ClientManager.php b/Entity/ClientManager.php index 346c9732..b0699530 100644 --- a/Entity/ClientManager.php +++ b/Entity/ClientManager.php @@ -14,7 +14,6 @@ namespace FOS\OAuthServerBundle\Entity; use Doctrine\ORM\EntityManagerInterface; -use Doctrine\ORM\EntityRepository; use FOS\OAuthServerBundle\Model\ClientInterface; use FOS\OAuthServerBundle\Model\ClientManager as BaseClientManager; @@ -25,11 +24,6 @@ class ClientManager extends BaseClientManager */ protected $em; - /** - * @var EntityRepository - */ - protected $repository; - /** * @var string */ @@ -37,12 +31,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 +48,7 @@ public function getClass() */ public function findClientBy(array $criteria) { - return $this->repository->findOneBy($criteria); + return $this->em->getRepository($this->class)->findOneBy($criteria); } /** diff --git a/Entity/TokenManager.php b/Entity/TokenManager.php index 4a87c17f..207aafd6 100644 --- a/Entity/TokenManager.php +++ b/Entity/TokenManager.php @@ -14,7 +14,6 @@ namespace FOS\OAuthServerBundle\Entity; use Doctrine\ORM\EntityManagerInterface; -use Doctrine\ORM\EntityRepository; use FOS\OAuthServerBundle\Model\TokenInterface; use FOS\OAuthServerBundle\Model\TokenManager as BaseTokenManager; @@ -25,11 +24,6 @@ class TokenManager extends BaseTokenManager */ protected $em; - /** - * @var EntityRepository - */ - protected $repository; - /** * @var string */ @@ -37,12 +31,7 @@ class TokenManager extends BaseTokenManager 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 +48,7 @@ public function getClass() */ public function findTokenBy(array $criteria) { - return $this->repository->findOneBy($criteria); + return $this->em->getRepository($this->class)->findOneBy($criteria); } /** @@ -85,7 +74,7 @@ public function deleteToken(TokenInterface $token) */ public function deleteExpired() { - $qb = $this->repository->createQueryBuilder('t'); + $qb = $this->em->getRepository($this->class)->createQueryBuilder('t'); $qb ->delete() ->where('t.expiresAt < ?1') diff --git a/Tests/Entity/ClientManagerTest.php b/Tests/Entity/ClientManagerTest.php index d575d320..cfcd9e68 100644 --- a/Tests/Entity/ClientManagerTest.php +++ b/Tests/Entity/ClientManagerTest.php @@ -57,13 +57,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); parent::setUp(); @@ -72,7 +65,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); } @@ -88,6 +80,13 @@ public function testFindClientBy() ]; $randomResult = \random_bytes(5); + $this->entityManager + ->expects($this->once()) + ->method('getRepository') + ->with($this->className) + ->willReturn($this->repository) + ; + $this->repository ->expects($this->once()) ->method('findOneBy') diff --git a/Tests/Entity/TokenManagerTest.php b/Tests/Entity/TokenManagerTest.php index acdeca16..7bce37e0 100644 --- a/Tests/Entity/TokenManagerTest.php +++ b/Tests/Entity/TokenManagerTest.php @@ -63,20 +63,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); } @@ -112,6 +104,13 @@ public function testFindTokenBy() \random_bytes(5), ]; + $this->entityManager + ->expects($this->once()) + ->method('getRepository') + ->with($this->className) + ->willReturn($this->repository) + ; + $this->repository ->expects($this->once()) ->method('findOneBy') @@ -174,6 +173,13 @@ public function testDeleteExpired() { $randomResult = \random_bytes(10); + $this->entityManager + ->expects($this->once()) + ->method('getRepository') + ->with($this->className) + ->willReturn($this->repository) + ; + $queryBuilder = $this->getMockBuilder(QueryBuilder::class) ->disableOriginalConstructor() ->getMock()