From 5bff2b21fb609b606d8bc001e76e3a1729d48400 Mon Sep 17 00:00:00 2001 From: Patrick O'Meara Date: Wed, 21 Aug 2024 02:05:09 +1000 Subject: [PATCH] Add assert with params convenience methods (#286) --- src/Concerns/AsJob.php | 20 +++++++++++ tests/AsJobTest.php | 4 +-- tests/AsJobWithAssertionsTest.php | 58 ++++++++++++++++++++++++++++++- tests/Helpers.php | 7 ---- 4 files changed, 79 insertions(+), 10 deletions(-) diff --git a/src/Concerns/AsJob.php b/src/Concerns/AsJob.php index dba6a1a..acd7468 100644 --- a/src/Concerns/AsJob.php +++ b/src/Concerns/AsJob.php @@ -169,4 +169,24 @@ public static function assertPushedOn(string $queue, Closure|int|null $times = n return $callback ? $callback(...func_get_args()) : true; }); } + + public static function assertPushedWith(Closure|array $callback, ?string $queue = null): void + { + if (is_array($callback)) { + $callback = fn (...$params) => $params === $callback; + } + + static::assertPushed( + fn ($action, $params, JobDecorator $job, $q) => $callback(...$params) && (is_null($queue) || $q === $queue) + ); + } + + public static function assertNotPushedWith(Closure|array $callback): void + { + if (is_array($callback)) { + $callback = fn (...$params) => $params === $callback; + } + + static::assertNotPushed(fn ($action, $params, JobDecorator $job) => $callback(...$job->getParameters())); + } } diff --git a/tests/AsJobTest.php b/tests/AsJobTest.php index 8058af6..1149ac8 100644 --- a/tests/AsJobTest.php +++ b/tests/AsJobTest.php @@ -54,7 +54,7 @@ public function handle(): void AsJobTest::dispatchNow(...$parameters); // Then it was pushed to the queue with these parameters. - assertJobPushedWith(AsJobTest::class, $parameters); + AsJobTest::assertPushedWith($parameters); }); it('can be dispatched asynchronously', function () { @@ -75,7 +75,7 @@ public function handle(): void AsJobTest::dispatch(...$parameters); // Then it was pushed to the queue with these parameters. - assertJobPushedWith(AsJobTest::class, $parameters); + AsJobTest::assertPushedWith($parameters); }); it('can make a job statically', function (string $expectedJobClass) { diff --git a/tests/AsJobWithAssertionsTest.php b/tests/AsJobWithAssertionsTest.php index 5868ab9..f6490aa 100644 --- a/tests/AsJobWithAssertionsTest.php +++ b/tests/AsJobWithAssertionsTest.php @@ -5,6 +5,7 @@ use Illuminate\Support\Facades\Queue; use Lorisleiva\Actions\Concerns\AsJob; use Lorisleiva\Actions\Decorators\JobDecorator; +use Lorisleiva\Actions\Tests\Stubs\User; use PHPUnit\Framework\ExpectationFailedException; class AsJobWithAssertionsTest @@ -23,7 +24,7 @@ public function configureJob(JobDecorator $job): void $job->onQueue(static::$queue); } - public function handle(): void + public function handle(User $user): void { // } @@ -120,3 +121,58 @@ public function handle(): void // When we pushed it on some other queue. AsJobWithAssertionsTest::assertPushedOn('some-other-queue'); })->with('custom job decorators'); + +it('asserts an action has been pushed with params - success', function () { + loadMigrations(); + $user = createUser(); + + // When we dispatch the action with some parameters. + AsJobWithAssertionsTest::dispatch($user); + + // Then we can assert it has been dispatched with these parameters. + AsJobWithAssertionsTest::assertPushedWith(fn (User $u) => $user->is($u)); + AsJobWithAssertionsTest::assertPushedWith([$user]); +})->with('custom job decorators'); + +it('asserts an action has been pushed with params - failure (using closure)', function () { + loadMigrations(); + $userA = createUser(); + $userB = createUser(); + + // When we dispatch the action with some parameters. + AsJobWithAssertionsTest::dispatch($userA); + + $this->expectException(ExpectationFailedException::class); + $this->expectExceptionMessage('The expected ['.AsJobWithAssertionsTest::class.'] job was not pushed'); + + // Then we can expect a failure when asserting it has been dispatched with other parameters. + AsJobWithAssertionsTest::assertPushedWith(fn (User $u) => $userB->is($u)); +})->with('custom job decorators'); + +it('asserts an action has been pushed with params - failure (using array)', function () { + loadMigrations(); + $userA = createUser(); + $userB = createUser(); + + // When we dispatch the action with some parameters. + AsJobWithAssertionsTest::dispatch($userA); + + $this->expectException(ExpectationFailedException::class); + $this->expectExceptionMessage('The expected ['.AsJobWithAssertionsTest::class.'] job was not pushed'); + + // Then we can expect a failure when asserting it has been dispatched with other parameters. + AsJobWithAssertionsTest::assertPushedWith([$userB]); +})->with('custom job decorators'); + +it('asserts an action has not been pushed with params - success', function () { + loadMigrations(); + $userA = createUser(); + $userB = createUser(); + + // When we dispatch the action with some parameters. + AsJobWithAssertionsTest::dispatch($userA); + + // Then we can assert it has not been dispatched with these parameters. + AsJobWithAssertionsTest::assertNotPushedWith(fn (User $u) => $userB->is($u)); + AsJobWithAssertionsTest::assertNotPushedWith([$userB]); +})->with('custom job decorators'); diff --git a/tests/Helpers.php b/tests/Helpers.php index 219f63e..cd35f46 100644 --- a/tests/Helpers.php +++ b/tests/Helpers.php @@ -55,10 +55,3 @@ function assertJobPushed(string $class, ?Closure $callback = null): void return $callback ? $callback($job) : true; }); } - -function assertJobPushedWith(string $class, array $parameters = []): void -{ - assertJobPushed($class, function (JobDecorator $job) use ($parameters) { - return $job->getParameters() === $parameters; - }); -}