diff --git a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithConsole.php b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithConsole.php index 5e962e80a75d..98f5c042abea 100644 --- a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithConsole.php +++ b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithConsole.php @@ -43,7 +43,7 @@ public function artisan($command, $parameters = []) return $this->app[Kernel::class]->call($command, $parameters); } - $this->beforeApplicationDestroyed(function () { + $this->afterApplicationDestroyed(function () { if (count($this->expectedQuestions)) { $this->fail('Question "'.Arr::first($this->expectedQuestions)[0].'" was not asked.'); } diff --git a/src/Illuminate/Foundation/Testing/PendingCommand.php b/src/Illuminate/Foundation/Testing/PendingCommand.php index 379eba14c8d7..eeab16b8f60a 100644 --- a/src/Illuminate/Foundation/Testing/PendingCommand.php +++ b/src/Illuminate/Foundation/Testing/PendingCommand.php @@ -163,6 +163,8 @@ protected function mockConsoleOutput() (new ArrayInput($this->parameters)), $this->createABufferedOutputMock(), ]); + $mock->_mockery_ignoreVerification = true; + foreach ($this->test->expectedQuestions as $i => $question) { $mock->shouldReceive('askQuestion') ->once() @@ -193,6 +195,8 @@ private function createABufferedOutputMock() ->shouldAllowMockingProtectedMethods() ->shouldIgnoreMissing(); + $mock->_mockery_ignoreVerification = true; + foreach ($this->test->expectedOutput as $i => $output) { $mock->shouldReceive('doWrite') ->once() diff --git a/src/Illuminate/Foundation/Testing/TestCase.php b/src/Illuminate/Foundation/Testing/TestCase.php index f712d80b0f4b..66e345f5e734 100644 --- a/src/Illuminate/Foundation/Testing/TestCase.php +++ b/src/Illuminate/Foundation/Testing/TestCase.php @@ -42,6 +42,13 @@ abstract class TestCase extends BaseTestCase */ protected $beforeApplicationDestroyedCallbacks = []; + /** + * The callbacks that should be run after the application is destroyed. + * + * @var array + */ + protected $afterApplicationDestroyedCallbacks = []; + /** * Indicates if we have made it through the base setUp function. * @@ -135,46 +142,52 @@ protected function setUpTraits() */ protected function tearDown(): void { - if ($this->app) { - foreach ($this->beforeApplicationDestroyedCallbacks as $callback) { - call_user_func($callback); + try { + if ($this->app) { + foreach ($this->beforeApplicationDestroyedCallbacks as $callback) { + \rescue($callback, null, false); + } + + $this->app->flush(); + $this->app = null; + + foreach ($this->afterApplicationDestroyedCallbacks as $callback) { + call_user_func($callback); + } } + } finally { + $this->setUpHasRun = false; - $this->app->flush(); - - $this->app = null; - } + if (property_exists($this, 'serverVariables')) { + $this->serverVariables = []; + } - $this->setUpHasRun = false; + if (property_exists($this, 'defaultHeaders')) { + $this->defaultHeaders = []; + } - if (property_exists($this, 'serverVariables')) { - $this->serverVariables = []; - } + if (class_exists(Mockery::class)) { + if ($container = Mockery::getContainer()) { + $this->addToAssertionCount($container->mockery_getExpectationCount()); + } - if (property_exists($this, 'defaultHeaders')) { - $this->defaultHeaders = []; - } + Mockery::close(); + } - if (class_exists('Mockery')) { - if ($container = Mockery::getContainer()) { - $this->addToAssertionCount($container->mockery_getExpectationCount()); + if (class_exists(Carbon::class)) { + Carbon::setTestNow(); } - Mockery::close(); - } + if (class_exists(CarbonImmutable::class)) { + CarbonImmutable::setTestNow(); + } - if (class_exists(Carbon::class)) { - Carbon::setTestNow(); - } + $this->afterApplicationCreatedCallbacks = []; + $this->beforeApplicationDestroyedCallbacks = []; + $this->afterApplicationDestroyedCallbacks = []; - if (class_exists(CarbonImmutable::class)) { - CarbonImmutable::setTestNow(); + Artisan::forgetBootstrappers(); } - - $this->afterApplicationCreatedCallbacks = []; - $this->beforeApplicationDestroyedCallbacks = []; - - Artisan::forgetBootstrappers(); } /** @@ -202,4 +215,15 @@ protected function beforeApplicationDestroyed(callable $callback) { $this->beforeApplicationDestroyedCallbacks[] = $callback; } + + /** + * Register a callback to be run after the application is destroyed. + * + * @param callable $callback + * @return void + */ + protected function afterApplicationDestroyed(callable $callback) + { + $this->afterApplicationDestroyedCallbacks[] = $callback; + } }