Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix argument 3 of AdminMaker #6772

Merged
merged 4 commits into from
Jan 15, 2021
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
42 changes: 42 additions & 0 deletions src/DependencyInjection/Compiler/AdminMakerCompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* 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());
}
}
2 changes: 1 addition & 1 deletion src/Maker/AdminMaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, ModelManagerInterface> $modelManagers
Expand Down
2 changes: 2 additions & 0 deletions src/SonataAdminBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
100 changes: 100 additions & 0 deletions tests/DependencyInjection/Compiler/AdminMakerCompilerPassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* 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());
}
}
7 changes: 6 additions & 1 deletion tests/Maker/AdminMakerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
63 changes: 14 additions & 49 deletions tests/SonataAdminBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
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;
use Sonata\AdminBundle\DependencyInjection\Compiler\ModelManagerCompilerPass;
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;

/**
Expand All @@ -36,54 +35,20 @@ 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) {
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;
}

$this->fail(sprintf(
'CompilerPass is not one of the expected types. Expects "%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,
\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);
Expand Down