From 7bf9ec6210a70584f076991d61933890a32aed48 Mon Sep 17 00:00:00 2001 From: DarkGhosthunter Date: Fri, 3 Dec 2021 00:21:12 -0300 Subject: [PATCH 1/2] Adds first-level test traits setUp/tearDown (opt-in). --- .../Contracts/Testing/InitializeTraits.php | 8 +++++++ .../Foundation/Testing/TestCase.php | 23 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 src/Illuminate/Contracts/Testing/InitializeTraits.php diff --git a/src/Illuminate/Contracts/Testing/InitializeTraits.php b/src/Illuminate/Contracts/Testing/InitializeTraits.php new file mode 100644 index 000000000000..f558b6630d03 --- /dev/null +++ b/src/Illuminate/Contracts/Testing/InitializeTraits.php @@ -0,0 +1,8 @@ +initializeFirstLevelTraits(); + } + Model::setEventDispatcher($this->app['events']); $this->setUpHasRun = true; @@ -145,6 +150,24 @@ protected function setUpTraits() return $uses; } + /** + * Initialize first-level testing traits and register their tear-down callbacks. + * + * @return void + */ + protected function initializeFirstLevelTraits() + { + foreach (class_uses($this) as $trait) { + if (method_exists($this, $setUpMethod = 'setUp'.class_basename($trait))) { + $this->{$setUpMethod}(); + } + + if (method_exists($this, $tearDownMethod = 'tearDown'.class_basename($trait))) { + $this->beforeApplicationDestroyed([$this, $tearDownMethod]); + } + } + } + /** * Clean up the testing environment before the next test. * From 01de3d65e6b1e087a1e29183b1b0bc44216d055d Mon Sep 17 00:00:00 2001 From: DarkGhosthunter Date: Fri, 3 Dec 2021 00:37:18 -0300 Subject: [PATCH 2/2] Ensures late binding for getting traits. --- 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 89f5439ffade..2b286fc41052 100644 --- a/src/Illuminate/Foundation/Testing/TestCase.php +++ b/src/Illuminate/Foundation/Testing/TestCase.php @@ -157,7 +157,7 @@ protected function setUpTraits() */ protected function initializeFirstLevelTraits() { - foreach (class_uses($this) as $trait) { + foreach (class_uses(static::class) as $trait) { if (method_exists($this, $setUpMethod = 'setUp'.class_basename($trait))) { $this->{$setUpMethod}(); }