From 46686ae80466bcdf6d9fe1931aeb1408f1f1f72a Mon Sep 17 00:00:00 2001 From: Joseph Date: Mon, 17 Aug 2020 12:28:28 -0400 Subject: [PATCH 1/2] simulate exit code for closure scheduled tasks --- .../Console/Scheduling/CallbackEvent.php | 7 +++ .../Console/Scheduling/CallbackEventTest.php | 61 +++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 tests/Console/Scheduling/CallbackEventTest.php diff --git a/src/Illuminate/Console/Scheduling/CallbackEvent.php b/src/Illuminate/Console/Scheduling/CallbackEvent.php index 96b1c1e1bfbd..f1df43c0b6fd 100644 --- a/src/Illuminate/Console/Scheduling/CallbackEvent.php +++ b/src/Illuminate/Console/Scheduling/CallbackEvent.php @@ -5,6 +5,7 @@ use Illuminate\Contracts\Container\Container; use InvalidArgumentException; use LogicException; +use Exception; class CallbackEvent extends Event { @@ -76,12 +77,18 @@ public function run(Container $container) $response = is_object($this->callback) ? $container->call([$this->callback, '__invoke'], $this->parameters) : $container->call($this->callback, $this->parameters); + } catch(Exception $e) { + $this->exitCode = 1; + + throw $e; } finally { $this->removeMutex(); parent::callAfterCallbacks($container); } + $this->exitCode = $response === false ? 1 : 0; + return $response; } diff --git a/tests/Console/Scheduling/CallbackEventTest.php b/tests/Console/Scheduling/CallbackEventTest.php new file mode 100644 index 000000000000..dd6cbf5ff6b1 --- /dev/null +++ b/tests/Console/Scheduling/CallbackEventTest.php @@ -0,0 +1,61 @@ +run($this->app); + + $this->assertSame(0, $event->exitCode); + } + + public function testFalseResponseIsFailure() + { + $event = new CallbackEvent(m::mock(EventMutex::class), function() { + return false; + }); + + $event->run($this->app); + + $this->assertSame(1, $event->exitCode); + } + + public function testExceptionIsFailure() + { + $event = new CallbackEvent(m::mock(EventMutex::class), function() { + throw new \Exception; + }); + + try { + $event->run($this->app); + } catch(Exception $e) {} + + $this->assertSame(1, $event->exitCode); + } + + public function testExceptionBubbles() + { + $event = new CallbackEvent(m::mock(EventMutex::class), function() { + throw new Exception; + }); + + $this->expectException(Exception::class); + + $event->run($this->app); + } +} From 3c8fde2b6e9f72933b6cf7142717c84a91747c1c Mon Sep 17 00:00:00 2001 From: Joseph Date: Mon, 17 Aug 2020 13:13:21 -0400 Subject: [PATCH 2/2] styleci --- .../Console/Scheduling/CallbackEvent.php | 4 ++-- tests/Console/Scheduling/CallbackEventTest.php | 14 ++++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Illuminate/Console/Scheduling/CallbackEvent.php b/src/Illuminate/Console/Scheduling/CallbackEvent.php index f1df43c0b6fd..45d0c2a3d324 100644 --- a/src/Illuminate/Console/Scheduling/CallbackEvent.php +++ b/src/Illuminate/Console/Scheduling/CallbackEvent.php @@ -2,10 +2,10 @@ namespace Illuminate\Console\Scheduling; +use Exception; use Illuminate\Contracts\Container\Container; use InvalidArgumentException; use LogicException; -use Exception; class CallbackEvent extends Event { @@ -77,7 +77,7 @@ public function run(Container $container) $response = is_object($this->callback) ? $container->call([$this->callback, '__invoke'], $this->parameters) : $container->call($this->callback, $this->parameters); - } catch(Exception $e) { + } catch (Exception $e) { $this->exitCode = 1; throw $e; diff --git a/tests/Console/Scheduling/CallbackEventTest.php b/tests/Console/Scheduling/CallbackEventTest.php index dd6cbf5ff6b1..33c448e1e56c 100644 --- a/tests/Console/Scheduling/CallbackEventTest.php +++ b/tests/Console/Scheduling/CallbackEventTest.php @@ -2,11 +2,11 @@ namespace Illuminate\Tests\Console\Scheduling; +use Exception; use Illuminate\Console\Scheduling\CallbackEvent; use Illuminate\Console\Scheduling\EventMutex; use Mockery as m; use Orchestra\Testbench\TestCase; -use Exception; class CallbackEventTest extends TestCase { @@ -17,7 +17,8 @@ protected function tearDown(): void public function testDefaultResultIsSuccess() { - $event = new CallbackEvent(m::mock(EventMutex::class), function() {}); + $event = new CallbackEvent(m::mock(EventMutex::class), function () { + }); $event->run($this->app); @@ -26,7 +27,7 @@ public function testDefaultResultIsSuccess() public function testFalseResponseIsFailure() { - $event = new CallbackEvent(m::mock(EventMutex::class), function() { + $event = new CallbackEvent(m::mock(EventMutex::class), function () { return false; }); @@ -37,20 +38,21 @@ public function testFalseResponseIsFailure() public function testExceptionIsFailure() { - $event = new CallbackEvent(m::mock(EventMutex::class), function() { + $event = new CallbackEvent(m::mock(EventMutex::class), function () { throw new \Exception; }); try { $event->run($this->app); - } catch(Exception $e) {} + } catch (Exception $e) { + } $this->assertSame(1, $event->exitCode); } public function testExceptionBubbles() { - $event = new CallbackEvent(m::mock(EventMutex::class), function() { + $event = new CallbackEvent(m::mock(EventMutex::class), function () { throw new Exception; });