Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the ability to use setUp and tearDown methods in traits #62

Merged
merged 2 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 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,35 @@ protected function getTestAttributes(string $attribute, string $method = null):
return [];
}
}

protected function setUpTraits(): void
{
$this->runTraitSetUpOrTearDown('setUp');
}

protected function tearDownTraits(): void
{
$this->runTraitSetUpOrTearDown('tearDown');
}

private function runTraitSetUpOrTearDown(string $method): void
{
$ref = new \ReflectionClass(static::class);

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

while($parent = $ref->getParentClass()) {
foreach ($parent->getTraits() as $trait) {
if (\method_exists($this, $name = $method . $trait->getShortName())) {
$this->{$name}();
}
}

$ref = $parent;
}
}
}
9 changes: 9 additions & 0 deletions tests/src/TestCase/Fixture/OtherParentClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Spiral\Testing\Tests\TestCase\Fixture;

class OtherParentClass extends ParentClass
{
}
13 changes: 13 additions & 0 deletions tests/src/TestCase/Fixture/ParentClass.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;

class ParentClass extends TestCase
{
use WithMethodsTrait;
}
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;
}
9 changes: 9 additions & 0 deletions tests/src/TestCase/Fixture/WithMethodsInNestedParent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Spiral\Testing\Tests\TestCase\Fixture;

final class WithMethodsInNestedParent extends OtherParentClass
{
}
9 changes: 9 additions & 0 deletions tests/src/TestCase/Fixture/WithMethodsInParent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Spiral\Testing\Tests\TestCase\Fixture;

final class WithMethodsInParent extends ParentClass
{
}
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
{
}
95 changes: 95 additions & 0 deletions tests/src/TestCase/TestCaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?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\WithMethodsInNestedParent;
use Spiral\Testing\Tests\TestCase\Fixture\WithMethodsInParent;
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
*/
#[DoesNotPerformAssertions]
public function testItDoesNotThrowWhenCallingSetUp(): void
{
$testCase = new WithoutTraits('foo');
$testCase->setUp();
}

/**
* @doesNotPerformAssertions
*/
#[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);
}

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

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

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

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