1

I am trying to call a page that returns JSON. This is the response sent by the controller

    $response = new JsonResponse();
    $response->setData($result);
    $response->send();
    exit;

When I test it with phpunit, with this code:

    $publicClient = static::createClient();
    $publicClient->request('POST', '/list/', []);
    $this->assertTrue(true);

the assert is never reached. Instead the ouptut (with --debug and -v) is simply

bin/phpunit -c app/phpunit.xml.dist --debug -v
PHPUnit 4.3.4 by Sebastian Bergmann.

Configuration read from /var/www/eliphi/public_html/app/phpunit.xml.dist


Starting test '[...]\Tests\Controller\FrontendControllerTest::testListAjax'.
[{"seriesTitle":"public","publications":[]},{"seriesTitle":"member","publications":[]},{"seriesTitle":"abo","publications":[]},{"seriesTitle":"public Magazine","publications":[]},{"seriesTitle":"member Magazine","publications":[]},{"seriesTitle":"abo Magazine","publications":[]}]

That's all! When I call the page from the browser, the response header is correctly set with

Date: Sat, 01 Nov 2014 14:56:31 GMT    
Server: Apache/2.2.22 (Ubuntu)
X-Powered-By: PHP/5.4.34-1+deb.sury.org~precise+1    
Cache-Control: no-cache    
Connection: close    
Content-Type: application/json    

200 OK

If I do a request on a different url that returns html, it works just fine. What could possibly be the reason for this?

0

1 Answer 1

2

Your exit call in the controller is terminating the test script. In general, you should use return instead.

Exiting from a controller will break tests and bypass any post-controller events set up - it may have unexpected results if you use any third party bundles.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.