1212namespace Symfony \Bundle \MonologBundle \Tests \DependencyInjection \Compiler ;
1313
1414use PHPUnit \Framework \TestCase ;
15+ use Psr \Log \LoggerInterface ;
1516use Symfony \Component \DependencyInjection \Reference ;
1617use Symfony \Component \DependencyInjection \Definition ;
1718use Symfony \Component \DependencyInjection \ContainerBuilder ;
@@ -63,7 +64,55 @@ public function testProcessSetters()
6364 $ this ->assertEquals ('monolog.logger.test ' , (string ) $ calls [0 ][1 ][0 ], '->process replaces the logger by the new one in setters ' );
6465 }
6566
66- protected function getContainer ()
67+ public function testAutowiredLoggerArgumentsAreReplacedWithChannelLogger ()
68+ {
69+ if (!\method_exists ('Symfony\Component\DependencyInjection\Definition ' , 'getBindings ' )) {
70+ $ this ->markTestSkipped ('Need DependencyInjection 3.4+ to autowire channel logger. ' );
71+ }
72+
73+ $ container = $ this ->getFunctionalContainer ();
74+
75+ $ dummyService = $ container ->register ('dummy_service ' , 'Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Compiler\DummyService ' )
76+ ->setAutowired (true )
77+ ->setPublic (true )
78+ ->addTag ('monolog.logger ' , array ('channel ' => 'test ' ));
79+
80+ $ container ->compile ();
81+
82+ $ this ->assertEquals ('monolog.logger.test ' , (string ) $ dummyService ->getArgument (0 ));
83+ }
84+
85+ public function testAutowiredLoggerArgumentsAreNotReplacedWithChannelLoggerIfLoggerArgumentIsConfiguredExplicitly ()
86+ {
87+ if (!\method_exists ('Symfony\Component\DependencyInjection\Definition ' , 'getBindings ' )) {
88+ $ this ->markTestSkipped ('Need DependencyInjection 3.4+ to autowire channel logger. ' );
89+ }
90+
91+ $ container = $ this ->getFunctionalContainer ();
92+
93+ $ dummyService = $ container ->register ('dummy_service ' , 'Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Compiler\DummyService ' )
94+ ->setAutowired (true )
95+ ->addArgument (new Reference ('monolog.logger ' ))
96+ ->addTag ('monolog.logger ' , array ('channel ' => 'test ' ));
97+
98+ $ container ->compile ();
99+
100+ $ this ->assertEquals ('monolog.logger ' , (string ) $ dummyService ->getArgument (0 ));
101+ }
102+
103+ public function testTagNotBreakingIfNoLogger ()
104+ {
105+ $ container = $ this ->getFunctionalContainer ();
106+
107+ $ dummyService = $ container ->register ('dummy_service ' , 'stdClass ' )
108+ ->addTag ('monolog.logger ' , array ('channel ' => 'test ' ));
109+
110+ $ container ->compile ();
111+
112+ $ this ->assertEquals (array (), $ dummyService ->getArguments ());
113+ }
114+
115+ private function getContainer ()
67116 {
68117 $ container = new ContainerBuilder ();
69118 $ loader = new XmlFileLoader ($ container , new FileLocator (__DIR__ .'/../../../Resources/config ' ));
@@ -105,7 +154,7 @@ protected function getContainer()
105154 return $ container ;
106155 }
107156
108- protected function getContainerWithSetter ()
157+ private function getContainerWithSetter ()
109158 {
110159 $ container = new ContainerBuilder ();
111160 $ loader = new XmlFileLoader ($ container , new FileLocator (__DIR__ .'/../../../Resources/config ' ));
@@ -130,4 +179,29 @@ protected function getContainerWithSetter()
130179
131180 return $ container ;
132181 }
182+
183+ private function getFunctionalContainer ()
184+ {
185+ $ container = new ContainerBuilder ();
186+ $ container ->setParameter ('monolog.additional_channels ' , array ());
187+ $ container ->setParameter ('monolog.handlers_to_channels ' , array ());
188+ $ container ->setParameter ('monolog.use_microseconds ' , true );
189+
190+ $ loader = new XmlFileLoader ($ container , new FileLocator (__DIR__ .'/../../../Resources/config ' ));
191+ $ loader ->load ('monolog.xml ' );
192+
193+ $ container ->addCompilerPass (new LoggerChannelPass ());
194+
195+ // disable removing passes to be able to inspect the container before all the inlining optimizations
196+ $ container ->getCompilerPassConfig ()->setRemovingPasses (array ());
197+
198+ return $ container ;
199+ }
200+ }
201+
202+ class DummyService
203+ {
204+ public function __construct (LoggerInterface $ logger )
205+ {
206+ }
133207}
0 commit comments