|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Console\Tests\EventListener; |
| 13 | + |
| 14 | +use Psr\Log\LoggerInterface; |
| 15 | +use Symfony\Component\Console\Command\Command; |
| 16 | +use Symfony\Component\Console\Event\ConsoleExceptionEvent; |
| 17 | +use Symfony\Component\Console\Event\ConsoleTerminateEvent; |
| 18 | +use Symfony\Component\Console\EventListener\ExceptionListener; |
| 19 | +use Symfony\Component\Console\Input\ArrayInput; |
| 20 | +use Symfony\Component\Console\Tests\Output\TestOutput; |
| 21 | + |
| 22 | +class ExceptionListenerTest extends \PHPUnit_Framework_TestCase |
| 23 | +{ |
| 24 | + public function testOnKernelException() |
| 25 | + { |
| 26 | + $logger = $this->getLogger(); |
| 27 | + $listener = new ExceptionListener($logger); |
| 28 | + |
| 29 | + $exception = new \RuntimeException('An error occurred'); |
| 30 | + |
| 31 | + $logger |
| 32 | + ->expects($this->once()) |
| 33 | + ->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 | + ; |
| 36 | + |
| 37 | + $input = array( |
| 38 | + 'name' => 'test:run', |
| 39 | + '--foo' => 'baz', |
| 40 | + 'bar' => 'buzz' |
| 41 | + ); |
| 42 | + |
| 43 | + $listener->onKernelException($this->getConsoleExceptionEvent($exception, $input, 1)); |
| 44 | + } |
| 45 | + |
| 46 | + public function testOnKernelTerminateForNonZeroExitCodeWritesToLog() |
| 47 | + { |
| 48 | + $logger = $this->getLogger(); |
| 49 | + $listener = new ExceptionListener($logger); |
| 50 | + |
| 51 | + $logger |
| 52 | + ->expects($this->once()) |
| 53 | + ->method('error') |
| 54 | + ->with('Command "{command}" exited with status code "{code}"', array('command' => '\'test:run\'', 'code' => 255)) |
| 55 | + ; |
| 56 | + |
| 57 | + $listener->onKernelTerminate($this->getConsoleTerminateEvent(array('name' => 'test:run'), 255)); |
| 58 | + } |
| 59 | + |
| 60 | + public function testOnKernelTerminateForZeroExitCodeDoesNotWriteToLog() |
| 61 | + { |
| 62 | + $logger = $this->getLogger(); |
| 63 | + $listener = new ExceptionListener($logger); |
| 64 | + |
| 65 | + $logger |
| 66 | + ->expects($this->never()) |
| 67 | + ->method('error') |
| 68 | + ; |
| 69 | + |
| 70 | + $listener->onKernelTerminate($this->getConsoleTerminateEvent(array('name' => 'test:run'), 0)); |
| 71 | + } |
| 72 | + |
| 73 | + public function testGetSubscribedEvents() |
| 74 | + { |
| 75 | + $this->assertEquals( |
| 76 | + array( |
| 77 | + 'console.exception' => array('onKernelException', -128), |
| 78 | + 'console.terminate' => array('onKernelTerminate', -128), |
| 79 | + ), |
| 80 | + ExceptionListener::getSubscribedEvents() |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + private function getLogger() |
| 85 | + { |
| 86 | + return $this->getMockForAbstractClass(LoggerInterface::class); |
| 87 | + } |
| 88 | + |
| 89 | + private function getConsoleExceptionEvent(\Exception $exception, $input, $exitCode) |
| 90 | + { |
| 91 | + return new ConsoleExceptionEvent(new Command('test:run'), new ArrayInput($input), new TestOutput(), $exception, $exitCode); |
| 92 | + } |
| 93 | + |
| 94 | + private function getConsoleTerminateEvent($input, $exitCode) |
| 95 | + { |
| 96 | + return new ConsoleTerminateEvent(new Command('test:run'), new ArrayInput($input), new TestOutput(), $exitCode); |
| 97 | + } |
| 98 | +} |
0 commit comments