/** * {@inheritdoc} */ public function run(RawArgs $args = null, InputStream $inputStream = null, OutputStream $outputStream = null, OutputStream $errorStream = null) { // Render errors to the preliminary IO until the final IO is created $io = $this->preliminaryIo; try { if (null === $args) { $args = new ArgvArgs(); } $ioFactory = $this->config->getIOFactory(); if (null === $ioFactory) { throw new LogicException('The IO factory must be set.'); } /** @var IO $io */ $io = call_user_func($ioFactory, $this, $args, $inputStream, $outputStream, $errorStream); $resolvedCommand = $this->resolveCommand($args); $command = $resolvedCommand->getCommand(); $parsedArgs = $resolvedCommand->getArgs(); $statusCode = $command->handle($parsedArgs, $io); } catch (Exception $e) { if (!$this->config->isExceptionCaught()) { throw $e; } $trace = new ExceptionTrace($e); $trace->render($io); $statusCode = $this->exceptionToExitCode($e->getCode()); } if ($this->config->isTerminatedAfterRun()) { exit($statusCode); } return $statusCode; }
public function testRenderWithCauseIfVeryVerbose() { $this->io->setVerbosity(IO::VERY_VERBOSE); $cause = new RuntimeException('The message of the cause.'); $exception = NoSuchCommandException::forCommandName('foobar', 0, $cause); $trace = new ExceptionTrace($exception); $trace->render($this->io); // Prevent trimming of trailing spaces in the box $box1 = ' ' . "\n" . ' [Webmozart\\Console\\Api\\Command\\NoSuchCommandException] ' . "\n" . ' The command "foobar" does not exist. ' . "\n" . ' '; $expected1 = <<<EOF {$box1} Exception trace: () src/Api/Command/NoSuchCommandException.php:?? Webmozart\\Console\\Api\\Command\\NoSuchCommandException::forCommandName() tests/UI/Component/ExceptionTraceTest.php EOF; $box2 = ' ' . "\n" . ' [RuntimeException] ' . "\n" . ' The message of the cause. ' . "\n" . ' '; $expected2 = <<<EOF Caused by: {$box2} Exception trace: () tests/UI/Component/ExceptionTraceTest.php EOF; $actual = $this->io->fetchErrors(); // Normalize line numbers across PHP and HHVM $actual = preg_replace('~(NoSuchCommandException.php:)\\d+~', '$1??', $actual); // Normalize slashes across OS $expected1 = str_replace(array("\n", '/'), array(PHP_EOL, DIRECTORY_SEPARATOR), $expected1); $expected2 = str_replace(array("\n", '/'), array(PHP_EOL, DIRECTORY_SEPARATOR), $expected2); $this->assertStringStartsWith($expected1, $actual); $this->assertContains($expected2, $actual); $this->assertStringEndsWith(PHP_EOL . PHP_EOL, $actual); }