Skip to content
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

[8.x] Allow using dot syntax for $responseKey #36196

Merged
merged 5 commits into from
Feb 10, 2021
Merged

[8.x] Allow using dot syntax for $responseKey #36196

merged 5 commits into from
Feb 10, 2021

Conversation

paulandroshchuk
Copy link
Contributor

@paulandroshchuk paulandroshchuk commented Feb 9, 2021

I just noticed that the assertJsonMissingValidationErrors() assertion does not appreciate the dot syntax of the $responseKey parameter. However, the assertJsonValidationErrors() assertion appreciates that.

So if we had a Laravel project with a customised way of exposing JSON validation errors as something like this:

// customised
'message' => $exception->getMessage(),
'data' => [
  'errors' => $exception->errors(),
],

// default
'message' => $exception->getMessage(),
'errors' => $exception->errors(),

the following test would pass:

$this->postJson('/api/users')
    ->assertJsonValidationErrors(
        ['email' => 'The email field is required.'], 
        $responseKey = 'data.errors',
    )

    // This one will pass as the assertion does not appreciate the dot syntax of the $responseKey parameter
    // as it will try to get this: `$jsonResponse['validation.errors']` rather than `$jsonResponse['validation']['errors']`.
    ->assertJsonMissingValidationErrors(
        ['email'],
        $responseKey = 'data.errors',
    );

So, this PR makes it so it appreciates the dot syntax for the $responseKey parameter on the assertJsonMissingValidationErrors() assertion as it's partially related assertion assertJsonValidationErrors() appreciates that.

if (! array_key_exists($responseKey, $json)) {
PHPUnit::assertArrayNotHasKey($responseKey, $json);
if (! Arr::has($json, $responseKey)) {
PHPUnit::assertFalse(Arr::has($json, $responseKey));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to make the error message much more vague because you changed this to a plain assertFalse with no additional message information.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

Co-authored-by: Paul A. <paul.androshchuk@gmail.com>
@taylorotwell taylorotwell merged commit fe72373 into laravel:8.x Feb 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants