New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.5] Add assertValidationErrors and assertJsonCount to TestResponse #21917

Merged
merged 4 commits into from Nov 6, 2017

Conversation

Projects
None yet
7 participants
@Gimcrack
Contributor

Gimcrack commented Nov 1, 2017

This commit adds two assert helpers to the TestResponse class.

assertValidationErrors( string|array $keys )

Asserts that the given key(s) are in the errors key of the Json response.

Sample use case - validating a new user

// UserController@store
$request->validate([
    'first_name' => 'required',
    'last_name' => 'required'
]);



// Feature/UserTest
$this->post('users', [
    'first_name' => null,
    'last_name' => null
]);

$this
  ->response()
  ->assertValidationErrors(['first_name','last_name']);


// sample response
array:2 [
  "message" => "The given data was invalid."
  "errors" => array:2 [
    "first_name" => array:1 [
      0 => "The first_name field is required."
    ],
    "last_name" => array:1 [
      0 => "The last_name field is required."
    ]
  ]
]

// the test passes

assertJsonCount( int $count )

Asserts that the response data has the expected count.

Sample use case - getting a user resource index

// Feature/UserTest
/** @test */
public function it_can_get_a_listing_of_users()
{
    $users = factory(User::class,3)->create();

    $this->get("users");

    $this->response()
        ->assertStatus(200)
        ->assertJsonCount(3);
}

msb-jeremy added some commits Nov 1, 2017

@thecrypticace

This comment has been minimized.

Show comment
Hide comment
@thecrypticace

thecrypticace Nov 2, 2017

Contributor

I really like assertJsonCount. It's a good option for when you need to assert that you received only N records in an array. assertJson deals in fragments so it is unable to cover this case and assertExactJson can end up being too verbose if you're asserting on targeted data.

However, this does not work as expected if all your responses have a top-level data key (or something similar) How about adding an optional key (w/ a default value of null) It would allow assertions on data like so:

$response->assertJsonCount(3, "data")
$response->assertJsonCount(9, "data.*.contacts")

You should be able to assertCount on the result of data_get($this->json(), $key) as, iirc, data_get will return the original array if you pass null

Contributor

thecrypticace commented Nov 2, 2017

I really like assertJsonCount. It's a good option for when you need to assert that you received only N records in an array. assertJson deals in fragments so it is unable to cover this case and assertExactJson can end up being too verbose if you're asserting on targeted data.

However, this does not work as expected if all your responses have a top-level data key (or something similar) How about adding an optional key (w/ a default value of null) It would allow assertions on data like so:

$response->assertJsonCount(3, "data")
$response->assertJsonCount(9, "data.*.contacts")

You should be able to assertCount on the result of data_get($this->json(), $key) as, iirc, data_get will return the original array if you pass null

@carusogabriel

Fix this PHP Docblocks and you're ready to go 😄

@Dylan-DPC

This comment has been minimized.

Show comment
Hide comment
@Dylan-DPC

Dylan-DPC Nov 2, 2017

Contributor

I think it is better to have a provision for custom validation messages and localisation

Contributor

Dylan-DPC commented Nov 2, 2017

I think it is better to have a provision for custom validation messages and localisation

@taylorotwell

This comment has been minimized.

Show comment
Hide comment
@taylorotwell

taylorotwell Nov 2, 2017

Member

I agree that assertJsonCount isn't very helpful without being able to provide a key.

Member

taylorotwell commented Nov 2, 2017

I agree that assertJsonCount isn't very helpful without being able to provide a key.

msb-jeremy added some commits Nov 3, 2017

@Gimcrack

This comment has been minimized.

Show comment
Hide comment
@Gimcrack

Gimcrack Nov 3, 2017

Contributor

@taylorotwell Good catch. Now if you are using pagination or similar you can specify a key.

$this->response()->assertJsonCount(3, 'data');
Contributor

Gimcrack commented Nov 3, 2017

@taylorotwell Good catch. Now if you are using pagination or similar you can specify a key.

$this->response()->assertJsonCount(3, 'data');
@carusogabriel

This comment has been minimized.

Show comment
Hide comment
@carusogabriel

carusogabriel Nov 3, 2017

Contributor

Rebase your commits to a single one 😊

If you don’t know how: http://gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html

Contributor

carusogabriel commented Nov 3, 2017

Rebase your commits to a single one 😊

If you don’t know how: http://gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html

@Dylan-DPC

This comment has been minimized.

Show comment
Hide comment
@Dylan-DPC

Dylan-DPC Nov 3, 2017

Contributor

@gabriel-caruso there is an option to squash while merging the PR. So Taylor can take care of that

Contributor

Dylan-DPC commented Nov 3, 2017

@gabriel-caruso there is an option to squash while merging the PR. So Taylor can take care of that

@carusogabriel

This comment has been minimized.

Show comment
Hide comment
@carusogabriel

carusogabriel Nov 3, 2017

Contributor

@Dylan-DPC Oh, I didn’t know about this option 😱😍

Contributor

carusogabriel commented Nov 3, 2017

@Dylan-DPC Oh, I didn’t know about this option 😱😍

@taylorotwell taylorotwell merged commit 15b3eef into laravel:5.5 Nov 6, 2017

2 checks passed

continuous-integration/styleci/pr The StyleCI analysis has passed
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@mvdnbrk

This comment has been minimized.

Show comment
Hide comment
@mvdnbrk

mvdnbrk Nov 9, 2017

In the release notes this is mentioned as added assertValidationErrors. Taylor changed this to assertJsonValidationErrors. 😄

mvdnbrk commented Nov 9, 2017

In the release notes this is mentioned as added assertValidationErrors. Taylor changed this to assertJsonValidationErrors. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment