[Console] fixed PHP7 Errors are now handled and converted to Exceptions
This commit is contained in:
parent
09e4e49b2a
commit
d3c613be36
|
@ -38,6 +38,7 @@ use Symfony\Component\Console\Helper\TableHelper;
|
||||||
use Symfony\Component\Console\Event\ConsoleCommandEvent;
|
use Symfony\Component\Console\Event\ConsoleCommandEvent;
|
||||||
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
|
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
|
||||||
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
|
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
|
||||||
|
use Symfony\Component\Debug\Exception\FatalThrowableError;
|
||||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -849,17 +850,25 @@ class Application
|
||||||
|
|
||||||
if ($event->commandShouldRun()) {
|
if ($event->commandShouldRun()) {
|
||||||
try {
|
try {
|
||||||
|
$e = null;
|
||||||
$exitCode = $command->run($input, $output);
|
$exitCode = $command->run($input, $output);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $x) {
|
||||||
|
$e = $x;
|
||||||
|
} catch (\Throwable $x) {
|
||||||
|
$e = new FatalThrowableError($x);
|
||||||
|
}
|
||||||
|
if (null !== $e) {
|
||||||
$event = new ConsoleExceptionEvent($command, $input, $output, $e, $e->getCode());
|
$event = new ConsoleExceptionEvent($command, $input, $output, $e, $e->getCode());
|
||||||
$this->dispatcher->dispatch(ConsoleEvents::EXCEPTION, $event);
|
$this->dispatcher->dispatch(ConsoleEvents::EXCEPTION, $event);
|
||||||
|
|
||||||
$e = $event->getException();
|
if ($e !== $event->getException()) {
|
||||||
|
$x = $e = $event->getException();
|
||||||
|
}
|
||||||
|
|
||||||
$event = new ConsoleTerminateEvent($command, $input, $output, $e->getCode());
|
$event = new ConsoleTerminateEvent($command, $input, $output, $e->getCode());
|
||||||
$this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event);
|
$this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event);
|
||||||
|
|
||||||
throw $e;
|
throw $x;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$exitCode = ConsoleCommandEvent::RETURN_CODE_DISABLED;
|
$exitCode = ConsoleCommandEvent::RETURN_CODE_DISABLED;
|
||||||
|
|
|
@ -949,6 +949,62 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
|
||||||
$this->assertContains('before.foo.caught.after.', $tester->getDisplay());
|
$this->assertContains('before.foo.caught.after.', $tester->getDisplay());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException \LogicException
|
||||||
|
* @expectedExceptionMessage caught
|
||||||
|
*/
|
||||||
|
public function testRunWithErrorAndDispatcher()
|
||||||
|
{
|
||||||
|
$application = new Application();
|
||||||
|
$application->setDispatcher($this->getDispatcher());
|
||||||
|
$application->setAutoExit(false);
|
||||||
|
$application->setCatchExceptions(false);
|
||||||
|
|
||||||
|
$application->register('dym')->setCode(function (InputInterface $input, OutputInterface $output) {
|
||||||
|
$output->write('dym.');
|
||||||
|
|
||||||
|
throw new \Error('dymerr');
|
||||||
|
});
|
||||||
|
|
||||||
|
$tester = new ApplicationTester($application);
|
||||||
|
$tester->run(array('command' => 'dym'));
|
||||||
|
$this->assertContains('before.dym.caught.after.', $tester->getDisplay(), 'The PHP Error did not dispached events');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRunDispatchesAllEventsWithError()
|
||||||
|
{
|
||||||
|
$application = new Application();
|
||||||
|
$application->setDispatcher($this->getDispatcher());
|
||||||
|
$application->setAutoExit(false);
|
||||||
|
|
||||||
|
$application->register('dym')->setCode(function (InputInterface $input, OutputInterface $output) {
|
||||||
|
$output->write('dym.');
|
||||||
|
|
||||||
|
throw new \Error('dymerr');
|
||||||
|
});
|
||||||
|
|
||||||
|
$tester = new ApplicationTester($application);
|
||||||
|
$tester->run(array('command' => 'dym'));
|
||||||
|
$this->assertContains('before.dym.caught.after.', $tester->getDisplay(), 'The PHP Error did not dispached events');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRunWithErrorFailingStatusCode()
|
||||||
|
{
|
||||||
|
$application = new Application();
|
||||||
|
$application->setDispatcher($this->getDispatcher());
|
||||||
|
$application->setAutoExit(false);
|
||||||
|
|
||||||
|
$application->register('dus')->setCode(function (InputInterface $input, OutputInterface $output) {
|
||||||
|
$output->write('dus.');
|
||||||
|
|
||||||
|
throw new \Error('duserr');
|
||||||
|
});
|
||||||
|
|
||||||
|
$tester = new ApplicationTester($application);
|
||||||
|
$tester->run(array('command' => 'dus'));
|
||||||
|
$this->assertSame(1, $tester->getStatusCode(), 'Status code should be 1');
|
||||||
|
}
|
||||||
|
|
||||||
public function testRunWithDispatcherSkippingCommand()
|
public function testRunWithDispatcherSkippingCommand()
|
||||||
{
|
{
|
||||||
$application = new Application();
|
$application = new Application();
|
||||||
|
|
|
@ -16,7 +16,8 @@
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=5.3.9"
|
"php": ">=5.3.9",
|
||||||
|
"symfony/debug": "~2.7,>=2.7.2"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"symfony/event-dispatcher": "~2.1",
|
"symfony/event-dispatcher": "~2.1",
|
||||||
|
|
Reference in New Issue