Skip to content

Commit

Permalink
Use factories instead of dynamic di (#11)
Browse files Browse the repository at this point in the history
* Fix sf4

* Tests

* Added UT and comments

* Tested parameters values

Co-authored-by: Romain Delouard <[email protected]>
  • Loading branch information
pierrerolland and rdelouard authored Apr 7, 2020
1 parent 3a5cb0f commit 9e479eb
Show file tree
Hide file tree
Showing 9 changed files with 322 additions and 148 deletions.
23 changes: 5 additions & 18 deletions Command/VhostDefineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace Ola\RabbitMqAdminToolkitBundle\Command;

use Ola\RabbitMqAdminToolkitBundle\DependencyInjection\OlaRabbitMqAdminToolkitExtension;
use Ola\RabbitMqAdminToolkitBundle\VhostConfiguration;
use Ola\RabbitMqAdminToolkitBundle\VhostConfigurationFactory;
use Ola\RabbitMqAdminToolkitBundle\VhostHandler;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -16,7 +15,7 @@ class VhostDefineCommand extends Command
{
protected static $defaultName = 'rabbitmq:vhost:define';

private ContainerInterface $serviceLocator;
private VhostConfigurationFactory $vhostConfigurationFactory;

private array $vhostList;

Expand All @@ -25,14 +24,14 @@ class VhostDefineCommand extends Command
private bool $silentFailure;

public function __construct(
ContainerInterface $serviceLocator,
VhostConfigurationFactory $vhostConfigurationFactory,
array $vhostList,
VhostHandler $vhostHandler,
bool $silentFailure
) {
parent::__construct();

$this->serviceLocator = $serviceLocator;
$this->vhostConfigurationFactory = $vhostConfigurationFactory;
$this->vhostList = $vhostList;
$this->vhostHandler = $vhostHandler;
$this->silentFailure = $silentFailure;
Expand Down Expand Up @@ -104,19 +103,7 @@ private function getVhostList(InputInterface $input): array
*/
private function getVhostConfiguration(string $vhost): VhostConfiguration
{
$serviceName = sprintf(
OlaRabbitMqAdminToolkitExtension::VHOST_MANAGER_SERVICE_TEMPLATE,
$vhost
);

if (!$this->serviceLocator->has($serviceName)) {
throw new \InvalidArgumentException(sprintf(
'No configuration service found for vhost : "%s"',
$vhost
));
}

return $this->serviceLocator->get($serviceName);
return $this->vhostConfigurationFactory->getVhostConfiguration($vhost);
}

/**
Expand Down
56 changes: 3 additions & 53 deletions DependencyInjection/OlaRabbitMqAdminToolkitExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@

namespace Ola\RabbitMqAdminToolkitBundle\DependencyInjection;

use Ola\RabbitMqAdminToolkitBundle\ClientFactory;
use Ola\RabbitMqAdminToolkitBundle\VhostConfiguration;
use RabbitMq\ManagementApi\Client;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

Expand All @@ -20,8 +15,6 @@
class OlaRabbitMqAdminToolkitExtension extends Extension
{
const PARAMETER_TEMPLATE = 'ola_rabbit_mq_admin_toolkit.%s';
const CONNECTION_SERVICE_TEMPLATE = 'ola_rabbit_mq_admin_toolkit.connection.%s';
const VHOST_MANAGER_SERVICE_TEMPLATE = 'ola_rabbit_mq_admin_toolkit.configuration.%s';

/**
* {@inheritdoc}
Expand All @@ -33,54 +26,11 @@ public function load(array $configs, ContainerBuilder $container)

$container->setParameter(sprintf(self::PARAMETER_TEMPLATE, 'vhost_list'), array_keys($config['vhosts']));
$container->setParameter(sprintf(self::PARAMETER_TEMPLATE, 'silent_failure'), $config['silent_failure']);

$this->loadConnections($config['connections'], $container);
$this->loadVhostManagers($config['vhosts'], $container, $config['delete_allowed']);
$container->setParameter(sprintf(self::PARAMETER_TEMPLATE, 'connections'), $config['connections']);
$container->setParameter(sprintf(self::PARAMETER_TEMPLATE, 'vhosts'), $config['vhosts']);
$container->setParameter(sprintf(self::PARAMETER_TEMPLATE, 'delete_allowed'), $config['delete_allowed']);

$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
}

/**
* @param array $connections
* @param ContainerBuilder $container
*/
private function loadConnections(array $connections, ContainerBuilder $container): void
{
foreach ($connections as $name => $uri) {
$parsedUri = parse_url($uri);

$definition = new Definition(Client::class, [
$parsedUri['scheme'],
$parsedUri['host'],
$parsedUri['user'],
$parsedUri['pass'],
$parsedUri['port'] ?? 80
]);

$definition->setFactory([ClientFactory::class, 'getClient']); // necessary to have the exception handling
$definition->setPublic(true);
$container->setDefinition(sprintf(self::CONNECTION_SERVICE_TEMPLATE, $name), $definition);
}
}

/**
* @param array $vhosts
* @param ContainerBuilder $container
* @param bool $deleteAllowed
*/
private function loadVhostManagers(array $vhosts, ContainerBuilder $container, bool $deleteAllowed): void
{
foreach ($vhosts as $name => $vhost) {
$definition = new Definition(VhostConfiguration::class, [
new Reference(sprintf(self::CONNECTION_SERVICE_TEMPLATE, $vhost['connection'])),
!empty($vhost['name']) ? $vhost['name'] : $name,
$vhost,
$deleteAllowed
]);
$definition->setPublic(true);
$definition->addTag('ola_rabbit_mq_admin_toolkit.vhost_configuration');
$container->setDefinition(sprintf(self::VHOST_MANAGER_SERVICE_TEMPLATE, $name), $definition);
}
}
}
7 changes: 7 additions & 0 deletions Exception/ConfigurationNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Ola\RabbitMqAdminToolkitBundle\Exception;

class ConfigurationNotFoundException extends \Exception
{
}
7 changes: 7 additions & 0 deletions Exception/ConnectionNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Ola\RabbitMqAdminToolkitBundle\Exception;

class ConnectionNotFoundException extends \Exception
{
}
9 changes: 8 additions & 1 deletion Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="Ola\RabbitMqAdminToolkitBundle\ClientFactory" />
<service id="Ola\RabbitMqAdminToolkitBundle\VhostConfigurationFactory">
<argument type="service" id="Ola\RabbitMqAdminToolkitBundle\ClientFactory" />
<argument>%ola_rabbit_mq_admin_toolkit.delete_allowed%</argument>
<argument>%ola_rabbit_mq_admin_toolkit.connections%</argument>
<argument>%ola_rabbit_mq_admin_toolkit.vhosts%</argument>
</service>
<service id="Ola\RabbitMqAdminToolkitBundle\VhostHandler">
<argument type="service" id="Ola\RabbitMqAdminToolkitBundle\Manager\VhostManager"/>
<argument type="service" id="Ola\RabbitMqAdminToolkitBundle\Manager\PermissionManager"/>
Expand All @@ -19,7 +26,7 @@
<service id="Ola\RabbitMqAdminToolkitBundle\Manager\BindingManager" />
<service id="Ola\RabbitMqAdminToolkitBundle\Command\VhostDefineCommand">
<tag name="console.command" />
<argument type="tagged_locator" tag="ola_rabbit_mq_admin_toolkit.vhost_configuration" />
<argument type="service" id="Ola\RabbitMqAdminToolkitBundle\VhostConfigurationFactory" />
<argument>%ola_rabbit_mq_admin_toolkit.vhost_list%</argument>
<argument type="service" id="Ola\RabbitMqAdminToolkitBundle\VhostHandler" />
<argument>%ola_rabbit_mq_admin_toolkit.silent_failure%</argument>
Expand Down
30 changes: 11 additions & 19 deletions Tests/Command/VhostDefineCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

use Ola\RabbitMqAdminToolkitBundle\Command\VhostDefineCommand;
use Ola\RabbitMqAdminToolkitBundle\VhostConfiguration;
use Ola\RabbitMqAdminToolkitBundle\VhostConfigurationFactory;
use Ola\RabbitMqAdminToolkitBundle\VhostHandler;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\DependencyInjection\ContainerInterface;

class VhostDefineCommandTest extends TestCase
{
private ObjectProphecy $container;
private ObjectProphecy $vhostConfigurationFactory;
private ObjectProphecy $configuration;
private ObjectProphecy $handler;

Expand All @@ -24,24 +24,24 @@ class VhostDefineCommandTest extends TestCase
public function setUp(): void
{
$this->configuration = $this->prophesize(VhostConfiguration::class);
$this->container = $this->prophesize(ContainerInterface::class);
$this->vhostConfigurationFactory = $this->prophesize(VhostConfigurationFactory::class);
$this->handler = $this->prophesize(VhostHandler::class);

$this->defineCommand(false);
}

public function test_execute_withoutDefaultVhost(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectException(\Exception::class);

$this->container->has('ola_rabbit_mq_admin_toolkit.configuration.foo')->willReturn(false);
$this->vhostConfigurationFactory->getVhostConfiguration('foo')->willThrow(\Exception::class);

$this->commandTester->execute(['command' => 'rabbitmq:define:vhost']);
}

public function test_execute_withoutDefaultVhostButSilentFailure(): void
{
$this->container->has('ola_rabbit_mq_admin_toolkit.configuration.foo')->willReturn(false);
$this->vhostConfigurationFactory->getVhostConfiguration('foo')->willThrow(\Exception::class);
$this->defineCommand(true);

$this->assertEquals(0, $this->commandTester->execute(['command' => 'rabbitmq:define:vhost']));
Expand All @@ -52,9 +52,7 @@ public function test_execute_creationWithDefaultVhost(): void
$this->handler->exists($this->configuration)->willReturn(false);
$this->handler->define($this->configuration)->shouldBeCalled();

$this->container->has('ola_rabbit_mq_admin_toolkit.configuration.foo')->willReturn(true);
$this->container->get('ola_rabbit_mq_admin_toolkit.configuration.foo')
->willReturn($this->configuration->reveal());
$this->vhostConfigurationFactory->getVhostConfiguration('foo')->willReturn($this->configuration->reveal());

$this->commandTester->execute(['command' => 'rabbitmq:define:vhost']);

Expand All @@ -66,9 +64,7 @@ public function test_execute_creationWithSpecificVhost(): void
$this->handler->exists($this->configuration)->willReturn(false);
$this->handler->define($this->configuration)->shouldBeCalled();

$this->container->has('ola_rabbit_mq_admin_toolkit.configuration.bar')->willReturn(true);
$this->container->get('ola_rabbit_mq_admin_toolkit.configuration.bar')
->willReturn($this->configuration->reveal());
$this->vhostConfigurationFactory->getVhostConfiguration('bar')->willReturn($this->configuration->reveal());

$this->commandTester->execute(['vhost' => 'bar']);

Expand All @@ -80,9 +76,7 @@ public function test_execute_updateWithDefaultVhost(): void
$this->handler->exists($this->configuration)->willReturn(true);
$this->handler->define($this->configuration)->shouldBeCalled();

$this->container->has('ola_rabbit_mq_admin_toolkit.configuration.foo')->willReturn(true);
$this->container->get('ola_rabbit_mq_admin_toolkit.configuration.foo')
->willReturn($this->configuration->reveal());
$this->vhostConfigurationFactory->getVhostConfiguration('foo')->willReturn($this->configuration->reveal());

$this->commandTester->execute(['command' => 'rabbitmq:define:vhost']);

Expand All @@ -94,9 +88,7 @@ public function test_execute_updateWithSpecificVhost(): void
$this->handler->exists($this->configuration)->willReturn(true);
$this->handler->define($this->configuration)->shouldBeCalled();

$this->container->has('ola_rabbit_mq_admin_toolkit.configuration.bar')->willReturn(true);
$this->container->get('ola_rabbit_mq_admin_toolkit.configuration.bar')
->willReturn($this->configuration->reveal());
$this->vhostConfigurationFactory->getVhostConfiguration('bar')->willReturn($this->configuration->reveal());

$this->commandTester->execute(['vhost' => 'bar']);

Expand All @@ -107,7 +99,7 @@ private function defineCommand(bool $silentFailure): void
{
$this->application = new Application();
$this->command = new VhostDefineCommand(
$this->container->reveal(),
$this->vhostConfigurationFactory->reveal(),
['foo'],
$this->handler->reveal(),
$silentFailure
Expand Down
Loading

0 comments on commit 9e479eb

Please sign in to comment.