Skip to content
New issue

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

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

Already on GitHub? # to your account

Update test deps, fixes CI for 6.x #399

Merged
merged 3 commits into from
Jan 21, 2024
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
6 changes: 4 additions & 2 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<?php

declare(strict_types=1);
use PhpCsFixer\Config;
use PhpCsFixer\Finder;

$finder = (new PhpCsFixer\Finder())
$finder = (new Finder())
->in(__DIR__.'/src')
->notPath('DependencyInjection/Configuration.php')
->in(__DIR__.'/tests')
->exclude('App/var')
->append([__FILE__])
;

return (new PhpCsFixer\Config())
return (new Config())
->setRiskyAllowed(true)
->setRules([
'@PhpCsFixer' => true,
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ parameters:

excludePaths:
- '%currentWorkingDirectory%/src/DependencyInjection/Configuration.php'
- '%currentWorkingDirectory%/src/Routing/RoutingLoader.php'

ignoreErrors:
4 changes: 2 additions & 2 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
50 changes: 0 additions & 50 deletions src/Routing/RoutingAnnotationLoader.php

This file was deleted.

93 changes: 93 additions & 0 deletions src/Routing/RoutingLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

namespace DH\AuditorBundle\Routing;

use DH\AuditorBundle\Controller\ViewerController;
use RuntimeException;
use Symfony\Bundle\FrameworkBundle\Routing\AnnotatedRouteControllerLoader;
use Symfony\Bundle\FrameworkBundle\Routing\AttributeRouteControllerLoader;
use Symfony\Component\Config\Loader\Loader;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\Routing\RouteCollection;

if (BaseKernel::MAJOR_VERSION >= 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');

Check warning on line 33 in src/Routing/RoutingLoader.php

View check run for this annotation

Codecov / codecov/patch

src/Routing/RoutingLoader.php#L33

Added line #L33 was not covered by tests
}

$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');

Check warning on line 72 in src/Routing/RoutingLoader.php

View check run for this annotation

Codecov / codecov/patch

src/Routing/RoutingLoader.php#L72

Added line #L72 was not covered by tests
}

$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;
}
}
}
23 changes: 7 additions & 16 deletions tests/App/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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');
}
}
}
18 changes: 12 additions & 6 deletions tests/App/config/bundles.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
<?php

declare(strict_types=1);
use DH\AuditorBundle\DHAuditorBundle;
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\SecurityBundle\SecurityBundle;
use Symfony\Bundle\TwigBundle\TwigBundle;
use Twig\Extra\TwigExtraBundle\TwigExtraBundle;

return [
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['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],
];
2 changes: 1 addition & 1 deletion tests/App/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ services:
autowire: true
autoconfigure: true

DH\AuditorBundle\Tests\App\Command\CreatePostCommand:
DH\AuditorBundle\Tests\App\Command\CreatePostCommand:
13 changes: 13 additions & 0 deletions tests/App/config/services_legacy.yaml
Original file line number Diff line number Diff line change
@@ -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 }
73 changes: 42 additions & 31 deletions tests/DHAuditorBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}
}
Loading