Skip to content

Commit

Permalink
Allow bootable test traits to teardown. (#42521)
Browse files Browse the repository at this point in the history
  • Loading branch information
ziadoz authored May 26, 2022
1 parent cc23b2e commit a4723ec
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
8 changes: 5 additions & 3 deletions src/Illuminate/Foundation/Testing/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,13 @@ protected function setUpTraits()
}

foreach ($uses as $trait) {
$method = 'setUp'.class_basename($trait);

if (method_exists($this, $method)) {
if (method_exists($this, $method = 'setUp'.class_basename($trait))) {
$this->{$method}();
}

if (method_exists($this, $method = 'tearDown'.class_basename($trait))) {
$this->beforeApplicationDestroyed(fn () => $this->{$method}());
}
}

return $uses;
Expand Down
21 changes: 16 additions & 5 deletions tests/Foundation/Testing/BootTraitsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@

trait TestTrait
{
public $booted = false;
public $setUp = false;
public $tearDown = false;

public function setUpTestTrait()
{
$this->booted = true;
$this->setUp = true;
}

public function tearDownTestTrait()
{
$this->tearDown = true;
}
}

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

class BootTraitsTest extends TestCase
{
public function testSetUpTraitsWithBootMethod()
public function testSetUpAndTearDownTraits()
{
$testCase = new TestCaseWithTrait;

$method = new \ReflectionMethod(get_class($testCase), 'setUpTraits');
$method = new \ReflectionMethod($testCase, 'setUpTraits');
tap($method)->setAccessible(true)->invoke($testCase);

$this->assertTrue($testCase->setUp);

$method = new \ReflectionMethod($testCase, 'callBeforeApplicationDestroyedCallbacks');
tap($method)->setAccessible(true)->invoke($testCase);

$this->assertTrue($testCase->booted);
$this->assertTrue($testCase->tearDown);
}
}

0 comments on commit a4723ec

Please sign in to comment.