Last active
March 21, 2020 14:37
-
-
Save Gummibeer/17de73a8d8144371a1724df5e5399340 to your computer and use it in GitHub Desktop.
phpunit testcase auto setup/teardown traits
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Tests; | |
use Illuminate\Foundation\Testing\TestCase as BaseTestCase; | |
use JMac\Testing\Traits\HttpTestAssertions; | |
use Tests\Utils\CreateUploadedFileFromFilePath; | |
use Tests\Utils\IsRoutesAware; | |
use Tests\Utils\ResourceAsserts; | |
abstract class TestCase extends BaseTestCase | |
{ | |
use CreatesApplication, HttpTestAssertions, ResourceAsserts, IsRoutesAware, CreateUploadedFileFromFilePath; | |
public function setUp(): void | |
{ | |
parent::setUp(); | |
$this->runTraitMethods('setUp'); | |
} | |
public function tearDown(): void | |
{ | |
parent::tearDown(); | |
$this->runTraitMethods('tearDown'); | |
} | |
protected function runTraitMethods(string $prefix): void | |
{ | |
$booted = []; | |
foreach (class_uses_recursive($this) as $trait) { | |
$method = $prefix.class_basename($trait).'Trait'; | |
if (method_exists($this, $method) && ! in_array($method, $booted)) { | |
call_user_func([$this, $method]); | |
$booted[] = $method; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment