How to pass mock from test to controller in Symfony 3

0

Good, I'm trying to test a controller that uses the find method and the entity manager could not be mocked. I managed to pass the mocks with the request method using the function returnURL. I would like to know if there is any other way to do it. Thank you so much. Next, the test code and the driver class to be tested.

public function setUp()
{
parent::setUp();

$this->client = static::createClient();
$this->peopleManager = $this->getMockBuilder(PeopleManager::class)
    ->setMethods(['createPerson','peopleUpdate', 'peopleDelete', 
    'peopleRead'])
    ->disableOriginalConstructor()
    ->getMock();

 $this->repository = $this->getMockBuilder(EntityRepository::class)
   ->disableOriginalConstructor()
   ->getMock();

$this->em = $this->getMockBuilder(EntityManager::class)
   ->disableOriginalConstructor()
   ->getMock();

$this->client->getContainer()->set('people.manager', $this->peopleManager);
}

public function returnURL($secc)
{
   return $this->client->request('POST', '/api/' . $secc, array('array' => array([
   "name"=>"Juan",
   "surname"=>"Hernandez",
   "secondSurname"=>"Macias",
   "nationality"=>null,
   "birthday"=>null,
   "identityCard"=> "12345678a",
   "identityCardType"=> null
 ]),
    'em' => $this->em,
    'repository' => $this->repository
));
}

public function test_update_person_action()
{
$persona= $this->returnPerson();
$personaSinActualizar = $this->returnPeople('210', 'Anton', 'Antonyan', 'A', '42226114T');
$this->peopleManager->method('peopleUpdate')->will($this->returnValue($persona));

$this->repository->expects($this->exactly(1))->method('find')->will($this->returnValue($personaSinActualizar));
$this->em->expects($this->exactly(1))->method('getRepository')->will($this->returnValue($this->repository));

$this->returnURL('updateperson/210');
$content = json_decode($this->client->getResponse()->getContent());
$testValues = array
(
    '212',
    'Juan',
    'Hernandez',
    'Macias',
    '12345678a'
);
$contador=0;
foreach ($content as $partContent)
{
    $this->assertEquals($testValues[$contador], $partContent);
    $contador++;
}
}




class RestController extends FOSRestController
{
private $repository;
private $em;

public function updatePersonAction($id, Request $request)
{
$this->em = $request->request->get('em');
$this->repository = $request->request->get('repository');

$this->repository = $this->em->getRepository('GeneralBundle:People');
$person= $this->repository->find($id);
if($person)
{
    $data = $request->request->get('array');
    $createdPeople = array();
    $UpdatedPerson = "";
    foreach($data as $content)
    {
        $UpdatedPerson = $this->get('people.manager')->peopleUpdate(
            $person,
            $content['name'],
            $content['surname'],
            $content['secondSurname'],
            $content['nationality'],
            $content['birthday'],
            $content['identityCard'],
            $content['identityCardType']
        );
        array_push($createdPeople, $person);
    }
    $serializedEntity = $this->get('serializer')->serialize($UpdatedPerson, 'json');
    return new Response($serializedEntity);
} else {
    $serializedEntity = $this->get('serializer')->serialize('Doesn\'t exists any person with this id', 'json');
    return new Response($serializedEntity);
}
}
    
asked by Jano CL 26.04.2017 в 15:13
source

1 answer

0

I am not sure that your controller works well outside the test because you are passing through two services in the request that are only built in the test:

$this->em = $request->request->get('em');
$this->repository = $request->request->get('repository');

in a normal request that would not work.

As you set the people.manager you can do:

$this->getContainer()->set('doctrine.orm.entity_manager')

to overwrite with the mock.

The controller should not be built waiting for something that should or should not be in the tests, nor set conditions for the tests to work. Rather, the test should use what is in the controller or class that is being tested. On the other hand it is never too easy to make the classes as easy as possible to facilitate the tests.

    
answered by 05.05.2017 в 21:25