samedi 6 octobre 2018

How do we mock a symfony rest api controller with a Repository in it's parameters himself having ManagerRegistry in it's constructor

I am trying to make functional testing for my API controller in symfony 3.4 but I having problems since I have my repostitory like this.

class AdminRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Admin::class);
    }
}

As a matter of fact, since i made these changes, i decided to inject my repos inside my controller like this.

public function getMyReservationAction(Request $request, AdminRepository $adminRepos, ReservationRepository $reservationRepos)
{
    $values = json_decode($request->getContent(), true);

    return $this->verifValid(
            Header::getToken($request)
            , $values['reservation_id']
            , $adminRepos
            , $reservationRepos
    );
}

The problem is that I pass my AdminRepository in a verifValid method using $adminRepos it to call database. and I would like test my controller so decided to mock repository like this.

public function testGetMyReservationAction() {

    $token = $this->createUser(true,'ROLE_USER');


    $admin = new Admin();
    $admin->setEmail('test@test.com');

    // Now, mock the repository so it returns the mock of the admin
    $adminRepos = $this->getMockBuilder(AdminRepository::class)
        ->disableOriginalConstructor()
        ->getMock();
    $adminRepos->expects($this->any())
        ->method('findBy')
        ->willReturn(Array($admin));

    $entityManager = $this
        ->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
        ->disableOriginalConstructor()
        ->setMethods(['getRepository', 'clear'])
        ->getMock();

    $entityManager
        ->expects($this->once())
        ->method('getRepository')
        ->with(Admin::class)
        ->will($this->returnValue($adminRepos));

    $this->client->getContainer()->set('doctrine.orm.default_entity_manager', $entityManager);

    // Set the client
    $this->client->getContainer()->set('doctrine', $entityManager);

    //Initialisation des paramètres
    $params = [
        'token' => $token
        , 'reservation_id' => $this->getReservation()->getId()
    ];


    //On appelle la route
    $this->client->request('POST', '/auth/reservation/getone'
        , array() , array()
        , array('HTTP_AUTHORIZATION' => 'Bearer '. $token)
        , json_encode($params)
    );

    //On vérifie que le code de retour est bien 200
    $this->assertEquals(200 , $this->client->getResponse()->getStatusCode());
}

but it is telling me I have to instantiate my ManagerRegistry..

  PHP Fatal error:  Class Mock_ManagerRegistry_90efed4b contains 11 abstract methods and must therefore be declared abstract or implement the remaining
 methods (Doctrine\Common\Persistence\ManagerRegistry::getDefaultManagerName, Doctrine\Common\Persistence\ManagerRegistry::getManager, Doctrine\Common
\Persistence\ManagerRegistry::getManagers, ...) in phar:///usr/local
/bin/phpunit/phpunit-mock-objects/Generator.php(263) : eval()'d code on line 1

I do not think such thing has to be done so I think I am doing something wrong but I don't know where.

Can you help me please ? Thank you in advance.

Aucun commentaire:

Enregistrer un commentaire