From 3f31c91b9406ef5c2ffc005d15241f8798bce6ff Mon Sep 17 00:00:00 2001 From: Damien Harper Date: Wed, 14 Dec 2022 19:13:04 +0100 Subject: [PATCH] Adjusted middleware registration priority (fixed failing tests) --- composer.json | 10 +++++----- src/DHAuditorBundle.php | 3 ++- tests/App/Command/CreatePostCommand.php | 14 ++++---------- tests/Console/ConsoleUserProviderTest.php | 6 ++++-- tests/User/UserProviderTest.php | 4 ++++ 5 files changed, 19 insertions(+), 18 deletions(-) diff --git a/composer.json b/composer.json index b65dc7c3..fa557cb7 100644 --- a/composer.json +++ b/composer.json @@ -42,11 +42,11 @@ "gedmo/doctrine-extensions": "^2.4|^3.0", "matthiasnoback/symfony-dependency-injection-test": "^3.1|^4.0", "nyholm/symfony-bundle-test": "1.x-dev", - "phpunit/phpunit": "^8.0|^9.0", - "symfony/browser-kit": "^4.0|^5.1|^6.0", - "symfony/css-selector": "^5.1|^6.0", - "symfony/framework-bundle": "^4.4|^5.0|^6.0", - "symfony/var-dumper": "^4.0|^5.0|^6.0", + "phpunit/phpunit": "^9.0", + "symfony/browser-kit": "^5.4|^6.0", + "symfony/css-selector": "^5.4|^6.0", + "symfony/framework-bundle": "^5.4|^6.0", + "symfony/var-dumper": "^5.4|^6.0", "symfony/webpack-encore-bundle": "^1.12", "twig/extensions": "^1.5", "doctrine/data-fixtures": "^1.4" diff --git a/src/DHAuditorBundle.php b/src/DHAuditorBundle.php index 0d55d7d2..8221eb9b 100644 --- a/src/DHAuditorBundle.php +++ b/src/DHAuditorBundle.php @@ -7,6 +7,7 @@ use DH\AuditorBundle\DependencyInjection\Compiler\AddProviderCompilerPass; use DH\AuditorBundle\DependencyInjection\Compiler\CustomConfigurationCompilerPass; use DH\AuditorBundle\DependencyInjection\Compiler\DoctrineProviderConfigurationCompilerPass; +use Symfony\Component\DependencyInjection\Compiler\PassConfig; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; @@ -20,7 +21,7 @@ public function build(ContainerBuilder $container): void parent::build($container); $container->addCompilerPass(new AddProviderCompilerPass()); - $container->addCompilerPass(new DoctrineProviderConfigurationCompilerPass()); + $container->addCompilerPass(new DoctrineProviderConfigurationCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 1); $container->addCompilerPass(new CustomConfigurationCompilerPass()); } } diff --git a/tests/App/Command/CreatePostCommand.php b/tests/App/Command/CreatePostCommand.php index aee89d56..0d4b649d 100644 --- a/tests/App/Command/CreatePostCommand.php +++ b/tests/App/Command/CreatePostCommand.php @@ -11,7 +11,6 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; -use Symfony\Component\HttpKernel\Kernel; class CreatePostCommand extends Command { @@ -32,7 +31,6 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle($input, $output); - $io->write('Creating a new post'); $post = new Post(); @@ -42,14 +40,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int ->setCreatedAt(new DateTimeImmutable('2020-01-17 22:17:34')) ; - $this->doctrineProvider->getAuditingServiceForEntity(Post::class)->getEntityManager()->persist($post); - $this->doctrineProvider->getAuditingServiceForEntity(Post::class)->getEntityManager()->flush(); - - // Symfony 4 compatibility - if (Kernel::MAJOR_VERSION >= 5) { - return self::SUCCESS; - } + $entityManager = $this->doctrineProvider->getAuditingServiceForEntity(Post::class)->getEntityManager(); + $entityManager->persist($post); + $entityManager->flush(); - return 0; + return self::SUCCESS; } } diff --git a/tests/Console/ConsoleUserProviderTest.php b/tests/Console/ConsoleUserProviderTest.php index 01da3814..382c06ea 100644 --- a/tests/Console/ConsoleUserProviderTest.php +++ b/tests/Console/ConsoleUserProviderTest.php @@ -60,10 +60,12 @@ public function testBlameUser(): void } $this->flushAll($auditingServices); + // get history $entries = $this->createReader()->createQuery(Post::class)->execute(); - self::assertSame('app:post:create', $entries[0]->getUsername()); - self::assertSame('command', $entries[0]->getUserId()); + self::assertNotEmpty($entries, 'There are audit entries'); + self::assertSame('app:post:create', $entries[0]->getUsername(), 'Username is OK'); + self::assertSame('command', $entries[0]->getUserId(), 'User ID is OK'); } protected function getBundleClass() diff --git a/tests/User/UserProviderTest.php b/tests/User/UserProviderTest.php index 00f9b8bb..5a3b1efa 100644 --- a/tests/User/UserProviderTest.php +++ b/tests/User/UserProviderTest.php @@ -11,6 +11,7 @@ use DH\Auditor\Tests\Provider\Doctrine\Traits\Schema\BlogSchemaSetupTrait; use DH\AuditorBundle\DHAuditorBundle; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; +use Symfony\Component\HttpKernel\HttpKernelBrowser; use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken; use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; @@ -29,6 +30,7 @@ final class UserProviderTest extends WebTestCase use ReaderTrait; private DoctrineProvider $provider; + private HttpKernelBrowser $client; protected function setUp(): void { @@ -78,6 +80,7 @@ public function testBlameUser(): void ; $auditingServices[Post::class]->getEntityManager()->persist($post); $this->flushAll($auditingServices); + // get history $entries = $this->createReader()->createQuery(Post::class)->execute(); self::assertSame('dark.vador', $entries[0]->getUsername()); @@ -117,6 +120,7 @@ public function testBlameImpersonator(): void ; $auditingServices[Post::class]->getEntityManager()->persist($post); $this->flushAll($auditingServices); + // get history $entries = $this->createReader()->createQuery(Post::class)->execute(); self::assertSame('second_user[impersonator dark.vador]', $entries[0]->getUsername());