Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Simulate exit code for closure scheduled tasks #33914

Merged
merged 2 commits into from
Aug 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Illuminate/Console/Scheduling/CallbackEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Console\Scheduling;

use Exception;
use Illuminate\Contracts\Container\Container;
use InvalidArgumentException;
use LogicException;
Expand Down Expand Up @@ -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;
}

Expand Down
63 changes: 63 additions & 0 deletions tests/Console/Scheduling/CallbackEventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

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;

class CallbackEventTest extends TestCase
{
protected function tearDown(): void
{
m::close();
}

public function testDefaultResultIsSuccess()
{
$event = new CallbackEvent(m::mock(EventMutex::class), function () {
});

$event->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);
}
}