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 wrong setup for Symfony 3.3 #46

Merged
merged 2 commits into from
Jun 1, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
stopOnFailure="false">
<testsuites>
<testsuite name="TacticianBundle Test Suite">
<directory>Tests/</directory>
<directory>tests/</directory>
</testsuite>
</testsuites>
<filter>
Expand Down
14 changes: 8 additions & 6 deletions src/DependencyInjection/Compiler/CommandHandlerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Symfony\Component\DependencyInjection\ServiceLocator;

/**
* This compiler pass maps Handler DI tags to specific commands
* This compiler pass maps Handler DI tags to specific commands.
*/
class CommandHandlerPass implements CompilerPassInterface
{
Expand All @@ -22,6 +22,7 @@ class CommandHandlerPass implements CompilerPassInterface
* @param ContainerBuilder $container
*
* @throws \Exception
*
* @api
*/
public function process(ContainerBuilder $container)
Expand All @@ -31,7 +32,6 @@ public function process(ContainerBuilder $container)
$busIdToHandlerMapping = [];

foreach ($container->findTaggedServiceIds('tactician.handler') as $id => $tags) {

foreach ($tags as $attributes) {
if (!isset($attributes['command'])) {
throw new \Exception('The tactician.handler tag must always have a command attribute');
Expand Down Expand Up @@ -65,8 +65,9 @@ public function process(ContainerBuilder $container)

// Leverage symfony/dependency-injection:^3.3 service locators
if (class_exists(ServiceLocator::class)) {
$this->registerHandlerServiceLocator($container, $locatorServiceId, $handlerMapping);
$locatorDefinition = $this->buildLocatorDefinition($handlerMapping, 'tactician.commandbus.'.$busId.'.handler.service_locator', ContainerLocator::class);
$serviceLocatorId = 'tactician.commandbus.'.$busId.'.handler.service_locator';
$this->registerHandlerServiceLocator($container, $serviceLocatorId, $handlerMapping);
$locatorDefinition = $this->buildLocatorDefinition($handlerMapping, ContainerLocator::class, $serviceLocatorId);
} else {
$locatorDefinition = $this->buildLocatorDefinition($handlerMapping);
}
Expand All @@ -83,7 +84,7 @@ public function process(ContainerBuilder $container)
[
new Reference('tactician.handler.command_name_extractor.class_name'),
new Reference($locatorServiceId),
new Reference($methodInflectorId)
new Reference($methodInflectorId),
]
)
);
Expand All @@ -93,7 +94,8 @@ public function process(ContainerBuilder $container)

/**
* @param string $id
* @param array $busIds
* @param array $busIds
*
* @throws \Exception
*/
protected function abortIfInvalidBusId($id, array $busIds)
Expand Down
50 changes: 34 additions & 16 deletions tests/DependencyInjection/Compiler/CommandHandlerPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ public function testProcess()
[
'tactician.commandbus.default' => 'default',
'tactician.method_inflector.default' => 'tactician.handler.method_name_inflector.handle',
'tactician.commandbus.ids' => ['default']
'tactician.commandbus.ids' => ['default'],
]
);

$this->servicesTaggedShouldBe(
$this->container,
[
'service_id_1' => [
['command' => 'my_command']
['command' => 'my_command'],
],
'service_id_2' => [
['command' => 'my_command']
['command' => 'my_command'],
],
]
);
Expand All @@ -73,18 +73,18 @@ public function testProcessAbortsOnMissingCommandAttribute()
[
'tactician.commandbus.default' => 'default',
'tactician.method_inflector.default' => 'tactician.handler.method_name_inflector.handle',
'tactician.commandbus.ids' => ['default']
'tactician.commandbus.ids' => ['default'],
]
);

$this->servicesTaggedShouldBe(
$this->container,
[
'service_id_1' => [
['not_command' => 'my_command']
['not_command' => 'my_command'],
],
'service_id_2' => [
['command' => 'my_command']
['command' => 'my_command'],
],
]);

Expand All @@ -101,15 +101,15 @@ public function testProcessAbortsOnInvalidBus()
[
'tactician.commandbus.default' => 'default',
'tactician.method_inflector.default' => 'tactician.handler.method_name_inflector.handle',
'tactician.commandbus.ids' => ['default']
'tactician.commandbus.ids' => ['default'],
]
);

