4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Tests\EventListener;
14 use PHPUnit\Framework\TestCase;
15 use Psr\Log\LoggerInterface;
16 use Symfony\Component\Console\Command\Command;
17 use Symfony\Component\Console\Event\ConsoleErrorEvent;
18 use Symfony\Component\Console\Event\ConsoleTerminateEvent;
19 use Symfony\Component\Console\EventListener\ErrorListener;
20 use Symfony\Component\Console\Input\ArgvInput;
21 use Symfony\Component\Console\Input\ArrayInput;
22 use Symfony\Component\Console\Input\Input;
23 use Symfony\Component\Console\Input\InputInterface;
24 use Symfony\Component\Console\Input\StringInput;
25 use Symfony\Component\Console\Output\OutputInterface;
27 class ErrorListenerTest extends TestCase
29 public function testOnConsoleError()
31 $error = new \TypeError('An error occurred');
33 $logger = $this->getLogger();
35 ->expects($this->once())
37 ->with('Error thrown while running command "{command}". Message: "{message}"', array('exception' => $error, 'command' => 'test:run --foo=baz buzz', 'message' => 'An error occurred'))
40 $listener = new ErrorListener($logger);
41 $listener->onConsoleError(new ConsoleErrorEvent(new ArgvInput(array('console.php', 'test:run', '--foo=baz', 'buzz')), $this->getOutput(), $error, new Command('test:run')));
44 public function testOnConsoleErrorWithNoCommandAndNoInputString()
46 $error = new \RuntimeException('An error occurred');
48 $logger = $this->getLogger();
50 ->expects($this->once())
52 ->with('An error occurred while using the console. Message: "{message}"', array('exception' => $error, 'message' => 'An error occurred'))
55 $listener = new ErrorListener($logger);
56 $listener->onConsoleError(new ConsoleErrorEvent(new NonStringInput(), $this->getOutput(), $error));
59 public function testOnConsoleTerminateForNonZeroExitCodeWritesToLog()
61 $logger = $this->getLogger();
63 ->expects($this->once())
65 ->with('Command "{command}" exited with code "{code}"', array('command' => 'test:run', 'code' => 255))
68 $listener = new ErrorListener($logger);
69 $listener->onConsoleTerminate($this->getConsoleTerminateEvent(new ArgvInput(array('console.php', 'test:run')), 255));
72 public function testOnConsoleTerminateForZeroExitCodeDoesNotWriteToLog()
74 $logger = $this->getLogger();
76 ->expects($this->never())
80 $listener = new ErrorListener($logger);
81 $listener->onConsoleTerminate($this->getConsoleTerminateEvent(new ArgvInput(array('console.php', 'test:run')), 0));
84 public function testGetSubscribedEvents()
88 'console.error' => array('onConsoleError', -128),
89 'console.terminate' => array('onConsoleTerminate', -128),
91 ErrorListener::getSubscribedEvents()
95 public function testAllKindsOfInputCanBeLogged()
97 $logger = $this->getLogger();
99 ->expects($this->exactly(3))
101 ->with('Command "{command}" exited with code "{code}"', array('command' => 'test:run --foo=bar', 'code' => 255))
104 $listener = new ErrorListener($logger);
105 $listener->onConsoleTerminate($this->getConsoleTerminateEvent(new ArgvInput(array('console.php', 'test:run', '--foo=bar')), 255));
106 $listener->onConsoleTerminate($this->getConsoleTerminateEvent(new ArrayInput(array('name' => 'test:run', '--foo' => 'bar')), 255));
107 $listener->onConsoleTerminate($this->getConsoleTerminateEvent(new StringInput('test:run --foo=bar'), 255));
110 public function testCommandNameIsDisplayedForNonStringableInput()
112 $logger = $this->getLogger();
114 ->expects($this->once())
116 ->with('Command "{command}" exited with code "{code}"', array('command' => 'test:run', 'code' => 255))
119 $listener = new ErrorListener($logger);
120 $listener->onConsoleTerminate($this->getConsoleTerminateEvent($this->getMockBuilder(InputInterface::class)->getMock(), 255));
123 private function getLogger()
125 return $this->getMockForAbstractClass(LoggerInterface::class);
128 private function getConsoleTerminateEvent(InputInterface $input, $exitCode)
130 return new ConsoleTerminateEvent(new Command('test:run'), $input, $this->getOutput(), $exitCode);
133 private function getOutput()
135 return $this->getMockBuilder(OutputInterface::class)->getMock();
139 class NonStringInput extends Input
141 public function getFirstArgument()
145 public function hasParameterOption($values, $onlyParams = false)
149 public function getParameterOption($values, $default = false, $onlyParams = false)
153 public function parse()