Skip to content

Commit a4723ec

Browse files
authored
Allow bootable test traits to teardown. (#42521)
1 parent cc23b2e commit a4723ec

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

src/Illuminate/Foundation/Testing/TestCase.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,13 @@ protected function setUpTraits()
144144
}
145145

146146
foreach ($uses as $trait) {
147-
$method = 'setUp'.class_basename($trait);
148-
149-
if (method_exists($this, $method)) {
147+
if (method_exists($this, $method = 'setUp'.class_basename($trait))) {
150148
$this->{$method}();
151149
}
150+
151+
if (method_exists($this, $method = 'tearDown'.class_basename($trait))) {
152+
$this->beforeApplicationDestroyed(fn () => $this->{$method}());
153+
}
152154
}
153155

154156
return $uses;

tests/Foundation/Testing/BootTraitsTest.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,17 @@
88

99
trait TestTrait
1010
{
11-
public $booted = false;
11+
public $setUp = false;
12+
public $tearDown = false;
1213

1314
public function setUpTestTrait()
1415
{
15-
$this->booted = true;
16+
$this->setUp = true;
17+
}
18+
19+
public function tearDownTestTrait()
20+
{
21+
$this->tearDown = true;
1622
}
1723
}
1824

@@ -24,13 +30,18 @@ class TestCaseWithTrait extends FoundationTestCase
2430

2531
class BootTraitsTest extends TestCase
2632
{
27-
public function testSetUpTraitsWithBootMethod()
33+
public function testSetUpAndTearDownTraits()
2834
{
2935
$testCase = new TestCaseWithTrait;
3036

31-
$method = new \ReflectionMethod(get_class($testCase), 'setUpTraits');
37+
$method = new \ReflectionMethod($testCase, 'setUpTraits');
38+
tap($method)->setAccessible(true)->invoke($testCase);
39+
40+
$this->assertTrue($testCase->setUp);
41+
42+
$method = new \ReflectionMethod($testCase, 'callBeforeApplicationDestroyedCallbacks');
3243
tap($method)->setAccessible(true)->invoke($testCase);
3344

34-
$this->assertTrue($testCase->booted);
45+
$this->assertTrue($testCase->tearDown);
3546
}
3647
}

0 commit comments

Comments
 (0)