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

[5.8] Add message value assertion to assertJsonValidationErrors #27655

Closed
wants to merge 9 commits into from
Closed

[5.8] Add message value assertion to assertJsonValidationErrors #27655

wants to merge 9 commits into from

Conversation

ahinkle
Copy link
Contributor

@ahinkle ahinkle commented Feb 25, 2019

Moved to 5.8 branch per request (#27640).

This PR adds the assertion for JSON validation error messages in assertJsonValidationErrors.

Currently, we can only check json validation error keys:

$this->assertJsonValidationErrors('key');
$this->assertJsonValidationErrors(['foo', 'bar']);

With this addition, you can check the key and the message:
$this->assertJsonValidationErrors(['key' => 'validation message']);

You can still continue to use the normal functionality by just passing a key or an array of keys.

@taylorotwell
Copy link
Member

So you can't have a mix of checking keys and checking both keys and messages?

@ahinkle
Copy link
Contributor Author

ahinkle commented Feb 27, 2019

After some thought I've done some refactoring to check if the key was originally initialized and check for mixed types:

$this->assertJsonValidationErrors(['key' => 'foo', 'bar'])

This will cover everything 👍

"Failed to find a validation error in the response for key: '{$value}'".PHP_EOL.PHP_EOL.$errorMessage
);
} else {
PHPUnit::assertArraySubset(
Copy link
Member

@driesvints driesvints Feb 27, 2019

Choose a reason for hiding this comment

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

Please do not introduce a new usage of assertArraySubset in Laravel. This is deprecated in PHPUnit 8 and we should avoid adding more uses of it.

Copy link
Contributor Author

@ahinkle ahinkle Feb 27, 2019

Choose a reason for hiding this comment

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

@driesvints Thanks. I just read the the deprecation post(sebastianbergmann/phpunit#3494) and it's said there is no alternative.

hmm, unfortunately, now there isn't a clean way to check if a key and value matches within an array in PHPUnit and changes to use PHP here would be ugly by checking if key is set in jsonErrors then if the error message matches. Might have to table this until PHPUnit's issue/what you decide for assertJson is sorted out. Thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

If we can't find an alternative we're going to backport it into the framework directly but until we figure it out it's best to avoid adding more uses.

Copy link

Choose a reason for hiding this comment

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

@driesvints if you are looking for a quick way out, I ported them to a phpunit extension https://packagist.org/packages/dms/phpunit-arraysubset-asserts

Copy link
Member

Choose a reason for hiding this comment

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

@rdohms ah thanks. I'll put this on my todo list. Thanks for sharing 👍

@ahinkle
Copy link
Contributor Author

ahinkle commented Feb 27, 2019

Going to table this for now until I find a better solution for assertArraySubset and resubmit at a later time. Thanks!

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.

4 participants