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@gmail.com'), 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...

COil
  • 7,201
  • 2
  • 50
  • 98
  • In tests you should not "generate" routes, they must be hardcoded. – COil Apr 04 '17 at 13:34
  • I tried this : $client = static::createClient(); $crawler = $client->request('GET', '/api/register/emailverification/', array('email' => 'email@gmail.com')); $response = $client->getResponse(); $this->assertEquals(200, $response->getStatusCode()); it returns 404 – Christophegonfrere Apr 04 '17 at 13:43

2 Answers2

2

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

$client = $this->createClient();
$client->request('GET', '/api/register/emailverification/email@gmail.com');
$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.

COil
  • 7,201
  • 2
  • 50
  • 98
0

I believe you should try:

$controllerResponse = $client->getContainer()->get('router')->generate('_validation_email', array('email' => 'email@gmail.com'), true)
$response= $controllerResponse->getResponse();
$this->assertEquals(200, $response);