|
16 | 16 | use Symfony\Component\Console\Event\ConsoleExceptionEvent;
|
17 | 17 | use Symfony\Component\Console\Event\ConsoleTerminateEvent;
|
18 | 18 | use Symfony\Component\Console\EventListener\ExceptionListener;
|
| 19 | +use Symfony\Component\Console\Input\ArgvInput; |
19 | 20 | use Symfony\Component\Console\Input\ArrayInput;
|
20 |
| -use Symfony\Component\Console\Tests\Output\TestOutput; |
| 21 | +use Symfony\Component\Console\Input\StringInput; |
| 22 | +use Symfony\Component\Console\Input\InputInterface; |
| 23 | +use Symfony\Component\Console\Output\OutputInterface; |
21 | 24 |
|
22 | 25 | class ExceptionListenerTest extends \PHPUnit_Framework_TestCase
|
23 | 26 | {
|
24 |
| - public function testOnKernelException() |
| 27 | + public function testOnConsoleException() |
25 | 28 | {
|
26 |
| - $logger = $this->getLogger(); |
27 |
| - $listener = new ExceptionListener($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 = new ExceptionListener($logger); |
| 39 | + $listener->onConsoleException($this->getConsoleExceptionEvent($exception, new ArgvInput(array('console.php', 'test:run', '--foo=baz', 'buzz')), 1)); |
44 | 40 | }
|
45 | 41 |
|
46 |
| - public function testOnKernelTerminateForNonZeroExitCodeWritesToLog() |
| 42 | + public function testOnConsoleTerminateForNonZeroExitCodeWritesToLog() |
47 | 43 | {
|
48 | 44 | $logger = $this->getLogger();
|
49 |
| - $listener = new ExceptionListener($logger); |
50 |
| - |
51 | 45 | $logger
|
52 | 46 | ->expects($this->once())
|
53 | 47 | ->method('error')
|
54 |
| - ->with('Command "{command}" exited with status code "{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 = new ExceptionListener($logger); |
| 52 | + $listener->onConsoleTerminate($this->getConsoleTerminateEvent(new ArgvInput(array('console.php', 'test:run')), 255)); |
58 | 53 | }
|
59 | 54 |
|
60 |
| - public function testOnKernelTerminateForZeroExitCodeDoesNotWriteToLog() |
| 55 | + public function testOnConsoleTerminateForZeroExitCodeDoesNotWriteToLog() |
61 | 56 | {
|
62 | 57 | $logger = $this->getLogger();
|
63 |
| - $listener = new ExceptionListener($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 = new ExceptionListener($logger); |
| 64 | + $listener->onConsoleTerminate($this->getConsoleTerminateEvent(new ArgvInput(array('console.php', 'test:run')), 0)); |
71 | 65 | }
|
72 | 66 |
|
73 | 67 | public function testGetSubscribedEvents()
|
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 | + public function testAllKindsOfInputCanBeLogged() |
| 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 = new ExceptionListener($logger); |
| 88 | + $listener->onConsoleTerminate($this->getConsoleTerminateEvent(new ArgvInput(array('console.php', 'test:run', '--foo=bar')), 255)); |
| 89 | + $listener->onConsoleTerminate($this->getConsoleTerminateEvent(new ArrayInput(array('name' => 'test:run', '--foo' => 'bar')), 255)); |
| 90 | + $listener->onConsoleTerminate($this->getConsoleTerminateEvent(new StringInput('test:run --foo=bar'), 255)); |
| 91 | + } |
| 92 | + |
| 93 | + public function testCommandNameIsDisplayedForNonStringableInput() |
| 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 = new ExceptionListener($logger); |
| 103 | + $listener->onConsoleTerminate($this->getConsoleTerminateEvent($this->getMockBuilder(InputInterface::class)->getMock(), 255)); |
| 104 | + } |
| 105 | + |
84 | 106 | private function getLogger()
|
85 | 107 | {
|
86 | 108 | return $this->getMockForAbstractClass(LoggerInterface::class);
|
87 | 109 | }
|
88 | 110 |
|
89 |
| - private function getConsoleExceptionEvent(\Exception $exception, $input, $exitCode) |
| 111 | + private function getConsoleExceptionEvent(\Exception $exception, InputInterface $input, $exitCode) |
| 112 | + { |
| 113 | + return new ConsoleExceptionEvent(new Command('test:run'), $input, $this->getOutput(), $exception, $exitCode); |
| 114 | + } |
| 115 | + |
| 116 | + private function getConsoleTerminateEvent(InputInterface $input, $exitCode) |
90 | 117 | {
|
91 |
| - return new ConsoleExceptionEvent(new Command('test:run'), new ArrayInput($input), new TestOutput(), $exception, $exitCode); |
| 118 | + return new ConsoleTerminateEvent(new Command('test:run'), $input, $this->getOutput(), $exitCode); |
92 | 119 | }
|
93 | 120 |
|
94 |
| - private function getConsoleTerminateEvent($input, $exitCode) |
| 121 | + private function getOutput() |
95 | 122 | {
|
96 |
| - return new ConsoleTerminateEvent(new Command('test:run'), new ArrayInput($input), new TestOutput(), $exitCode); |
| 123 | + return $this->getMockBuilder(OutputInterface::class)->getMock(); |
97 | 124 | }
|
98 | 125 | }
|
0 commit comments