Skip to content

Commit

Permalink
Add assert with params convenience methods (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickomeara committed Aug 20, 2024
1 parent e55c1ea commit 5bff2b2
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 10 deletions.
20 changes: 20 additions & 0 deletions src/Concerns/AsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}
4 changes: 2 additions & 2 deletions tests/AsJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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) {
Expand Down
58 changes: 57 additions & 1 deletion tests/AsJobWithAssertionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,7 +24,7 @@ public function configureJob(JobDecorator $job): void
$job->onQueue(static::$queue);
}

public function handle(): void
public function handle(User $user): void
{
//
}
Expand Down Expand Up @@ -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');
7 changes: 0 additions & 7 deletions tests/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}

0 comments on commit 5bff2b2

Please sign in to comment.