Buy
Buy

Validation Errors Test

Tip

In this course we're using Symfony 2, but starting in episode 4, we use Symfony 3. If you'd like to see the finished code for this tutorial in Symfony 3, download the code from episode 4 and check out the start directory!

Errors! A lot of things can go wrong - like 500 errors when your database server is on fire, 404 errors, validation errors, authentication errors and errors in judgement, like wearing a khaki shirt with khaki shorts, unless you're on Safari.

In your API, handling all of these errors correctly ends up taking some effort. So that's why we're devoting an entire episode on getting a beautiful, robust error system into your Symfony API.

First, we'll talk about what most people think of when you mention errors: validation errors! In episode 1, we created this cool ProgrammerControllerTest class where we can test all of our endpoints for creating a programmer, updating a programmer deleting a programmer, etc etc. We don't have validation on any of these endpoints yet.

Test for a Required Username

So let's add some: when we POST to create, we really need to make sure that the username isn't blank. That would be crazy! Copy testPOST(). Down at the bottom, paste that, and rename it to testValidationErrors(). Get rid of the nickname data field and most of the asserts:

... lines 1 - 5
class ProgrammerControllerTest extends ApiTestCase
{
... lines 8 - 123
public function testValidationErrors()
{
$data = array(
'avatarNumber' => 2,
'tagLine' => 'I\'m from a test!'
);
// 1) Create a programmer resource
$response = $this->client->post('/api/programmers', [
'body' => json_encode($data)
]);
... lines 135 - 144
}
}

Validation Error Status Code: 400

Ok, design time! Writing the test is our time to think about how each endpoint should work. Since we're not sending a username, what status code should the endpoint return? Use 400:

... lines 1 - 123
public function testValidationErrors()
{
... lines 126 - 131
$response = $this->client->post('/api/programmers', [
'body' => json_encode($data)
]);
$this->assertEquals(400, $response->getStatusCode());
... lines 137 - 144
}
... lines 146 - 147

There are a few other status codes that you could use and we talk about them in our original REST series. But 400 is a solid choice.

Validation Errors Response Body

Next, what should the JSON content of our response hold? Let me suggest a format. Trust me for now - I'll explain why soon. We need to tell the client what went wrong - a validation error - and what the validation errors are.

Use $this->asserter()->assertResponsePropertiesExist() to assert that the response will have 3 properties. The first is type - a string error code - the second is title - a human description of what went wrong - and the third is errors - an array of all of the validation errors:

... lines 1 - 123
public function testValidationErrors()
{
... lines 126 - 131
$response = $this->client->post('/api/programmers', [
'body' => json_encode($data)
]);
... lines 135 - 136
$this->asserter()->assertResponsePropertiesExist($response, array(
'type',
'title',
'errors',
));
... lines 142 - 144
}
... lines 146 - 147

Don't forget to pass the $response as the first argument. Now, think about that errors array property. I'm thinking it'll be an associative array where the keys are the fields that have errors, and the values are those errors. Use the asserter again to say assertResponsePropertyExists() to assert that errors.nickname exists:

... lines 1 - 123
public function testValidationErrors()
{
... lines 126 - 141
$this->asserter()->assertResponsePropertyExists($response, 'errors.nickname');
... lines 143 - 144
}
... lines 146 - 147

Basically, we want to assert that there is some error on the nickname field, because it should be required. Actually, go one step further and assert the exact validation message. Use assertResponsePropertyEquals with $response as the first argument, errors.nickname[0] as the second and, for the third, a nice message, how about "Please enter a clever nickname":

... lines 1 - 123
public function testValidationErrors()
{
... lines 126 - 141
$this->asserter()->assertResponsePropertyExists($response, 'errors.nickname');
$this->asserter()->assertResponsePropertyEquals($response, 'errors.nickname[0]', 'Please enter a clever nickname');
... line 144
}
... lines 146 - 147

Why the [0] part? It won't be too common, but one field could have multiple errors, like a username that contains invalid characters and is too short. So each field will have an array of errors, and we're checking that the first is set to our clever message.

And since we are sending a valid avatarNumber, let's make sure that there is no error for it. Use assertResponsePropertyDoesNotExist() and pass it errors.avatarNumber:

... lines 1 - 123
public function testValidationErrors()
{
... lines 126 - 141
$this->asserter()->assertResponsePropertyExists($response, 'errors.nickname');
$this->asserter()->assertResponsePropertyEquals($response, 'errors.nickname[0]', 'Please enter a clever nickname');
$this->asserter()->assertResponsePropertyDoesNotExist($response, 'errors.avatarNumber');
}
... lines 146 - 147

Love it! We've just planned how we want our validation errors to work. That'll make coding this a lot easier.

Leave a comment!

  • 2018-01-22 weaverryan

    Hey julien moulis!

    Hmm. That's a great question :). Are you using a CollectionType? In the eyes of the form component, if you have 3 of these things, then they are added to the form as indexes (e.g. form.products[0], form.products[1] and form.products[2]). Then, the errors are attached at the same "nodes".

    So, there *is* certainly a way to take the index (e.g. 0) and find the correct thing (e.g. Product) that it's referring to. What exactly do you want to accomplish? And what does your API endpoint look like? Because, for example, if your user is sending an array of Product objects (via JSON), then communicating the index (e.g. 0) *is* the best way to tell them which has a validation error. But I'm guessing your situation is probably not exactly this :).

    Cheers!

  • 2018-01-19 julien moulis

    Hi guys,
    I'm using this wonderful tuto to build an app. I have a question about returning an error on a oneToMany relation. I have the error returned but with no field name, just with a 0 index and as a string. Have you got any idea?