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] Assert invalid #38384

Merged
merged 7 commits into from Aug 16, 2021
Merged

[8.x] Assert invalid #38384

merged 7 commits into from Aug 16, 2021

Conversation

taylorotwell
Copy link
Member

@taylorotwell taylorotwell commented Aug 14, 2021

Currently, there are two different ways to assert that a given response contains validation errors: assertSessionHasErrors and assertJsonValidationErrors.

You need to call the correct one depending on your response. In addition, they behave slightly differently - on JSON validation errors you may simply assert that the validation error contains a given string, while for session validation errors you must pass the entire validation error.

This PR introduces new assertValid and assertInvalid assertions to the TestResponse class that behaves similarly to the JSON validation error assertion in that you only need to provide a portion of the error message but it will work for both session validation errors and JSON response validation errors.

This allows you to always use assertValid / assertInvalid regardless of the type of response returned by your application.

$response->assertValid(['address']);

$response->assertInvalid(['name', 'email']);

$response->assertInvalid([
    'name' => 'required',
]);

@GrahamCampbell GrahamCampbell changed the title Assert invalid [8.x] Assert invalid Aug 14, 2021
Comment on lines +950 to +953
$testResponse->assertValid(['last_name']);
$testResponse->assertInvalid(['first_name']);
$testResponse->assertInvalid(['first_name' => 'required']);
$testResponse->assertInvalid(['first_name' => 'character']);

Choose a reason for hiding this comment

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

Lovely API ❤️

@taylorotwell taylorotwell merged commit 73bf339 into 8.x Aug 16, 2021
@taylorotwell taylorotwell deleted the assert-invalid branch August 16, 2021 13:32
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