From ad2edbc3b2d3ce714d2b6e6f9eddb7bd79f44916 Mon Sep 17 00:00:00 2001 From: DarkGhosthunter Date: Thu, 28 Oct 2021 02:11:54 -0300 Subject: [PATCH 1/8] Changed to a more open trait initialization. --- .../Foundation/Testing/DatabaseMigrations.php | 2 +- .../Testing/DatabaseTransactions.php | 2 +- .../Foundation/Testing/RefreshDatabase.php | 2 +- .../Foundation/Testing/TestCase.php | 35 +++++++------------ .../Foundation/Testing/WithFaker.php | 2 +- .../Foundation/Testing/WithoutEvents.php | 2 +- .../Foundation/Testing/WithoutMiddleware.php | 2 +- 7 files changed, 19 insertions(+), 28 deletions(-) diff --git a/src/Illuminate/Foundation/Testing/DatabaseMigrations.php b/src/Illuminate/Foundation/Testing/DatabaseMigrations.php index 889a45328898..eb28daed2e92 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 7204d9be1909..d1da1f3b4519 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 d66fd0f94911..afe98f4454f0 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 c085a3ce1418..21091bf3bd19 100644 --- a/src/Illuminate/Foundation/Testing/TestCase.php +++ b/src/Illuminate/Foundation/Testing/TestCase.php @@ -15,6 +15,9 @@ use PHPUnit\Framework\TestCase as BaseTestCase; use Throwable; +use function array_flip; +use function method_exists; + abstract class TestCase extends BaseTestCase { use Concerns\InteractsWithContainer, @@ -115,30 +118,18 @@ protected function refreshApplication() */ 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(); - } + foreach ($uses = class_uses_recursive(static::class) as $trait) { + if (method_exists($this, $method = 'setUp' . $trait)) { + $this->{$method}(); + } - if (isset($uses[WithoutEvents::class])) { - $this->disableEventsForAllTests(); - } + if (method_exists($this, $method = 'after' . $trait)) { + $this->afterApplicationCreated([$this, $method]); + } - if (isset($uses[WithFaker::class])) { - $this->setUpFaker(); + if (method_exists($this, $method = 'before' . $trait)) { + $this->beforeApplicationDestroyed([$this, $method]); + } } return $uses; diff --git a/src/Illuminate/Foundation/Testing/WithFaker.php b/src/Illuminate/Foundation/Testing/WithFaker.php index cd276fbd4eb0..d44955460693 100644 --- a/src/Illuminate/Foundation/Testing/WithFaker.php +++ b/src/Illuminate/Foundation/Testing/WithFaker.php @@ -19,7 +19,7 @@ trait WithFaker * * @return void */ - protected function setUpFaker() + protected function setUpWithFaker() { $this->faker = $this->makeFaker(); } 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(); From 194773e98a37f89da3037bd1867c764db993ba40 Mon Sep 17 00:00:00 2001 From: DarkGhosthunter Date: Thu, 28 Oct 2021 02:35:41 -0300 Subject: [PATCH 2/8] Changed method names to something more lexically correct. --- src/Illuminate/Foundation/Testing/TestCase.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Foundation/Testing/TestCase.php b/src/Illuminate/Foundation/Testing/TestCase.php index 21091bf3bd19..7f7108e5ff5c 100644 --- a/src/Illuminate/Foundation/Testing/TestCase.php +++ b/src/Illuminate/Foundation/Testing/TestCase.php @@ -123,11 +123,11 @@ protected function setUpTraits() $this->{$method}(); } - if (method_exists($this, $method = 'after' . $trait)) { + if (method_exists($this, $method = 'created' . $trait)) { $this->afterApplicationCreated([$this, $method]); } - if (method_exists($this, $method = 'before' . $trait)) { + if (method_exists($this, $method = 'destroying' . $trait)) { $this->beforeApplicationDestroyed([$this, $method]); } } From 2d2f9326e03ae6a41b56e5de7b8e64908a8da778 Mon Sep 17 00:00:00 2001 From: DarkGhosthunter Date: Thu, 28 Oct 2021 02:41:22 -0300 Subject: [PATCH 3/8] StyleCI corrections. --- src/Illuminate/Foundation/Testing/TestCase.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Illuminate/Foundation/Testing/TestCase.php b/src/Illuminate/Foundation/Testing/TestCase.php index 7f7108e5ff5c..78a003217899 100644 --- a/src/Illuminate/Foundation/Testing/TestCase.php +++ b/src/Illuminate/Foundation/Testing/TestCase.php @@ -10,14 +10,12 @@ use Illuminate\Support\Facades\Facade; use Illuminate\Support\Facades\ParallelTesting; use Illuminate\Support\Str; +use function method_exists; use Mockery; use Mockery\Exception\InvalidCountException; use PHPUnit\Framework\TestCase as BaseTestCase; use Throwable; -use function array_flip; -use function method_exists; - abstract class TestCase extends BaseTestCase { use Concerns\InteractsWithContainer, @@ -119,15 +117,15 @@ protected function refreshApplication() protected function setUpTraits() { foreach ($uses = class_uses_recursive(static::class) as $trait) { - if (method_exists($this, $method = 'setUp' . $trait)) { + if (method_exists($this, $method = 'setUp'.$trait)) { $this->{$method}(); } - if (method_exists($this, $method = 'created' . $trait)) { + if (method_exists($this, $method = 'created'.$trait)) { $this->afterApplicationCreated([$this, $method]); } - if (method_exists($this, $method = 'destroying' . $trait)) { + if (method_exists($this, $method = 'destroying'.$trait)) { $this->beforeApplicationDestroyed([$this, $method]); } } From b78f0b58c62983546ee013d98d2c4a9cdd69f940 Mon Sep 17 00:00:00 2001 From: DarkGhosthunter Date: Thu, 28 Oct 2021 02:41:53 -0300 Subject: [PATCH 4/8] Removed function class import. --- src/Illuminate/Foundation/Testing/TestCase.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Illuminate/Foundation/Testing/TestCase.php b/src/Illuminate/Foundation/Testing/TestCase.php index 78a003217899..8bae3bcdb2e7 100644 --- a/src/Illuminate/Foundation/Testing/TestCase.php +++ b/src/Illuminate/Foundation/Testing/TestCase.php @@ -10,7 +10,6 @@ use Illuminate\Support\Facades\Facade; use Illuminate\Support\Facades\ParallelTesting; use Illuminate\Support\Str; -use function method_exists; use Mockery; use Mockery\Exception\InvalidCountException; use PHPUnit\Framework\TestCase as BaseTestCase; From fbfc7647cecf865122ad96a1d470f605deed080c Mon Sep 17 00:00:00 2001 From: DarkGhosthunter Date: Thu, 28 Oct 2021 18:17:19 -0300 Subject: [PATCH 5/8] Reverted back traditional built-in traits and pushed initialization after them. --- .../Foundation/Testing/DatabaseMigrations.php | 2 +- .../Testing/DatabaseTransactions.php | 2 +- .../Foundation/Testing/RefreshDatabase.php | 2 +- .../Foundation/Testing/TestCase.php | 30 ++++++++++++++++++- .../Foundation/Testing/WithFaker.php | 2 +- .../Foundation/Testing/WithoutEvents.php | 2 +- .../Foundation/Testing/WithoutMiddleware.php | 2 +- 7 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/Illuminate/Foundation/Testing/DatabaseMigrations.php b/src/Illuminate/Foundation/Testing/DatabaseMigrations.php index eb28daed2e92..889a45328898 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 setUpDatabaseMigrations() + public function runDatabaseMigrations() { $this->artisan('migrate:fresh'); diff --git a/src/Illuminate/Foundation/Testing/DatabaseTransactions.php b/src/Illuminate/Foundation/Testing/DatabaseTransactions.php index d1da1f3b4519..7204d9be1909 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 setUpDatabaseTransactions() + public function beginDatabaseTransaction() { $database = $this->app->make('db'); diff --git a/src/Illuminate/Foundation/Testing/RefreshDatabase.php b/src/Illuminate/Foundation/Testing/RefreshDatabase.php index afe98f4454f0..d66fd0f94911 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 setUpRefreshDatabase() + public function refreshDatabase() { $this->usingInMemoryDatabase() ? $this->refreshInMemoryDatabase() diff --git a/src/Illuminate/Foundation/Testing/TestCase.php b/src/Illuminate/Foundation/Testing/TestCase.php index 8bae3bcdb2e7..553b0aacd97f 100644 --- a/src/Illuminate/Foundation/Testing/TestCase.php +++ b/src/Illuminate/Foundation/Testing/TestCase.php @@ -15,6 +15,8 @@ use PHPUnit\Framework\TestCase as BaseTestCase; use Throwable; +use function class_uses_recursive; + abstract class TestCase extends BaseTestCase { use Concerns\InteractsWithContainer, @@ -115,7 +117,33 @@ protected function refreshApplication() */ protected function setUpTraits() { - foreach ($uses = class_uses_recursive(static::class) as $trait) { + $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(); + } + + foreach ($uses as $trait => $int) { if (method_exists($this, $method = 'setUp'.$trait)) { $this->{$method}(); } diff --git a/src/Illuminate/Foundation/Testing/WithFaker.php b/src/Illuminate/Foundation/Testing/WithFaker.php index d44955460693..cd276fbd4eb0 100644 --- a/src/Illuminate/Foundation/Testing/WithFaker.php +++ b/src/Illuminate/Foundation/Testing/WithFaker.php @@ -19,7 +19,7 @@ trait WithFaker * * @return void */ - protected function setUpWithFaker() + protected function setUpFaker() { $this->faker = $this->makeFaker(); } diff --git a/src/Illuminate/Foundation/Testing/WithoutEvents.php b/src/Illuminate/Foundation/Testing/WithoutEvents.php index 039e26287db0..fa5df3ce8f5b 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 setUpWithoutEvents() + public function disableEventsForAllTests() { if (method_exists($this, 'withoutEvents')) { $this->withoutEvents(); diff --git a/src/Illuminate/Foundation/Testing/WithoutMiddleware.php b/src/Illuminate/Foundation/Testing/WithoutMiddleware.php index cc58405c0618..269b532d3150 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 setUpWithoutMiddleware() + public function disableMiddlewareForAllTests() { if (method_exists($this, 'withoutMiddleware')) { $this->withoutMiddleware(); From e5250c07aa5b85e7cd0ab57e8d536b4f50e08051 Mon Sep 17 00:00:00 2001 From: DarkGhosthunter Date: Thu, 28 Oct 2021 18:28:31 -0300 Subject: [PATCH 6/8] Style changes. --- src/Illuminate/Foundation/Testing/TestCase.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Illuminate/Foundation/Testing/TestCase.php b/src/Illuminate/Foundation/Testing/TestCase.php index 553b0aacd97f..8943aea2b754 100644 --- a/src/Illuminate/Foundation/Testing/TestCase.php +++ b/src/Illuminate/Foundation/Testing/TestCase.php @@ -15,8 +15,6 @@ use PHPUnit\Framework\TestCase as BaseTestCase; use Throwable; -use function class_uses_recursive; - abstract class TestCase extends BaseTestCase { use Concerns\InteractsWithContainer, From 5900929d7a32832e42a8dd6dd0123b1c92d42d57 Mon Sep 17 00:00:00 2001 From: DarkGhosthunter Date: Thu, 28 Oct 2021 19:22:51 -0300 Subject: [PATCH 7/8] Changes method check from Test Class to trait. --- src/Illuminate/Foundation/Testing/TestCase.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Foundation/Testing/TestCase.php b/src/Illuminate/Foundation/Testing/TestCase.php index 8943aea2b754..46dde5f00a40 100644 --- a/src/Illuminate/Foundation/Testing/TestCase.php +++ b/src/Illuminate/Foundation/Testing/TestCase.php @@ -142,15 +142,15 @@ protected function setUpTraits() } foreach ($uses as $trait => $int) { - if (method_exists($this, $method = 'setUp'.$trait)) { + if (method_exists($trait, $method = 'setUp'.$trait)) { $this->{$method}(); } - if (method_exists($this, $method = 'created'.$trait)) { + if (method_exists($trait, $method = 'created'.$trait)) { $this->afterApplicationCreated([$this, $method]); } - if (method_exists($this, $method = 'destroying'.$trait)) { + if (method_exists($trait, $method = 'destroying'.$trait)) { $this->beforeApplicationDestroyed([$this, $method]); } } From 29e972dba373bef52899f0c6c1719a2fd1234a50 Mon Sep 17 00:00:00 2001 From: DarkGhosthunter Date: Thu, 28 Oct 2021 19:32:06 -0300 Subject: [PATCH 8/8] Reverses the use traits so deeply nested traits are executed first. --- src/Illuminate/Foundation/Testing/TestCase.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Testing/TestCase.php b/src/Illuminate/Foundation/Testing/TestCase.php index 46dde5f00a40..bfb890b3bf9d 100644 --- a/src/Illuminate/Foundation/Testing/TestCase.php +++ b/src/Illuminate/Foundation/Testing/TestCase.php @@ -141,7 +141,7 @@ protected function setUpTraits() $this->setUpFaker(); } - foreach ($uses as $trait => $int) { + foreach (array_reverse($uses) as $trait => $int) { if (method_exists($trait, $method = 'setUp'.$trait)) { $this->{$method}(); }