From 9bdfa12b0a9116622447da1afd5ea45cce0ee896 Mon Sep 17 00:00:00 2001 From: Fran Moreno Date: Thu, 14 Jan 2021 01:10:54 +0100 Subject: [PATCH 1/4] Add AdminMakerCompilerPass This fixes when the third argument of AdminMaker is a service name instead of a class. It retrieves the service name and replaces it with the class name. --- .../Compiler/AdminMakerCompilerPass.php | 42 ++++++++ src/SonataAdminBundle.php | 2 + .../Compiler/AdminMakerCompilerPassTest.php | 100 ++++++++++++++++++ tests/SonataAdminBundleTest.php | 10 +- 4 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 src/DependencyInjection/Compiler/AdminMakerCompilerPass.php create mode 100644 tests/DependencyInjection/Compiler/AdminMakerCompilerPassTest.php diff --git a/src/DependencyInjection/Compiler/AdminMakerCompilerPass.php b/src/DependencyInjection/Compiler/AdminMakerCompilerPass.php new file mode 100644 index 0000000000..275481e984 --- /dev/null +++ b/src/DependencyInjection/Compiler/AdminMakerCompilerPass.php @@ -0,0 +1,42 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\AdminBundle\DependencyInjection\Compiler; + +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +use Symfony\Component\DependencyInjection\ContainerBuilder; + +final class AdminMakerCompilerPass implements CompilerPassInterface +{ + public function process(ContainerBuilder $container): void + { + if (!$container->hasDefinition('sonata.admin.maker')) { + return; + } + + if (!$container->hasParameter('sonata.admin.configuration.default_controller')) { + return; + } + + $defaultController = $container->getParameter('sonata.admin.configuration.default_controller'); + + if (!$container->hasDefinition($defaultController)) { + return; + } + + $adminMaker = $container->getDefinition('sonata.admin.maker'); + $controllerDefinition = $container->getDefinition($defaultController); + + $adminMaker->replaceArgument(2, $controllerDefinition->getClass()); + } +} diff --git a/src/SonataAdminBundle.php b/src/SonataAdminBundle.php index 4aa333d8d2..548b32da48 100644 --- a/src/SonataAdminBundle.php +++ b/src/SonataAdminBundle.php @@ -16,6 +16,7 @@ use Mopa\Bundle\BootstrapBundle\Form\Type\TabType; use Sonata\AdminBundle\DependencyInjection\Compiler\AddDependencyCallsCompilerPass; use Sonata\AdminBundle\DependencyInjection\Compiler\AddFilterTypeCompilerPass; +use Sonata\AdminBundle\DependencyInjection\Compiler\AdminMakerCompilerPass; use Sonata\AdminBundle\DependencyInjection\Compiler\AdminSearchCompilerPass; use Sonata\AdminBundle\DependencyInjection\Compiler\ExtensionCompilerPass; use Sonata\AdminBundle\DependencyInjection\Compiler\GlobalVariablesCompilerPass; @@ -57,6 +58,7 @@ public function build(ContainerBuilder $container) $container->addCompilerPass(new ModelManagerCompilerPass()); $container->addCompilerPass(new ObjectAclManipulatorCompilerPass()); $container->addCompilerPass(new TwigStringExtensionCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 1); + $container->addCompilerPass(new AdminMakerCompilerPass()); $this->registerFormMapping(); } diff --git a/tests/DependencyInjection/Compiler/AdminMakerCompilerPassTest.php b/tests/DependencyInjection/Compiler/AdminMakerCompilerPassTest.php new file mode 100644 index 0000000000..4743e718af --- /dev/null +++ b/tests/DependencyInjection/Compiler/AdminMakerCompilerPassTest.php @@ -0,0 +1,100 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\AdminBundle\Tests\DependencyInjection\Compiler; + +use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase; +use Sonata\AdminBundle\Controller\CRUDController; +use Sonata\AdminBundle\DependencyInjection\Compiler\AdminMakerCompilerPass; +use Sonata\AdminBundle\Maker\AdminMaker; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Definition; + +final class AdminMakerCompilerPassTest extends AbstractCompilerPassTestCase +{ + public function testDoesNothingWithoutAdminMaker(): void + { + $this->compile(); + + $this->assertContainerBuilderNotHasService('sonata.admin.maker'); + } + + public function testDoesNothingWithoutDefaultControllerParameter(): void + { + $definition = new Definition(AdminMaker::class); + $definition->setArguments([ + 'dir', + [], + CRUDController::class, + ]); + $this->container->setDefinition('sonata.admin.maker', $definition); + + $this->compile(); + + $this->assertContainerBuilderHasServiceDefinitionWithArgument( + 'sonata.admin.maker', + 2, + CRUDController::class + ); + } + + public function testDoesNothingWithoutDefaultControllerNotBeingAService(): void + { + $definition = new Definition(AdminMaker::class); + $definition->setArguments([ + 'dir', + [], + CRUDController::class, + ]); + $this->container->setDefinition('sonata.admin.maker', $definition); + + $this->container->setParameter('sonata.admin.configuration.default_controller', CRUDController::class); + + $this->compile(); + + $this->assertContainerBuilderHasServiceDefinitionWithArgument( + 'sonata.admin.maker', + 2, + CRUDController::class + ); + } + + public function testReplacesTheServiceArgumentWithClassName(): void + { + $definition = new Definition(AdminMaker::class); + $definition->setArguments([ + 'dir', + [], + 'sonata.admin.controller.crud', + ]); + $this->container->setDefinition('sonata.admin.maker', $definition); + + $definition = new Definition(CRUDController::class); + $this->container->setDefinition('sonata.admin.controller.crud', $definition); + + $this->container->setParameter('sonata.admin.configuration.default_controller', 'sonata.admin.controller.crud'); + + $this->compile(); + + $this->assertContainerBuilderHasServiceDefinitionWithArgument( + 'sonata.admin.maker', + 2, + CRUDController::class + ); + } + + protected function registerCompilerPass(ContainerBuilder $container): void + { + $container->addCompilerPass(new AdminMakerCompilerPass()); + } +} diff --git a/tests/SonataAdminBundleTest.php b/tests/SonataAdminBundleTest.php index c77d787dcc..3fdd321335 100644 --- a/tests/SonataAdminBundleTest.php +++ b/tests/SonataAdminBundleTest.php @@ -16,6 +16,7 @@ use PHPUnit\Framework\TestCase; use Sonata\AdminBundle\DependencyInjection\Compiler\AddDependencyCallsCompilerPass; use Sonata\AdminBundle\DependencyInjection\Compiler\AddFilterTypeCompilerPass; +use Sonata\AdminBundle\DependencyInjection\Compiler\AdminMakerCompilerPass; use Sonata\AdminBundle\DependencyInjection\Compiler\AdminSearchCompilerPass; use Sonata\AdminBundle\DependencyInjection\Compiler\ExtensionCompilerPass; use Sonata\AdminBundle\DependencyInjection\Compiler\GlobalVariablesCompilerPass; @@ -36,7 +37,7 @@ public function testBuild(): void { $containerBuilder = $this->createMock(ContainerBuilder::class); - $containerBuilder->expects($this->exactly(8)) + $containerBuilder->expects($this->exactly(9)) ->method('addCompilerPass') ->willReturnCallback(function (CompilerPassInterface $pass, $type = PassConfig::TYPE_BEFORE_OPTIMIZATION): void { if ($pass instanceof AddDependencyCallsCompilerPass) { @@ -71,8 +72,12 @@ public function testBuild(): void return; } + if ($pass instanceof AdminMakerCompilerPass) { + return; + } + $this->fail(sprintf( - 'CompilerPass is not one of the expected types. Expects "%s", "%s", "%s", "%s", "%s", "%s" or "%s", but got "%s".', + 'CompilerPass is not one of the expected types. Expects "%s", "%s", "%s", "%s", "%s", "%s", "%s" or "%s", but got "%s".', AddDependencyCallsCompilerPass::class, AddFilterTypeCompilerPass::class, AdminSearchCompilerPass::class, @@ -81,6 +86,7 @@ public function testBuild(): void ModelManagerCompilerPass::class, ObjectAclManipulatorCompilerPass::class, TwigStringExtensionCompilerPass::class, + AdminMakerCompilerPass::class, \get_class($pass) )); }); From e904fe181f7deaa2b3070d574f41f8ca5df2396b Mon Sep 17 00:00:00 2001 From: Fran Moreno Date: Thu, 14 Jan 2021 01:12:33 +0100 Subject: [PATCH 2/4] Simplify SonataAdminBundleTest --- tests/SonataAdminBundleTest.php | 65 ++++++--------------------------- 1 file changed, 12 insertions(+), 53 deletions(-) diff --git a/tests/SonataAdminBundleTest.php b/tests/SonataAdminBundleTest.php index 3fdd321335..9adb355562 100644 --- a/tests/SonataAdminBundleTest.php +++ b/tests/SonataAdminBundleTest.php @@ -24,8 +24,6 @@ use Sonata\AdminBundle\DependencyInjection\Compiler\ObjectAclManipulatorCompilerPass; use Sonata\AdminBundle\DependencyInjection\Compiler\TwigStringExtensionCompilerPass; use Sonata\AdminBundle\SonataAdminBundle; -use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; -use Symfony\Component\DependencyInjection\Compiler\PassConfig; use Symfony\Component\DependencyInjection\ContainerBuilder; /** @@ -39,57 +37,18 @@ public function testBuild(): void $containerBuilder->expects($this->exactly(9)) ->method('addCompilerPass') - ->willReturnCallback(function (CompilerPassInterface $pass, $type = PassConfig::TYPE_BEFORE_OPTIMIZATION): void { - if ($pass instanceof AddDependencyCallsCompilerPass) { - return; - } - - if ($pass instanceof AddFilterTypeCompilerPass) { - return; - } - - if ($pass instanceof AdminSearchCompilerPass) { - return; - } - - if ($pass instanceof ExtensionCompilerPass) { - return; - } - - if ($pass instanceof GlobalVariablesCompilerPass) { - return; - } - - if ($pass instanceof ModelManagerCompilerPass) { - return; - } - - if ($pass instanceof ObjectAclManipulatorCompilerPass) { - return; - } - - if ($pass instanceof TwigStringExtensionCompilerPass) { - return; - } - - if ($pass instanceof AdminMakerCompilerPass) { - return; - } - - $this->fail(sprintf( - 'CompilerPass is not one of the expected types. Expects "%s", "%s", "%s", "%s", "%s", "%s", "%s" or "%s", but got "%s".', - AddDependencyCallsCompilerPass::class, - AddFilterTypeCompilerPass::class, - AdminSearchCompilerPass::class, - ExtensionCompilerPass::class, - GlobalVariablesCompilerPass::class, - ModelManagerCompilerPass::class, - ObjectAclManipulatorCompilerPass::class, - TwigStringExtensionCompilerPass::class, - AdminMakerCompilerPass::class, - \get_class($pass) - )); - }); + ->withConsecutive( + [new AddDependencyCallsCompilerPass()], + [new AddFilterTypeCompilerPass()], + [new AdminSearchCompilerPass()], + [new ExtensionCompilerPass()], + [new GlobalVariablesCompilerPass()], + [new ModelManagerCompilerPass()], + [new ObjectAclManipulatorCompilerPass()], + [new TwigStringExtensionCompilerPass()], + [new AdminMakerCompilerPass()], + ) + ; $bundle = new SonataAdminBundle(); $bundle->build($containerBuilder); From 1c01804a725d207a2734a89b7eb78b73ab5ea931 Mon Sep 17 00:00:00 2001 From: Fran Moreno Date: Thu, 14 Jan 2021 12:29:38 +0100 Subject: [PATCH 3/4] Added missing NEXT_MAJOR comment --- src/Maker/AdminMaker.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Maker/AdminMaker.php b/src/Maker/AdminMaker.php index 9df620ab67..0243b53885 100644 --- a/src/Maker/AdminMaker.php +++ b/src/Maker/AdminMaker.php @@ -78,7 +78,7 @@ final class AdminMaker extends AbstractMaker private $defaultController; /** - * NEXT_MAJOR: Make $defaultController mandatory. + * NEXT_MAJOR: Make $modelManagers and $defaultController mandatory. * * @param string $projectDirectory * @param array $modelManagers From c9849862507159fefa2ad3a8c1cf0b4e8bb58c67 Mon Sep 17 00:00:00 2001 From: Fran Moreno Date: Thu, 14 Jan 2021 12:30:33 +0100 Subject: [PATCH 4/4] Remove deprecation notice in AdminMakerTest --- composer.json | 2 +- tests/Maker/AdminMakerTest.php | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 06c5280820..715adcb36d 100644 --- a/composer.json +++ b/composer.json @@ -83,7 +83,7 @@ "symfony/browser-kit": "^4.4 || ^5.1", "symfony/css-selector": "^4.4 || ^5.1", "symfony/filesystem": "^4.4 || ^5.1", - "symfony/maker-bundle": "^1.17", + "symfony/maker-bundle": "^1.25", "symfony/phpunit-bridge": "^5.1.8", "symfony/yaml": "^4.4 || ^5.1", "vimeo/psalm": "^4.3.2" diff --git a/tests/Maker/AdminMakerTest.php b/tests/Maker/AdminMakerTest.php index c2fda27823..3b33d62556 100644 --- a/tests/Maker/AdminMakerTest.php +++ b/tests/Maker/AdminMakerTest.php @@ -22,6 +22,7 @@ use Symfony\Bundle\MakerBundle\Generator; use Symfony\Bundle\MakerBundle\Util\AutoloaderUtil; use Symfony\Bundle\MakerBundle\Util\MakerFileLinkFormatter; +use Symfony\Bundle\MakerBundle\Util\PhpCompatUtil; use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputDefinition; @@ -131,7 +132,11 @@ public function testExecute(): void ); $fileManager->setIO($this->io); - $this->generator = new Generator($fileManager, 'Sonata\AdminBundle\Tests'); + $this->generator = new Generator( + $fileManager, + 'Sonata\AdminBundle\Tests', + new PhpCompatUtil($fileManager) + ); $maker->generate($this->input, $this->io, $this->generator); } }