1+ <?php
2+
3+ namespace SymfonyRollbarBundle \Tests \SymfonyRollbarBundle \EventListener ;
4+
5+ use Monolog \Logger ;
6+ use Symfony \Bundle \FrameworkBundle \Test \KernelTestCase ;
7+ use Symfony \Component \Console \ConsoleEvents ;
8+ use Symfony \Component \Console \Event \ConsoleErrorEvent ;
9+ use Symfony \Component \Console \Event \ConsoleExceptionEvent ;
10+ use Symfony \Component \Console \Input \ArrayInput ;
11+ use Symfony \Component \Console \Output \OutputInterface ;
12+ use Symfony \Component \Console \Output \StreamOutput ;
13+ use Symfony \Component \EventDispatcher \Debug \TraceableEventDispatcher ;
14+ use SymfonyRollbarBundle \Command \DeployCommand ;
15+ use SymfonyRollbarBundle \EventListener \AbstractListener ;
16+ use SymfonyRollbarBundle \Tests \Fixtures \ErrorHandler ;
17+
18+ /**
19+ * Class ConsoleListenerTest
20+ * @package SymfonyRollbarBundle\Tests\SymfonyRollbarBundle\EventListener
21+ * @runTestsInSeparateProcesses
22+ */
23+ class ConsoleListenerTest extends KernelTestCase
24+ {
25+ public function setUp ()
26+ {
27+ parent ::setUp ();
28+ static ::bootKernel ();
29+ }
30+
31+ /**
32+ * @dataProvider provideLegacyEvents
33+ *
34+ * @param $error
35+ * @param $event
36+ */
37+ public function testLegacyConsoleException ($ error , $ event )
38+ {
39+ $ container = static ::$ kernel ->getContainer ();
40+
41+ /**
42+ * @var TraceableEventDispatcher $eventDispatcher
43+ */
44+ $ eventDispatcher = $ container ->get ('event_dispatcher ' );
45+
46+ $ key = '' ;
47+ if (class_exists ('Symfony\Component\Console\ConsoleEvents ' )) {
48+ $ key = class_exists ('Symfony\Component\Console\Event\ConsoleErrorEvent ' )
49+ ? ConsoleEvents::ERROR
50+ : ConsoleEvents::EXCEPTION ;
51+ } else {
52+ $ this ->markTestSkipped ('Nothing to test. ' );
53+ }
54+
55+ foreach ($ eventDispatcher ->getListeners ('kernel.exception ' ) as $ listener ) {
56+ $ eventDispatcher ->removeListener ('kernel.exception ' , $ listener );
57+ }
58+
59+ $ handler = new ErrorHandler ();
60+ $ handler ->setAssert (function ($ record ) use ($ error ) {
61+ $ this ->assertNotEmpty ($ record );
62+
63+ $ this ->assertNotEmpty ($ record ['context ' ]['exception ' ]);
64+ $ exception = $ record ['context ' ]['exception ' ];
65+
66+ $ this ->assertEquals ($ error ->getMessage (), $ record ['message ' ]);
67+ $ this ->assertEquals (Logger::ERROR , $ record ['level ' ]);
68+
69+ $ this ->assertInstanceOf (\Exception::class, $ exception );
70+ });
71+
72+ foreach ($ eventDispatcher ->getListeners ($ key ) as $ listener ) {
73+ /**
74+ * @var AbstractListener $listener
75+ */
76+ if (!$ listener [0 ] instanceof AbstractListener) {
77+ // disable default symfony listeners
78+ $ eventDispatcher ->removeListener ($ key , $ listener );
79+ continue ;
80+ }
81+
82+ $ listener [0 ]->getLogger ()->setHandlers ([$ handler ]);
83+ }
84+
85+ $ eventDispatcher ->dispatch ($ key , $ event );
86+ restore_error_handler ();
87+ }
88+
89+ /**
90+ * @return array
91+ */
92+ public function provideLegacyEvents ()
93+ {
94+ $ input = new ArrayInput ([]);
95+ $ output = new StreamOutput (
96+ fopen ('php://memory ' , 'w ' , false ),
97+ OutputInterface::VERBOSITY_QUIET ,
98+ false
99+ );
100+
101+ $ error = new \Exception ('This is console exception ' );
102+ $ command = new DeployCommand ();
103+
104+ $ events = [];
105+
106+ if (class_exists ('Symfony\Component\Console\ConsoleEvents ' )) {
107+ if (class_exists ('Symfony\Component\Console\Event\ConsoleErrorEvent ' )) {
108+ $ events [] = [$ error , new ConsoleErrorEvent ($ input , $ output , $ error , $ command )];
109+ }
110+
111+ if (class_exists ('\Symfony\Component\Console\Event\ConsoleExceptionEvent ' )) {
112+ $ events [] = [$ error , new ConsoleExceptionEvent ($ command , $ input , $ output , $ error , 1 )];
113+ }
114+ }
115+
116+
117+ return $ events ;
118+ }
119+ }
0 commit comments