Skip to content

Don't get repository at construction time, it could be not loaded yet! #574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions Entity/ClientManager.php
Original file line number Diff line number Diff line change
@@ -25,24 +25,14 @@ class ClientManager extends BaseClientManager
*/
protected $em;

/**
* @var EntityRepository
*/
protected $repository;

/**
* @var string
*/
protected $class;

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;
}
}
21 changes: 9 additions & 12 deletions Entity/TokenManager.php
Original file line number Diff line number Diff line change
@@ -25,24 +25,14 @@ class TokenManager extends BaseTokenManager
*/
protected $em;

/**
* @var EntityRepository
*/
protected $repository;

/**
* @var string
*/
protected $class;

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 findTokenBy(array $criteria)
{
return $this->repository->findOneBy($criteria);
return $this->getRepository()->findOneBy($criteria);
}

/**
@@ -85,7 +75,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')
@@ -94,4 +84,11 @@ public function deleteExpired()

return $qb->getQuery()->execute();
}

private function getRepository(): EntityRepository
{
$repository = $this->em->getRepository($this->class);

return $repository;
}
}
2 changes: 0 additions & 2 deletions Tests/Entity/ClientManagerTest.php
Original file line number Diff line number Diff line change
@@ -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);
}

8 changes: 3 additions & 5 deletions Tests/Entity/TokenManagerTest.php
Original file line number Diff line number Diff line change
@@ -15,12 +15,12 @@

use Doctrine\ORM\AbstractQuery;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use FOS\OAuthServerBundle\Entity\AccessToken;
use FOS\OAuthServerBundle\Entity\TokenManager;
use FOS\OAuthServerBundle\Model\TokenInterface;
use PHPUnit\Framework\MockObject\MockObject;

/**
* @group time-sensitive
@@ -32,12 +32,12 @@
class TokenManagerTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \PHPUnit_Framework_MockObject_MockObject|EntityManagerInterface
* @var MockObject
*/
protected $entityManager;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|EntityRepository
* @var MockObject
*/
protected $repository;

@@ -64,7 +64,6 @@ public function setUp()
;

$this->entityManager
->expects($this->once())
->method('getRepository')
->with($this->className)
->willReturn($this->repository)
@@ -76,7 +75,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);
}