-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TesterBundle] PHPUnit DoctrineTransactionExtension base on dama/doct…
…rine-test-bundle
- Loading branch information
Showing
13 changed files
with
226 additions
and
27 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
when@test: | ||
dama_doctrine_test: | ||
enable_static_connection: true | ||
enable_static_meta_data_cache: true | ||
enable_static_query_cache: true |
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
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
80 changes: 80 additions & 0 deletions
80
...ages/tester-bundle/PHPUnit/Extension/DoctrineTransaction/DoctrineTransactionExtension.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace Draw\Bundle\TesterBundle\PHPUnit\Extension\DoctrineTransaction; | ||
|
||
use DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticDriver; | ||
use PHPUnit\Event\TestSuite\Finished as TestSuiteFinished; | ||
use PHPUnit\Event\TestSuite\FinishedSubscriber as TestSuiteFinishedSubscriber; | ||
use PHPUnit\Event\TestSuite\Started as TestSuiteStarted; | ||
use PHPUnit\Event\TestSuite\StartedSubscriber as TestSuiteStartedSubscriber; | ||
use PHPUnit\Runner\Extension\Extension; | ||
use PHPUnit\Runner\Extension\Facade; | ||
use PHPUnit\Runner\Extension\ParameterCollection; | ||
use PHPUnit\TextUI\Configuration\Configuration; | ||
|
||
final class DoctrineTransactionExtension implements Extension | ||
{ | ||
public static bool $transactionStarted = false; | ||
|
||
public static function rollBack(): void | ||
{ | ||
if (!self::$transactionStarted) { | ||
return; | ||
} | ||
|
||
StaticDriver::rollBack(); | ||
self::$transactionStarted = false; | ||
} | ||
|
||
public static function begin(): void | ||
{ | ||
if (self::$transactionStarted) { | ||
return; | ||
} | ||
|
||
StaticDriver::beginTransaction(); | ||
self::$transactionStarted = true; | ||
} | ||
|
||
#[\Override] | ||
public function bootstrap(Configuration $configuration, Facade $facade, ParameterCollection $parameters): void | ||
{ | ||
$facade->registerSubscribers( | ||
new class() implements TestSuiteStartedSubscriber { | ||
public function notify(TestSuiteStarted $event): void | ||
{ | ||
$class = $event->testSuite()->name(); | ||
|
||
DoctrineTransactionExtension::startTransactionIfNeeded($class); | ||
} | ||
}, | ||
new class() implements TestSuiteFinishedSubscriber { | ||
public function notify(TestSuiteFinished $event): void | ||
{ | ||
DoctrineTransactionExtension::rollBack(); | ||
StaticDriver::setKeepStaticConnections(false); | ||
} | ||
}, | ||
); | ||
} | ||
|
||
private static function asNoTransaction(string $class): bool | ||
{ | ||
if (!class_exists($class)) { | ||
return false; | ||
} | ||
|
||
return (bool) \count((new \ReflectionClass($class))->getAttributes(NoTransaction::class)); | ||
} | ||
|
||
public static function startTransactionIfNeeded(string $class): void | ||
{ | ||
if (self::asNoTransaction($class)) { | ||
return; | ||
} | ||
|
||
StaticDriver::setKeepStaticConnections(true); | ||
|
||
static::begin(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/tester-bundle/PHPUnit/Extension/DoctrineTransaction/NoTransaction.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Draw\Bundle\TesterBundle\PHPUnit\Extension\DoctrineTransaction; | ||
|
||
#[\Attribute(\Attribute::TARGET_CLASS)] | ||
class NoTransaction | ||
{ | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,9 +8,8 @@ | |
use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireService; | ||
use Draw\Bundle\TesterBundle\WebTestCase; | ||
use Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire\AutowiredInterface; | ||
use PHPUnit\Framework\Attributes\AfterClass; | ||
use PHPUnit\Framework\Attributes\BeforeClass; | ||
use PHPUnit\Framework\Attributes\Depends; | ||
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions; | ||
use Symfony\Bundle\FrameworkBundle\KernelBrowser; | ||
use Symfony\Component\DomCrawler\Crawler; | ||
|
||
|
@@ -26,11 +25,9 @@ class TwoFactorAuthorizationTest extends WebTestCase implements AutowiredInterfa | |
|
||
private static User $user; | ||
|
||
public static function setUpBeforeClass(): void | ||
#[DoesNotPerformAssertions] | ||
public function testCreateUser(): void | ||
{ | ||
$entityManager = static::getContainer() | ||
->get(EntityManagerInterface::class); | ||
|
||
$user = new User(); | ||
$user->setEmail('[email protected]'); | ||
$user->setPlainPassword('test'); | ||
|
@@ -39,28 +36,13 @@ public static function setUpBeforeClass(): void | |
// This role for enabling 2fa as per configuration | ||
$user->enableTwoFActorAuthenticationProvider('totp'); | ||
|
||
$entityManager->persist($user); | ||
$entityManager->flush(); | ||
$this->entityManager->persist($user); | ||
$this->entityManager->flush(); | ||
|
||
self::$user = $user; | ||
} | ||
|
||
#[ | ||
AfterClass, | ||
BeforeClass, | ||
] | ||
public static function cleanUp(): void | ||
{ | ||
static::getContainer() | ||
->get(EntityManagerInterface::class) | ||
->createQueryBuilder() | ||
->delete(User::class, 'user') | ||
->andWhere('user.email like :email') | ||
->setParameter('email', 'test-2fa%@example.com') | ||
->getQuery() | ||
->execute(); | ||
} | ||
|
||
#[Depends('testCreateUser')] | ||
public function testLoginRedirectEnable2fa(): void | ||
{ | ||
static::assertTrue(self::$user->needToEnableTotpAuthenticationEnabled()); | ||
|
@@ -76,6 +58,7 @@ public function testLoginRedirectEnable2fa(): void | |
); | ||
} | ||
|
||
#[Depends('testCreateUser')] | ||
public function testCancel(): void | ||
{ | ||
$this->client->followRedirects(); | ||
|
@@ -102,7 +85,7 @@ public function testEnable2faInAdminInvalidCode(): void | |
self::$user->setRoles(['ROLE_2FA_ADMIN']); | ||
$this->entityManager->flush(); | ||
|
||
$crawler = $this->loginToAdmin(); | ||
$this->loginToAdmin(); | ||
|
||
$crawler = $this->client->submit( | ||
$this->client->getCrawler() | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
use App\Tests\SonataIntegrationBundle\WebTestCaseTrait; | ||
use Draw\Bundle\UserBundle\Entity\UserLock; | ||
use Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire\AutowiredInterface; | ||
use PHPUnit\Framework\Attributes\Depends; | ||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | ||
|
||
class UnlockUserActionTest extends WebTestCase implements AutowiredInterface | ||
|
@@ -46,6 +47,7 @@ public function testUnlock(): void | |
static::assertEqualsWithDelta(new \DateTimeImmutable('+ 24 hours'), $userLock->getUnlockUntil(), 2); | ||
} | ||
|
||
#[Depends('testUnlock')] | ||
public function testNoAccess(): void | ||
{ | ||
$this->login('[email protected]'); | ||
|