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

CommandStarted and CommandFinished no longer testable in PhpUnit #46629

Closed
henzeb opened this issue Mar 29, 2023 · 7 comments
Closed

CommandStarted and CommandFinished no longer testable in PhpUnit #46629

henzeb opened this issue Mar 29, 2023 · 7 comments

Comments

@henzeb
Copy link
Contributor

henzeb commented Mar 29, 2023

  • Laravel Version: 10.4.1
  • PHP Version: 8.2.4
  • Database Driver & Version: -

Description:

I traced it back to this pull request which was about memory leaks, but now it does no longer dispatch the Events when running from phpunit.

That is because of the if statement around it.

if (! $this->app->runningUnitTests()) {
$this->rerouteSymfonyCommandEvents();
}

This was possible in earlier versions of Laravel, so this is definitely a breaking change.

Steps To Reproduce:

the following test will succeed in earlier versions of laravel:

public function testFailingCommandEvents()
{
      $actual = false;
      
      Event::listen(CommandStarting::class, function () use (&$actual) {
          $actual = true;
      });

      Artisan::command(
          'myApplication',
          function () {
          }
      );

      Artisan::call('myApplication');

      $this->assertTrue($actual);
}
@crynobone
Copy link
Member

Add the following command if you want to test command events:

$this->app[\Illuminate\Contracts\Console\Kernel::class]->rerouteSymfonyCommandEvents();

@henzeb
Copy link
Contributor Author

henzeb commented Mar 29, 2023

Add the following command if you want to test command events:

$this->app[\Illuminate\Contracts\Console\Kernel::class]->rerouteSymfonyCommandEvents();

Its a good work around, but I still consider this a breaking change.

@henzeb henzeb closed this as completed Mar 29, 2023
@henzeb henzeb reopened this Mar 29, 2023
@crynobone
Copy link
Member

crynobone commented Mar 30, 2023

Current

image

If we revert the changes

Memory usage increased by more than 500MB

image


If we want to revert it then both #46508 and #46442 should be reverted.

@henzeb
Copy link
Contributor Author

henzeb commented Mar 30, 2023

so you say, that if we JUST remove the if($app->runningUnitTests()) statement, we have a memoryleak?

@crynobone
Copy link
Member

yes

@henzeb
Copy link
Contributor Author

henzeb commented Mar 30, 2023

Weird. will it be mentioned in the docs? because it took me all day to figure out it was laravel and not my changes in my project.

@driesvints
Copy link
Member

I'm sorry you got caught by this. But given the fact that the above PR solves a significant memory leak we won't be reverting this. Since probably very people are using/relying on this we'll not mention this anywhere explicitly.

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

No branches or pull requests

3 participants