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

[8.x] Add assertDispatchedWithChain and assertDispatchedWithoutChain to BusFake #35523

Merged
merged 1 commit into from Dec 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
108 changes: 108 additions & 0 deletions src/Illuminate/Support/Testing/Fakes/BusFake.php
Expand Up @@ -198,6 +198,114 @@ public function assertBatched(callable $callback)
);
}

/**
* Assert if a job was dispatched based on a truth-test callback.
*
* @param string|\Closure $command
* @param array $expectedChain
* @param callable|null $callback
* @return void
*/
public function assertDispatchedWithChain($command, $expectedChain = [], $callback = null)
{
if ($command instanceof Closure) {
[$command, $callback] = [$this->firstClosureParameterType($command), $command];
}

PHPUnit::assertTrue(
$this->dispatched($command, $callback)->isNotEmpty(),
"The expected [{$command}] job was not dispatched."
);

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

$this->isChainOfObjects($expectedChain)
? $this->assertDispatchedWithChainOfObjects($command, $expectedChain, $callback)
: $this->assertDispatchedWithChainOfClasses($command, $expectedChain, $callback);
}

/**
* Assert if a job was dispatched with an empty chain based on a truth-test callback.
*
* @param string|\Closure $command
* @param callable|null $callback
* @return void
*/
public function assertDispatchedWithoutChain($command, $callback = null)
{
if ($command instanceof Closure) {
[$command, $callback] = [$this->firstClosureParameterType($command), $command];
}

PHPUnit::assertTrue(
$this->dispatched($command, $callback)->isNotEmpty(),
"The expected [{$command}] job was not dispatched."
);

$this->assertDispatchedWithChainOfClasses($command, [], $callback);
}

/**
* Assert if a job was dispatched with chained jobs based on a truth-test callback.
*
* @param string $command
* @param array $expectedChain
* @param callable|null $callback
* @return void
*/
protected function assertDispatchedWithChainOfObjects($command, $expectedChain, $callback)
{
$chain = collect($expectedChain)->map(function ($job) {
return serialize($job);
})->all();

PHPUnit::assertTrue(
$this->dispatched($command, $callback)->filter(function ($job) use ($chain) {
return $job->chained == $chain;
})->isNotEmpty(),
'The expected chain was not dispatched.'
);
}

/**
* Assert if a job was dispatched with chained jobs based on a truth-test callback.
*
* @param string $command
* @param array $expectedChain
* @param callable|null $callback
* @return void
*/
protected function assertDispatchedWithChainOfClasses($command, $expectedChain, $callback)
{
$matching = $this->dispatched($command, $callback)->map->chained->map(function ($chain) {
return collect($chain)->map(function ($job) {
return get_class(unserialize($job));
});
})->filter(function ($chain) use ($expectedChain) {
return $chain->all() === $expectedChain;
});

PHPUnit::assertTrue(
$matching->isNotEmpty(), 'The expected chain was not dispatched.'
);
}

/**
* Determine if the given chain is entirely composed of objects.
*
* @param array $chain
* @return bool
*/
protected function isChainOfObjects($chain)
{
return ! collect($chain)->contains(function ($job) {
return ! is_object($job);
});
}

/**
* Get all of the jobs matching a truth-test callback.
*
Expand Down