Skip to content

Commit

Permalink
test full workflow execution
Browse files Browse the repository at this point in the history
  • Loading branch information
cappuc committed Jan 2, 2024
1 parent a696cfc commit 54eafe3
Show file tree
Hide file tree
Showing 14 changed files with 166 additions and 22 deletions.
7 changes: 6 additions & 1 deletion tests/Fixtures/WorkflowDiscovery/Activities/DemoActivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

namespace Keepsuit\LaravelTemporal\Tests\Fixtures\WorkflowDiscovery\Activities;

class DemoActivity implements DemoActivityInterface
use Temporal\Activity\ActivityInterface;
use Temporal\Activity\ActivityMethod;

#[ActivityInterface(prefix: 'demo')]
class DemoActivity
{
#[ActivityMethod(name: 'greet')]
public function greet(string $name): string
{
return sprintf('Hello %s', $name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Temporal\Activity\ActivityInterface;
use Temporal\Activity\ActivityMethod;

#[ActivityInterface(prefix: 'demo')]
#[ActivityInterface(prefix: 'demo_interface')]
interface DemoActivityInterface
{
#[ActivityMethod(name: 'greet')]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Keepsuit\LaravelTemporal\Tests\Fixtures\WorkflowDiscovery\Activities;

class DemoActivityWithInterface implements DemoActivityInterface
{
public function greet(string $name): string
{
return sprintf('Hello %s', $name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

namespace Keepsuit\LaravelTemporal\Tests\Fixtures\WorkflowDiscovery\Activities;

use Temporal\Activity\ActivityInterface;
use Temporal\Activity\ActivityMethod;
use Temporal\Activity\LocalActivityInterface;

#[ActivityInterface(prefix: 'demo_without_interface')]
class DemoActivityWithoutInterface
#[LocalActivityInterface(prefix: 'demo_local')]
class DemoLocalActivity
{
#[ActivityMethod(name: 'greet')]
public function greet(string $name): string
{
return sprintf('Hello %s', $name);
return sprintf('(local) Hello %s', $name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
use Temporal\Workflow\WorkflowMethod;

#[WorkflowInterface]
class DebugOptionsWorkflow
class ActivityOptionsWorkflow
{
#[WorkflowMethod(name: 'debug.activityOptions')]
#[WorkflowMethod(name: 'activityOptions')]
#[Workflow\ReturnType('array')]
public function activityOptions(): \Generator
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixtures/WorkflowDiscovery/Workflows/DemoWorkflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Carbon\CarbonInterval;
use Keepsuit\LaravelTemporal\Tests\Fixtures\WorkflowDiscovery\Activities\DemoActivityInterface;
use Temporal\Activity\LocalActivityOptions;
use Temporal\Activity\ActivityOptions;
use Temporal\Workflow;

class DemoWorkflow implements DemoWorkflowInterface
Expand All @@ -18,7 +18,7 @@ public function __construct()
{
$this->activity = Workflow::newActivityStub(
DemoActivityInterface::class,
LocalActivityOptions::new()
ActivityOptions::new()
->withStartToCloseTimeout(CarbonInterval::seconds(1))
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace Keepsuit\LaravelTemporal\Tests\Fixtures\WorkflowDiscovery\Workflows;

use Temporal\Workflow\ReturnType;
use Temporal\Workflow\WorkflowInterface;
use Temporal\Workflow\WorkflowMethod;

#[WorkflowInterface]
interface DemoWorkflowInterface
{
#[WorkflowMethod(name: 'demo.greet')]
#[ReturnType('string')]
public function greet(string $name): \Generator;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Keepsuit\LaravelTemporal\Tests\Fixtures\WorkflowDiscovery\Workflows;

use Carbon\CarbonInterval;
use Keepsuit\LaravelTemporal\Tests\Fixtures\WorkflowDiscovery\Activities\DemoActivityWithoutInterface;
use Keepsuit\LaravelTemporal\Tests\Fixtures\WorkflowDiscovery\Activities\DemoActivity;
use Temporal\Activity\ActivityOptions;
use Temporal\Workflow;
use Temporal\Workflow\WorkflowInterface;
Expand All @@ -16,7 +16,7 @@ class DemoWorkflowWithoutInterface
public function greet(string $name): \Generator
{
$activity = Workflow::newActivityStub(
DemoActivityWithoutInterface::class,
DemoActivity::class,
ActivityOptions::new()->withStartToCloseTimeout(CarbonInterval::seconds(1))
);

Expand Down
58 changes: 58 additions & 0 deletions tests/Fixtures/WorkflowDiscovery/Workflows/TesterWorkflow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Keepsuit\LaravelTemporal\Tests\Fixtures\WorkflowDiscovery\Workflows;

use Carbon\CarbonInterval;
use Keepsuit\LaravelTemporal\Facade\Temporal;
use Keepsuit\LaravelTemporal\Tests\Fixtures\WorkflowDiscovery\Activities\DemoActivityInterface;
use Keepsuit\LaravelTemporal\Tests\Fixtures\WorkflowDiscovery\Activities\DemoActivityWithInterface;
use Keepsuit\LaravelTemporal\Tests\Fixtures\WorkflowDiscovery\Activities\DemoLocalActivity;
use Temporal\Internal\Workflow\ActivityProxy;
use Temporal\Workflow;
use Temporal\Workflow\WorkflowInterface;
use Temporal\Workflow\WorkflowMethod;

#[WorkflowInterface]
class TesterWorkflow
{
/**
* @var ActivityProxy<DemoActivityWithInterface>
*/
protected ActivityProxy $activity;

/**
* @var ActivityProxy<DemoLocalActivity>
*/
protected ActivityProxy $localActivity;

public function __construct()
{
$this->activity = Temporal::newActivity()
->withStartToCloseTimeout(CarbonInterval::seconds(1))
->build(DemoActivityInterface::class);

$this->localActivity = Temporal::newLocalActivity()
->withStartToCloseTimeout(CarbonInterval::seconds(1))
->build(DemoLocalActivity::class);

$this->childWorkflow = Temporal::newChildWorkflow()
->withWorkflowRunTimeout(CarbonInterval::seconds(1))
->build(DemoWorkflow::class);
}

#[WorkflowMethod(name: 'tester')]
#[Workflow\ReturnType('array')]
public function tester(string $target): \Generator
{
return match ($target) {
'activity' => ['activity' => yield $this->activity->greet('John')],
'local_activity' => ['local_activity' => yield $this->localActivity->greet('John')],
'child' => ['child' => sprintf('(child) %s', yield $this->childWorkflow->greet('John'))],
default => [
'activity' => yield $this->activity->greet('John'),
'local_activity' => yield $this->localActivity->greet('John'),
'child' => sprintf('(child) %s', yield $this->childWorkflow->greet('John')),
]
};
}
}
59 changes: 59 additions & 0 deletions tests/Integrations/Temporal/FullWorkflowExecutionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

use Carbon\CarbonInterval;
use Keepsuit\LaravelTemporal\Facade\Temporal;
use Keepsuit\LaravelTemporal\Testing\WithTemporal;
use Keepsuit\LaravelTemporal\Tests\Fixtures\WorkflowDiscovery\Workflows\TesterWorkflow;
use Temporal\Common\RetryOptions;

uses(WithTemporal::class);

beforeEach(function () {
Temporal::fake();
});

it('execute workflow with activity', function () {
$workflow = Temporal::newWorkflow()
->withWorkflowRunTimeout(CarbonInterval::seconds(2))
->build(TesterWorkflow::class);

$result = $workflow->tester('activity');

expect($result)->toBe(['activity' => 'Hello John']);
});

it('execute workflow with local activity', function () {
$workflow = Temporal::newWorkflow()
->withWorkflowRunTimeout(CarbonInterval::seconds(2))
->build(TesterWorkflow::class);

$result = $workflow->tester('local_activity');

expect($result)->toBe(['local_activity' => '(local) Hello John']);
});

it('execute workflow with child', function () {
$workflow = Temporal::newWorkflow()
->withWorkflowRunTimeout(CarbonInterval::seconds(2))
->withRetryOptions(RetryOptions::new()->withMaximumAttempts(2))
->build(TesterWorkflow::class);

$result = $workflow->tester('child');

expect($result)->toBe(['child' => '(child) Hello John']);
});

it('execute workflow full', function () {
$workflow = Temporal::newWorkflow()
->withWorkflowRunTimeout(CarbonInterval::seconds(2))
->withRetryOptions(RetryOptions::new()->withMaximumAttempts(2))
->build(TesterWorkflow::class);

$result = $workflow->tester('all');

expect($result)->toBe([
'activity' => 'Hello John',
'local_activity' => '(local) Hello John',
'child' => '(child) Hello John',
]);
});
1 change: 1 addition & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Temporal\Workflow\ScopedContextInterface;

uses(TestCase::class)->in(__DIR__.'/Unit');
uses(TestCase::class)->in(__DIR__.'/Integrations/Temporal');
uses(LaravelDataTestCase::class)->in(__DIR__.'/Integrations/LaravelData');

/**
Expand Down
12 changes: 8 additions & 4 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
use Illuminate\Config\Repository;
use Illuminate\Database\Eloquent\Factories\Factory;
use Keepsuit\LaravelTemporal\LaravelTemporalServiceProvider;
use Keepsuit\LaravelTemporal\Tests\Fixtures\WorkflowDiscovery\Activities\DemoActivity;
use Keepsuit\LaravelTemporal\Tests\Fixtures\WorkflowDiscovery\Workflows\DebugOptionsWorkflow;
use Keepsuit\LaravelTemporal\Tests\Fixtures\WorkflowDiscovery\Activities\DemoActivityWithInterface;
use Keepsuit\LaravelTemporal\Tests\Fixtures\WorkflowDiscovery\Activities\DemoLocalActivity;
use Keepsuit\LaravelTemporal\Tests\Fixtures\WorkflowDiscovery\Workflows\ActivityOptionsWorkflow;
use Keepsuit\LaravelTemporal\Tests\Fixtures\WorkflowDiscovery\Workflows\DemoWorkflow;
use Keepsuit\LaravelTemporal\Tests\Fixtures\WorkflowDiscovery\Workflows\TesterWorkflow;
use Orchestra\Testbench\TestCase as Orchestra;

class TestCase extends Orchestra
Expand All @@ -33,11 +35,13 @@ public function defineEnvironment($app)
tap($app['config'], function (Repository $config) {
$config->set('database.default', 'testing');
$config->set('temporal.workflows', [
DebugOptionsWorkflow::class,
ActivityOptionsWorkflow::class,
DemoWorkflow::class,
TesterWorkflow::class,
]);
$config->set('temporal.activities', [
DemoActivity::class,
DemoActivityWithInterface::class,
DemoLocalActivity::class,
]);
});
}
Expand Down
10 changes: 6 additions & 4 deletions tests/Unit/Discovery/ActivityDiscoveryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
use Keepsuit\LaravelTemporal\Tests\Fixtures\WorkflowDiscovery\Activities\DemoActivity;
use Keepsuit\LaravelTemporal\Tests\Fixtures\WorkflowDiscovery\Activities\DemoActivityInterface;
use Keepsuit\LaravelTemporal\Tests\Fixtures\WorkflowDiscovery\Activities\DemoActivityInterfaceOnly;
use Keepsuit\LaravelTemporal\Tests\Fixtures\WorkflowDiscovery\Activities\DemoActivityWithoutInterface;
use Keepsuit\LaravelTemporal\Tests\Fixtures\WorkflowDiscovery\Activities\DemoActivityWithInterface;
use Keepsuit\LaravelTemporal\Tests\Fixtures\WorkflowDiscovery\Activities\DemoLocalActivity;

it('can discovery activities', function () {
class_alias(DemoActivity::class, 'Tests\Fixtures\WorkflowDiscovery\Activities\DemoActivity');
class_alias(DemoActivityWithInterface::class, 'Tests\Fixtures\WorkflowDiscovery\Activities\DemoActivity');
class_alias(DemoActivityInterface::class, 'Tests\Fixtures\WorkflowDiscovery\Activities\DemoActivityInterface');
class_alias(DemoActivityInterfaceOnly::class, 'Tests\Fixtures\WorkflowDiscovery\Activities\DemoActivityInterfaceOnly');
class_alias(DemoActivityWithoutInterface::class, 'Tests\Fixtures\WorkflowDiscovery\Activities\DemoActivityWithoutInterface');
class_alias(DemoActivity::class, 'Tests\Fixtures\WorkflowDiscovery\Activities\DemoActivityWithoutInterface');

$activities = DiscoverActivities::within(
__DIR__.'/../../Fixtures/WorkflowDiscovery/Activities',
Expand All @@ -19,6 +20,7 @@ class_alias(DemoActivityWithoutInterface::class, 'Tests\Fixtures\WorkflowDiscove
expect($activities)->toBe([
DemoActivity::class,
DemoActivityInterfaceOnly::class,
DemoActivityWithoutInterface::class,
DemoActivityWithInterface::class,
DemoLocalActivity::class,
]);
});
6 changes: 4 additions & 2 deletions tests/Unit/Discovery/WorkflowDiscoveryTest.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
<?php

use Keepsuit\LaravelTemporal\Support\DiscoverWorkflows;
use Keepsuit\LaravelTemporal\Tests\Fixtures\WorkflowDiscovery\Workflows\DebugOptionsWorkflow;
use Keepsuit\LaravelTemporal\Tests\Fixtures\WorkflowDiscovery\Workflows\ActivityOptionsWorkflow;
use Keepsuit\LaravelTemporal\Tests\Fixtures\WorkflowDiscovery\Workflows\DemoWorkflow;
use Keepsuit\LaravelTemporal\Tests\Fixtures\WorkflowDiscovery\Workflows\DemoWorkflowInterfaceOnly;
use Keepsuit\LaravelTemporal\Tests\Fixtures\WorkflowDiscovery\Workflows\DemoWorkflowWithoutInterface;
use Keepsuit\LaravelTemporal\Tests\Fixtures\WorkflowDiscovery\Workflows\TesterWorkflow;

it('can discovery workflows', function () {
$workflows = DiscoverWorkflows::within(
__DIR__.'/../../Fixtures/WorkflowDiscovery/Workflows',
);

expect($workflows)->toBe([
DebugOptionsWorkflow::class,
ActivityOptionsWorkflow::class,
DemoWorkflow::class,
DemoWorkflowInterfaceOnly::class,
DemoWorkflowWithoutInterface::class,
TesterWorkflow::class,
]);
});

0 comments on commit 54eafe3

Please sign in to comment.