12
12
namespace Symfony \Bundle \MonologBundle \Tests \DependencyInjection \Compiler ;
13
13
14
14
use PHPUnit \Framework \TestCase ;
15
+ use Psr \Log \LoggerInterface ;
15
16
use Symfony \Component \DependencyInjection \Reference ;
16
17
use Symfony \Component \DependencyInjection \Definition ;
17
18
use Symfony \Component \DependencyInjection \ContainerBuilder ;
@@ -63,7 +64,55 @@ public function testProcessSetters()
63
64
$ this ->assertEquals ('monolog.logger.test ' , (string ) $ calls [0 ][1 ][0 ], '->process replaces the logger by the new one in setters ' );
64
65
}
65
66
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 ()
67
116
{
68
117
$ container = new ContainerBuilder ();
69
118
$ loader = new XmlFileLoader ($ container , new FileLocator (__DIR__ .'/../../../Resources/config ' ));
@@ -105,7 +154,7 @@ protected function getContainer()
105
154
return $ container ;
106
155
}
107
156
108
- protected function getContainerWithSetter ()
157
+ private function getContainerWithSetter ()
109
158
{
110
159
$ container = new ContainerBuilder ();
111
160
$ loader = new XmlFileLoader ($ container , new FileLocator (__DIR__ .'/../../../Resources/config ' ));
@@ -130,4 +179,29 @@ protected function getContainerWithSetter()
130
179
131
180
return $ container ;
132
181
}
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
+ }
133
207
}
0 commit comments