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

EZP-31594: Refactored --siteaccess command option to not rely on custom Application class #145

Merged
merged 8 commits into from
Dec 9, 2020
52 changes: 0 additions & 52 deletions eZ/Bundle/EzPublishCoreBundle/Console/Application.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
webhdx marked this conversation as resolved.
Show resolved Hide resolved
namespace eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler;

use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class ConsoleCommandPass implements CompilerPassInterface
webhdx marked this conversation as resolved.
Show resolved Hide resolved
{
public function process(ContainerBuilder $container)
{
foreach ($container->findTaggedServiceIds('console.command') as $id => $attributes) {
$definition = $container->getDefinition($id);

$definition->addMethodCall('addOption', [
'siteaccess',
null,
InputOption::VALUE_OPTIONAL,
'SiteAccess to use for operations. If not provided, default siteaccess will be used',
]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
namespace eZ\Bundle\EzPublishCoreBundle\EventListener;

use eZ\Publish\Core\MVC\Exception\InvalidSiteAccessException;
use eZ\Publish\Core\MVC\Symfony\Event\ConsoleInitEvent;
use eZ\Publish\Core\MVC\Symfony\Event\ScopeChangeEvent;
use eZ\Publish\Core\MVC\Symfony\MVCEvents;
use eZ\Publish\Core\MVC\Symfony\SiteAccess;
use eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessAware;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

Expand Down Expand Up @@ -47,13 +48,13 @@ public function __construct(
public static function getSubscribedEvents()
{
return [
MVCEvents::CONSOLE_INIT => [
['onConsoleCommand', -1],
ConsoleEvents::COMMAND => [
['onConsoleCommand', 128],
],
];
}

public function onConsoleCommand(ConsoleInitEvent $event)
public function onConsoleCommand(ConsoleCommandEvent $event)
{
$this->siteAccess->name = $event->getInput()->getParameterOption('--siteaccess', $this->defaultSiteAccessName);
$this->siteAccess->matchingType = 'cli';
Expand Down
2 changes: 2 additions & 0 deletions eZ/Bundle/EzPublishCoreBundle/EzPublishCoreBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler\BinaryContentDownloadPass;
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler\ConsoleCacheWarmupPass;
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler\ConsoleCommandPass;
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler\FieldTypeParameterProviderRegistryPass;
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler\FragmentPass;
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler\ViewMatcherRegistryPass;
Expand Down Expand Up @@ -74,6 +75,7 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new ConsoleCacheWarmupPass());
$container->addCompilerPass(new ViewMatcherRegistryPass());
$container->addCompilerPass(new SiteAccessMatcherRegistryPass());
$container->addCompilerPass(new ConsoleCommandPass());

// Storage passes
$container->addCompilerPass(new ExternalStorageRegistryPass());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace eZ\Bundle\EzPublishCoreBundle\Tests\DependencyInjection\Compiler;

use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler\ConsoleCommandPass;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

class ConsoleCommandPassTest extends AbstractCompilerPassTestCase
webhdx marked this conversation as resolved.
Show resolved Hide resolved
{
protected function registerCompilerPass(ContainerBuilder $container): void
{
$container->addCompilerPass(new ConsoleCommandPass());
}

public function testAddSiteaccessOption(): void
{
$commandDefinition = new Definition();
$serviceId = 'some_service_id';
$commandDefinition->addTag('console.command');

$this->setDefinition($serviceId, $commandDefinition);
$this->compile();

$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
$serviceId,
'addOption',
[
'siteaccess',
null,
InputOption::VALUE_OPTIONAL,
'SiteAccess to use for operations. If not provided, default siteaccess will be used',
]
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

use eZ\Bundle\EzPublishCoreBundle\EventListener\ConsoleCommandListener;
use eZ\Bundle\EzPublishCoreBundle\Tests\EventListener\Stubs\TestOutput;
use eZ\Publish\Core\MVC\Symfony\Event\ConsoleInitEvent;
use eZ\Publish\Core\MVC\Symfony\MVCEvents;
use eZ\Publish\Core\MVC\Symfony\SiteAccess;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -37,6 +38,9 @@ class ConsoleCommandListenerTest extends TestCase
/** @var \Symfony\Component\Console\Output\Output */
private $testOutput;

/** @var \Symfony\Component\Console\Command\Command|\PHPUnit\Framework\MockObject\MockObject */
private $command;

protected function setUp(): void
{
parent::setUp();
Expand All @@ -47,13 +51,14 @@ protected function setUp(): void
$this->dispatcher->addSubscriber($this->listener);
$this->inputDefinition = new InputDefinition([new InputOption('siteaccess', null, InputOption::VALUE_OPTIONAL)]);
$this->testOutput = new TestOutput(Output::VERBOSITY_QUIET, true);
$this->command = $this->createMock(Command::class);
}

public function testGetSubscribedEvents()
{
$this->assertSame(
[
MVCEvents::CONSOLE_INIT => [['onConsoleCommand', -1]],
ConsoleEvents::COMMAND => [['onConsoleCommand', 128]],
],
$this->listener->getSubscribedEvents()
);
Expand All @@ -67,7 +72,7 @@ public function testInvalidSiteAccessDev()
$this->dispatcher->expects($this->never())
->method('dispatch');
$input = new ArrayInput(['--siteaccess' => 'foo'], $this->inputDefinition);
$event = new ConsoleInitEvent($input, $this->testOutput);
$event = new ConsoleCommandEvent($this->command, $input, $this->testOutput);
$this->listener->setDebug(true);
$this->listener->onConsoleCommand($event);
}
Expand All @@ -80,7 +85,7 @@ public function testInvalidSiteAccessProd()
$this->dispatcher->expects($this->never())
->method('dispatch');
$input = new ArrayInput(['--siteaccess' => 'foo'], $this->inputDefinition);
$event = new ConsoleInitEvent($input, $this->testOutput);
$event = new ConsoleCommandEvent($this->command, $input, $this->testOutput);
$this->listener->setDebug(false);
$this->listener->onConsoleCommand($event);
}
Expand All @@ -90,7 +95,7 @@ public function testValidSiteAccess()
$this->dispatcher->expects($this->once())
->method('dispatch');
$input = new ArrayInput(['--siteaccess' => 'site1'], $this->inputDefinition);
$event = new ConsoleInitEvent($input, $this->testOutput);
$event = new ConsoleCommandEvent($this->command, $input, $this->testOutput);
$this->listener->onConsoleCommand($event);
$this->assertEquals(new SiteAccess('site1', 'cli'), $this->siteAccess);
}
Expand All @@ -100,7 +105,7 @@ public function testDefaultSiteAccess()
$this->dispatcher->expects($this->once())
->method('dispatch');
$input = new ArrayInput([], $this->inputDefinition);
$event = new ConsoleInitEvent($input, $this->testOutput);
$event = new ConsoleCommandEvent($this->command, $input, $this->testOutput);
$this->listener->onConsoleCommand($event);
$this->assertEquals(new SiteAccess('default', 'cli'), $this->siteAccess);
}
Expand Down
10 changes: 0 additions & 10 deletions eZ/Publish/Core/MVC/Symfony/MVCEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,4 @@ final class MVCEvents
* @deprecated Since 6.12, not triggered anymore when using ezplatform-http-cache package.
*/
const CACHE_CLEAR_CONTENT = 'ezpublish.cache_clear.content';

/**
* The CONSOLE_INIT event allows you to attach listeners before any command is
* loaded by the console. It also allows you react to global arguments before commands are run.
*
* @Event("eZ\Publish\Core\MVC\Symfony\Event\ConsoleInitEvent")
*
* @internal For internal use by SiteAccess system on console.
*/
const CONSOLE_INIT = 'ezpublish.console.init';
}
4 changes: 4 additions & 0 deletions eZ/Publish/Core/settings/containerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/

use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler\ConsoleCommandPass;
use eZ\Publish\API\Repository\Tests\Container\Compiler\SetAllServicesPublicPass;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -80,6 +82,8 @@
$containerBuilder->addCompilerPass(new Compiler\Search\Legacy\CriterionFieldValueHandlerRegistryPass());
$containerBuilder->addCompilerPass(new Compiler\Search\Legacy\SortClauseConverterPass());

$containerBuilder->addCompilerPass(new ConsoleCommandPass());

//
// Symfony 4 makes services private by default. Test cases are not prepared for this.
// This is a simple workaround to override services as public.
Expand Down