vendredi 29 septembre 2017

PhpUnit and Symfony: Mock Service not working

I'm using Symfony 3.3 and PHPUnit 5.7 and I'm trying to mock a service for testing an api controller.

The controller:

class ApiTestManager extends BaseApiController{
    public function getAction(): View
    {
        $response = $this->get('app.business.test_api')->getResponse();
        return $this->view($response);
    }}

The test class:

class ApiTestManagerTest extends WebTestCase {
public function testApiCall()
{
    $client = static::createClient();

    $service = $this->getMockBuilder(ApiTestManager::class)
        ->disableOriginalConstructor()
        ->setMethods(['getResponse'])
        ->getMock()
        ->expects($this->any())
        ->method('getResponse')
        ->will($this->returnValue(new Response()));

    $client->getContainer()->set('app.business.test_api', $service);
    $client->request('GET', 'de/api/v1/getResponse');

    $this->assertEquals(200, $client->getResponse()->getStatusCode());
}}

I've spent hours in trying to find the mistake, but everytime I execute this test it gives me following error:

Error: Call to undefined method PHPUnit_Framework_MockObject_Builder_InvocationMocker::getResponse()

Can anyone tell me whats wrong with my code? Thanks :)

Aucun commentaire:

Enregistrer un commentaire