diff --git a/src/Illuminate/Foundation/Testing/Concerns/ComposesTemplateMethods.php b/src/Illuminate/Foundation/Testing/Concerns/ComposesTemplateMethods.php new file mode 100644 index 000000000000..b3035069d6f5 --- /dev/null +++ b/src/Illuminate/Foundation/Testing/Concerns/ComposesTemplateMethods.php @@ -0,0 +1,52 @@ +$method(); + } + } +} diff --git a/src/Illuminate/Foundation/Testing/DatabaseMigrations.php b/src/Illuminate/Foundation/Testing/DatabaseMigrations.php index 4760818dd937..7b975f4107a9 100644 --- a/src/Illuminate/Foundation/Testing/DatabaseMigrations.php +++ b/src/Illuminate/Foundation/Testing/DatabaseMigrations.php @@ -11,7 +11,7 @@ trait DatabaseMigrations * * @return void */ - public function runDatabaseMigrations() + public function setUpDatabaseMigrations() { $this->artisan('migrate:fresh'); diff --git a/src/Illuminate/Foundation/Testing/DatabaseTransactions.php b/src/Illuminate/Foundation/Testing/DatabaseTransactions.php index 9870153bb333..96dac1a3f739 100644 --- a/src/Illuminate/Foundation/Testing/DatabaseTransactions.php +++ b/src/Illuminate/Foundation/Testing/DatabaseTransactions.php @@ -9,7 +9,7 @@ trait DatabaseTransactions * * @return void */ - public function beginDatabaseTransaction() + public function setUpDatabaseTransactions() { $database = $this->app->make('db'); diff --git a/src/Illuminate/Foundation/Testing/RefreshDatabase.php b/src/Illuminate/Foundation/Testing/RefreshDatabase.php index 5358242f7f89..1f4194be8aa7 100644 --- a/src/Illuminate/Foundation/Testing/RefreshDatabase.php +++ b/src/Illuminate/Foundation/Testing/RefreshDatabase.php @@ -11,7 +11,7 @@ trait RefreshDatabase * * @return void */ - public function refreshDatabase() + public function setUpRefreshDatabase() { $this->usingInMemoryDatabase() ? $this->refreshInMemoryDatabase() diff --git a/src/Illuminate/Foundation/Testing/TestCase.php b/src/Illuminate/Foundation/Testing/TestCase.php index 2a0124ff1b76..f1ff8af8fdc3 100755 --- a/src/Illuminate/Foundation/Testing/TestCase.php +++ b/src/Illuminate/Foundation/Testing/TestCase.php @@ -18,7 +18,8 @@ abstract class TestCase extends BaseTestCase Concerns\InteractsWithDatabase, Concerns\InteractsWithExceptionHandling, Concerns\InteractsWithSession, - Concerns\MocksApplicationServices; + Concerns\MocksApplicationServices, + Concerns\ComposesTemplateMethods; /** * The Illuminate application instance. @@ -68,7 +69,7 @@ protected function setUp() $this->refreshApplication(); } - $this->setUpTraits(); + $this->callTraitTemplateMethods('setUp'); foreach ($this->afterApplicationCreatedCallbacks as $callback) { call_user_func($callback); @@ -91,38 +92,6 @@ 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(); - } - - return $uses; - } - /** * Clean up the testing environment before the next test. * @@ -130,6 +99,8 @@ protected function setUpTraits() */ protected function tearDown() { + $this->callTraitTemplateMethods('tearDown'); + if ($this->app) { foreach ($this->beforeApplicationDestroyedCallbacks as $callback) { call_user_func($callback); diff --git a/src/Illuminate/Foundation/Testing/WithoutEvents.php b/src/Illuminate/Foundation/Testing/WithoutEvents.php index fa5df3ce8f5b..039e26287db0 100644 --- a/src/Illuminate/Foundation/Testing/WithoutEvents.php +++ b/src/Illuminate/Foundation/Testing/WithoutEvents.php @@ -11,7 +11,7 @@ trait WithoutEvents * * @throws \Exception */ - public function disableEventsForAllTests() + public function setUpWithoutEvents() { if (method_exists($this, 'withoutEvents')) { $this->withoutEvents(); diff --git a/src/Illuminate/Foundation/Testing/WithoutMiddleware.php b/src/Illuminate/Foundation/Testing/WithoutMiddleware.php index 269b532d3150..cc58405c0618 100644 --- a/src/Illuminate/Foundation/Testing/WithoutMiddleware.php +++ b/src/Illuminate/Foundation/Testing/WithoutMiddleware.php @@ -11,7 +11,7 @@ trait WithoutMiddleware * * @throws \Exception */ - public function disableMiddlewareForAllTests() + public function setUpWithoutMiddleware() { if (method_exists($this, 'withoutMiddleware')) { $this->withoutMiddleware(); diff --git a/tests/Foundation/FoundationComposesTemplateMethodsTest.php b/tests/Foundation/FoundationComposesTemplateMethodsTest.php new file mode 100644 index 000000000000..daf7e9483512 --- /dev/null +++ b/tests/Foundation/FoundationComposesTemplateMethodsTest.php @@ -0,0 +1,57 @@ +callTraitTemplateMethods('setUp'); + + $this->assertTrue($this->testTraitOneSetUp); + $this->assertTrue($this->testTraitTwoSetUp); + } + + public function testCallsComposedTearDownMethods() + { + $this->callTraitTemplateMethods('tearDown'); + + $this->assertFalse($this->testTraitOneSetUp); + $this->assertFalse($this->testTraitTwoSetUp); + } +} + +trait TestTraitOne +{ + public $testTraitOneSetUp; + + public function setUpTestTraitOne() + { + $this->testTraitOneSetUp = true; + } + + public function tearDownTestTraitOne() + { + $this->testTraitOneSetUp = false; + } +} + +trait TestTraitTwo +{ + public $testTraitTwoSetUp; + + public function setUpTestTraitTwo() + { + $this->testTraitTwoSetUp = true; + } + + public function tearDownTestTraitTwo() + { + $this->testTraitTwoSetUp = false; + } +}