Skip to content
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

[5.6] Added test helper to assert that a Job has been queued with a Chain #23531

Merged
merged 1 commit into from
Mar 14, 2018
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
53 changes: 53 additions & 0 deletions src/Illuminate/Support/Testing/Fakes/QueueFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,59 @@ public function assertPushed($job, $callback = null)
);
}

/**
* Assert if a job was pushed with chained jobs based on a truth-test callback.
*
* @param string $job
* @param array $expectedChain
* @param callable|null $callback
* @return void
*/
public function assertPushedWithChain($job, $expectedChain = [], $callback = null)
{
PHPUnit::assertTrue(
$this->pushed($job, $callback)->count() > 0,
"The expected [{$job}] job was not pushed."
);

PHPUnit::assertTrue(
collect($expectedChain)->count() > 0,
"The expected chain can not be empty."
);

$isExpectedChainOfObjects = collect($expectedChain)
->filter(function ($job) { return is_object($job); })
->count() == collect($expectedChain)->count();

if ($isExpectedChainOfObjects)
{
$chain = collect($expectedChain)->map(function ($job) { return serialize($job); })->all();
PHPUnit::assertTrue(
$this->pushed($job, $callback)
->filter(function ($job) use ($chain) {
return $job->chained == $chain;
})->count() > 0,
"The expected chain was not pushed."
);

return;
}

PHPUnit::assertTrue(
$this->pushed($job, $callback)
->map(function ($job) {
return $job->chained;
})->map(function ($chain) {
return collect($chain)->map(function ($chainedJob) {
return get_class(unserialize($chainedJob));
});
})->filter(function ($chain) use ($expectedChain) {
return $chain == collect($expectedChain);
})->count() > 0,
'The expected chain was not pushed'
);
}

/**
* Assert if a job was pushed a number of times.
*
Expand Down
140 changes: 140 additions & 0 deletions tests/Support/SupportTestingQueueFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Tests\Support;

use Illuminate\Bus\Queueable;
use PHPUnit\Framework\TestCase;
use Illuminate\Foundation\Application;
use Illuminate\Support\Testing\Fakes\QueueFake;
Expand Down Expand Up @@ -101,6 +102,112 @@ public function testAssertPushedUsingBulk()
$this->fake->assertPushedOn($queue, JobStub::class);
$this->fake->assertPushed(JobStub::class, 2);
}

public function testAssertPushedWithChainUsingClassesOrObjectsArray()
{
$this->fake->push(new JobWithChainStub([
new JobStub
]));

$this->fake->assertPushedWithChain(JobWithChainStub::class, [
JobStub::class
]);

$this->fake->assertPushedWithChain(JobWithChainStub::class, [
new JobStub
]);
}

public function testAssertPushedWithChainSameJobDifferentChains()
{
$this->fake->push(new JobWithChainStub([
new JobStub
]));
$this->fake->push(new JobWithChainStub([
new JobStub,
new JobStub
]));

$this->fake->assertPushedWithChain(JobWithChainStub::class, [
JobStub::class,
]);

$this->fake->assertPushedWithChain(JobWithChainStub::class, [
JobStub::class,
JobStub::class
]);
}

public function testAssertPushedWithChainUsingCallback()
{
$this->fake->push(new JobWithChainAndParameterStub('first', [
new JobStub,
new JobStub
]));

$this->fake->push(new JobWithChainAndParameterStub('second', [
new JobStub
]));

$this->fake->assertPushedWithChain(JobWithChainAndParameterStub::class, [
JobStub::class
], function ($job) {
return $job->parameter == 'second';
});

try {
$this->fake->assertPushedWithChain(JobWithChainAndParameterStub::class, [
JobStub::class,
JobStub::class,
], function ($job) {
return $job->parameter == 'second';
});
$this->fail();
} catch (ExpectationFailedException $e) {
$this->assertThat($e, new ExceptionMessage('The expected chain was not pushed'));
}
}

public function testAssertPushedWithChainErrorHandling()
{
try {
$this->fake->assertPushedWithChain(JobWithChainStub::class, []);
$this->fail();
} catch (ExpectationFailedException $e) {
$this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\JobWithChainStub] job was not pushed'));
}

$this->fake->push(new JobWithChainStub([
new JobStub
]));

try {
$this->fake->assertPushedWithChain(JobWithChainStub::class, []);
$this->fail();
} catch (ExpectationFailedException $e) {
$this->assertThat($e, new ExceptionMessage('The expected chain can not be empty'));
}

try {
$this->fake->assertPushedWithChain(JobWithChainStub::class, [
new JobStub,
new JobStub
]);
$this->fail();
} catch (ExpectationFailedException $e) {
$this->assertThat($e, new ExceptionMessage('The expected chain was not pushed'));
}

try {
$this->fake->assertPushedWithChain(JobWithChainStub::class, [
JobStub::class,
JobStub::class
]);
$this->fail();
} catch (ExpectationFailedException $e) {
$this->assertThat($e, new ExceptionMessage('The expected chain was not pushed'));
}
}
}

class JobStub
Expand All @@ -110,3 +217,36 @@ public function handle()
//
}
}

class JobWithChainStub
{
use Queueable;

function __construct($chain)
{
$this->chain($chain);
}

public function handle()
{
//
}
}

class JobWithChainAndParameterStub
{
use Queueable;

public $parameter;

function __construct($parameter, $chain)
{
$this->parameter = $parameter;
$this->chain($chain);
}

public function handle()
{
//
}
}