From 26a0ade33a6786e12f4545958a7067afd1352e9a Mon Sep 17 00:00:00 2001 From: Erik Anders <5753604+Naugrimm@users.noreply.github.com> Date: Mon, 13 Nov 2023 09:52:22 +0100 Subject: [PATCH 1/8] feat: allow to assert that certain workflows/activities were dispatched --- src/ActivityStub.php | 2 + src/ChildWorkflowStub.php | 2 + .../AssertDispatchedWorkflowsOrActivities.php | 87 +++++++++++++++++++ src/WorkflowStub.php | 10 +++ tests/Unit/WorkflowFakerTest.php | 29 +++++++ 5 files changed, 130 insertions(+) create mode 100644 src/Traits/AssertDispatchedWorkflowsOrActivities.php diff --git a/src/ActivityStub.php b/src/ActivityStub.php index 4d6b5f4..4ba7ff3 100644 --- a/src/ActivityStub.php +++ b/src/ActivityStub.php @@ -44,6 +44,8 @@ public static function make($activity, ...$arguments): PromiseInterface 'class' => $activity, 'result' => Y::serialize(is_callable($result) ? $result($context, ...$arguments) : $result), ]); + + WorkflowStub::recordDispatchedWorkflowOrActivity($activity, $arguments); } if ($log) { diff --git a/src/ChildWorkflowStub.php b/src/ChildWorkflowStub.php index 226bff4..6ce9a9b 100644 --- a/src/ChildWorkflowStub.php +++ b/src/ChildWorkflowStub.php @@ -37,6 +37,8 @@ public static function make($workflow, ...$arguments): PromiseInterface 'class' => $workflow, 'result' => Y::serialize(is_callable($result) ? $result($context, ...$arguments) : $result), ]); + + WorkflowStub::recordDispatchedWorkflowOrActivity($workflow, $arguments); } if ($log) { diff --git a/src/Traits/AssertDispatchedWorkflowsOrActivities.php b/src/Traits/AssertDispatchedWorkflowsOrActivities.php new file mode 100644 index 0000000..3fe9de2 --- /dev/null +++ b/src/Traits/AssertDispatchedWorkflowsOrActivities.php @@ -0,0 +1,87 @@ +count() > 0, + "The expected [{$workflowOrActivity}] workflow/activity was not dispatched." + ); + } + + public static function assertDispatchedTimes(string $workflowOrActivity, int $times = 1) + { + $count = self::dispatched($workflowOrActivity)->count(); + + PHPUnit::assertSame( + $times, $count, + "The expected [{$workflowOrActivity}] workflow/activity was dispatched {$count} times instead of {$times} times." + ); + } + + /** + * Get all of the activities matching a truth-test callback. + * + * @param string $workflowOrActivity + * @param callable|null $callback + * @return Collection + */ + public static function dispatched(string $workflowOrActivity, $callback = null) : Collection + { + $dispatchedWorkflowsOrActivities = App::make(self::DISPATCHED_WORKFLOWS_OR_ACTIVITIES_LIST); + if (! isset($dispatchedWorkflowsOrActivities[$workflowOrActivity])) { + return collect(); + } + + $callback = $callback ?: fn () => true; + + return collect($dispatchedWorkflowsOrActivities[$workflowOrActivity])->filter( + fn ($arguments) => $callback(...$arguments) + ); + } +} diff --git a/src/WorkflowStub.php b/src/WorkflowStub.php index e42f4f4..db5a1b1 100644 --- a/src/WorkflowStub.php +++ b/src/WorkflowStub.php @@ -4,11 +4,14 @@ namespace Workflow; +use Closure; use Illuminate\Database\QueryException; use Illuminate\Support\Arr; use Illuminate\Support\Carbon; +use Illuminate\Support\Collection; use Illuminate\Support\Facades\App; use LimitIterator; +use PHPUnit\Framework\Assert as PHPUnit; use ReflectionClass; use SplFileObject; use Workflow\Events\WorkflowFailed; @@ -19,6 +22,7 @@ use Workflow\States\WorkflowCreatedStatus; use Workflow\States\WorkflowFailedStatus; use Workflow\States\WorkflowPendingStatus; +use Workflow\Traits\AssertDispatchedWorkflowsOrActivities; use Workflow\Traits\Awaits; use Workflow\Traits\AwaitWithTimeouts; use Workflow\Traits\SideEffects; @@ -26,12 +30,14 @@ final class WorkflowStub { + use AssertDispatchedWorkflowsOrActivities; use Awaits; use AwaitWithTimeouts; use SideEffects; use Timers; public const MOCKS_LIST = 'workflow.mocks'; + public const DISPATCHED_WORKFLOWS_OR_ACTIVITIES_LIST = 'workflow.dispatched_workflows_or_activities'; private static ?\stdClass $context = null; @@ -89,6 +95,10 @@ public static function fake(): void App::bind(static::MOCKS_LIST, static function ($app) { return []; }); + + App::bind(static::DISPATCHED_WORKFLOWS_OR_ACTIVITIES_LIST, static function ($app) { + return []; + }); } public static function faked(): bool diff --git a/tests/Unit/WorkflowFakerTest.php b/tests/Unit/WorkflowFakerTest.php index bfb8b0b..2b3cd69 100644 --- a/tests/Unit/WorkflowFakerTest.php +++ b/tests/Unit/WorkflowFakerTest.php @@ -29,6 +29,9 @@ public function testTimeTravelWorkflow(): void $workflow = WorkflowStub::make(TestTimeTravelWorkflow::class); $workflow->start(); + WorkflowStub::assertDispatchedTimes(TestActivity::class, 0); + WorkflowStub::assertDispatchedTimes(TestOtherActivity::class); + $this->assertFalse($workflow->isCanceled()); $this->assertNull($workflow->output()); @@ -54,6 +57,9 @@ public function testParentWorkflow(): void $workflow = WorkflowStub::make(TestParentWorkflow::class); $workflow->start(); + WorkflowStub::assertDispatchedTimes(TestActivity::class); + WorkflowStub::assertDispatchedTimes(TestChildWorkflow::class); + $this->assertSame($workflow->output(), 'workflow_activity_other_activity'); } @@ -71,6 +77,29 @@ public function testConcurrentWorkflow(): void $workflow = WorkflowStub::make(TestConcurrentWorkflow::class); $workflow->start(); + WorkflowStub::assertDispatchedTimes(TestActivity::class); + WorkflowStub::assertDispatchedTimes(TestOtherActivity::class); + + $this->assertSame($workflow->output(), 'workflow_activity_other_activity'); + } + + + public function testChildWorkflowActivities(): void + { + WorkflowStub::fake(); + + WorkflowStub::mock(TestActivity::class, 'activity'); + + WorkflowStub::mock(TestOtherActivity::class, 'other_activity'); + + $workflow = WorkflowStub::make(TestParentWorkflow::class); + $workflow->start(); + + $this->markTestIncomplete("WIP: This currently fails because of an issue with locking when using the sync queue"); + + WorkflowStub::assertDispatchedTimes(TestOtherActivity::class); + WorkflowStub::assertDispatchedTimes(TestActivity::class); + $this->assertSame($workflow->output(), 'workflow_activity_other_activity'); } } From 2fbfb86f3250b8e82081b2b93bd93a1c5ae96d16 Mon Sep 17 00:00:00 2001 From: Erik Anders <5753604+Naugrimm@users.noreply.github.com> Date: Mon, 13 Nov 2023 13:48:05 +0100 Subject: [PATCH 2/8] chore: cleanup --- src/WorkflowStub.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/WorkflowStub.php b/src/WorkflowStub.php index db5a1b1..48e1fd2 100644 --- a/src/WorkflowStub.php +++ b/src/WorkflowStub.php @@ -4,14 +4,11 @@ namespace Workflow; -use Closure; use Illuminate\Database\QueryException; use Illuminate\Support\Arr; use Illuminate\Support\Carbon; -use Illuminate\Support\Collection; use Illuminate\Support\Facades\App; use LimitIterator; -use PHPUnit\Framework\Assert as PHPUnit; use ReflectionClass; use SplFileObject; use Workflow\Events\WorkflowFailed; From efc8b17247203ad5c8fdb5634937ac5a275b6e6a Mon Sep 17 00:00:00 2001 From: Erik Anders <5753604+Naugrimm@users.noreply.github.com> Date: Tue, 14 Nov 2023 08:05:06 +0100 Subject: [PATCH 3/8] chore: remove unneeded test --- tests/Unit/WorkflowFakerTest.php | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/tests/Unit/WorkflowFakerTest.php b/tests/Unit/WorkflowFakerTest.php index 2b3cd69..52e5a49 100644 --- a/tests/Unit/WorkflowFakerTest.php +++ b/tests/Unit/WorkflowFakerTest.php @@ -82,24 +82,4 @@ public function testConcurrentWorkflow(): void $this->assertSame($workflow->output(), 'workflow_activity_other_activity'); } - - - public function testChildWorkflowActivities(): void - { - WorkflowStub::fake(); - - WorkflowStub::mock(TestActivity::class, 'activity'); - - WorkflowStub::mock(TestOtherActivity::class, 'other_activity'); - - $workflow = WorkflowStub::make(TestParentWorkflow::class); - $workflow->start(); - - $this->markTestIncomplete("WIP: This currently fails because of an issue with locking when using the sync queue"); - - WorkflowStub::assertDispatchedTimes(TestOtherActivity::class); - WorkflowStub::assertDispatchedTimes(TestActivity::class); - - $this->assertSame($workflow->output(), 'workflow_activity_other_activity'); - } } From 6306540f0d06092f44d7d61ea8d23b0c140e0377 Mon Sep 17 00:00:00 2001 From: Erik Anders <5753604+Naugrimm@users.noreply.github.com> Date: Tue, 14 Nov 2023 19:30:48 +0100 Subject: [PATCH 4/8] style: fix ecs errors --- .../AssertDispatchedWorkflowsOrActivities.php | 22 +++++++++++-------- src/WorkflowStub.php | 1 + 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/Traits/AssertDispatchedWorkflowsOrActivities.php b/src/Traits/AssertDispatchedWorkflowsOrActivities.php index 3fe9de2..faa7513 100644 --- a/src/Traits/AssertDispatchedWorkflowsOrActivities.php +++ b/src/Traits/AssertDispatchedWorkflowsOrActivities.php @@ -1,4 +1,5 @@ count(); PHPUnit::assertSame( - $times, $count, + $times, + $count, "The expected [{$workflowOrActivity}] workflow/activity was dispatched {$count} times instead of {$times} times." ); } @@ -67,21 +73,19 @@ public static function assertDispatchedTimes(string $workflowOrActivity, int $ti /** * Get all of the activities matching a truth-test callback. * - * @param string $workflowOrActivity * @param callable|null $callback - * @return Collection */ - public static function dispatched(string $workflowOrActivity, $callback = null) : Collection + public static function dispatched(string $workflowOrActivity, $callback = null): Collection { $dispatchedWorkflowsOrActivities = App::make(self::DISPATCHED_WORKFLOWS_OR_ACTIVITIES_LIST); if (! isset($dispatchedWorkflowsOrActivities[$workflowOrActivity])) { return collect(); } - $callback = $callback ?: fn () => true; + $callback = $callback ?: static fn () => true; return collect($dispatchedWorkflowsOrActivities[$workflowOrActivity])->filter( - fn ($arguments) => $callback(...$arguments) + static fn ($arguments) => $callback(...$arguments) ); } } diff --git a/src/WorkflowStub.php b/src/WorkflowStub.php index 48e1fd2..e89a9aa 100644 --- a/src/WorkflowStub.php +++ b/src/WorkflowStub.php @@ -34,6 +34,7 @@ final class WorkflowStub use Timers; public const MOCKS_LIST = 'workflow.mocks'; + public const DISPATCHED_WORKFLOWS_OR_ACTIVITIES_LIST = 'workflow.dispatched_workflows_or_activities'; private static ?\stdClass $context = null; From 1a6f32fa88a0b9689dd61652dfc55911bd188513 Mon Sep 17 00:00:00 2001 From: Richard McDaniel Date: Sun, 19 Nov 2023 00:11:05 +0000 Subject: [PATCH 5/8] Add macroable --- src/ActivityStub.php | 4 +- src/ChildWorkflowStub.php | 4 +- .../AssertDispatchedWorkflowsOrActivities.php | 91 ---------------- src/Traits/Fakes.php | 102 ++++++++++++++++++ src/WorkflowStub.php | 49 +-------- 5 files changed, 112 insertions(+), 138 deletions(-) delete mode 100644 src/Traits/AssertDispatchedWorkflowsOrActivities.php create mode 100644 src/Traits/Fakes.php diff --git a/src/ActivityStub.php b/src/ActivityStub.php index 4ba7ff3..dbbcc55 100644 --- a/src/ActivityStub.php +++ b/src/ActivityStub.php @@ -45,7 +45,9 @@ public static function make($activity, ...$arguments): PromiseInterface 'result' => Y::serialize(is_callable($result) ? $result($context, ...$arguments) : $result), ]); - WorkflowStub::recordDispatchedWorkflowOrActivity($activity, $arguments); + if (WorkflowStub::faked()) { + WorkflowStub::recordDispatched($activity, $arguments); + } } if ($log) { diff --git a/src/ChildWorkflowStub.php b/src/ChildWorkflowStub.php index 6ce9a9b..8bc839d 100644 --- a/src/ChildWorkflowStub.php +++ b/src/ChildWorkflowStub.php @@ -38,7 +38,9 @@ public static function make($workflow, ...$arguments): PromiseInterface 'result' => Y::serialize(is_callable($result) ? $result($context, ...$arguments) : $result), ]); - WorkflowStub::recordDispatchedWorkflowOrActivity($workflow, $arguments); + if (WorkflowStub::faked()) { + WorkflowStub::recordDispatched($workflow, $arguments); + } } if ($log) { diff --git a/src/Traits/AssertDispatchedWorkflowsOrActivities.php b/src/Traits/AssertDispatchedWorkflowsOrActivities.php deleted file mode 100644 index faa7513..0000000 --- a/src/Traits/AssertDispatchedWorkflowsOrActivities.php +++ /dev/null @@ -1,91 +0,0 @@ -count() > 0, - "The expected [{$workflowOrActivity}] workflow/activity was not dispatched." - ); - } - - public static function assertDispatchedTimes(string $workflowOrActivity, int $times = 1) - { - $count = self::dispatched($workflowOrActivity)->count(); - - PHPUnit::assertSame( - $times, - $count, - "The expected [{$workflowOrActivity}] workflow/activity was dispatched {$count} times instead of {$times} times." - ); - } - - /** - * Get all of the activities matching a truth-test callback. - * - * @param callable|null $callback - */ - public static function dispatched(string $workflowOrActivity, $callback = null): Collection - { - $dispatchedWorkflowsOrActivities = App::make(self::DISPATCHED_WORKFLOWS_OR_ACTIVITIES_LIST); - if (! isset($dispatchedWorkflowsOrActivities[$workflowOrActivity])) { - return collect(); - } - - $callback = $callback ?: static fn () => true; - - return collect($dispatchedWorkflowsOrActivities[$workflowOrActivity])->filter( - static fn ($arguments) => $callback(...$arguments) - ); - } -} diff --git a/src/Traits/Fakes.php b/src/Traits/Fakes.php new file mode 100644 index 0000000..221922c --- /dev/null +++ b/src/Traits/Fakes.php @@ -0,0 +1,102 @@ +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('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) + ); + }); + } +} diff --git a/src/WorkflowStub.php b/src/WorkflowStub.php index e89a9aa..2d76967 100644 --- a/src/WorkflowStub.php +++ b/src/WorkflowStub.php @@ -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; @@ -19,24 +19,21 @@ use Workflow\States\WorkflowCreatedStatus; use Workflow\States\WorkflowFailedStatus; use Workflow\States\WorkflowPendingStatus; -use Workflow\Traits\AssertDispatchedWorkflowsOrActivities; use Workflow\Traits\Awaits; use Workflow\Traits\AwaitWithTimeouts; +use Workflow\Traits\Fakes; use Workflow\Traits\SideEffects; use Workflow\Traits\Timers; final class WorkflowStub { - use AssertDispatchedWorkflowsOrActivities; use Awaits; use AwaitWithTimeouts; + use Fakes; + use Macroable; use SideEffects; use Timers; - public const MOCKS_LIST = 'workflow.mocks'; - - public const DISPATCHED_WORKFLOWS_OR_ACTIVITIES_LIST = 'workflow.dispatched_workflows_or_activities'; - private static ?\stdClass $context = null; private function __construct( @@ -88,44 +85,6 @@ public function __call($method, $arguments) } } - public static function fake(): void - { - App::bind(static::MOCKS_LIST, static function ($app) { - return []; - }); - - App::bind(static::DISPATCHED_WORKFLOWS_OR_ACTIVITIES_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( From 10381e245fcb764eee9b5b3c232bdbdf68292e18 Mon Sep 17 00:00:00 2001 From: Richard McDaniel Date: Sun, 19 Nov 2023 03:11:23 +0000 Subject: [PATCH 6/8] More test coverage --- src/ActivityStub.php | 22 +++++++++---------- src/ChildWorkflowStub.php | 22 +++++++++---------- src/Traits/Fakes.php | 37 ++++++++++++-------------------- tests/Unit/WorkflowFakerTest.php | 12 ++++++----- 4 files changed, 43 insertions(+), 50 deletions(-) diff --git a/src/ActivityStub.php b/src/ActivityStub.php index dbbcc55..63990f2 100644 --- a/src/ActivityStub.php +++ b/src/ActivityStub.php @@ -32,20 +32,20 @@ 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), + ]); - if (WorkflowStub::faked()) { WorkflowStub::recordDispatched($activity, $arguments); } } diff --git a/src/ChildWorkflowStub.php b/src/ChildWorkflowStub.php index 8bc839d..5d20fb6 100644 --- a/src/ChildWorkflowStub.php +++ b/src/ChildWorkflowStub.php @@ -25,20 +25,20 @@ 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), + ]); - if (WorkflowStub::faked()) { WorkflowStub::recordDispatched($workflow, $arguments); } } diff --git a/src/Traits/Fakes.php b/src/Traits/Fakes.php index 221922c..b98206c 100644 --- a/src/Traits/Fakes.php +++ b/src/Traits/Fakes.php @@ -18,36 +18,27 @@ 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() + public static function fake(): void { - if (! static::faked()) { + App::bind(static::$MOCKS_LIST, static function ($app) { return []; - } - return App::make(static::$MOCKS_LIST); - } + }); - public static function fake(): void - { App::bind(static::$DISPATCHED_LIST, static function ($app) { return []; }); - App::bind(static::$MOCKS_LIST, static function ($app) { - 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 ($app) use ($mocks, $class, $result) { + $mocks[$class] = $result; + return $mocks; + }); }); static::macro('recordDispatched', static function ($class, $arguments) { diff --git a/tests/Unit/WorkflowFakerTest.php b/tests/Unit/WorkflowFakerTest.php index 52e5a49..edfa623 100644 --- a/tests/Unit/WorkflowFakerTest.php +++ b/tests/Unit/WorkflowFakerTest.php @@ -29,8 +29,8 @@ public function testTimeTravelWorkflow(): void $workflow = WorkflowStub::make(TestTimeTravelWorkflow::class); $workflow->start(); - WorkflowStub::assertDispatchedTimes(TestActivity::class, 0); WorkflowStub::assertDispatchedTimes(TestOtherActivity::class); + WorkflowStub::assertDispatchedTimes(TestActivity::class, 0); $this->assertFalse($workflow->isCanceled()); $this->assertNull($workflow->output()); @@ -44,6 +44,8 @@ public function testTimeTravelWorkflow(): void $this->assertTrue($workflow->isCanceled()); $this->assertSame($workflow->output(), 'workflow_activity_other_activity'); + + WorkflowStub::assertDispatched(TestActivity::class); } public function testParentWorkflow(): void @@ -57,8 +59,8 @@ public function testParentWorkflow(): void $workflow = WorkflowStub::make(TestParentWorkflow::class); $workflow->start(); - WorkflowStub::assertDispatchedTimes(TestActivity::class); - WorkflowStub::assertDispatchedTimes(TestChildWorkflow::class); + WorkflowStub::assertDispatchedTimes(TestActivity::class, 1); + WorkflowStub::assertDispatchedTimes(TestChildWorkflow::class, 1); $this->assertSame($workflow->output(), 'workflow_activity_other_activity'); } @@ -77,8 +79,8 @@ public function testConcurrentWorkflow(): void $workflow = WorkflowStub::make(TestConcurrentWorkflow::class); $workflow->start(); - WorkflowStub::assertDispatchedTimes(TestActivity::class); - WorkflowStub::assertDispatchedTimes(TestOtherActivity::class); + WorkflowStub::assertDispatched(TestActivity::class); + WorkflowStub::assertDispatched(TestOtherActivity::class); $this->assertSame($workflow->output(), 'workflow_activity_other_activity'); } From 88266e6b1052b1189019953f0ddd8c328e1be57b Mon Sep 17 00:00:00 2001 From: Richard McDaniel Date: Sun, 19 Nov 2023 03:50:46 +0000 Subject: [PATCH 7/8] More test helpers --- src/Traits/Fakes.php | 15 +++++++++++++++ tests/Unit/WorkflowFakerTest.php | 10 ++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/Traits/Fakes.php b/src/Traits/Fakes.php index b98206c..05904d7 100644 --- a/src/Traits/Fakes.php +++ b/src/Traits/Fakes.php @@ -77,6 +77,21 @@ public static function fake(): void ); }); + 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])) { diff --git a/tests/Unit/WorkflowFakerTest.php b/tests/Unit/WorkflowFakerTest.php index edfa623..bad1e79 100644 --- a/tests/Unit/WorkflowFakerTest.php +++ b/tests/Unit/WorkflowFakerTest.php @@ -26,11 +26,13 @@ public function testTimeTravelWorkflow(): void return 'other_activity'; }); + WorkflowStub::assertNothingDispatched(); + $workflow = WorkflowStub::make(TestTimeTravelWorkflow::class); $workflow->start(); - WorkflowStub::assertDispatchedTimes(TestOtherActivity::class); - WorkflowStub::assertDispatchedTimes(TestActivity::class, 0); + WorkflowStub::assertDispatched(TestOtherActivity::class, 1); + WorkflowStub::assertNotDispatched(TestActivity::class); $this->assertFalse($workflow->isCanceled()); $this->assertNull($workflow->output()); @@ -45,6 +47,10 @@ 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); } From 8821835dff4d1467c6fcfee359bf26df454b4375 Mon Sep 17 00:00:00 2001 From: Richard McDaniel Date: Sun, 19 Nov 2023 04:00:35 +0000 Subject: [PATCH 8/8] Cleanup --- src/Traits/Fakes.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Traits/Fakes.php b/src/Traits/Fakes.php index 05904d7..0f0f5bd 100644 --- a/src/Traits/Fakes.php +++ b/src/Traits/Fakes.php @@ -20,11 +20,11 @@ public static function faked(): bool public static function fake(): void { - App::bind(static::$MOCKS_LIST, static function ($app) { + App::bind(static::$MOCKS_LIST, static function () { return []; }); - App::bind(static::$DISPATCHED_LIST, static function ($app) { + App::bind(static::$DISPATCHED_LIST, static function () { return []; }); @@ -35,7 +35,7 @@ public static function fake(): void static::macro('mock', static function ($class, $result) { $mocks = static::mocks(); - App::bind(static::$MOCKS_LIST, static function ($app) use ($mocks, $class, $result) { + App::bind(static::$MOCKS_LIST, static function () use ($mocks, $class, $result) { $mocks[$class] = $result; return $mocks; }); @@ -44,7 +44,7 @@ public static function fake(): void static::macro('recordDispatched', static function ($class, $arguments) { $dispatched = App::make(static::$DISPATCHED_LIST); - App::bind(static::$DISPATCHED_LIST, static function ($app) use ($dispatched, $class, $arguments) { + App::bind(static::$DISPATCHED_LIST, static function () use ($dispatched, $class, $arguments) { if (! isset($dispatched[$class])) { $dispatched[$class] = []; } 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