Skip to content

Assert Dispatched #151

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

Merged
merged 8 commits into from
Nov 19, 2023
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
24 changes: 14 additions & 10 deletions src/ActivityStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,22 @@ public static function make($activity, ...$arguments): PromiseInterface
->whereIndex($context->index)
->first();

$mocks = WorkflowStub::mocks();
if (WorkflowStub::faked()) {
$mocks = WorkflowStub::mocks();

if (! $log && array_key_exists($activity, $mocks)) {
$result = $mocks[$activity];
if (! $log && array_key_exists($activity, $mocks)) {
$result = $mocks[$activity];

$log = $context->storedWorkflow->logs()
->create([
'index' => $context->index,
'now' => $context->now,
'class' => $activity,
'result' => Y::serialize(is_callable($result) ? $result($context, ...$arguments) : $result),
]);
$log = $context->storedWorkflow->logs()
->create([
'index' => $context->index,
'now' => $context->now,
'class' => $activity,
'result' => Y::serialize(is_callable($result) ? $result($context, ...$arguments) : $result),
]);

WorkflowStub::recordDispatched($activity, $arguments);
}
}

if ($log) {
Expand Down
24 changes: 14 additions & 10 deletions src/ChildWorkflowStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,22 @@ public static function make($workflow, ...$arguments): PromiseInterface
->whereIndex($context->index)
->first();

$mocks = WorkflowStub::mocks();
if (WorkflowStub::faked()) {
$mocks = WorkflowStub::mocks();

if (! $log && array_key_exists($workflow, $mocks)) {
$result = $mocks[$workflow];
if (! $log && array_key_exists($workflow, $mocks)) {
$result = $mocks[$workflow];

$log = $context->storedWorkflow->logs()
->create([
'index' => $context->index,
'now' => $context->now,
'class' => $workflow,
'result' => Y::serialize(is_callable($result) ? $result($context, ...$arguments) : $result),
]);
$log = $context->storedWorkflow->logs()
->create([
'index' => $context->index,
'now' => $context->now,
'class' => $workflow,
'result' => Y::serialize(is_callable($result) ? $result($context, ...$arguments) : $result),
]);

WorkflowStub::recordDispatched($workflow, $arguments);
}
}

if ($log) {
Expand Down
108 changes: 108 additions & 0 deletions src/Traits/Fakes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

declare(strict_types=1);

namespace Workflow\Traits;

use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;

trait Fakes
{
protected static string $DISPATCHED_LIST = 'workflow.dispatched';

protected static string $MOCKS_LIST = 'workflow.mocks';

public static function faked(): bool
{
return App::bound(static::$MOCKS_LIST);
}

public static function fake(): void
{
App::bind(static::$MOCKS_LIST, static function () {
return [];
});

App::bind(static::$DISPATCHED_LIST, static function () {
return [];
});

static::macro('mocks', static function () {
return App::make(static::$MOCKS_LIST);
});

static::macro('mock', static function ($class, $result) {
$mocks = static::mocks();

App::bind(static::$MOCKS_LIST, static function () use ($mocks, $class, $result) {
$mocks[$class] = $result;
return $mocks;
});
});

static::macro('recordDispatched', static function ($class, $arguments) {
$dispatched = App::make(static::$DISPATCHED_LIST);

App::bind(static::$DISPATCHED_LIST, static function () use ($dispatched, $class, $arguments) {
if (! isset($dispatched[$class])) {
$dispatched[$class] = [];
}

$dispatched[$class][] = $arguments;

return $dispatched;
});
});

static::macro('assertDispatched', static function (string $workflowOrActivity, $callback = null) {
if (is_int($callback)) {
self::assertDispatchedTimes($workflowOrActivity, $callback);
return;
}

\PHPUnit\Framework\Assert::assertTrue(
self::dispatched($workflowOrActivity, $callback)->count() > 0,
"The expected [{$workflowOrActivity}] workflow/activity was not dispatched."
);
});

static::macro('assertDispatchedTimes', static function (string $workflowOrActivity, int $times = 1) {
$count = self::dispatched($workflowOrActivity)->count();

\PHPUnit\Framework\Assert::assertSame(
$times,
$count,
"The expected [{$workflowOrActivity}] workflow/activity was dispatched {$count} times instead of {$times} times."
);
});

static::macro('assertNotDispatched', static function (string $workflowOrActivity, $callback = null) {
\PHPUnit\Framework\Assert::assertTrue(
self::dispatched($workflowOrActivity, $callback)->count() === 0,
"The unexpected [{$workflowOrActivity}] workflow/activity was dispatched."
);
});

static::macro('assertNothingDispatched', static function () {
$dispatched = App::make(self::$DISPATCHED_LIST);
\PHPUnit\Framework\Assert::assertTrue(
count($dispatched) === 0,
'An unexpected workflow/activity was dispatched.'
);
});

static::macro('dispatched', static function (string $workflowOrActivity, $callback = null): Collection {
$dispatched = App::make(self::$DISPATCHED_LIST);
if (! isset($dispatched[$workflowOrActivity])) {
return collect();
}

$callback = $callback ?: static fn () => true;

return collect($dispatched[$workflowOrActivity])->filter(
static fn ($arguments) => $callback(...$arguments)
);
});
}
}
41 changes: 4 additions & 37 deletions src/WorkflowStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Illuminate\Database\QueryException;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Traits\Macroable;
use LimitIterator;
use ReflectionClass;
use SplFileObject;
Expand All @@ -21,18 +21,19 @@
use Workflow\States\WorkflowPendingStatus;
use Workflow\Traits\Awaits;
use Workflow\Traits\AwaitWithTimeouts;
use Workflow\Traits\Fakes;
use Workflow\Traits\SideEffects;
use Workflow\Traits\Timers;

final class WorkflowStub
{
use Awaits;
use AwaitWithTimeouts;
use Fakes;
use Macroable;
use SideEffects;
use Timers;

public const MOCKS_LIST = 'workflow.mocks';

private static ?\stdClass $context = null;

private function __construct(
Expand Down Expand Up @@ -84,40 +85,6 @@ public function __call($method, $arguments)
}
}

public static function fake(): void
{
App::bind(static::MOCKS_LIST, static function ($app) {
return [];
});
}

public static function faked(): bool
{
return App::bound(static::MOCKS_LIST);
}

public static function mock($class, $result)
{
if (! static::faked()) {
return;
}

$mocks = static::mocks();

App::bind(static::MOCKS_LIST, static function ($app) use ($mocks, $class, $result) {
$mocks[$class] = $result;
return $mocks;
});
}

public static function mocks()
{
if (! static::faked()) {
return [];
}
return App::make(static::MOCKS_LIST);
}

public static function connection()
{
return Arr::get(
Expand Down
17 changes: 17 additions & 0 deletions tests/Unit/WorkflowFakerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@ public function testTimeTravelWorkflow(): void
return 'other_activity';
});

WorkflowStub::assertNothingDispatched();

$workflow = WorkflowStub::make(TestTimeTravelWorkflow::class);
$workflow->start();

WorkflowStub::assertDispatched(TestOtherActivity::class, 1);
WorkflowStub::assertNotDispatched(TestActivity::class);

$this->assertFalse($workflow->isCanceled());
$this->assertNull($workflow->output());

Expand All @@ -41,6 +46,12 @@ public function testTimeTravelWorkflow(): void

$this->assertTrue($workflow->isCanceled());
$this->assertSame($workflow->output(), 'workflow_activity_other_activity');

WorkflowStub::assertDispatched(TestOtherActivity::class, static function ($string) {
return $string === 'other';
});

WorkflowStub::assertDispatched(TestActivity::class);
}

public function testParentWorkflow(): void
Expand All @@ -54,6 +65,9 @@ public function testParentWorkflow(): void
$workflow = WorkflowStub::make(TestParentWorkflow::class);
$workflow->start();

WorkflowStub::assertDispatchedTimes(TestActivity::class, 1);
WorkflowStub::assertDispatchedTimes(TestChildWorkflow::class, 1);

$this->assertSame($workflow->output(), 'workflow_activity_other_activity');
}

Expand All @@ -71,6 +85,9 @@ public function testConcurrentWorkflow(): void
$workflow = WorkflowStub::make(TestConcurrentWorkflow::class);
$workflow->start();

WorkflowStub::assertDispatched(TestActivity::class);
WorkflowStub::assertDispatched(TestOtherActivity::class);

$this->assertSame($workflow->output(), 'workflow_activity_other_activity');
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy