|
| 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\ArgvInput; |
| 20 | +use Symfony\Component\Console\Input\ArrayInput; |
| 21 | +use Symfony\Component\Console\Input\StringInput; |
| 22 | +use Symfony\Component\Console\Input\InputInterface; |
| 23 | +use Symfony\Component\Console\Output\OutputInterface; |
| 24 | + |
| 25 | +class ExceptionListenerTest extends \PHPUnit_Framework_TestCase |
| 26 | +{ |
| 27 | + public function testOnConsoleException() |
| 28 | + { |
| 29 | + $exception = new \RuntimeException('An error occurred'); |
| 30 | + |
| 31 | + $logger = $this->getLogger(); |
| 32 | + $logger |
| 33 | + ->expects($this->once()) |
| 34 | + ->method('error') |
| 35 | + ->with('Exception thrown while running command "{command}". Message: "{message}"', array('exception' => $exception, 'command' => 'test:run --foo=baz buzz', 'message' => 'An error occurred')) |
| 36 | + ; |
| 37 | + |
| 38 | + $listener = new ExceptionListener($logger); |
| 39 | + $listener->onConsoleException($this->getConsoleExceptionEvent($exception, new ArgvInput(array('console.php', 'test:run', '--foo=baz', 'buzz')), 1)); |
| 40 | + } |
| 41 | + |
| 42 | + public function testOnConsoleTerminateForNonZeroExitCodeWritesToLog() |
| 43 | + { |
| 44 | + $logger = $this->getLogger(); |
| 45 | + $logger |
| 46 | + ->expects($this->once()) |
| 47 | + ->method('error') |
| 48 | + ->with('Command "{command}" exited with code "{code}"', array('command' => 'test:run', 'code' => 255)) |
| 49 | + ; |
| 50 | + |
| 51 | + $listener = new ExceptionListener($logger); |
| 52 | + $listener->onConsoleTerminate($this->getConsoleTerminateEvent(new ArgvInput(array('console.php', 'test:run')), 255)); |
| 53 | + } |
| 54 | + |
| 55 | + public function testOnConsoleTerminateForZeroExitCodeDoesNotWriteToLog() |
| 56 | + { |
| 57 | + $logger = $this->getLogger(); |
| 58 | + $logger |
| 59 | + ->expects($this->never()) |
| 60 | + ->method('error') |
| 61 | + ; |
| 62 | + |
| 63 | + $listener = new ExceptionListener($logger); |
| 64 | + $listener->onConsoleTerminate($this->getConsoleTerminateEvent(new ArgvInput(array('console.php', 'test:run')), 0)); |
| 65 | + } |
| 66 | + |
| 67 | + public function testGetSubscribedEvents() |
| 68 | + { |
| 69 | + $this->assertEquals( |
| 70 | + array( |
| 71 | + 'console.exception' => array('onConsoleException', -128), |
| 72 | + 'console.terminate' => array('onConsoleTerminate', -128), |
| 73 | + ), |
| 74 | + ExceptionListener::getSubscribedEvents() |
| 75 | + ); |
| 76 | + } |
| 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 | + |
| 106 | + private function getLogger() |
| 107 | + { |
| 108 | + return $this->getMockForAbstractClass(LoggerInterface::class); |
| 109 | + } |
| 110 | + |
| 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) |
| 117 | + { |
| 118 | + return new ConsoleTerminateEvent(new Command('test:run'), $input, $this->getOutput(), $exitCode); |
| 119 | + } |
| 120 | + |
| 121 | + private function getOutput() |
| 122 | + { |
| 123 | + return $this->getMockBuilder(OutputInterface::class)->getMock(); |
| 124 | + } |
| 125 | +} |
0 commit comments