@@ -101,6 +101,59 @@ public static function provideVerbosityMappingTests(): array
101101 ];
102102 }
103103
104+ /**
105+ * @dataProvider provideHandleOrBubbleSilentTests
106+ */
107+ public function testHandleOrBubbleSilent (int $ verbosity , Level $ level , bool $ isHandling , bool $ isBubbling , array $ map = [])
108+ {
109+ $ output = $ this ->createMock (OutputInterface::class);
110+ $ output
111+ ->expects ($ this ->atLeastOnce ())
112+ ->method ('getVerbosity ' )
113+ ->willReturn ($ verbosity )
114+ ;
115+ $ handler = new ConsoleHandler ($ output , false , $ map );
116+ $ this ->assertSame ($ isHandling , $ handler ->isHandling (RecordFactory::create ($ level )), '->isHandling returns correct value depending on console verbosity and log level ' );
117+
118+ // check that the handler actually outputs the record if it handles it at verbosity above SILENT
119+ $ levelName = Logger::getLevelName ($ level );
120+ $ levelName = \sprintf ('%-9s ' , $ levelName );
121+
122+ $ realOutput = $ this ->getMockBuilder (Output::class)->onlyMethods (['doWrite ' ])->getMock ();
123+ $ realOutput ->setVerbosity ($ verbosity );
124+ $ log = "16:21:54 $ levelName [app] My info message \n" ;
125+ $ realOutput
126+ ->expects ($ isHandling && $ verbosity > OutputInterface::VERBOSITY_SILENT ? $ this ->once () : $ this ->never ())
127+ ->method ('doWrite ' )
128+ ->with ($ log , false );
129+ $ handler = new ConsoleHandler ($ realOutput , false , $ map );
130+
131+ $ infoRecord = RecordFactory::create ($ level , 'My info message ' , 'app ' , datetime: new \DateTimeImmutable ('2013-05-29 16:21:54 ' ));
132+ $ this ->assertSame (!$ isBubbling , $ handler ->handle ($ infoRecord ), 'The handler bubbled correctly when it did not output the message. ' );
133+ }
134+
135+ public static function provideHandleOrBubbleSilentTests (): array
136+ {
137+ // The VERBOSITY_SILENT const is not defined for Console below 7.2, but in that case, the code behaves as before
138+ if (!\defined ('\Symfony\Component\Console\Output\OutputInterface::VERBOSITY_SILENT ' )) {
139+ return [
140+ [OutputInterface::VERBOSITY_NORMAL , Level::Warning, true , false ],
141+ [OutputInterface::VERBOSITY_QUIET , Level::Warning, false , true ],
142+ ];
143+ }
144+
145+ return [
146+ [OutputInterface::VERBOSITY_SILENT , Level::Warning, false , true ],
147+ [OutputInterface::VERBOSITY_NORMAL , Level::Warning, true , false ],
148+ [OutputInterface::VERBOSITY_QUIET , Level::Warning, false , true ],
149+ [OutputInterface::VERBOSITY_SILENT , Level::Warning, true , false , [OutputInterface::VERBOSITY_SILENT => Level::Warning]],
150+ [OutputInterface::VERBOSITY_SILENT , Level::Warning, false , true , [OutputInterface::VERBOSITY_SILENT => Level::Error]],
151+ [OutputInterface::VERBOSITY_SILENT , Level::Emergency, false , true ],
152+ [OutputInterface::VERBOSITY_SILENT , Level::Emergency, true , false , [OutputInterface::VERBOSITY_SILENT => Level::Emergency]],
153+ ];
154+ }
155+
156+
104157 public function testVerbosityChanged ()
105158 {
106159 $ output = $ this ->createMock (OutputInterface::class);
0 commit comments