-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
TestResponse, assertSessionHasErrors, getBag() on array #42494
Comments
I don't get the same issue on a fresh install with pest tests enabled. |
I never figured out why Fortify decides to return an array instead of an error bag, so I had to create my own macro to fix the failing test. if (app()->runningUnitTests()) {
//based on TestResponse->assertSessionHasErrors, sometimes $errors are an array and not an Error Bag
TestResponse::macro('assertSessionBagOrArrayHasErrors', function ($keys = [], $format = null, $errorBag = 'default'): self {
$this->assertSessionHas('errors');
$keys = (array)$keys;
$errors = $this->session()->get('errors');
if (is_array($errors)) {
$errors = collect(data_get($errors, $errorBag . '.messages', []));
PHPUnit::assertTrue($errors->has($keys), "Session missing error: " . implode(', ', $keys));
} else {
$errors = $errors->getBag($errorBag);
foreach ($keys as $key => $value) {
if (is_int($key)) {
PHPUnit::assertTrue($errors->has($value), "Session missing error: $value");
} else {
PHPUnit::assertContains(is_bool($value) ? (string)$value : $value, $errors->get($key, $format));
}
}
}
return $this;
}); |
@driesvints Would you accept a PR for the |
@tanthammar I think it's important that we attempt to recreate the situation on a fresh laravel app first to fully understand where this is coming from. |
I'd love to, I just don't know how to trace back to the source of the array conversion. Doing some "dd" in the Fortify controller and in the Fortify password confirm view, the type of $errors is correct. It is only in the test where it becomes an array. |
Then I feel that we shouldn't take any action as this is only happing for you specifically. I feel we can't warrant a change for that as everything works as expected for a default Laravel app. I'm sorry for that. |
@driesvints I finally found what it was.
With the help of @Krisell who directed me to; Anyway. If the option comes back, I know how to solve it in And if needed to note, yes, the test works again in its original form. |
It seems like the Adding |
The following seems reasonable, and passes all framework tests and also the suite of a mid-sized application.
|
@Krisell thanks for investigating 👍 Feel free to attempt a PR with that if you want. Try to provide some background to Taylor why that would be needed. |
Description:
This test used to work, I never changed it from my initial Laravel, Jetstream (Livewire, teams, pest) installation.
Now
$response->assertSessionHasErrors();
is failing.I did print out the
$errors
from theIlluminate/Testing/TestResponse->assertSessionHasErrors(...)
method, and$this->session()->get('errors')
does return an array:And
array
doesn't have thegetBag()
method...The text was updated successfully, but these errors were encountered: