app) { $this->refreshApplication(); ParallelTesting::callSetUpTestCaseCallbacks($this); } $this->setUpTraits(); foreach ($this->afterApplicationCreatedCallbacks as $callback) { $callback(); } Model::setEventDispatcher($this->app['events']); $this->setUpHasRun = true; } /** * Refresh the application instance. * * @return void */ protected function refreshApplication() { $this->app = $this->createApplication(); } /** * Boot the testing helper traits. * * @return array */ protected function setUpTraits() { $uses = array_flip(class_uses_recursive(static::class)); if (isset($uses[RefreshDatabase::class])) { $this->refreshDatabase(); } if (isset($uses[DatabaseMigrations::class])) { $this->runDatabaseMigrations(); } if (isset($uses[DatabaseTruncation::class])) { $this->truncateDatabaseTables(); } if (isset($uses[DatabaseTransactions::class])) { $this->beginDatabaseTransaction(); } if (isset($uses[WithoutMiddleware::class])) { $this->disableMiddlewareForAllTests(); } if (isset($uses[WithoutEvents::class])) { $this->disableEventsForAllTests(); } if (isset($uses[WithFaker::class])) { $this->setUpFaker(); } foreach ($uses as $trait) { if (method_exists($this, $method = 'setUp'.class_basename($trait))) { $this->{$method}(); } if (method_exists($this, $method = 'tearDown'.class_basename($trait))) { $this->beforeApplicationDestroyed(fn () => $this->{$method}()); } } return $uses; } /** * {@inheritdoc} */ protected function runTest(): mixed { $result = null; try { $result = parent::runTest(); } catch (Throwable $e) { if (! is_null(static::$latestResponse)) { static::$latestResponse->transformNotSuccessfulException($e); } throw $e; } return $result; } /** * Clean up the testing environment before the next test. * * @return void * * @throws \Mockery\Exception\InvalidCountException */ protected function tearDown(): void { if ($this->app) { $this->callBeforeApplicationDestroyedCallbacks(); ParallelTesting::callTearDownTestCaseCallbacks($this); $this->app->flush(); $this->app = null; } $this->setUpHasRun = false; if (property_exists($this, 'serverVariables')) { $this->serverVariables = []; } if (property_exists($this, 'defaultHeaders')) { $this->defaultHeaders = []; } if (class_exists('Mockery')) { if ($container = Mockery::getContainer()) { $this->addToAssertionCount($container->mockery_getExpectationCount()); } try { Mockery::close(); } catch (InvalidCountException $e) { if (! Str::contains($e->getMethodName(), ['doWrite', 'askQuestion'])) { throw $e; } } } if (class_exists(Carbon::class)) { Carbon::setTestNow(); } if (class_exists(CarbonImmutable::class)) { CarbonImmutable::setTestNow(); } $this->afterApplicationCreatedCallbacks = []; $this->beforeApplicationDestroyedCallbacks = []; $this->originalExceptionHandler = null; $this->originalDeprecationHandler = null; Artisan::forgetBootstrappers(); Component::flushCache(); Component::forgetComponentsResolver(); Component::forgetFactory(); Queue::createPayloadUsing(null); HandleExceptions::forgetApp(); if ($this->callbackException) { throw $this->callbackException; } } /** * Clean up the testing environment before the next test case. * * @return void */ public static function tearDownAfterClass(): void { static::$latestResponse = null; foreach ([ \PHPUnit\Util\Annotation\Registry::class, \PHPUnit\Metadata\Annotation\Parser\Registry::class, ] as $class) { if (class_exists($class)) { (function () { $this->classDocBlocks = []; $this->methodDocBlocks = []; })->call($class::getInstance()); } } } /** * Register a callback to be run after the application is created. * * @param callable $callback * @return void */ public function afterApplicationCreated(callable $callback) { $this->afterApplicationCreatedCallbacks[] = $callback; if ($this->setUpHasRun) { $callback(); } } /** * Register a callback to be run before the application is destroyed. * * @param callable $callback * @return void */ protected function beforeApplicationDestroyed(callable $callback) { $this->beforeApplicationDestroyedCallbacks[] = $callback; } /** * Execute the application's pre-destruction callbacks. * * @return void */ protected function callBeforeApplicationDestroyedCallbacks() { foreach ($this->beforeApplicationDestroyedCallbacks as $callback) { try { $callback(); } catch (Throwable $e) { if (! $this->callbackException) { $this->callbackException = $e; } } } } }