Skip to content

Commit

Permalink
Update Client Manager
Browse files Browse the repository at this point in the history
  • Loading branch information
GuilhemN committed Sep 14, 2018
1 parent b1271e1 commit 24c2595
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 45 deletions.
19 changes: 8 additions & 11 deletions Entity/ClientManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -59,7 +49,7 @@ public function getClass()
*/
public function findClientBy(array $criteria)
{
return $this->repository->findOneBy($criteria);
return $this->getRepository()->findOneBy($criteria);
}

/**
Expand All @@ -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;
}
}
8 changes: 1 addition & 7 deletions Entity/TokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 0 additions & 2 deletions Tests/Entity/ClientManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
}

Expand Down
25 changes: 0 additions & 25 deletions Tests/Entity/TokenManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([]);
}
}

0 comments on commit 24c2595

Please sign in to comment.