Skip to content
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
4 changes: 2 additions & 2 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:
mysql -e 'CREATE DATABASE testbench' -h127.0.0.1 -uroot -ppassword -P ${{ job.services.mysql.ports[3306] }}

- name: Run test suite (MySQL)
run: vendor/bin/phpunit --testdox --testsuite feature
run: vendor/bin/phpunit --testdox --debug --testsuite feature
env:
APP_KEY: base64:i3g6f+dV8FfsIkcxqd7gbiPn2oXk5r00sTmdD6V5utI=
DB_CONNECTION: mysql
Expand All @@ -81,7 +81,7 @@ jobs:
REDIS_PORT: 6379

- name: Run test suite (PostgreSQL)
run: vendor/bin/phpunit --testdox --testsuite feature
run: vendor/bin/phpunit --testdox --debug --testsuite feature
env:
APP_KEY: base64:i3g6f+dV8FfsIkcxqd7gbiPn2oXk5r00sTmdD6V5utI=
DB_CONNECTION: pgsql
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
"autoload": {
"psr-4": {
"Workflow\\": "src/"
}
},
"files": [
"src/functions.php"
]
},
"autoload-dev": {
"psr-4": {
Expand Down
130 changes: 130 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

declare(strict_types=1);

namespace Workflow;

use Carbon\CarbonInterval;
use React\Promise\PromiseInterface;

if (! function_exists(__NAMESPACE__ . '\\await')) {
function await($condition): PromiseInterface
{
return WorkflowStub::await($condition);
}
}

if (! function_exists(__NAMESPACE__ . '\\timer')) {
function timer(int|string|CarbonInterval $seconds): PromiseInterface
{
return WorkflowStub::timer($seconds);
}
}

if (! function_exists(__NAMESPACE__ . '\\awaitWithTimeout')) {
function awaitWithTimeout(int|string|CarbonInterval $seconds, $condition): PromiseInterface
{
return WorkflowStub::awaitWithTimeout($seconds, $condition);
}
}

if (! function_exists(__NAMESPACE__ . '\\sideEffect')) {
function sideEffect($callable): PromiseInterface
{
return WorkflowStub::sideEffect($callable);
}
}

if (! function_exists(__NAMESPACE__ . '\\continueAsNew')) {
function continueAsNew(...$arguments): PromiseInterface
{
return WorkflowStub::continueAsNew(...$arguments);
}
}

if (! function_exists(__NAMESPACE__ . '\\getVersion')) {
function getVersion(
string $changeId,
int $minSupported = WorkflowStub::DEFAULT_VERSION,
int $maxSupported = 1
): PromiseInterface {
return WorkflowStub::getVersion($changeId, $minSupported, $maxSupported);
}
}

if (! function_exists(__NAMESPACE__ . '\\activity')) {
function activity($activity, ...$arguments): PromiseInterface
{
return ActivityStub::make($activity, ...$arguments);
}
}

if (! function_exists(__NAMESPACE__ . '\\child')) {
function child($workflow, ...$arguments): PromiseInterface
{
return ChildWorkflowStub::make($workflow, ...$arguments);
}
}

if (! function_exists(__NAMESPACE__ . '\\all')) {
function all(iterable $promises): PromiseInterface
{
return \React\Promise\all([...$promises]);
}
}

if (! function_exists(__NAMESPACE__ . '\\async')) {
function async(callable $callback): PromiseInterface
{
return ActivityStub::async($callback);
}
}

if (! function_exists(__NAMESPACE__ . '\\seconds')) {
function seconds(int $seconds): PromiseInterface
{
return WorkflowStub::timer($seconds);
}
}

if (! function_exists(__NAMESPACE__ . '\\minutes')) {
function minutes(int $minutes): PromiseInterface
{
return WorkflowStub::timer($minutes * 60);
}
}

if (! function_exists(__NAMESPACE__ . '\\hours')) {
function hours(int $hours): PromiseInterface
{
return WorkflowStub::timer($hours * 3600);
}
}

if (! function_exists(__NAMESPACE__ . '\\days')) {
function days(int $days): PromiseInterface
{
return WorkflowStub::timer($days * 86400);
}
}

if (! function_exists(__NAMESPACE__ . '\\weeks')) {
function weeks(int $weeks): PromiseInterface
{
return WorkflowStub::timer($weeks * 604800);
}
}

if (! function_exists(__NAMESPACE__ . '\\months')) {
function months(int $months): PromiseInterface
{
return WorkflowStub::timer("{$months} months");
}
}

if (! function_exists(__NAMESPACE__ . '\\years')) {
function years(int $years): PromiseInterface
{
return WorkflowStub::timer("{$years} years");
}
}
11 changes: 5 additions & 6 deletions tests/Fixtures/TestActivityAwaitActivityAwaitWorkflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

namespace Tests\Fixtures;

use Workflow\ActivityStub;
use Workflow\SignalMethod;
use Workflow\Workflow;
use Workflow\WorkflowStub;
use function Workflow\{activity, await};

final class TestActivityAwaitActivityAwaitWorkflow extends Workflow
{
Expand All @@ -29,13 +28,13 @@ public function approveSecond(bool $value): void

public function execute()
{
yield ActivityStub::make(TestActivity::class);
yield activity(TestActivity::class);

yield WorkflowStub::await(fn () => $this->firstApproved);
yield await(fn () => $this->firstApproved);

yield ActivityStub::make(TestActivity::class);
yield activity(TestActivity::class);

yield WorkflowStub::await(fn () => $this->secondApproved);
yield await(fn () => $this->secondApproved);

return 'completed';
}
Expand Down
7 changes: 3 additions & 4 deletions tests/Fixtures/TestActivityThenAwaitWorkflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

namespace Tests\Fixtures;

use Workflow\ActivityStub;
use Workflow\SignalMethod;
use Workflow\Workflow;
use Workflow\WorkflowStub;
use function Workflow\{activity, await};

final class TestActivityThenAwaitWorkflow extends Workflow
{
Expand All @@ -21,9 +20,9 @@ public function approve(bool $value): void

public function execute()
{
yield ActivityStub::make(TestActivity::class);
yield activity(TestActivity::class);

yield WorkflowStub::await(fn () => $this->approved);
yield await(fn () => $this->approved);

return 'approved';
}
Expand Down
7 changes: 3 additions & 4 deletions tests/Fixtures/TestActivityThrowsAwaitRetryWorkflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
namespace Tests\Fixtures;

use Throwable;
use Workflow\ActivityStub;
use Workflow\SignalMethod;
use Workflow\Workflow;
use Workflow\WorkflowStub;
use function Workflow\{activity, await};

final class TestActivityThrowsAwaitRetryWorkflow extends Workflow
{
Expand All @@ -29,10 +28,10 @@ public function execute()
$shouldThrow = true;
while (true) {
try {
yield ActivityStub::make(TestSingleTryExceptionActivity::class, $shouldThrow);
yield activity(TestSingleTryExceptionActivity::class, $shouldThrow);
return true;
} catch (Throwable) {
yield WorkflowStub::await(fn () => $this->shouldRetry);
yield await(fn () => $this->shouldRetry);
$this->shouldRetry = false;
$shouldThrow = false;
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Fixtures/TestAsyncWorkflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
namespace Tests\Fixtures;

use Illuminate\Contracts\Foundation\Application;
use Workflow\ActivityStub;
use Workflow\Workflow;
use function Workflow\{activity, async};

final class TestAsyncWorkflow extends Workflow
{
public function execute()
{
$results = yield ActivityStub::async(static function (Application $app) {
$results = yield async(static function (Application $app) {
assert($app->runningInConsole());

$otherResult = yield ActivityStub::make(TestOtherActivity::class, 'other');
$otherResult = yield activity(TestOtherActivity::class, 'other');

$result = yield ActivityStub::make(TestActivity::class);
$result = yield activity(TestActivity::class);

return [$otherResult, $result];
});
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixtures/TestAwaitWithTimeoutWorkflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace Tests\Fixtures;

use function Workflow\awaitWithTimeout;
use Workflow\Workflow;
use Workflow\WorkflowStub;

final class TestAwaitWithTimeoutWorkflow extends Workflow
{
public function execute($shouldTimeout = false)
{
$result = yield WorkflowStub::awaitWithTimeout(5, static fn (): bool => ! $shouldTimeout);
$result = yield awaitWithTimeout(5, static fn (): bool => ! $shouldTimeout);

return $result ? 'workflow' : 'workflow_timed_out';
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixtures/TestAwaitWorkflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Tests\Fixtures;

use function Workflow\await;
use Workflow\SignalMethod;
use Workflow\Workflow;
use Workflow\WorkflowStub;

final class TestAwaitWorkflow extends Workflow
{
Expand All @@ -20,7 +20,7 @@ public function cancel(): void

public function execute()
{
yield WorkflowStub::await(fn (): bool => $this->canceled);
yield await(fn (): bool => $this->canceled);

return 'workflow';
}
Expand Down
9 changes: 4 additions & 5 deletions tests/Fixtures/TestBadConnectionWorkflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
namespace Tests\Fixtures;

use Illuminate\Contracts\Foundation\Application;
use Workflow\ActivityStub;
use Workflow\QueryMethod;
use Workflow\SignalMethod;
use Workflow\Workflow;
use Workflow\WorkflowStub;
use function Workflow\{activity, await};

class TestBadConnectionWorkflow extends Workflow
{
Expand Down Expand Up @@ -39,15 +38,15 @@ public function execute(Application $app, $shouldAssert = false)
assert(! $this->canceled);
}

$otherResult = yield ActivityStub::make(TestOtherActivity::class, 'other');
$otherResult = yield activity(TestOtherActivity::class, 'other');

if ($shouldAssert) {
assert(! $this->canceled);
}

yield WorkflowStub::await(fn (): bool => $this->canceled);
yield await(fn (): bool => $this->canceled);

$result = yield ActivityStub::make(TestActivity::class);
$result = yield activity(TestActivity::class);

return 'workflow_' . $result . '_' . $otherResult;
}
Expand Down
7 changes: 3 additions & 4 deletions tests/Fixtures/TestChildContinueAsNewWorkflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@

namespace Tests\Fixtures;

use Workflow\ActivityStub;
use Workflow\Workflow;
use Workflow\WorkflowStub;
use function Workflow\{activity, continueAsNew};

class TestChildContinueAsNewWorkflow extends Workflow
{
public function execute(int $count = 0, int $totalCount = 3)
{
$activityResult = yield ActivityStub::make(TestCountActivity::class, $count);
$activityResult = yield activity(TestCountActivity::class, $count);

if ($count >= $totalCount) {
return 'child_workflow_' . $activityResult;
}

return yield WorkflowStub::continueAsNew($count + 1, $totalCount);
return yield continueAsNew($count + 1, $totalCount);
}
}
4 changes: 2 additions & 2 deletions tests/Fixtures/TestChildExceptionWorkflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Tests\Fixtures;

use Exception;
use Workflow\ActivityStub;
use function Workflow\activity;
use Workflow\Workflow;

class TestChildExceptionWorkflow extends Workflow
Expand All @@ -20,7 +20,7 @@ public function execute($shouldThrow = false)
throw new Exception('failed');
}

$otherResult = yield ActivityStub::make(TestOtherActivity::class, 'other');
$otherResult = yield activity(TestOtherActivity::class, 'other');

return $otherResult;
}
Expand Down
7 changes: 3 additions & 4 deletions tests/Fixtures/TestChildTimerWorkflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

namespace Tests\Fixtures;

use Workflow\ActivityStub;
use Workflow\Workflow;
use Workflow\WorkflowStub;
use function Workflow\{activity, timer};

class TestChildTimerWorkflow extends Workflow
{
Expand All @@ -16,9 +15,9 @@ class TestChildTimerWorkflow extends Workflow

public function execute($seconds = 1)
{
$otherResult = yield ActivityStub::make(TestOtherActivity::class, 'other');
$otherResult = yield activity(TestOtherActivity::class, 'other');

yield WorkflowStub::timer($seconds);
yield timer($seconds);

return $otherResult;
}
Expand Down
Loading