Skip to content

Instantly share code, notes, and snippets.

@Gummibeer
Last active March 21, 2020 14:37
Show Gist options
  • Save Gummibeer/17de73a8d8144371a1724df5e5399340 to your computer and use it in GitHub Desktop.
Save Gummibeer/17de73a8d8144371a1724df5e5399340 to your computer and use it in GitHub Desktop.
phpunit testcase auto setup/teardown traits
<?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