Skip to content

Commit a230cf4

Browse files
committed
[client] migrate passes.
1 parent 08a20a5 commit a230cf4

File tree

3 files changed

+94
-65
lines changed

3 files changed

+94
-65
lines changed

pkg/enqueue-bundle/EnqueueBundle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function build(ContainerBuilder $container): void
3333
$container->addCompilerPass(new BuildClientCommandSubscriberRoutesPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 100);
3434
$container->addCompilerPass(new BuildClientProcessorRoutesPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 100);
3535
$container->addCompilerPass(new AnalyzeRouteCollectionPass('default'), PassConfig::TYPE_BEFORE_OPTIMIZATION, 30);
36-
$container->addCompilerPass(new BuildClientProcessorRegistryPass('default'));
36+
$container->addCompilerPass(new BuildClientProcessorRegistryPass());
3737

3838
if (class_exists(AsyncEventDispatcherExtension::class)) {
3939
$container->addCompilerPass(new AsyncEventsPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 100);

pkg/enqueue/Symfony/Client/DependencyInjection/BuildProcessorRegistryPass.php

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,52 +10,52 @@
1010

1111
final class BuildProcessorRegistryPass implements CompilerPassInterface
1212
{
13-
/**
14-
* @var string
15-
*/
16-
private $name;
13+
use FormatClientNameTrait;
1714

18-
public function __construct(string $clientName)
19-
{
20-
if (empty($clientName)) {
21-
throw new \InvalidArgumentException('The name could not be empty.');
22-
}
23-
24-
$this->name = $clientName;
25-
}
15+
protected $name;
2616

2717
public function process(ContainerBuilder $container): void
2818
{
29-
$processorRegistryId = sprintf('enqueue.client.%s.processor_registry', $this->name);
30-
if (false == $container->hasDefinition($processorRegistryId)) {
31-
return;
19+
if (false == $container->hasParameter('enqueue.clients')) {
20+
throw new \LogicException('The "enqueue.clients" parameter must be set.');
3221
}
3322

34-
$routeCollectionId = sprintf('enqueue.client.%s.route_collection', $this->name);
35-
if (false == $container->hasDefinition($routeCollectionId)) {
36-
return;
37-
}
23+
$names = $container->getParameter('enqueue.clients');
3824

39-
$routerProcessorId = sprintf('enqueue.client.%s.router_processor', $this->name);
40-
if (false == $container->hasDefinition($routerProcessorId)) {
41-
return;
42-
}
25+
foreach ($names as $name) {
26+
$this->name = $name;
27+
28+
$processorRegistryId = $this->format('processor_registry');
29+
if (false == $container->hasDefinition($processorRegistryId)) {
30+
throw new \LogicException(sprintf('Service "%s" not found', $processorRegistryId));
31+
}
4332

44-
$routeCollection = RouteCollection::fromArray($container->getDefinition($routeCollectionId)->getArgument(0));
33+
$routeCollectionId = $this->format('route_collection');
34+
if (false == $container->hasDefinition($routeCollectionId)) {
35+
throw new \LogicException(sprintf('Service "%s" not found', $routeCollectionId));
36+
}
4537

46-
$map = [];
47-
foreach ($routeCollection->all() as $route) {
48-
if (false == $processorServiceId = $route->getOption('processor_service_id')) {
49-
throw new \LogicException('The route option "processor_service_id" is required');
38+
$routerProcessorId = $this->format('router_processor');
39+
if (false == $container->hasDefinition($routerProcessorId)) {
40+
throw new \LogicException(sprintf('Service "%s" not found', $routerProcessorId));
5041
}
5142

52-
$map[$route->getProcessor()] = new Reference($processorServiceId);
53-
}
43+
$routeCollection = RouteCollection::fromArray($container->getDefinition($routeCollectionId)->getArgument(0));
5444

55-
$map["%enqueue.client.{$this->name}.router_processor%"] = new Reference($routerProcessorId);
45+
$map = [];
46+
foreach ($routeCollection->all() as $route) {
47+
if (false == $processorServiceId = $route->getOption('processor_service_id')) {
48+
throw new \LogicException('The route option "processor_service_id" is required');
49+
}
5650

57-
$registry = $container->getDefinition($processorRegistryId);
58-
$registry->setArgument(0, ServiceLocatorTagPass::register($container, $map, $processorRegistryId));
51+
$map[$route->getProcessor()] = new Reference($processorServiceId);
52+
}
53+
54+
$map[$this->parameter('router_processor')] = new Reference($routerProcessorId);
55+
56+
$registry = $container->getDefinition($processorRegistryId);
57+
$registry->setArgument(0, ServiceLocatorTagPass::register($container, $map, $processorRegistryId));
58+
}
5959
}
6060

6161
private function getName(): string

pkg/enqueue/Tests/Symfony/Client/DependencyInjection/BuildProcessorRegistryPassTest.php

Lines changed: 60 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Enqueue\Symfony\Client\DependencyInjection\BuildProcessorRegistryPass;
77
use Enqueue\Test\ClassExtensionTrait;
88
use PHPUnit\Framework\TestCase;
9+
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
910
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1011
use Symfony\Component\DependencyInjection\ContainerBuilder;
1112
use Symfony\Component\DependencyInjection\Definition;
@@ -25,70 +26,70 @@ public function testShouldBeFinal()
2526
$this->assertClassFinal(BuildProcessorRegistryPass::class);
2627
}
2728

28-
public function testCouldBeConstructedWithName()
29+
public function testCouldBeConstructedWithoutArguments()
2930
{
30-
$pass = new BuildProcessorRegistryPass('aName');
31-
32-
$this->assertAttributeSame('aName', 'name', $pass);
31+
new BuildProcessorRegistryPass();
3332
}
3433

35-
public function testThrowIfNameEmptyOnConstruct()
34+
public function testThrowIfEnqueueClientsParameterNotSet()
3635
{
37-
$this->expectException(\InvalidArgumentException::class);
38-
$this->expectExceptionMessage('The name could not be empty.');
39-
new BuildProcessorRegistryPass('');
36+
$pass = new BuildProcessorRegistryPass();
37+
38+
$this->expectException(\LogicException::class);
39+
$this->expectExceptionMessage('The "enqueue.clients" parameter must be set.');
40+
$pass->process(new ContainerBuilder());
4041
}
4142

42-
public function testShouldDoNothingIfRouteCollectionServiceIsNotRegistered()
43+
public function testThrowsIfNoProcessorRegistryServiceFoundForConfiguredTransport()
4344
{
4445
$container = new ContainerBuilder();
46+
$container->setParameter('enqueue.clients', ['foo', 'bar']);
4547

46-
//guard
47-
$this->assertFalse($container->hasDefinition('enqueue.client.aName.route_collection'));
48+
$pass = new BuildProcessorRegistryPass();
4849

49-
$pass = new BuildProcessorRegistryPass('aName');
50+
$this->expectException(\LogicException::class);
51+
$this->expectExceptionMessage('Service "enqueue.client.foo.processor_registry" not found');
5052
$pass->process($container);
5153
}
5254

53-
public function testShouldDoNothingIfProcessorRegistryCollectionServiceIsNotRegistered()
55+
public function testThrowsIfNoRouteCollectionServiceFoundForConfiguredTransport()
5456
{
5557
$container = new ContainerBuilder();
56-
$container->register('enqueue.client.aName.route_collection');
58+
$container->setParameter('enqueue.clients', ['foo', 'bar']);
59+
$container->register('enqueue.client.foo.processor_registry');
5760

58-
//guard
59-
$this->assertFalse($container->hasDefinition('enqueue.client.aName.processor_registry'));
61+
$pass = new BuildProcessorRegistryPass();
6062

61-
$pass = new BuildProcessorRegistryPass('aName');
63+
$this->expectException(\LogicException::class);
64+
$this->expectExceptionMessage('Service "enqueue.client.foo.route_collection" not found');
6265
$pass->process($container);
6366
}
6467

65-
public function testShouldDoNothingIfRouterProcessorServiceIsNotRegistered()
68+
public function testThrowsIfNoRouteProcessorServiceFoundForConfiguredTransport()
6669
{
6770
$container = new ContainerBuilder();
68-
$container->register('enqueue.client.aName.route_collection');
69-
$container->register('enqueue.client.aName.processor_registry')
70-
->addArgument([])
71-
;
71+
$container->setParameter('enqueue.clients', ['foo', 'bar']);
72+
$container->register('enqueue.client.foo.processor_registry');
73+
$container->register('enqueue.client.foo.route_collection');
7274

73-
//guard
74-
$this->assertFalse($container->hasDefinition('enqueue.client.aName.router_processor'));
75+
$pass = new BuildProcessorRegistryPass();
7576

76-
$pass = new BuildProcessorRegistryPass('aName');
77+
$this->expectException(\LogicException::class);
78+
$this->expectExceptionMessage('Service "enqueue.client.foo.router_processor" not found');
7779
$pass->process($container);
78-
79-
$this->assertSame([], $container->getDefinition('enqueue.client.aName.processor_registry')->getArgument(0));
8080
}
8181

8282
public function testThrowIfProcessorServiceIdOptionNotSet()
8383
{
8484
$container = new ContainerBuilder();
85+
$container->setParameter('enqueue.clients', ['aName']);
8586
$container->register('enqueue.client.aName.route_collection')->addArgument([
8687
(new Route('aCommand', Route::COMMAND, 'aProcessor'))->toArray(),
8788
]);
8889
$container->register('enqueue.client.aName.processor_registry')->addArgument([]);
8990
$container->register('enqueue.client.aName.router_processor');
9091

91-
$pass = new BuildProcessorRegistryPass('aName');
92+
$pass = new BuildProcessorRegistryPass();
9293

9394
$this->expectException(\LogicException::class);
9495
$this->expectExceptionMessage('The route option "processor_service_id" is required');
@@ -101,6 +102,7 @@ public function testShouldPassLocatorAsFirstArgument()
101102
$registry->addArgument([]);
102103

103104
$container = new ContainerBuilder();
105+
$container->setParameter('enqueue.clients', ['aName']);
104106
$container->register('enqueue.client.aName.route_collection')->addArgument([
105107
(new Route(
106108
'aCommand',
@@ -118,10 +120,37 @@ public function testShouldPassLocatorAsFirstArgument()
118120
$container->setDefinition('enqueue.client.aName.processor_registry', $registry);
119121
$container->register('enqueue.client.aName.router_processor');
120122

121-
$pass = new BuildProcessorRegistryPass('aName');
123+
$pass = new BuildProcessorRegistryPass();
122124
$pass->process($container);
123125

124-
$this->assertInstanceOf(Reference::class, $registry->getArgument(0));
125-
$this->assertRegExp('/service_locator\..*?\.enqueue\.client\.aName\.processor_registry/', (string) $registry->getArgument(0));
126+
$this->assertLocatorServices($container, $registry->getArgument(0), [
127+
'%enqueue.client.aName.router_processor%' => 'enqueue.client.aName.router_processor',
128+
'aBarProcessor' => 'aBarServiceId',
129+
'aFooProcessor' => 'aFooServiceId',
130+
]);
131+
}
132+
133+
private function assertLocatorServices(ContainerBuilder $container, $locatorId, array $locatorServices)
134+
{
135+
$this->assertInstanceOf(Reference::class, $locatorId);
136+
$locatorId = (string) $locatorId;
137+
138+
$this->assertTrue($container->hasDefinition($locatorId));
139+
$this->assertRegExp('/service_locator\..*?\.enqueue\./', $locatorId);
140+
141+
$match = [];
142+
if (false == preg_match('/(service_locator\..*?)\.enqueue\./', $locatorId, $match)) {
143+
$this->fail('preg_match should not failed');
144+
}
145+
146+
$this->assertTrue($container->hasDefinition($match[1]));
147+
$locator = $container->getDefinition($match[1]);
148+
149+
$this->assertContainsOnly(ServiceClosureArgument::class, $locator->getArgument(0));
150+
$actualServices = array_map(function (ServiceClosureArgument $value) {
151+
return (string) $value->getValues()[0];
152+
}, $locator->getArgument(0));
153+
154+
$this->assertEquals($locatorServices, $actualServices);
126155
}
127156
}

0 commit comments

Comments
 (0)