You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
mysql Ver 8.0.28 for Linux on x86_64 (MySQL Community Server - GPL)
Description
I noticed a lazy violation exception in production, and I had created a test to reproduce the issue but the test passed. After I added the following code to my TestCase ($this->withoutExceptionHandling();), the test started to fail, and a bunch of other tests failed. I think "LazyLoadingViolationException" should be reported during tests (this is the perfect moment to catch these errors).
However they get correctly reported, but silently in the logs.
That may be a confusion/misunderstanding over what is the purpose of handling exceptions during feature tests (I did not find any usable information in the documentation about the why).
I was also surprised to have detected other "hidden" exceptions since I disabled exception handling (but that is outside the scope of this issue).
Steps To Reproduce
With a model with a one to many relationship (example shopping list -> items), controller loops through the items, and the tests should ensure 2 or more items are attached to the tested model (shopping list).
// app/tests/TestCase.phpdeclare(strict_types=1);
namespaceTests;
useIlluminate\Foundation\Testing\LazilyRefreshDatabase;
useIlluminate\Foundation\Testing\TestCaseasBaseTestCase;
useIlluminate\Support\Facades\Http;
abstractclassTestCaseextendsBaseTestCase
{
useCreatesApplication;
useLazilyRefreshDatabase;
protectedbool$seed = true;
protectedfunctionsetUp(): void
{
parent::setUp();
$this->withoutVite();
$this->withoutMix();
$this->withoutExceptionHandling(); // When this line is commented, the test below does not fail (not wanted)Http::preventStrayRequests();
}
}
// app/app/Exceptions/Handler.phpdeclare(strict_types=1);
namespaceApp\Exceptions;
useIlluminate\Foundation\Exceptions\HandlerasExceptionHandler;
useIlluminate\Support\Facades\App;
useSentry\Laravel\Integration;
useThrowable;
finalclassHandlerextendsExceptionHandler
{
/** * A list of the exception types that are not reported.> */protected$dontReport = [];
protected$dontFlash = [
"current_password",
"password",
"password_confirmation",
];
publicfunctionregister(): void
{
$this->reportable(function (Throwable$e): void {
/** * @see https://docs.sentry.io/platforms/php/guides/laravel/#install */if (!App::environment("local")) {
Integration::captureUnhandledException($e);
}
});
}
}
Edit: I had to explicitly list which exceptions the framework should handle so that all new exceptions are not handled and I can decide which one should be. In my opinion this should be done behind the scene so that everything that can be asserted (validation, authorization, ...) is handled by default and everything that is not assertable (lazy loading violations etc...) keep popping up in the CLI.
This code below produces the results I expect, so that new missing attributes or violation exceptions gets detected.
The text was updated successfully, but these errors were encountered:
khalyomede
changed the title
LazyLoadingViolationException are not reported when running tests (unless $this->withoutExceptionHandling() is called)
LazyLoadingViolationException are not reported in console when running tests (unless $this->withoutExceptionHandling() is called)
Mar 8, 2024
As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.
If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.
Laravel Version
10.47.1
PHP Version
8.3.3
Database Driver & Version
mysql Ver 8.0.28 for Linux on x86_64 (MySQL Community Server - GPL)
Description
I noticed a lazy violation exception in production, and I had created a test to reproduce the issue but the test passed. After I added the following code to my TestCase (
$this->withoutExceptionHandling();
), the test started to fail, and a bunch of other tests failed. I think "LazyLoadingViolationException" should be reported during tests (this is the perfect moment to catch these errors).However they get correctly reported, but silently in the logs.
That may be a confusion/misunderstanding over what is the purpose of handling exceptions during feature tests (I did not find any usable information in the documentation about the why).
I was also surprised to have detected other "hidden" exceptions since I disabled exception handling (but that is outside the scope of this issue).
Steps To Reproduce
With a model with a one to many relationship (example shopping list -> items), controller loops through the items, and the tests should ensure 2 or more items are attached to the tested model (shopping list).
Edit: I had to explicitly list which exceptions the framework should handle so that all new exceptions are not handled and I can decide which one should be. In my opinion this should be done behind the scene so that everything that can be asserted (validation, authorization, ...) is handled by default and everything that is not assertable (lazy loading violations etc...) keep popping up in the CLI.
This code below produces the results I expect, so that new missing attributes or violation exceptions gets detected.
The text was updated successfully, but these errors were encountered: