8000 bug #19813 [Console] fixed PHP7 Errors are now handled and converted … · symfony/symfony@ddf8b27 · GitHub
[go: up one dir, main page]

Skip to content

Commit ddf8b27

Browse files
committed
bug #19813 [Console] fixed PHP7 Errors are now handled and converted to Exceptions (fonsecas72)
This PR was squashed before being merged into the 2.7 branch (closes #19813). Discussion ---------- [Console] fixed PHP7 Errors are now handled and converted to Exceptions | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #18484 | License | MIT | Doc PR | Commits ------- d3c613b [Console] fixed PHP7 Errors are now handled and converted to Exceptions
2 parents d893192 + d3c613b commit ddf8b27

File tree

3 files changed

+70
-4
lines changed

3 files changed

+70
-4
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
use Symfony\Component\Console\Event\ConsoleCommandEvent;
3939
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
4040
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
41+
use Symfony\Component\Debug\Exception\FatalThrowableError;
4142
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
4243

4344
/**
@@ -849,17 +850,25 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
849850

850851
if ($event->commandShouldRun()) {
851852
try {
853+
$e = null;
852854
$exitCode = $command->run($input, $output);
853-
} catch (\Exception $e) {
855+
} catch (\Exception $x) {
856+
$e = $x;
857+
} catch (\Throwable $x) {
858+
$e = new FatalThrowableError($x);
859+
}
860+
if (null !== $e) {
854861
$event = new ConsoleExceptionEvent($command, $input, $output, $e, $e->getCode());
855862
$this->dispatcher->dispatch(ConsoleEvents::EXCEPTION, $event);
856863

857-
$e = $event->getException();
864+
if ($e !== $event->getException()) {
865+
$x = $e = $event->getException();
866+
}
858867

859868
$event = new ConsoleTerminateEvent($command, $input, $output, $e->getCode());
860869
$this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event);
861870

862-
throw $e;
871+
throw $x;
863872
}
864873
} else {
865874
$exitCode = ConsoleCommandEvent::RETURN_CODE_DISABLED;

src/Symfony/Component/Console/Tests/ApplicationTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,62 @@ public function testRunDispatchesAllEventsWithException()
949949
$this->assertContains('before.foo.caught.after.', $tester->getDisplay());
950950
}
951951

952+
/**
953+
* @expectedException \LogicException
954+
* @expectedExceptionMessage caught
955+
*/
956+
public function testRunWithErrorAndDispatcher()
957+
{
958+
$application = new Application();
959+
$application->setDispatcher($this->getDispatcher());
960+
$application->setAutoExit(false);
961+
$application->setCatchExceptions(false);
962+
963+
$application->register('dym')->setCode(function (InputInterface $input, OutputInterface $output) {
964+
$output->write('dym.');
965+
966+
throw new \Error('dymerr');
967+
});
968+
969+
$tester = new ApplicationTester($application);
970+
$tester->run(array('command' => 'dym'));
971+
$this->assertContains('before.dym.caught.after.', $tester->getDisplay(), 'The PHP Error did not dispached events');
972+
}
973+
974+
public function testRunDispatchesAllEventsWithError()
975+
{
976+
$application = new Application();
977+
$application->setDispatcher($this->getDispatcher());
978+
$application->setAutoExit(false);
979+
980+
$application->register('dym')->setCode(function (InputInterface $input, OutputInterface $output) {
981+
$output->write('dym.');
982+
983+
throw new \Error('dymerr');
984+
});
985+
986+
$tester = new ApplicationTester($application);
987+
$tester->run(array('command' => 'dym'));
988+
$this->assertContains('before.dym.caught.after.', $tester->getDisplay(), 'The PHP Error did not dispached events');
989+
}
990+
991+
public function testRunWithErrorFailingStatusCode()
992+
{
993+
$application = new Application();
994+
$application->setDispatcher($this->getDispatcher());
995+
$application->setAutoExit(false);
996+
997+
$application->register('dus')->setCode(function (InputInterface $input, OutputInterface $output) {
998+
$output->write('dus.');
999+
1000+
throw new \Error('duserr');
1001+
});
1002+
1003+
$tester = new ApplicationTester($application);
1004+
$tester->run(array('command' => 'dus'));
1005+
$this->assertSame(1, $tester->getStatusCode(), 'Status code should be 1');
1006+
}
1007+
9521008
public function testRunWithDispatcherSkippingCommand()
9531009
{
9541010
$application = new Application();

src/Symfony/Component/Console/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
}
1717
],
1818
"require": {
19-
"php": ">=5.3.9"
19+
"php": ">=5.3.9",
20+
"symfony/debug": "~2.7,>=2.7.2"
2021
},
2122
"require-dev": {
2223
"symfony/event-dispatcher": "~2.1",

0 commit comments

Comments
 (0)
0