Skip to content

Commit

Permalink
Add test for prepended UniqueJobDecorator
Browse files Browse the repository at this point in the history
  • Loading branch information
lorisleiva committed Mar 30, 2021
1 parent 52c3371 commit 277d901
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/AsJobWithJobDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function asJob(JobDecorator $job, int $left, int $right)

// Then it received the JobDecorator as its first argument.
$job = AsJobWithJobDecoratorTest::$latestJobDecorator;
expect($job)->toBeInstanceOf(JobDecorator::class);
expect($job->getParameters())->toBe([1, 2]);
expect(get_class($job->getAction()))->toBe(AsJobWithJobDecoratorTest::class);

Expand Down
47 changes: 47 additions & 0 deletions tests/AsJobWithUniqueJobDecoratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Lorisleiva\Actions\Tests;

use Illuminate\Contracts\Queue\ShouldBeUnique;
use Lorisleiva\Actions\Concerns\AsJob;
use Lorisleiva\Actions\Decorators\JobDecorator;
use Lorisleiva\Actions\Decorators\UniqueJobDecorator;

class AsJobWithUniqueJobDecoratorTest implements ShouldBeUnique
{
use AsJob;

public static ?int $latestResult;
public static ?JobDecorator $latestJobDecorator;

public function handle(int $left, int $right)
{
static::$latestResult = $left + $right;
}

public function asJob(UniqueJobDecorator $job, int $left, int $right)
{
static::$latestJobDecorator = $job;
$this->handle($left, $right);
}
}

beforeEach(function () {
// Given we reset the static variables.
AsJobWithUniqueJobDecoratorTest::$latestResult = null;
AsJobWithUniqueJobDecoratorTest::$latestJobDecorator = null;
});

it('can access the UniqueJobDecorator instance from the asJob method', function () {
// When we dispatch a job that expects a UniqueJobDecorator as a first argument.
AsJobWithUniqueJobDecoratorTest::dispatch(1, 2);

// Then it received the UniqueJobDecorator as its first argument.
$job = AsJobWithUniqueJobDecoratorTest::$latestJobDecorator;
expect($job)->toBeInstanceOf(UniqueJobDecorator::class);
expect($job->getParameters())->toBe([1, 2]);
expect(get_class($job->getAction()))->toBe(AsJobWithUniqueJobDecoratorTest::class);

// And the received the expected result.
expect(AsJobWithUniqueJobDecoratorTest::$latestResult)->toBe(3);
});

0 comments on commit 277d901

Please sign in to comment.