mercredi 26 avril 2017

How to inject mocked objects from Test to Controller

Currently I am sending the mocks to the controller through the request method in the returnURL function, but I'd like to know if there is a better way to use/send this mocks in the controller, because it seems like this way is a bad practise.

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);
    }
}

Aucun commentaire:

Enregistrer un commentaire