Skip to content

Commit

Permalink
formatting and method extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Mar 14, 2018
1 parent e970dbd commit 696f4d8
Showing 1 changed file with 80 additions and 49 deletions.
129 changes: 80 additions & 49 deletions src/Illuminate/Support/Testing/Fakes/QueueFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,40 @@ public function assertPushed($job, $callback = null)
);
}

/**
* Assert if a job was pushed a number of times.
*
* @param string $job
* @param int $times
* @return void
*/
protected function assertPushedTimes($job, $times = 1)
{
PHPUnit::assertTrue(
($count = $this->pushed($job)->count()) === $times,
"The expected [{$job}] job was pushed {$count} times instead of {$times} times."
);
}

/**
* Assert if a job was pushed based on a truth-test callback.
*
* @param string $queue
* @param string $job
* @param callable|null $callback
* @return void
*/
public function assertPushedOn($queue, $job, $callback = null)
{
return $this->assertPushed($job, function ($job, $pushedQueue) use ($callback, $queue) {
if ($pushedQueue !== $queue) {
return false;
}

return $callback ? $callback(...func_get_args()) : true;
});
}

/**
* Assert if a job was pushed with chained jobs based on a truth-test callback.
*
Expand All @@ -45,80 +79,77 @@ public function assertPushed($job, $callback = null)
public function assertPushedWithChain($job, $expectedChain = [], $callback = null)
{
PHPUnit::assertTrue(
$this->pushed($job, $callback)->count() > 0,
$this->pushed($job, $callback)->isNotEmpty(),
"The expected [{$job}] job was not pushed."
);

PHPUnit::assertTrue(
collect($expectedChain)->count() > 0,
collect($expectedChain)->isNotEmpty(),
"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;
}
$this->isChainOfObjects($expectedChain)
? $this->assertPushedWithChainOfObjects($job, $expectedChain, $callback)
: $this->assertPushedWithChainOfClasses($job, $expectedChain, $callback);
}

/**
* 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
*/
protected function assertPushedWithChainOfObjects($job, $expectedChain, $callback)
{
$chain = collect($expectedChain)->map(function ($job) {
return serialize($job);
})->all();

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'
$this->pushed($job, $callback)->filter(function ($job) use ($chain) {
return $job->chained == $chain;
})->isNotEmpty(),
"The expected chain was not pushed."
);
}

/**
* Assert if a job was pushed a number of times.
* Assert if a job was pushed with chained jobs based on a truth-test callback.
*
* @param string $job
* @param int $times
* @param string $job
* @param array $expectedChain
* @param callable|null $callback
* @return void
*/
protected function assertPushedTimes($job, $times = 1)
protected function assertPushedWithChainOfClasses($job, $expectedChain, $callback)
{
$matching = $this->pushed($job, $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(
($count = $this->pushed($job)->count()) === $times,
"The expected [{$job}] job was pushed {$count} times instead of {$times} times."
$matching->isNotEmpty(), 'The expected chain was not pushed.'
);
}

/**
* Assert if a job was pushed based on a truth-test callback.
* Determine if the given chain is entirely composed of objects.
*
* @param string $queue
* @param string $job
* @param callable|null $callback
* @return void
* @param array $chain
* @return bool
*/
public function assertPushedOn($queue, $job, $callback = null)
protected function isChainOfObjects($chain)
{
return $this->assertPushed($job, function ($job, $pushedQueue) use ($callback, $queue) {
if ($pushedQueue !== $queue) {
return false;
}

return $callback ? $callback(...func_get_args()) : true;
});
return collect($chain)->count() == collect($chain)
->filter(function ($job) {
return is_object($job);
})->count();
}

/**
Expand Down

0 comments on commit 696f4d8

Please sign in to comment.