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