Skip to content

Commit c73253e

Browse files
[9.x] Adds bootable traits to TestCase (#42394)
* Boot test traits * use setUp * fix test Co-authored-by: Taylor Otwell <[email protected]>
1 parent f16544d commit c73253e

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/Illuminate/Foundation/Testing/TestCase.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ protected function setUpTraits()
143143
$this->setUpFaker();
144144
}
145145

146+
foreach ($uses as $trait) {
147+
$method = 'setUp'.class_basename($trait);
148+
149+
if (method_exists($this, $method)) {
150+
$this->{$method}();
151+
}
152+
}
153+
146154
return $uses;
147155
}
148156

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Foundation\Testing;
4+
5+
use Illuminate\Foundation\Testing\TestCase as FoundationTestCase;
6+
use Orchestra\Testbench\Concerns\CreatesApplication;
7+
use PHPUnit\Framework\TestCase;
8+
9+
trait TestTrait
10+
{
11+
public $booted = false;
12+
13+
public function setUpTestTrait()
14+
{
15+
$this->booted = true;
16+
}
17+
}
18+
19+
class TestCaseWithTrait extends FoundationTestCase
20+
{
21+
use CreatesApplication;
22+
use TestTrait;
23+
}
24+
25+
class BootTraitsTest extends TestCase
26+
{
27+
public function testSetUpTraitsWithBootMethod()
28+
{
29+
$testCase = new TestCaseWithTrait;
30+
31+
$method = new \ReflectionMethod(get_class($testCase), 'setUpTraits');
32+
tap($method)->setAccessible(true)->invoke($testCase);
33+
34+
$this->assertTrue($testCase->booted);
35+
}
36+
}

0 commit comments

Comments
 (0)