@@ -256,7 +256,7 @@ argument of type ``service_locator``:
256
256
# config/services.yaml
257
257
services :
258
258
App\CommandBus :
259
- arguments :
259
+ arguments :
260
260
- !service_locator
261
261
App\FooCommand : ' @app.command_handler.foo'
262
262
App\BarCommand : ' @app.command_handler.bar'
@@ -723,4 +723,55 @@ and compose your services with them::
723
723
as this will include the trait name, not the class name. Instead, use
724
724
``__CLASS__.'::'.__FUNCTION__ `` as the service id.
725
725
726
+ Testing a Service Subscriber
727
+ ----------------------------
728
+
729
+ When you need to unit test a service subscriber, you can either create a fake
730
+ ``ServiceLocator ``::
731
+
732
+ use Symfony\Component\DependencyInjection\ServiceLocator;
733
+
734
+ $container = new class() extends ServiceLocator {
735
+ private $services = [];
736
+
737
+ public function __construct()
738
+ {
739
+ parent::__construct([
740
+ 'foo' => function () {
741
+ return $this->services['foo'] = $this->services['foo'] ?? new stdClass();
742
+ },
743
+ 'bar' => function () {
744
+ return $this->services['bar'] = $this->services['bar'] ?? $this->createBar();
745
+ },
746
+ ]);
747
+ }
748
+
749
+ private function createBar()
750
+ {
751
+ $bar = new stdClass();
752
+ $bar->foo = $this->get('foo');
753
+
754
+ return $bar;
755
+ }
756
+ };
757
+
758
+ $serviceSubscriber = new MyService($container);
759
+ // ...
760
+
761
+ Or mock it when using ``PHPUnit ``::
762
+
763
+ use Psr\Container\ContainerInterface;
764
+
765
+ $container = $this->createMock(ContainerInterface::class);
766
+ $container->expects(self::any())
767
+ ->method('get')
768
+ ->willReturnMap([
769
+ ['foo', $this->createStub(Foo::class)],
770
+ ['bar', $this->createStub(Bar::class)],
771
+ ])
772
+ ;
773
+
774
+ $serviceSubscriber = new MyService($container);
775
+ // ...
776
+
726
777
.. _`Command pattern` : https://en.wikipedia.org/wiki/Command_pattern
0 commit comments