1
+ <?php
2
+
3
+ namespace Cspray \AnnotatedContainer ;
4
+
5
+ use Cspray \AnnotatedContainer \Helper \StubServiceGatheringListener ;
6
+ use Cspray \AnnotatedContainerFixture \Fixtures ;
7
+ use PHPUnit \Framework \TestCase ;
8
+
9
+ final class ServiceGatheringListenerTest extends TestCase {
10
+
11
+ public function testNoServicesReturnsEmptyArray () : void {
12
+ $ subject = new StubServiceGatheringListener (Fixtures::implicitAliasedServices ()->fooInterface ());
13
+ eventEmitter ()->registerListener ($ subject );
14
+ $ containerDefinition = compiler ()->compile (
15
+ ContainerDefinitionCompileOptionsBuilder::scanDirectories (Fixtures::singleConcreteService ()->getPath ())->build ()
16
+ );
17
+ containerFactory ()->createContainer ($ containerDefinition );
18
+
19
+ self ::assertSame ([] , $ subject ->getServices ());
20
+ }
21
+
22
+ public function testSingleConcreteServiceReturnsOneItemArray () : void {
23
+ $ subject = new StubServiceGatheringListener (Fixtures::implicitAliasedServices ()->fooInterface ());
24
+ eventEmitter ()->registerListener ($ subject );
25
+ $ containerDefinition = compiler ()->compile (
26
+ ContainerDefinitionCompileOptionsBuilder::scanDirectories (Fixtures::implicitAliasedServices ()->getPath ())->build ()
27
+ );
28
+ $ container = containerFactory ()->createContainer ($ containerDefinition );
29
+
30
+ self ::assertSame ([$ container ->get (Fixtures::implicitAliasedServices ()->fooImplementation ()->getName ())], $ subject ->getServices ());
31
+ }
32
+
33
+ public function testMultipleConcreteServices () : void {
34
+ $ subject = new StubServiceGatheringListener (Fixtures::ambiguousAliasedServices ()->fooInterface ());
35
+ eventEmitter ()->registerListener ($ subject );
36
+ $ containerDefinition = compiler ()->compile (
37
+ ContainerDefinitionCompileOptionsBuilder::scanDirectories (Fixtures::ambiguousAliasedServices ()->getPath ())->build ()
38
+ );
39
+ $ container = containerFactory ()->createContainer ($ containerDefinition );
40
+
41
+ $ services = $ subject ->getServices ();
42
+ usort ($ services , fn ($ a , $ b ) => $ a ::class <=> $ b ::class);
43
+
44
+ self ::assertSame ([
45
+ $ container ->get (Fixtures::ambiguousAliasedServices ()->barImplementation ()->getName ()),
46
+ $ container ->get (Fixtures::ambiguousAliasedServices ()->bazImplementation ()->getName ()),
47
+ $ container ->get (Fixtures::ambiguousAliasedServices ()->quxImplementation ()->getName ())
48
+ ], $ services );
49
+ }
50
+
51
+ }
0 commit comments