Skip to content

[5.6] Improve session errors assertions - #23055

Merged
taylorotwell merged 1 commit into
laravel:5.6from
roberto-aguilar:hotfix/session-errors-assertion
Feb 7, 2018
Merged

[5.6] Improve session errors assertions#23055
taylorotwell merged 1 commit into
laravel:5.6from
roberto-aguilar:hotfix/session-errors-assertion

Conversation

@roberto-aguilar

@roberto-aguilar roberto-aguilar commented Feb 7, 2018

Copy link
Copy Markdown
Contributor

In the previous implementation of TestResponse::assertSessionHasErrors i found that was not possible to find errors that had an integer key, for example:

// If somewhere in your application you did
return redirect()->back()->withErrors('Something wrong happened.')

// You ended up with a message bag similar to this one in the session errors
$errors = new Illuminate\Support\MessageBag('Something wrong happened.');
$errors->get(0); // Returns 'Something wrong happened.'

// And the following assertion was failing, even when the error was there
$this->post('/somewhere')
     ->assertSessionHasErrors(['Something wrong happened.']);

This was happening because if the assertion detected that the key was an integer, it was sending the error value to the MessageBag::has method, which was incorrect because that method uses the key to determine if there is any messages for it.

Also, as part of the refactor i achieved the following assertion API improvements, which now allow to find:

/*
 * All the messages for a given key
 */
$this->post('/somewhere', ['password' => 'abc 123'])
     ->assertSessionHasErrors([
         'password' => [
             'The password may only contain letters, numbers, and dashes.',
             'The password must be at least 8 characters.',
         ],
     ]);

/*
 * A subset of the messages for a given key
 */
$this->post('/somewhere', ['password' => 'abc 123'])
     ->assertSessionHasErrors([
         'password' => [
             'The password must be at least 8 characters.',
         ],
     ]);

/*
 * An specific message for a given key (without the need for a wrapping array).
 */
$this->post('/somewhere', ['password' => 'abc 123'])
     ->assertSessionHasErrors([
         'password' => 'The password must be at least 8 characters.',
     ]);

/*
 * Messages with integer keys
 */
$this->post('/somewhere')
     ->assertSessionHasErrors([
         'Something wrong happened.',
         'And this happened too.',
     ]);

/*
 * A subset of messages with integer keys
 */
$this->post('/somewhere')
     ->assertSessionHasErrors([
         'Something wrong happened.',
     ]);

/*
 * An specific message with an integer key (without the need for a wrapping array).
 */
$this->post('/somewhere')
     ->assertSessionHasErrors('Something wrong happened.');

@roberto-aguilar roberto-aguilar changed the title Improve session errors assertions [5.5] Improve session errors assertions Feb 7, 2018
In the previous implementation of `TestResponse::assertSessionHasErrors` i
found that was not possible to find errors that had an integer key.

This was happening because if the assertion detected that the key was an
integer, it was sending the error value to the `MessageBag::has` method,
which was incorrect because that method uses the key to determine if
there is any messages for it.
@carusogabriel

Copy link
Copy Markdown
Contributor

👍 for 5.6

@taylorotwell

Copy link
Copy Markdown
Member

Are there any breaking changes in this PR at all @DojoGeekRA?

@roberto-aguilar

roberto-aguilar commented Feb 7, 2018

Copy link
Copy Markdown
Contributor Author

Hi @taylorotwell,

No, there are no breaking changes, the previous API still works 😃

@roberto-aguilar

roberto-aguilar commented Feb 7, 2018

Copy link
Copy Markdown
Contributor Author

@carusogabriel yeah, i have no problem with that, but i though that the LTS users could still benefit from this 😄

Thanks for the suggestion!

@taylorotwell
taylorotwell changed the base branch from 5.5 to 5.6 February 7, 2018 21:14
@taylorotwell
taylorotwell merged commit 4fbb54b into laravel:5.6 Feb 7, 2018
@roberto-aguilar
roberto-aguilar deleted the hotfix/session-errors-assertion branch February 7, 2018 21:25
@taylorotwell

Copy link
Copy Markdown
Member

Actually broke the most simple use case of this function.

@taylorotwell

Copy link
Copy Markdown
Member

Feel free to re-submit with tests proving this actually works.

@roberto-aguilar

roberto-aguilar commented Feb 9, 2018

Copy link
Copy Markdown
Contributor Author

I'm sorry @taylorotwell i tried several scenarios but missed the one with just the error key.

Thanks for the help, i have been working on this and will submit again when is ready 😃

@GrahamCampbell GrahamCampbell changed the title [5.5] Improve session errors assertions [5.6] Improve session errors assertions Feb 10, 2018
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.

3 participants