Skip to content

Commit

Permalink
allow to mock local activities
Browse files Browse the repository at this point in the history
  • Loading branch information
cappuc committed Oct 13, 2022
1 parent a62f420 commit 13d76c6
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased
- Allow to mock workflows without a running temporal server and worker
- Allow to mock local activities

## v0.4
- Added support for `Eloquent` models serialization/deserialization
Expand Down
6 changes: 3 additions & 3 deletions src/Builder/LocalActivityBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace Keepsuit\LaravelTemporal\Builder;

use InvalidArgumentException;
use Keepsuit\LaravelTemporal\Facade\Temporal;
use Temporal\Activity\LocalActivityOptions;
use Temporal\Common\RetryOptions;
use Temporal\Internal\Workflow\ActivityProxy;
use Temporal\Workflow;
use Temporal\Workflow\ActivityStubInterface;

/**
Expand Down Expand Up @@ -39,12 +39,12 @@ public static function new(): LocalActivityBuilder
*/
public function build(string $class)
{
return Workflow::newActivityStub($class, $this->activityOptions);
return Temporal::getTemporalContext()->newActivityStub($class, $this->activityOptions);
}

public function buildUntyped(): ActivityStubInterface
{
return Workflow::newUntypedActivityStub($this->activityOptions);
return Temporal::getTemporalContext()->newUntypedActivityStub($this->activityOptions);
}

public function __call(string $name, array $arguments): self
Expand Down
9 changes: 6 additions & 3 deletions src/Testing/Fakes/FakeActivityStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use React\Promise\PromiseInterface;
use Temporal\Activity\ActivityOptions;
use Temporal\Activity\ActivityOptionsInterface;
use Temporal\Activity\LocalActivityOptions;
use Temporal\DataConverter\EncodedValues;
use Temporal\Workflow\ActivityStubInterface;

Expand All @@ -25,16 +26,18 @@ public function getOptions(): ActivityOptionsInterface

public function execute(string $name, array $args = [], $returnType = null, bool $isLocalActivity = false): PromiseInterface
{
/** @var ActivityOptions $options */
/** @var ActivityOptions|LocalActivityOptions $options */
$options = $this->getOptions();

$mock = $this->getTemporalMocker()->getActivityResult($name, $options->taskQueue);
$taskQueue = $isLocalActivity ? null : $options->taskQueue;

$mock = $this->getTemporalMocker()->getActivityResult($name, $taskQueue);

if (! $mock instanceof \Closure) {
return $this->stub->execute($name, $args, $returnType, $isLocalActivity);
}

$this->getTemporalMocker()->recordActivityDispatch($name, $options->taskQueue, $args);
$this->getTemporalMocker()->recordActivityDispatch($name, $taskQueue, $args);

$this->result = $mock->__invoke(...$args);

Expand Down
4 changes: 2 additions & 2 deletions src/Testing/TemporalMocker.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function mockActivityResult(string $activityName, mixed $activityResult,
$this->cache->saveActivityMock($activityName, $result, $taskQueue);
}

public function getActivityResult(string $activityName, string $taskQueue): ?Closure
public function getActivityResult(string $activityName, ?string $taskQueue): ?Closure
{
return $this->cache->getActivityMock($activityName, $taskQueue);
}
Expand All @@ -49,7 +49,7 @@ public function getWorkflowDispatches(string $workflowName): array
return $this->cache->getWorkflowDispatches($workflowName);
}

public function recordActivityDispatch(string $activityName, string $taskQueue, array $args): void
public function recordActivityDispatch(string $activityName, ?string $taskQueue, array $args): void
{
$this->cache->recordActivityDispatch($activityName, $taskQueue, $args);
}
Expand Down
5 changes: 3 additions & 2 deletions src/Testing/TemporalMockerCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ public function saveActivityMock(string $activityName, mixed $value, ?string $ta
]);
}

public function getActivityMock(string $activityName, string $taskQueue): ?Closure
public function getActivityMock(string $activityName, ?string $taskQueue): ?Closure
{
$value = $this->cache->get(sprintf('activity::%s', $activityName));

if (! is_array($value)) {
return null;
}
Expand Down Expand Up @@ -146,7 +147,7 @@ public function getWorkflowDispatches(string $workflowName): array
);
}

public function recordActivityDispatch(string $activityName, string $taskQueue, array $args): void
public function recordActivityDispatch(string $activityName, ?string $taskQueue, array $args): void
{
$cacheKey = sprintf('activity_dispatch::%s', $activityName);

Expand Down

0 comments on commit 13d76c6

Please sign in to comment.