PhpUnit tests: Application Differences in between tests, Artisan commands breaks mocking #55065
Replies: 5 comments
-
Hey there, thanks for reporting this issue. We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as one separate commit on the main/master branch and share the repository here? Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.
Do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue. Thanks! |
Beta Was this translation helpful? Give feedback.
-
Hi, here is the requested repository I'll edit the original Issue description to include this link thank you! |
Beta Was this translation helpful? Give feedback.
-
Issue 1 Hi, I've investigate a bit and the first test just work by luck as you have the During the first test, it will run your migrations through Artisan. Then, for each next tests, schedule events callback will just never be loaded that's why you'll see 0 event. On dirty way to make it work is by asking something linked to Artisan so it will trigger Artisan app creation which include your schedule events, see : Method protected function setUp(): void
{
parent::setUp();
$this->app->make(Kernel::class)->all();
} |
Beta Was this translation helpful? Give feedback.
-
Issue 2 Just check this now and the issue is coming from your code. This code should be removed : public function __construct(protected ClassToBeInjected $injected)
{
parent::__construct();
} Using You should instead do that : public function handle(ClassToBeInjected $injected): int
{
$this->info($injected->sayHello());
return self::SUCCESS;
} |
Beta Was this translation helpful? Give feedback.
-
Hi! Thx for the feedback. However, calling With the goal of testing that the schedule is set up correctly in the project, writing a unit test that works with the scheduled events seems natural, and it shouldn't matter if it's the first test in the sequence or not. There should be a way of getting the schedule just like how it is intantiated within the real application. The question is how. |
Beta Was this translation helpful? Give feedback.
-
Laravel Version
12.2.0
PHP Version
8.2.27
Database Driver & Version
sqlite3 3.37.2
Description
Since laravel11 (at least I couldn't reproduce the issue in L10), I have found some issues in phpunit tests with laravel.
There seem to be substantial differences in the application when the context is a single phpunit test (or the first test
of many when ran together) or a subsequent test. This is most likely due to the way the application is bootstrapped and/or reset between tests.
Tests that succeed in isolation then fail when run together with at least one other test, unless they happen to be the very first test that runs.
Examples:
In the linked repository (see steps to reproduce) I have provided examples and a more detailed description of the issue.
Steps To Reproduce
Demonstration: https://github.com/growsphere/laravel-bug-report-test-application
Edit: new repo following the requested requirements: https://github.com/growsphere/bug-report
All detailed steps are described in the readme of that repository.
Beta Was this translation helpful? Give feedback.
All reactions