From ad99d013a1b485169f3e7f8def44ac325323cc45 Mon Sep 17 00:00:00 2001 From: Damien Harper Date: Sun, 21 Jan 2024 17:58:48 +0100 Subject: [PATCH] Update test deps, fixes CI for 6.x (#399) * Update test deps, fixes CI for 6.x * PHP-CS-Fixer (3.48.0) + route loader fixes * Fixed compatibility with sf5/6/7 --- .php-cs-fixer.php | 6 +- composer.json | 2 +- phpstan.neon | 1 + src/Resources/config/services.yaml | 4 +- src/Routing/RoutingAnnotationLoader.php | 50 ------------- src/Routing/RoutingLoader.php | 93 +++++++++++++++++++++++++ tests/App/Kernel.php | 23 ++---- tests/App/config/bundles.php | 18 +++-- tests/App/config/services.yaml | 2 +- tests/App/config/services_legacy.yaml | 13 ++++ tests/DHAuditorBundleTest.php | 73 ++++++++++--------- tests/User/UserProviderTest.php | 4 +- 12 files changed, 178 insertions(+), 111 deletions(-) delete mode 100644 src/Routing/RoutingAnnotationLoader.php create mode 100644 src/Routing/RoutingLoader.php create mode 100644 tests/App/config/services_legacy.yaml diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php index e8536319..d766c38c 100644 --- a/.php-cs-fixer.php +++ b/.php-cs-fixer.php @@ -1,8 +1,10 @@ in(__DIR__.'/src') ->notPath('DependencyInjection/Configuration.php') ->in(__DIR__.'/tests') @@ -10,7 +12,7 @@ ->append([__FILE__]) ; -return (new PhpCsFixer\Config()) +return (new Config()) ->setRiskyAllowed(true) ->setRules([ '@PhpCsFixer' => true, diff --git a/composer.json b/composer.json index 5b348eef..38421d3d 100644 --- a/composer.json +++ b/composer.json @@ -39,7 +39,7 @@ "require-dev": { "gedmo/doctrine-extensions": "^2.4|^3.0", "matthiasnoback/symfony-dependency-injection-test": "^3.1|^4.0|^5.0", - "nyholm/symfony-bundle-test": "1.x-dev", + "nyholm/symfony-bundle-test": "^2.0|^3.0", "phpunit/phpunit": "^9.0", "symfony/browser-kit": "^5.4|^6.0|^7.0", "symfony/css-selector": "^5.4|^6.0|^7.0", diff --git a/phpstan.neon b/phpstan.neon index 265d1dd4..4bf5719a 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -10,5 +10,6 @@ parameters: excludePaths: - '%currentWorkingDirectory%/src/DependencyInjection/Configuration.php' + - '%currentWorkingDirectory%/src/Routing/RoutingLoader.php' ignoreErrors: diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml index 59aa34a7..8754c17a 100644 --- a/src/Resources/config/services.yaml +++ b/src/Resources/config/services.yaml @@ -26,9 +26,9 @@ services: - { name: dh_auditor.provider } dh_auditor.provider.doctrine: '@DH\Auditor\Provider\Doctrine\DoctrineProvider' - DH\AuditorBundle\Routing\RoutingAnnotationLoader: + DH\AuditorBundle\Routing\RoutingLoader: arguments: - - '@routing.loader.annotation' + - '@routing.loader.attribute' - '%dh_auditor.provider.doctrine.configuration%' tags: - { name: routing.loader } diff --git a/src/Routing/RoutingAnnotationLoader.php b/src/Routing/RoutingAnnotationLoader.php deleted file mode 100644 index 0f4010d6..00000000 --- a/src/Routing/RoutingAnnotationLoader.php +++ /dev/null @@ -1,50 +0,0 @@ -annotatedRouteControllerLoader = $annotatedRouteController; - $this->configuration = $configuration; - } - - public function load(mixed $resource, ?string $type = null): RouteCollection - { - if ($this->isLoaded) { - throw new RuntimeException('Do not add the "audit" loader twice'); - } - - $routeCollection = new RouteCollection(); - if (true === $this->configuration['viewer']) { - $routeCollection = $this->annotatedRouteControllerLoader->load(ViewerController::class); - } - - $this->isLoaded = true; - - return $routeCollection; - } - - /** - * @param ?string $type - */ - public function supports(mixed $resource, ?string $type = null): bool - { - return 'auditor' === $type; - } -} diff --git a/src/Routing/RoutingLoader.php b/src/Routing/RoutingLoader.php new file mode 100644 index 00000000..80a4f057 --- /dev/null +++ b/src/Routing/RoutingLoader.php @@ -0,0 +1,93 @@ += 6) { + class RoutingLoader extends Loader + { + private AttributeRouteControllerLoader $annotatedRouteControllerLoader; + + private bool $isLoaded = false; + + private array $configuration; + + public function __construct(AttributeRouteControllerLoader $annotatedRouteController, array $configuration) + { + $this->annotatedRouteControllerLoader = $annotatedRouteController; + $this->configuration = $configuration; + } + + public function load(mixed $resource, ?string $type = null): RouteCollection + { + if ($this->isLoaded) { + throw new RuntimeException('Do not add the "audit" loader twice'); + } + + $routeCollection = new RouteCollection(); + if (true === $this->configuration['viewer']) { + $routeCollection = $this->annotatedRouteControllerLoader->load(ViewerController::class); + } + + $this->isLoaded = true; + + return $routeCollection; + } + + /** + * @param ?string $type + */ + public function supports(mixed $resource, ?string $type = null): bool + { + return 'auditor' === $type; + } + } +} else { + class RoutingLoader extends Loader + { + private AnnotatedRouteControllerLoader $annotatedRouteControllerLoader; + + private bool $isLoaded = false; + + private array $configuration; + + public function __construct(AnnotatedRouteControllerLoader $annotatedRouteController, array $configuration) + { + $this->annotatedRouteControllerLoader = $annotatedRouteController; + $this->configuration = $configuration; + } + + public function load(mixed $resource, ?string $type = null): RouteCollection + { + if ($this->isLoaded) { + throw new RuntimeException('Do not add the "audit" loader twice'); + } + + $routeCollection = new RouteCollection(); + if (true === $this->configuration['viewer']) { + $routeCollection = $this->annotatedRouteControllerLoader->load(ViewerController::class); + } + + $this->isLoaded = true; + + return $routeCollection; + } + + /** + * @param ?string $type + */ + public function supports(mixed $resource, ?string $type = null): bool + { + return 'auditor' === $type; + } + } +} diff --git a/tests/App/Kernel.php b/tests/App/Kernel.php index 764ab490..94978ddf 100644 --- a/tests/App/Kernel.php +++ b/tests/App/Kernel.php @@ -10,9 +10,8 @@ use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Kernel as BaseKernel; use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator; -use Symfony\Component\Routing\RouteCollectionBuilder; -if (6 === BaseKernel::MAJOR_VERSION) { +if (BaseKernel::MAJOR_VERSION >= 6) { class Kernel extends BaseKernel { use MicroKernelTrait; @@ -51,12 +50,7 @@ protected function configureRoutes(RoutingConfigurator $routes): void $confDir = $this->getProjectDir().'/config'; $routes->import($confDir.'/routes/*'.self::CONFIG_EXTS, 'glob'); - - if (6 > BaseKernel::MAJOR_VERSION) { - $routes->import($confDir.'/routes/sf4_5/*'.self::CONFIG_EXTS, 'glob'); - } else { - $routes->import($confDir.'/routes/sf6_7/*'.self::CONFIG_EXTS, 'glob'); - } + $routes->import($confDir.'/routes/sf6_7/*'.self::CONFIG_EXTS, 'glob'); } } } else { @@ -88,20 +82,17 @@ protected function configureContainer(ContainerBuilder $container, LoaderInterfa $container->setParameter('container.dumper.inline_factories', true); $confDir = $this->getProjectDir().'/config'; - $loader->load($confDir.'/services'.self::CONFIG_EXTS, 'glob'); + $loader->load($confDir.'/services_legacy'.self::CONFIG_EXTS, 'glob'); $loader->load($confDir.'/{packages}/*'.self::CONFIG_EXTS, 'glob'); - if (6 > BaseKernel::MAJOR_VERSION) { - $loader->load($confDir.'/{packages}/sf4_5/*'.self::CONFIG_EXTS, 'glob'); - } else { - $loader->load($confDir.'/{packages}/sf6_7/*'.self::CONFIG_EXTS, 'glob'); - } + $loader->load($confDir.'/{packages}/sf4_5/*'.self::CONFIG_EXTS, 'glob'); } - protected function configureRoutes(RouteCollectionBuilder $routes): void + protected function configureRoutes(RoutingConfigurator $routes): void { $confDir = $this->getProjectDir().'/config'; - $routes->import($confDir.'/routes/*'.self::CONFIG_EXTS, '/', 'glob'); + $routes->import($confDir.'/routes/*'.self::CONFIG_EXTS, 'glob'); + $routes->import($confDir.'/routes/sf4_5/*'.self::CONFIG_EXTS, 'glob'); } } } diff --git a/tests/App/config/bundles.php b/tests/App/config/bundles.php index 6a9c8f6a..9d02f34d 100644 --- a/tests/App/config/bundles.php +++ b/tests/App/config/bundles.php @@ -1,12 +1,18 @@ ['all' => true], - Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true], - Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true], - Symfony\Bundle\SecurityBundle\SecurityBundle::class => ['all' => true], - DH\AuditorBundle\DHAuditorBundle::class => ['all' => true], - Twig\Extra\TwigExtraBundle\TwigExtraBundle::class => ['all' => true], + FrameworkBundle::class => ['all' => true], + DoctrineBundle::class => ['all' => true], + TwigBundle::class => ['all' => true], + SecurityBundle::class => ['all' => true], + DHAuditorBundle::class => ['all' => true], + TwigExtraBundle::class => ['all' => true], ]; diff --git a/tests/App/config/services.yaml b/tests/App/config/services.yaml index 0c94e5eb..220c6f08 100644 --- a/tests/App/config/services.yaml +++ b/tests/App/config/services.yaml @@ -3,4 +3,4 @@ services: autowire: true autoconfigure: true - DH\AuditorBundle\Tests\App\Command\CreatePostCommand: \ No newline at end of file + DH\AuditorBundle\Tests\App\Command\CreatePostCommand: diff --git a/tests/App/config/services_legacy.yaml b/tests/App/config/services_legacy.yaml new file mode 100644 index 00000000..2c322276 --- /dev/null +++ b/tests/App/config/services_legacy.yaml @@ -0,0 +1,13 @@ +services: + _defaults: + autowire: true + autoconfigure: true + + DH\AuditorBundle\Tests\App\Command\CreatePostCommand: + + DH\AuditorBundle\Routing\RoutingLoader: + arguments: + - '@routing.loader.annotation' + - '%dh_auditor.provider.doctrine.configuration%' + tags: + - { name: routing.loader } diff --git a/tests/DHAuditorBundleTest.php b/tests/DHAuditorBundleTest.php index 48efdf82..e98d90d7 100644 --- a/tests/DHAuditorBundleTest.php +++ b/tests/DHAuditorBundleTest.php @@ -15,48 +15,42 @@ use DH\AuditorBundle\DHAuditorBundle; use DH\AuditorBundle\Event\ConsoleEventSubscriber; use Doctrine\Bundle\DoctrineBundle\DoctrineBundle; -use Nyholm\BundleTest\BaseBundleTestCase; -use Nyholm\BundleTest\CompilerPass\PublicServicePass; +use Nyholm\BundleTest\TestKernel; +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\Bundle\SecurityBundle\SecurityBundle; use Symfony\Bundle\TwigBundle\TwigBundle; use Symfony\Component\HttpKernel\Kernel; +use Symfony\Component\HttpKernel\KernelInterface; /** * @internal * * @small */ -final class DHAuditorBundleTest extends BaseBundleTestCase +final class DHAuditorBundleTest extends KernelTestCase { - protected function setUp(): void - { - parent::setUp(); - - // Make services public - $this->addCompilerPass(new PublicServicePass('#^(DH\\\\Auditor(Bundle)?\\\\|dh_auditor\.).*$#')); - } - public function testInitBundle(): void { - $kernel = $this->createKernel(); - - $kernel->addConfigFile(__DIR__.'/Fixtures/Resources/config/dh_auditor.yaml'); - $kernel->addConfigFile(__DIR__.'/Fixtures/Resources/config/doctrine.yaml'); - if (6 > Kernel::MAJOR_VERSION) { - $kernel->addConfigFile(__DIR__.'/Fixtures/Resources/config/sf4_5/security.yaml'); - // $kernel->addConfigFile(__DIR__ . '/Fixtures/Resources/routes/sf4_5/annotations.yaml'); - } else { - $kernel->addConfigFile(__DIR__.'/Fixtures/Resources/config/sf6_7/security.yaml'); - // $kernel->addConfigFile(__DIR__ . '/Fixtures/Resources/routes/sf6_7/attributes.yaml'); - } - - $kernel->addBundle(DoctrineBundle::class); - $kernel->addBundle(SecurityBundle::class); - $kernel->addBundle(TwigBundle::class); - - $this->bootKernel(); - - $container = $this->getContainer(); + // Boot the kernel with a config closure, the handleOptions call in createKernel is important for that to work + $kernel = self::bootKernel(['config' => static function (TestKernel $kernel) { + // Add some other bundles we depend on + $kernel->addTestBundle(DoctrineBundle::class); + $kernel->addTestBundle(SecurityBundle::class); + $kernel->addTestBundle(TwigBundle::class); + + // Add some configuration + $kernel->addTestConfig(__DIR__.'/Fixtures/Resources/config/dh_auditor.yaml'); + $kernel->addTestConfig(__DIR__.'/Fixtures/Resources/config/doctrine.yaml'); + if (Kernel::MAJOR_VERSION < 6) { + $kernel->addTestConfig(__DIR__.'/App/config/services_legacy.yaml'); + $kernel->addTestConfig(__DIR__.'/Fixtures/Resources/config/sf4_5/security.yaml'); + } else { + $kernel->addTestConfig(__DIR__.'/Fixtures/Resources/config/sf6_7/security.yaml'); + } + }]); + + // Get the container + $container = self::getContainer(); self::assertTrue($container->has(AuditorConfiguration::class)); self::assertInstanceOf(AuditorConfiguration::class, $container->get(AuditorConfiguration::class)); @@ -89,8 +83,25 @@ public function testInitBundle(): void self::assertInstanceOf(ConsoleEventSubscriber::class, $container->get(ConsoleEventSubscriber::class)); } - protected function getBundleClass() + protected function getBundleClass(): string { return DHAuditorBundle::class; } + + protected static function getKernelClass(): string + { + return TestKernel::class; + } + + protected static function createKernel(array $options = []): KernelInterface + { + /** + * @var TestKernel $kernel + */ + $kernel = parent::createKernel($options); + $kernel->addTestBundle(DHAuditorBundle::class); + $kernel->handleOptions($options); + + return $kernel; + } } diff --git a/tests/User/UserProviderTest.php b/tests/User/UserProviderTest.php index 6f4298f1..d7c8be82 100644 --- a/tests/User/UserProviderTest.php +++ b/tests/User/UserProviderTest.php @@ -59,7 +59,7 @@ public function testBlameUser(): void $firewallName = 'main'; - if (6 === Kernel::MAJOR_VERSION) { + if (Kernel::MAJOR_VERSION >= 6) { $token = new UsernamePasswordToken($user, $firewallName, $user->getRoles()); } else { $token = new UsernamePasswordToken($user, null, $firewallName, $user->getRoles()); @@ -92,7 +92,7 @@ public function testBlameImpersonator(): void $firewallName = 'main'; - if (6 === Kernel::MAJOR_VERSION) { + if (Kernel::MAJOR_VERSION >= 6) { $userToken = new UsernamePasswordToken($user, $firewallName, $user->getRoles()); $token = new SwitchUserToken($secondUser, $firewallName, $secondUser->getRoles(), $userToken); } else {