$this->servicesTaggedShouldBe(
$this->container,
[
'service_id_1' => [
['command' => 'my_command', 'bus' => 'bad_bus_name']
['command' => 'my_command', 'bus' => 'bad_bus_name'],
],
]);

Expand All @@ -125,7 +125,7 @@ public function testProcessAddsLocatorAndHandlerDefinitionForTaggedBuses()
'tactician.method_inflector.default' => 'tactician.handler.method_name_inflector.handle',
'tactician.method_inflector.custom_bus' => 'tactician.handler.method_name_inflector.handle',
'tactician.method_inflector.other_bus' => 'tactician.handler.method_name_inflector.handle',
'tactician.commandbus.ids' => ['default', 'custom_bus', 'other_bus']
'tactician.commandbus.ids' => ['default', 'custom_bus', 'other_bus'],
]
);

Expand All @@ -135,7 +135,7 @@ public function testProcessAddsLocatorAndHandlerDefinitionForTaggedBuses()
'service_id_1' => [
['command' => 'my_command', 'bus' => 'custom_bus'],
['command' => 'my_command', 'bus' => 'other_bus'],
]
],
]);

$this->busShouldBeCorrectlyRegisteredInContainer(
Expand Down Expand Up @@ -166,15 +166,15 @@ public function testProcessAddsHandlerDefinitionWithNonDefaultMethodNameInflecto
[
'tactician.commandbus.default' => 'custom_bus',
'tactician.method_inflector.custom_bus' => 'tactician.handler.method_name_inflector.handle_class_name',
'tactician.commandbus.ids' => ['custom_bus']
'tactician.commandbus.ids' => ['custom_bus'],
]
);

$this->servicesTaggedShouldBe(
$this->container,
[
'service_id_1' => [
['command' => 'my_command', 'bus' => 'custom_bus']
['command' => 'my_command', 'bus' => 'custom_bus'],
],
]);

Expand Down Expand Up @@ -212,14 +212,30 @@ private function busShouldBeCorrectlyRegisteredInContainer($container, $busId, $
$handlerLocatorId = sprintf('tactician.commandbus.%s.handler.locator', $busId);
$handlerId = sprintf('tactician.commandbus.%s.middleware.command_handler', $busId);

if (class_exists(ServiceLocator::class)) {
$container->shouldReceive('setDefinition')
->with(
sprintf('tactician.commandbus.%s.handler.service_locator', $busId),
\Mockery::on(function (Definition $definition) {
$this->assertSame(ServiceLocator::class, $definition->getClass());

return true;
})
)
;
}

$container->shouldReceive('setDefinition')
->with(
$handlerLocatorId,
$this->callback(function ($definition) {
$this->assertInstanceOf(Definition::class, $definition);
\Mockery::on(function (Definition $definition) {
$this->assertSame(class_exists(ServiceLocator::class) ? ContainerLocator::class : ContainerBasedHandlerLocator::class, $definition->getClass());

return true;
})
);
)
->once()
;

$this->container->shouldReceive('setDefinition')
->with(
Expand All @@ -229,7 +245,9 @@ private function busShouldBeCorrectlyRegisteredInContainer($container, $busId, $

return $methodNameInflectorServiceId === $methodInflector;
})
);
)
->once()
;

$this->container->shouldReceive('setAlias')
->with(
Expand Down
3 changes: 1 addition & 2 deletions tests/Integration/BasicCommandAndBusMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace League\Tactician\Bundle\Tests\Integration;


/**
* To ensure cache is isolated from each test.
*
Expand Down Expand Up @@ -36,7 +35,7 @@ public function testHandleCommandWithInvalidMiddleware()
commandbus:
default:
middleware:
- tactician.middleware.validator
- tactician.middleware.whatever
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to not be redundant with the test in ValidatorTest

- tactician.middleware.command_handler
EOF
);
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ public function testHandleCommandOnMiddlewareWithDependencies()
$this->expectOutputString('Hello world');
$this->handleCommand('default', \League\Tactician\Bundle\Tests\EchoText::class, ['Hello world']);
}
}
}