Skip to content
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

Repository is now instanciated on the fly #440

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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: 5 additions & 14 deletions Document/AuthCodeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace FOS\OAuthServerBundle\Document;

use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\DocumentRepository;
use FOS\OAuthServerBundle\Model\AuthCodeInterface;
use FOS\OAuthServerBundle\Model\AuthCodeManager as BaseAuthCodeManager;

Expand All @@ -25,24 +24,14 @@ class AuthCodeManager extends BaseAuthCodeManager
*/
protected $dm;

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

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

public function __construct(DocumentManager $dm, $class)
{
// NOTE: bug in Doctrine, hinting DocumentRepository|ObjectRepository when only DocumentRepository is expected
/** @var DocumentRepository $repository */
$repository = $dm->getRepository($class);

$this->dm = $dm;
$this->repository = $repository;
$this->class = $class;
}

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

/**
Expand All @@ -85,8 +74,10 @@ public function deleteAuthCode(AuthCodeInterface $authCode)
*/
public function deleteExpired()
{
$result = $this
->repository
/** @var \Doctrine\ODM\MongoDB\DocumentRepository $repository */
$repository = $this->dm->getRepository($this->class);

$result = $repository
->createQueryBuilder()
->remove()
->field('expiresAt')->lt(time())
Expand Down
13 changes: 1 addition & 12 deletions Document/ClientManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace FOS\OAuthServerBundle\Document;

use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\DocumentRepository;
use FOS\OAuthServerBundle\Model\ClientInterface;
use FOS\OAuthServerBundle\Model\ClientManager as BaseClientManager;

Expand All @@ -25,24 +24,14 @@ class ClientManager extends BaseClientManager
*/
protected $dm;

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

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

public function __construct(DocumentManager $dm, $class)
{
// NOTE: bug in Doctrine, hinting DocumentRepository|ObjectRepository when only DocumentRepository is expected
/** @var DocumentRepository $repository */
$repository = $dm->getRepository($class);

$this->dm = $dm;
$this->repository = $repository;
$this->class = $class;
}

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

/**
Expand Down
18 changes: 5 additions & 13 deletions Document/TokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,14 @@ class TokenManager extends BaseTokenManager
*/
protected $dm;

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

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

public function __construct(DocumentManager $dm, $class)
{
// NOTE: bug in Doctrine, hinting DocumentRepository|ObjectRepository when only DocumentRepository is expected
/** @var DocumentRepository $repository */
$repository = $dm->getRepository($class);

$this->dm = $dm;
$this->repository = $repository;
$this->class = $class;
}

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

/**
Expand All @@ -85,8 +75,10 @@ public function deleteToken(TokenInterface $token)
*/
public function deleteExpired()
{
$result = $this
->repository
// NOTE: bug in Doctrine, hinting DocumentRepository|ObjectRepository when only DocumentRepository is expected
/** @var DocumentRepository $repository */
$repository = $this->dm->getRepository($this->class);
$result = $repository
->createQueryBuilder()
->remove()
->field('expiresAt')->lt(time())
Expand Down
13 changes: 1 addition & 12 deletions Entity/ClientManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -25,24 +24,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 +48,7 @@ public function getClass()
*/
public function findClientBy(array $criteria)
{
return $this->repository->findOneBy($criteria);
return $this->em->getRepository($this->class)->findOneBy($criteria);
}

/**
Expand Down
17 changes: 5 additions & 12 deletions Entity/TokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

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

/**
Expand All @@ -85,7 +75,10 @@ public function deleteToken(TokenInterface $token)
*/
public function deleteExpired()
{
$qb = $this->repository->createQueryBuilder('t');
// NOTE: bug in Doctrine, hinting EntityRepository|ObjectRepository when only EntityRepository is expected
/** @var EntityRepository $repository */
$repository = $this->em->getRepository($this->class);
$qb = $repository->createQueryBuilder('t');
$qb
->delete()
->where('t.expiresAt < ?1')
Expand Down
21 changes: 14 additions & 7 deletions Tests/Document/AuthCodeManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,6 @@ public function setUp()
;
$this->className = 'TestClassName'.\random_bytes(5);

$this->documentManager
->expects($this->once())
->method('getRepository')
->with($this->className)
->willReturn($this->repository)
;

$this->instance = new AuthCodeManager($this->documentManager, $this->className);

parent::setUp();
Expand All @@ -95,6 +88,13 @@ public function testFindAuthCodeBy()
\random_bytes(10),
];

$this->documentManager
->expects($this->once())
->method('getRepository')
->with($this->className)
->willReturn($this->repository)
;

$this->repository
->expects($this->once())
->method('findOneBy')
Expand Down Expand Up @@ -155,6 +155,13 @@ public function testDeleteAuthCode()

public function testDeleteExpired()
{
$this->documentManager
->expects($this->once())
->method('getRepository')
->with($this->className)
->willReturn($this->repository)
;

$queryBuilder = $this->getMockBuilder(Builder::class)
->disableOriginalConstructor()
->getMock()
Expand Down
15 changes: 7 additions & 8 deletions Tests/Document/ClientManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,6 @@ public function setUp()
;
$this->className = 'RandomClassName'.\random_bytes(5);

$this->documentManager
->expects($this->once())
->method('getRepository')
->with($this->className)
->willReturn($this->repository)
;

$this->instance = new ClientManager($this->documentManager, $this->className);

parent::setUp();
Expand All @@ -76,7 +69,6 @@ public function setUp()
public function testConstructWillSetParameters()
{
$this->assertAttributeSame($this->documentManager, 'dm', $this->instance);
$this->assertAttributeSame($this->repository, 'repository', $this->instance);
$this->assertAttributeSame($this->className, 'class', $this->instance);
}

Expand All @@ -92,6 +84,13 @@ public function testFindClientBy()
\random_bytes(5),
];

$this->documentManager
->expects($this->once())
->method('getRepository')
->with($this->className)
->willReturn($this->repository)
;

$this->repository
->expects($this->once())
->method('findOneBy')
Expand Down
21 changes: 14 additions & 7 deletions Tests/Document/TokenManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,6 @@ public function setUp()
->getMock()
;

$this->documentManager
->expects($this->once())
->method('getRepository')
->with($this->className)
->willReturn($this->repository)
;

$this->instance = new TokenManager($this->documentManager, $this->className);
}

Expand All @@ -80,6 +73,13 @@ public function testFindTokenByToken()
$randomToken = \random_bytes(5);
$randomResult = \random_bytes(5);

$this->documentManager
->expects($this->once())
->method('getRepository')
->with($this->className)
->willReturn($this->repository)
;

$this->repository
->expects($this->once())
->method('findOneBy')
Expand Down Expand Up @@ -145,6 +145,13 @@ public function testDeleteToken()

public function testDeleteExpired()
{
$this->documentManager
->expects($this->once())
->method('getRepository')
->with($this->className)
->willReturn($this->repository)
;

$queryBuilder = $this->getMockBuilder(Builder::class)
->disableOriginalConstructor()
->getMock()
Expand Down
15 changes: 7 additions & 8 deletions Tests/Entity/ClientManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
}

Expand All @@ -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')
Expand Down
Loading