I create a container in service.php

$container->setDefinition('repository.access_token', new Definition(\Ftob\OauthServerApp\Repositories\AccessTokenRepository::class)) ->setFactory([new Reference('doctrine'), 'getRepository']) ->setArguments([\Ftob\OauthServerApp\Entity\AccessToken::class]);

Trying to call him in the test -

 class AccessTokenRepositoryTest extends KernelTestCase { protected $repository; public function setUp() { $this->bootKernel(); $this->repository = self::$kernel->getContainer()->get('repository.access_token'); } public function testDi() { $this->assertInstanceOf( AccessTokenRepository::class, $this->repository); } } 

I get an error (fail) -

 AccessTokenRepositoryTest::testDi Failed asserting that Doctrine\ORM\EntityRepository Object (...) is an instance of class "Ftob\OauthServerApp\Repositories\AccessTokenRepository". /var/www/tests/Repositories/AccessTokenRepositoryTest.php:23 FAILURES! Tests: 1, Assertions: 1, Failures: 1. 

Actually, the question is ... Why is Doctrine\ORM\EntityRepository and not AccessTokenRepository ?

Thank you in advance!

    1 answer 1

    And in an application (not in tests), if you ask $entityManager->getRepository(\Ftob\OauthServerApp\Entity\AccessToken::class) , what does it return? Most likely the problem is not in the container and not in the instructions for creating the service.

    Doctrine does not see AccessTokenRepository as a custom repository for AccessToken and returns the default EntityRepository .

    How and when do you AccessTokenRepository Doctrine to use the AccessTokenRepository as a repository for your AccessToken entities?

    Do you have something like this in the class declaration? Announcement:

     /** * @ORM\Entity(repositoryClass="Ftob\OauthServerApp\Repositories\AccessTokenRepository") */ class AccessToken { //... } 

    ( or do you specify metadata in YAML / XML? In general, it does not matter which particular metadata format you chose. It is important that at the time of accessing EntityManager::getRepository() they can be read ).

    If, yes, and in the working application, calling $entityManager->getRepository(\Ftob\OauthServerApp\Entity\AccessToken::class) returns your custom repository, it means that you depend on the characteristics of the test environment (I don’t really imagine how), then see that you are interrupted in a test environment. Check through X-Debug how the collection of class metadata differs in the application and in the tests.

    • Added `$ this-> assertInstanceOf (AccessTokenRepository :: class, $ em-> getRepository (AccessToken :: class));` `The test passes, I thought, I cleaned the cache in redis - everything worked. The problem was in the test environment. - Nikita Volkov