|
16 | 16 | useSymfony\Component\Console\Event\ConsoleExceptionEvent; |
17 | 17 | useSymfony\Component\Console\Event\ConsoleTerminateEvent; |
18 | 18 | useSymfony\Component\Console\EventListener\ExceptionListener; |
| 19 | +useSymfony\Component\Console\Input\ArgvInput; |
19 | 20 | useSymfony\Component\Console\Input\ArrayInput; |
20 | | -useSymfony\Component\Console\Tests\Output\TestOutput; |
| 21 | +useSymfony\Component\Console\Input\StringInput; |
| 22 | +useSymfony\Component\Console\Input\InputInterface; |
| 23 | +useSymfony\Component\Console\Output\OutputInterface; |
21 | 24 |
|
22 | 25 | class ExceptionListenerTestextends \PHPUnit_Framework_TestCase |
23 | 26 | { |
24 | | -publicfunctiontestOnKernelException() |
| 27 | +publicfunctiontestOnConsoleException() |
25 | 28 | { |
26 | | -$logger =$this->getLogger(); |
27 | | -$listener =newExceptionListener($logger); |
28 | | - |
29 | 29 | $exception =new \RuntimeException('An error occurred'); |
30 | 30 |
|
| 31 | +$logger =$this->getLogger(); |
31 | 32 | $logger |
32 | 33 | ->expects($this->once()) |
33 | 34 | ->method('error') |
34 | | - ->with('Exception thrown while running command: "{command}". Message: "{message}"',array('exception' =>$exception,'command' =>'\'test:run\' --foo=baz buzz','message' =>'An error occurred')) |
| 35 | + ->with('Exception thrown while running command "{command}". Message: "{message}"',array('exception' =>$exception,'command' =>'test:run --foo=baz buzz','message' =>'An error occurred')) |
35 | 36 | ; |
36 | 37 |
|
37 | | -$input =array( |
38 | | -'name' =>'test:run', |
39 | | -'--foo' =>'baz', |
40 | | -'bar' =>'buzz' |
41 | | - ); |
42 | | - |
43 | | -$listener->onKernelException($this->getConsoleExceptionEvent($exception,$input,1)); |
| 38 | +$listener =newExceptionListener($logger); |
| 39 | +$listener->onConsoleException($this->getConsoleExceptionEvent($exception,newArgvInput(array('console.php','test:run','--foo=baz','buzz')),1)); |
44 | 40 | } |
45 | 41 |
|
46 | | -publicfunctiontestOnKernelTerminateForNonZeroExitCodeWritesToLog() |
| 42 | +publicfunctiontestOnConsoleTerminateForNonZeroExitCodeWritesToLog() |
47 | 43 | { |
48 | 44 | $logger =$this->getLogger(); |
49 | | -$listener =newExceptionListener($logger); |
50 | | - |
51 | 45 | $logger |
52 | 46 | ->expects($this->once()) |
53 | 47 | ->method('error') |
54 | | - ->with('Command "{command}" exited withstatuscode "{code}"',array('command' =>'\'test:run\'','code' =>255)) |
| 48 | + ->with('Command "{command}" exited with code "{code}"',array('command' =>'test:run','code' =>255)) |
55 | 49 | ; |
56 | 50 |
|
57 | | -$listener->onKernelTerminate($this->getConsoleTerminateEvent(array('name' =>'test:run'),255)); |
| 51 | +$listener =newExceptionListener($logger); |
| 52 | +$listener->onConsoleTerminate($this->getConsoleTerminateEvent(newArgvInput(array('console.php','test:run')),255)); |
58 | 53 | } |
59 | 54 |
|
60 | | -publicfunctiontestOnKernelTerminateForZeroExitCodeDoesNotWriteToLog() |
| 55 | +publicfunctiontestOnConsoleTerminateForZeroExitCodeDoesNotWriteToLog() |
61 | 56 | { |
62 | 57 | $logger =$this->getLogger(); |
63 | | -$listener =newExceptionListener($logger); |
64 | | - |
65 | 58 | $logger |
66 | 59 | ->expects($this->never()) |
67 | 60 | ->method('error') |
68 | 61 | ; |
69 | 62 |
|
70 | | -$listener->onKernelTerminate($this->getConsoleTerminateEvent(array('name' =>'test:run'),0)); |
| 63 | +$listener =newExceptionListener($logger); |
| 64 | +$listener->onConsoleTerminate($this->getConsoleTerminateEvent(newArgvInput(array('console.php','test:run')),0)); |
71 | 65 | } |
72 | 66 |
|
73 | 67 | publicfunctiontestGetSubscribedEvents() |
74 | 68 | { |
75 | 69 | $this->assertEquals( |
76 | 70 | array( |
77 | | -'console.exception' =>array('onKernelException', -128), |
78 | | -'console.terminate' =>array('onKernelTerminate', -128), |
| 71 | +'console.exception' =>array('onConsoleException', -128), |
| 72 | +'console.terminate' =>array('onConsoleTerminate', -128), |
79 | 73 | ), |
80 | 74 | ExceptionListener::getSubscribedEvents() |
81 | 75 | ); |
82 | 76 | } |
83 | 77 |
|
| 78 | +publicfunctiontestAllKindsOfInputCanBeLogged() |
| 79 | + { |
| 80 | +$logger =$this->getLogger(); |
| 81 | +$logger |
| 82 | + ->expects($this->exactly(3)) |
| 83 | + ->method('error') |
| 84 | + ->with('Command "{command}" exited with code "{code}"',array('command' =>'test:run --foo=bar','code' =>255)) |
| 85 | + ; |
| 86 | + |
| 87 | +$listener =newExceptionListener($logger); |
| 88 | +$listener->onConsoleTerminate($this->getConsoleTerminateEvent(newArgvInput(array('console.php','test:run','--foo=bar')),255)); |
| 89 | +$listener->onConsoleTerminate($this->getConsoleTerminateEvent(newArrayInput(array('name' =>'test:run','--foo' =>'bar')),255)); |
| 90 | +$listener->onConsoleTerminate($this->getConsoleTerminateEvent(newStringInput('test:run --foo=bar'),255)); |
| 91 | + } |
| 92 | + |
| 93 | +publicfunctiontestCommandNameIsDisplayedForNonStringableInput() |
| 94 | + { |
| 95 | +$logger =$this->getLogger(); |
| 96 | +$logger |
| 97 | + ->expects($this->once()) |
| 98 | + ->method('error') |
| 99 | + ->with('Command "{command}" exited with code "{code}"',array('command' =>'test:run','code' =>255)) |
| 100 | + ; |
| 101 | + |
| 102 | +$listener =newExceptionListener($logger); |
| 103 | +$listener->onConsoleTerminate($this->getConsoleTerminateEvent($this->getMockBuilder(InputInterface::class)->getMock(),255)); |
| 104 | + } |
| 105 | + |
84 | 106 | privatefunctiongetLogger() |
85 | 107 | { |
86 | 108 | return$this->getMockForAbstractClass(LoggerInterface::class); |
87 | 109 | } |
88 | 110 |
|
89 | | -privatefunctiongetConsoleExceptionEvent(\Exception$exception,$input,$exitCode) |
| 111 | +privatefunctiongetConsoleExceptionEvent(\Exception$exception,InputInterface$input,$exitCode) |
| 112 | + { |
| 113 | +returnnewConsoleExceptionEvent(newCommand('test:run'),$input,$this->getOutput(),$exception,$exitCode); |
| 114 | + } |
| 115 | + |
| 116 | +privatefunctiongetConsoleTerminateEvent(InputInterface$input,$exitCode) |
90 | 117 | { |
91 | | -returnnewConsoleExceptionEvent(newCommand('test:run'),newArrayInput($input),newTestOutput(),$exception,$exitCode); |
| 118 | +returnnewConsoleTerminateEvent(newCommand('test:run'),$input,$this->getOutput(),$exitCode); |
92 | 119 | } |
93 | 120 |
|
94 | | -privatefunctiongetConsoleTerminateEvent($input,$exitCode) |
| 121 | +privatefunctiongetOutput() |
95 | 122 | { |
96 | | -returnnewConsoleTerminateEvent(newCommand('test:run'),newArrayInput($input),newTestOutput(),$exitCode); |
| 123 | +return$this->getMockBuilder(OutputInterface::class)->getMock(); |
97 | 124 | } |
98 | 125 | } |