From 696f4d88c132ac39a3a805dbe490b3b754c9ce5f Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 14 Mar 2018 07:55:42 -0500 Subject: [PATCH] formatting and method extraction --- .../Support/Testing/Fakes/QueueFake.php | 129 +++++++++++------- 1 file changed, 80 insertions(+), 49 deletions(-) diff --git a/src/Illuminate/Support/Testing/Fakes/QueueFake.php b/src/Illuminate/Support/Testing/Fakes/QueueFake.php index 1b99d89d4aa6..fd0ea6f4e736 100644 --- a/src/Illuminate/Support/Testing/Fakes/QueueFake.php +++ b/src/Illuminate/Support/Testing/Fakes/QueueFake.php @@ -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. * @@ -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(); } /**