Skip to content

Commit

Permalink
Add the ability to use setUp and tearDown methods in traits
Browse files Browse the repository at this point in the history
  • Loading branch information
msmakouz committed Sep 28, 2023
1 parent c217343 commit f4b1111
Show file tree
Hide file tree
Showing 11 changed files with 220 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ protected function setUp(): void
$variables = [...static::ENV, ...$this->getEnvVariablesFromConfig()];
$this->initApp($variables);
}

$this->setUpTraits();
}

public function beforeBooting(Closure $callback): void
Expand Down Expand Up @@ -168,6 +170,8 @@ protected function tearDown(): void
{
parent::tearDown();

$this->tearDownTraits();

(new \ReflectionClass(ContainerScope::class))
->setStaticPropertyValue('container', null);
}
Expand Down Expand Up @@ -195,4 +199,26 @@ protected function getTestAttributes(string $attribute, string $method = null):
return [];
}
}

protected function setUpTraits(): void
{
$ref = new \ReflectionClass(static::class);

foreach ($ref->getTraits() as $trait) {
if (\method_exists($this, $method = 'setUp' . $trait->getShortName())) {
$this->{$method}();
}
}
}

protected function tearDownTraits(): void
{
$ref = new \ReflectionClass(static::class);

foreach ($ref->getTraits() as $trait) {
if (\method_exists($this, $method = 'tearDown' . $trait->getShortName())) {
$this->{$method}();
}
}
}
}
21 changes: 21 additions & 0 deletions tests/src/TestCase/Fixture/Trait/WithMethodsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Spiral\Testing\Tests\TestCase\Fixture\Trait;

trait WithMethodsTrait
{
public bool $calledSetUp = false;
public bool $calledTearDown = false;

public function setUpWithMethodsTrait(): void
{
$this->calledSetUp = true;
}

public function tearDownWithMethodsTrait(): void
{
$this->calledTearDown = true;
}
}
15 changes: 15 additions & 0 deletions tests/src/TestCase/Fixture/Trait/WithSetUpTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Spiral\Testing\Tests\TestCase\Fixture\Trait;

trait WithSetUpTrait
{
public bool $calledSetUp = false;

public function setUpWithSetUpTrait(): void
{
$this->calledSetUp = true;
}
}
15 changes: 15 additions & 0 deletions tests/src/TestCase/Fixture/Trait/WithTearDownTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Spiral\Testing\Tests\TestCase\Fixture\Trait;

trait WithTearDownTrait
{
public bool $calledTearDown = false;

public function tearDownWithTearDownTrait(): void
{
$this->calledTearDown = true;
}
}
13 changes: 13 additions & 0 deletions tests/src/TestCase/Fixture/Trait/WithoutMethodsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Spiral\Testing\Tests\TestCase\Fixture\Trait;

trait WithoutMethodsTrait
{
public function isAvailable(): bool
{
return true;
}
}
13 changes: 13 additions & 0 deletions tests/src/TestCase/Fixture/WithMethods.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Spiral\Testing\Tests\TestCase\Fixture;

use Spiral\Testing\TestCase;
use Spiral\Testing\Tests\TestCase\Fixture\Trait\WithMethodsTrait;

final class WithMethods extends TestCase
{
use WithMethodsTrait;
}
13 changes: 13 additions & 0 deletions tests/src/TestCase/Fixture/WithSetUp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Spiral\Testing\Tests\TestCase\Fixture;

use Spiral\Testing\TestCase;
use Spiral\Testing\Tests\TestCase\Fixture\Trait\WithSetUpTrait;

final class WithSetUp extends TestCase
{
use WithSetUpTrait;
}
13 changes: 13 additions & 0 deletions tests/src/TestCase/Fixture/WithTearDown.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Spiral\Testing\Tests\TestCase\Fixture;

use Spiral\Testing\TestCase;
use Spiral\Testing\Tests\TestCase\Fixture\Trait\WithTearDownTrait;

final class WithTearDown extends TestCase
{
use WithTearDownTrait;
}
13 changes: 13 additions & 0 deletions tests/src/TestCase/Fixture/WithoutMethods.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Spiral\Testing\Tests\TestCase\Fixture;

use Spiral\Testing\TestCase;
use Spiral\Testing\Tests\TestCase\Fixture\Trait\WithoutMethodsTrait;

final class WithoutMethods extends TestCase
{
use WithoutMethodsTrait;
}
11 changes: 11 additions & 0 deletions tests/src/TestCase/Fixture/WithoutTraits.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Spiral\Testing\Tests\TestCase\Fixture;

use Spiral\Testing\TestCase;

final class WithoutTraits extends TestCase
{
}
67 changes: 67 additions & 0 deletions tests/src/TestCase/TestCaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace Spiral\Testing\Tests\TestCase;

use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
use PHPUnit\Framework\TestCase;
use Spiral\Testing\Tests\TestCase\Fixture\WithMethods;
use Spiral\Testing\Tests\TestCase\Fixture\WithoutMethods;
use Spiral\Testing\Tests\TestCase\Fixture\WithoutTraits;
use Spiral\Testing\Tests\TestCase\Fixture\WithSetUp;
use Spiral\Testing\Tests\TestCase\Fixture\WithTearDown;

final class TestCaseTest extends TestCase
{
#[DoesNotPerformAssertions]
public function testItDoesNotThrowWhenCallingSetUp(): void
{
$testCase = new WithoutTraits('foo');
$testCase->setUp();
}

#[DoesNotPerformAssertions]
public function testItDoesNotThrowWhenCallingTearDown(): void
{
$testCase = new WithoutTraits('foo');
$testCase->tearDown();
}

public function testTraitWithoutMethods(): void
{
$testCase = new WithoutMethods('foo');
$testCase->setUp();
$testCase->tearDown();

$this->assertTrue($testCase->isAvailable());
}

public function testTraitWithSetUp(): void
{
$testCase = new WithSetUp('foo');
$testCase->setUp();
$testCase->tearDown();

$this->assertTrue($testCase->calledSetUp);
}

public function testTraitWithTearDown(): void
{
$testCase = new WithTearDown('foo');
$testCase->setUp();
$testCase->tearDown();

$this->assertTrue($testCase->calledTearDown);
}

public function testTraitWithSetUpAndTearDownMethods(): void
{
$testCase = new WithMethods('foo');
$testCase->setUp();
$testCase->tearDown();

$this->assertTrue($testCase->calledSetUp);
$this->assertTrue($testCase->calledTearDown);
}
}

0 comments on commit f4b1111

Please sign in to comment.