6
6
use Enqueue \Symfony \Client \DependencyInjection \BuildProcessorRegistryPass ;
7
7
use Enqueue \Test \ClassExtensionTrait ;
8
8
use PHPUnit \Framework \TestCase ;
9
+ use Symfony \Component \DependencyInjection \Argument \ServiceClosureArgument ;
9
10
use Symfony \Component \DependencyInjection \Compiler \CompilerPassInterface ;
10
11
use Symfony \Component \DependencyInjection \ContainerBuilder ;
11
12
use Symfony \Component \DependencyInjection \Definition ;
@@ -25,70 +26,70 @@ public function testShouldBeFinal()
25
26
$ this ->assertClassFinal (BuildProcessorRegistryPass::class);
26
27
}
27
28
28
- public function testCouldBeConstructedWithName ()
29
+ public function testCouldBeConstructedWithoutArguments ()
29
30
{
30
- $ pass = new BuildProcessorRegistryPass ('aName ' );
31
-
32
- $ this ->assertAttributeSame ('aName ' , 'name ' , $ pass );
31
+ new BuildProcessorRegistryPass ();
33
32
}
34
33
35
- public function testThrowIfNameEmptyOnConstruct ()
34
+ public function testThrowIfEnqueueClientsParameterNotSet ()
36
35
{
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 ());
40
41
}
41
42
42
- public function testShouldDoNothingIfRouteCollectionServiceIsNotRegistered ()
43
+ public function testThrowsIfNoProcessorRegistryServiceFoundForConfiguredTransport ()
43
44
{
44
45
$ container = new ContainerBuilder ();
46
+ $ container ->setParameter ('enqueue.clients ' , ['foo ' , 'bar ' ]);
45
47
46
- //guard
47
- $ this ->assertFalse ($ container ->hasDefinition ('enqueue.client.aName.route_collection ' ));
48
+ $ pass = new BuildProcessorRegistryPass ();
48
49
49
- $ pass = new BuildProcessorRegistryPass ('aName ' );
50
+ $ this ->expectException (\LogicException::class);
51
+ $ this ->expectExceptionMessage ('Service "enqueue.client.foo.processor_registry" not found ' );
50
52
$ pass ->process ($ container );
51
53
}
52
54
53
- public function testShouldDoNothingIfProcessorRegistryCollectionServiceIsNotRegistered ()
55
+ public function testThrowsIfNoRouteCollectionServiceFoundForConfiguredTransport ()
54
56
{
55
57
$ 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 ' );
57
60
58
- //guard
59
- $ this ->assertFalse ($ container ->hasDefinition ('enqueue.client.aName.processor_registry ' ));
61
+ $ pass = new BuildProcessorRegistryPass ();
60
62
61
- $ pass = new BuildProcessorRegistryPass ('aName ' );
63
+ $ this ->expectException (\LogicException::class);
64
+ $ this ->expectExceptionMessage ('Service "enqueue.client.foo.route_collection" not found ' );
62
65
$ pass ->process ($ container );
63
66
}
64
67
65
- public function testShouldDoNothingIfRouterProcessorServiceIsNotRegistered ()
68
+ public function testThrowsIfNoRouteProcessorServiceFoundForConfiguredTransport ()
66
69
{
67
70
$ 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 ' );
72
74
73
- //guard
74
- $ this ->assertFalse ($ container ->hasDefinition ('enqueue.client.aName.router_processor ' ));
75
+ $ pass = new BuildProcessorRegistryPass ();
75
76
76
- $ pass = new BuildProcessorRegistryPass ('aName ' );
77
+ $ this ->expectException (\LogicException::class);
78
+ $ this ->expectExceptionMessage ('Service "enqueue.client.foo.router_processor" not found ' );
77
79
$ pass ->process ($ container );
78
-
79
- $ this ->assertSame ([], $ container ->getDefinition ('enqueue.client.aName.processor_registry ' )->getArgument (0 ));
80
80
}
81
81
82
82
public function testThrowIfProcessorServiceIdOptionNotSet ()
83
83
{
84
84
$ container = new ContainerBuilder ();
85
+ $ container ->setParameter ('enqueue.clients ' , ['aName ' ]);
85
86
$ container ->register ('enqueue.client.aName.route_collection ' )->addArgument ([
86
87
(new Route ('aCommand ' , Route::COMMAND , 'aProcessor ' ))->toArray (),
87
88
]);
88
89
$ container ->register ('enqueue.client.aName.processor_registry ' )->addArgument ([]);
89
90
$ container ->register ('enqueue.client.aName.router_processor ' );
90
91
91
- $ pass = new BuildProcessorRegistryPass (' aName ' );
92
+ $ pass = new BuildProcessorRegistryPass ();
92
93
93
94
$ this ->expectException (\LogicException::class);
94
95
$ this ->expectExceptionMessage ('The route option "processor_service_id" is required ' );
@@ -101,6 +102,7 @@ public function testShouldPassLocatorAsFirstArgument()
101
102
$ registry ->addArgument ([]);
102
103
103
104
$ container = new ContainerBuilder ();
105
+ $ container ->setParameter ('enqueue.clients ' , ['aName ' ]);
104
106
$ container ->register ('enqueue.client.aName.route_collection ' )->addArgument ([
105
107
(new Route (
106
108
'aCommand ' ,
@@ -118,10 +120,37 @@ public function testShouldPassLocatorAsFirstArgument()
118
120
$ container ->setDefinition ('enqueue.client.aName.processor_registry ' , $ registry );
119
121
$ container ->register ('enqueue.client.aName.router_processor ' );
120
122
121
- $ pass = new BuildProcessorRegistryPass (' aName ' );
123
+ $ pass = new BuildProcessorRegistryPass ();
122
124
$ pass ->process ($ container );
123
125
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 );
126
155
}
127
156
}
0 commit comments