Skip to content

Commit

Permalink
feature/PHPGL-10 Add form_type tag to compiler pass collector
Browse files Browse the repository at this point in the history
  • Loading branch information
maciej committed Nov 30, 2015
1 parent 3386f3d commit 39be37e
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 4 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"matthiasnoback/symfony-console-form": "^1.1",
"symfony/finder": "~2.5",
"symfony/validator": "~2.5",
"symfony/var-dumper": "~2.5"
"symfony/var-dumper": "~2.5",
"matthiasnoback/symfony-dependency-injection-test": "0.*"
},
"autoload": {
"psr-4" : { "Clearcode\\CommandBusConsole\\": "src" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,24 @@ class CommandHandlersCompilerPass implements CompilerPassInterface
*/
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('command_bus_console.command_collector')) {
return;
}

$commandHandlersTags = $container->findTaggedServiceIds('command_handler');
$commandCollector = $container->getDefinition('command_bus_console.command_collector');

$commands = [];

foreach ($commandHandlersTags as $service) {
foreach ($service as $tags) {
$commands[] = $tags['handles'];
$arguments = ['handles' => $tags['handles']];

if (array_key_exists('form_type', $tags)) {
$arguments['form_type'] = $tags['form_type'];
}

$commands[] = $arguments;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/CommandCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class CommandCollector
public function processCommandServices(array $services)
{
foreach ($services as $service) {
$this->commands[] = CommandReflection::fromClass($service);
$this->commands[] = CommandReflection::fromClass($service['handles']);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace tests\Clearcode\CommandBusConsole\Bundle\DependencyInjection\Compiler;

use Clearcode\CommandBusConsole\Bundle\DependencyInjection\Compiler\CommandHandlersCompilerPass;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

class CommandHandlersCompilerPassTest extends AbstractCompilerPassTestCase
{
/**
* @test
*/
public function it_should_collects_classes_names_by_adding_method_when_there_is_handles_argument()
{
$collectingService = new Definition();
$this->setDefinition('command_bus_console.command_collector', $collectingService);

$collectedService = new Definition();
$collectedService->addTag('command_handler', ['handles' => 'test_class']);
$this->setDefinition('colected_service_id', $collectedService);

$this->compile();

$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
'command_bus_console.command_collector',
'processCommandServices',
[[['handles' => 'test_class']]]
);
}

/**
* @test
*/
public function it_should_collects_classes_names_and_form_types_by_adding_method_when_there_is_form_type_argument()
{
$collectingService = new Definition();
$this->setDefinition('command_bus_console.command_collector', $collectingService);

$collectedService = new Definition();
$collectedService->addTag('command_handler', ['handles' => 'test_class', 'form_type' => 'form_type_name']);
$this->setDefinition('colected_service_id', $collectedService);

$this->compile();

$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
'command_bus_console.command_collector',
'processCommandServices',
[[[
'handles' => 'test_class',
'form_type' => 'form_type_name'
]]]
);
}

/**
* Register the compiler pass under test, just like you would do inside a bundle's load()
* method:
*
* $container->addCompilerPass(new MyCompilerPass());
*/
protected function registerCompilerPass(ContainerBuilder $container)
{
$container->addCompilerPass(new CommandHandlersCompilerPass());
}
}
2 changes: 1 addition & 1 deletion tests/CommandCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function setUp()
$this->sut = new CommandCollector();

$this->commands = [
DummyCommand::class,
['handles' => DummyCommand::class],
];
}
}

0 comments on commit 39be37e

Please sign in to comment.