Skip to content

Commit e1fd262

Browse files
authored
[10.x] Add WithConsoleEvents helper (#46694)
* [10.x] Add `WithConsoleEvents` helper Make it easier to register console events on specific test without adding a massive memory leak and make it easier to be documented. fixes #46693 Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> --------- Signed-off-by: Mior Muhammad Zaki <[email protected]>
1 parent da71956 commit e1fd262

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103
"league/flysystem-read-only": "^3.3",
104104
"league/flysystem-sftp-v3": "^3.0",
105105
"mockery/mockery": "^1.5.1",
106-
"orchestra/testbench-core": "^8.3",
106+
"orchestra/testbench-core": "^8.4",
107107
"pda/pheanstalk": "^4.0",
108108
"phpstan/phpdoc-parser": "^1.15",
109109
"phpstan/phpstan": "^1.4.7",

src/Illuminate/Foundation/Console/Kernel.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,11 @@ public function __construct(Application $app, Dispatcher $events)
118118
$this->app = $app;
119119
$this->events = $events;
120120

121-
if (! $this->app->runningUnitTests()) {
122-
$this->rerouteSymfonyCommandEvents();
123-
}
124-
125121
$this->app->booted(function () {
122+
if (! $this->app->runningUnitTests()) {
123+
$this->rerouteSymfonyCommandEvents();
124+
}
125+
126126
$this->defineConsoleSchedule();
127127
});
128128
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Illuminate\Foundation\Testing;
4+
5+
use Illuminate\Contracts\Console\Kernel as ConsoleKernel;
6+
7+
trait WithConsoleEvents
8+
{
9+
/**
10+
* Register console events.
11+
*
12+
* @return void
13+
*/
14+
protected function setUpWithConsoleEvents()
15+
{
16+
$this->app[ConsoleKernel::class]->rerouteSymfonyCommandEvents();
17+
}
18+
}

tests/Integration/Console/CommandEventsTest.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77
use Illuminate\Contracts\Console\Kernel as ConsoleKernel;
88
use Illuminate\Events\Dispatcher;
99
use Illuminate\Filesystem\Filesystem;
10+
use Illuminate\Foundation\Testing\WithConsoleEvents;
1011
use Illuminate\Support\Facades\Event;
1112
use Illuminate\Support\Str;
1213
use Orchestra\Testbench\Foundation\Application as Testbench;
1314
use Orchestra\Testbench\TestCase;
1415

1516
class CommandEventsTest extends TestCase
1617
{
18+
use WithConsoleEvents;
19+
1720
/**
1821
* The path to the file that execution logs will be written to.
1922
*
@@ -45,17 +48,6 @@ protected function setUp(): void
4548
parent::setUp();
4649
}
4750

48-
/**
49-
* Define environment setup.
50-
*
51-
* @param \Illuminate\Foundation\Application $app
52-
* @return void
53-
*/
54-
protected function defineEnvironment($app)
55-
{
56-
$app->make(ConsoleKernel::class)->rerouteSymfonyCommandEvents();
57-
}
58-
5951
/**
6052
* @dataProvider foregroundCommandEventsProvider
6153
*/

0 commit comments

Comments
 (0)