Skip to content
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ With PHPUnit you can skip this bundle's database rollback handling for specific
#[SkipDatabaseRollback] // this will skip it for all tests in a class
public class MyTest extends \PHPUnit\Framework\TestCase {}

#[SkipDatabaseRollback] // also supported on (abstract) parent classes. So all tests in child classes will skip the rollback logic.
abstract class MyAbstractTest extends \PHPUnit\Framework\TestCase {}

#[SkipDatabaseRollback] // this will skip it for only one test method
public function MyTest() {}
```
Expand Down
18 changes: 16 additions & 2 deletions src/PHPUnit/PHPUnitExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PHPUnit\Event\TestRunner\FinishedSubscriber as TestRunnerFinishedSubscriber;
use PHPUnit\Event\TestRunner\Started as TestRunnerStartedEvent;
use PHPUnit\Event\TestRunner\StartedSubscriber as TestRunnerStartedSubscriber;
use PHPUnit\Framework\TestCase;
use PHPUnit\Runner\Extension\Extension;
use PHPUnit\Runner\Extension\Facade;
use PHPUnit\Runner\Extension\ParameterCollection;
Expand Down Expand Up @@ -93,8 +94,21 @@ public static function hasSkipAttribute(Test $test): bool
return true;
}

return $reflectionClass->hasMethod($methodName = $test->methodName())
&& $reflectionClass->getMethod($methodName)->getAttributes(SkipDatabaseRollback::class);
if ($reflectionClass->hasMethod($methodName = $test->methodName())
&& $reflectionClass->getMethod($methodName)->getAttributes(SkipDatabaseRollback::class)
) {
return true;
}

while (($reflectionClass = $reflectionClass->getParentClass())
&& $reflectionClass->name !== TestCase::class
) {
if ($reflectionClass->getAttributes(SkipDatabaseRollback::class)) {
return true;
}
}

return false;
}

public function bootstrap(Configuration $configuration, Facade $facade, ParameterCollection $parameters): void
Expand Down
13 changes: 13 additions & 0 deletions tests/Functional/AbstractTestClassWithSkipAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Tests\Functional;

use DAMA\DoctrineTestBundle\PHPUnit\SkipDatabaseRollback;
use PHPUnit\Framework\TestCase;

#[SkipDatabaseRollback]
abstract class AbstractTestClassWithSkipAttribute extends TestCase
{
}
3 changes: 1 addition & 2 deletions tests/Functional/PhpunitWithClassAttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

declare(strict_types=1);

namespace Functional;
namespace Tests\Functional;

use DAMA\DoctrineTestBundle\PHPUnit\SkipDatabaseRollback;
use PHPUnit\Framework\Attributes\Depends;
use PHPUnit\Framework\TestCase;
use Tests\Functional\FunctionalTestTrait;

#[SkipDatabaseRollback]
class PhpunitWithClassAttributeTest extends TestCase
Expand Down
27 changes: 27 additions & 0 deletions tests/Functional/PhpunitWithExtendingClassAttributeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Tests\Functional;

use PHPUnit\Framework\Attributes\Depends;

class PhpunitWithExtendingClassAttributeTest extends AbstractTestClassWithSkipAttribute
{
use FunctionalTestTrait;

public function testTransactionalBehaviorDisabledWithAttributeOnClassLevel(): void
{
$this->insertRow();
$this->assertRowCount(1);
}

#[Depends('testTransactionalBehaviorDisabledWithAttributeOnClassLevel')]
public function testChangesFromPreviousTestAreVisibleWhenDisabledWithAttributeOnClassLevel(): void
{
$this->assertRowCount(1);

// cleanup persisted row to not affect any other tests afterwards
$this->connection->executeQuery('DELETE FROM test');
}
}