From 5c5c310db7c1786832b30967c37c5577f9b7a6b0 Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Mon, 7 Dec 2020 17:02:51 +0200 Subject: [PATCH] add assertDispatchedWithChain and assertDispatchedWithoutChain to BusFake --- .../Support/Testing/Fakes/BusFake.php | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) diff --git a/src/Illuminate/Support/Testing/Fakes/BusFake.php b/src/Illuminate/Support/Testing/Fakes/BusFake.php index 91deda3bdeaa..e2535cc8f8ae 100644 --- a/src/Illuminate/Support/Testing/Fakes/BusFake.php +++ b/src/Illuminate/Support/Testing/Fakes/BusFake.php @@ -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. *