1

In order to test an API controller in a Symfony2 project that returns json response, I tried to generate a route of the action like here:

$client->getContainer()->get('router')->generate('/api/register/emailverification/', array('email' => '[email protected]'), true)
$response= $client->getResponse();
$this->assertEquals(200, $response);`

But the response returns null. I don't know if have to do a specific test for this type of response like using guzzle...

2
  • In tests you should not "generate" routes, they must be hardcoded. Commented Apr 4, 2017 at 13:34
  • I tried this : $client = static::createClient(); $crawler = $client->request('GET', '/api/register/emailverification/', array('email' => '[email protected]')); $response = $client->getResponse(); $this->assertEquals(200, $response->getStatusCode()); it returns 404 Commented Apr 4, 2017 at 13:43

2 Answers 2

2

In tests one shouldn't "generate" routes paths, they must be hard-coded:

$client = $this->createClient();
$client->request('GET', '/api/register/emailverification/[email protected]');
$this->assertTrue($client->getResponse()->isOk());

But if you want to test json, you can do:

$this->assertJson($client->getResponse()->getContent());

You can find additional PHPunit helpers in the rest extra bundle.

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

10 Comments

please how can i add a host to this request because it returns 404 (my host is 127.0.0.1:8000)
But does your code working in your browser? That's the 1st step.
copy/past your controller, if 404 it's /api/register/emailverification/ is not correct.
127.0.0.1:8000/api/register/emailverification/[email protected] it works like this in browser
So it's not the same, the email is not a get parameter but part of the route. Answer updated.
|
0

I believe you should try:

$controllerResponse = $client->getContainer()->get('router')->generate('_validation_email', array('email' => '[email protected]'), true)
$response= $controllerResponse->getResponse();
$this->assertEquals(200, $response);

1 Comment

still the same issue

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.