15
15
use Cspray \AnnotatedContainer \Compile \ContainerDefinitionBuilderContextConsumer ;
16
16
use Cspray \AnnotatedContainer \ContainerFactory \ParameterStore ;
17
17
use Cspray \AnnotatedContainer \Helper \TestLogger ;
18
+ use Cspray \AnnotatedContainerFixture \CustomServiceAttribute \Repository ;
18
19
use Cspray \AnnotatedContainerFixture \Fixtures ;
19
20
use PHPUnit \Framework \TestCase ;
20
21
use org \bovigo \vfs \vfsStream as VirtualFilesystem ;
@@ -519,4 +520,156 @@ protected function wireServices(AnnotatedContainer $container, ServiceGatherer $
519
520
], $ actualServices );
520
521
}
521
522
523
+ public function testServiceWiringObserverByAttributes () : void {
524
+ $ directoryResolver = new FixtureBootstrappingDirectoryResolver ();
525
+
526
+ $ goodXml = <<<XML
527
+ <?xml version="1.0" encoding="UTF-8" ?>
528
+ <annotatedContainer xmlns="https://annotated-container.cspray.io/schema/annotated-container.xsd">
529
+ <scanDirectories>
530
+ <source>
531
+ <dir>CustomServiceAttribute</dir>
532
+ </source>
533
+ </scanDirectories>
534
+ </annotatedContainer>
535
+ XML ;
536
+
537
+ VirtualFilesystem::newFile ('annotated-container.xml ' )
538
+ ->withContent ($ goodXml )
539
+ ->at ($ this ->vfs );
540
+
541
+ $ bootstrap = new Bootstrap (directoryResolver: $ directoryResolver );
542
+
543
+ $ observer = new class extends ServiceWiringObserver {
544
+
545
+ private ?AnnotatedContainer $ container = null ;
546
+ private array $ services = [];
547
+
548
+ public function getAnnotatedContainer () : ?AnnotatedContainer {
549
+ return $ this ->container ;
550
+ }
551
+
552
+ public function getServices () : array {
553
+ return $ this ->services ;
554
+ }
555
+
556
+ protected function wireServices (AnnotatedContainer $ container , ServiceGatherer $ gatherer ) : void {
557
+ $ this ->container = $ container ;
558
+ $ this ->services = $ gatherer ->getServicesWithAttribute (Repository::class);
559
+ }
560
+ };
561
+ $ bootstrap ->addObserver ($ observer );
562
+
563
+ $ container = $ bootstrap ->bootstrapContainer (['default ' , 'test ' ]);
564
+
565
+ $ actual = $ observer ->getServices ();
566
+ $ actualServices = array_map (fn (ServiceFromServiceDefinition $ fromServiceDefinition ) => $ fromServiceDefinition ->getService (), $ actual );
567
+
568
+ self ::assertSame ($ container , $ observer ->getAnnotatedContainer ());
569
+ self ::assertSame ([
570
+ $ container ->get (Fixtures::customServiceAttribute ()->myRepo ()->getName ()),
571
+ ], $ actualServices );
572
+ }
573
+
574
+ public function testServiceWiringObserverByTypeProfileAware () : void {
575
+ $ directoryResolver = new FixtureBootstrappingDirectoryResolver ();
576
+
577
+ $ goodXml = <<<XML
578
+ <?xml version="1.0" encoding="UTF-8" ?>
579
+ <annotatedContainer xmlns="https://annotated-container.cspray.io/schema/annotated-container.xsd">
580
+ <scanDirectories>
581
+ <source>
582
+ <dir>ProfileResolvedServices</dir>
583
+ </source>
584
+ </scanDirectories>
585
+ </annotatedContainer>
586
+ XML ;
587
+
588
+ VirtualFilesystem::newFile ('annotated-container.xml ' )
589
+ ->withContent ($ goodXml )
590
+ ->at ($ this ->vfs );
591
+
592
+ $ bootstrap = new Bootstrap (directoryResolver: $ directoryResolver );
593
+
594
+ $ observer = new class extends ServiceWiringObserver {
595
+
596
+ private ?AnnotatedContainer $ container = null ;
597
+ private array $ services = [];
598
+
599
+ public function getAnnotatedContainer () : ?AnnotatedContainer {
600
+ return $ this ->container ;
601
+ }
602
+
603
+ public function getServices () : array {
604
+ return $ this ->services ;
605
+ }
606
+
607
+ protected function wireServices (AnnotatedContainer $ container , ServiceGatherer $ gatherer ) : void {
608
+ $ this ->container = $ container ;
609
+ $ this ->services = $ gatherer ->getServicesForType (Fixtures::profileResolvedServices ()->fooInterface ()->getName ());
610
+ }
611
+ };
612
+ $ bootstrap ->addObserver ($ observer );
613
+
614
+ $ container = $ bootstrap ->bootstrapContainer (['default ' , 'prod ' ]);
615
+
616
+ $ actual = $ observer ->getServices ();
617
+
618
+ $ actualServices = array_map (fn (ServiceFromServiceDefinition $ fromServiceDefinition ) => $ fromServiceDefinition ->getService (), $ actual );
619
+
620
+ usort ($ actualServices , fn ($ a , $ b ) => $ a ::class <=> $ b ::class);
621
+
622
+ self ::assertSame ($ container , $ observer ->getAnnotatedContainer ());
623
+ self ::assertSame ([
624
+ $ container ->get (Fixtures::profileResolvedServices ()->prodImplementation ()->getName ()),
625
+ ], $ actualServices );
626
+ }
627
+
628
+ public function testServiceWiringObserverByAttributesProfileAware () : void {
629
+ $ directoryResolver = new FixtureBootstrappingDirectoryResolver ();
630
+
631
+ $ goodXml = <<<XML
632
+ <?xml version="1.0" encoding="UTF-8" ?>
633
+ <annotatedContainer xmlns="https://annotated-container.cspray.io/schema/annotated-container.xsd">
634
+ <scanDirectories>
635
+ <source>
636
+ <dir>CustomServiceAttribute</dir>
637
+ </source>
638
+ </scanDirectories>
639
+ </annotatedContainer>
640
+ XML ;
641
+
642
+ VirtualFilesystem::newFile ('annotated-container.xml ' )
643
+ ->withContent ($ goodXml )
644
+ ->at ($ this ->vfs );
645
+
646
+ $ bootstrap = new Bootstrap (directoryResolver: $ directoryResolver );
647
+
648
+ $ observer = new class extends ServiceWiringObserver {
649
+
650
+ private ?AnnotatedContainer $ container = null ;
651
+ private array $ services = [];
652
+
653
+ public function getAnnotatedContainer () : ?AnnotatedContainer {
654
+ return $ this ->container ;
655
+ }
656
+
657
+ public function getServices () : array {
658
+ return $ this ->services ;
659
+ }
660
+
661
+ protected function wireServices (AnnotatedContainer $ container , ServiceGatherer $ gatherer ) : void {
662
+ $ this ->container = $ container ;
663
+ $ this ->services = $ gatherer ->getServicesWithAttribute (Repository::class);
664
+ }
665
+ };
666
+ $ bootstrap ->addObserver ($ observer );
667
+
668
+ // The Repository is only active under 'test' profile and should not be included
669
+ $ container = $ bootstrap ->bootstrapContainer (['default ' , 'dev ' ]);
670
+
671
+ self ::assertSame ($ container , $ observer ->getAnnotatedContainer ());
672
+ self ::assertEmpty ($ observer ->getServices ());
673
+ }
674
+
522
675
}
0 commit comments