app) { $this->refreshApplication(); } $this->setUpTraits(); foreach ($this->afterApplicationCreatedCallbacks as $callback) { call_user_func($callback); } Facade::clearResolvedInstances(); 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[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(); } return $uses; } /** * Clean up the testing environment before the next test. * * @return void */ protected function tearDown() { if ($this->app) { foreach ($this->beforeApplicationDestroyedCallbacks as $callback) { call_user_func($callback); } $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()); } Mockery::close(); } if (class_exists(Carbon::class)) { Carbon::setTestNow(); } $this->afterApplicationCreatedCallbacks = []; $this->beforeApplicationDestroyedCallbacks = []; Artisan::forgetBootstrappers(); } /** * 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) { call_user_func($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; } }