diff --git a/src/Illuminate/Support/Testing/Fakes/QueueFake.php b/src/Illuminate/Support/Testing/Fakes/QueueFake.php index 66d191e90c95..ee11f9e93673 100644 --- a/src/Illuminate/Support/Testing/Fakes/QueueFake.php +++ b/src/Illuminate/Support/Testing/Fakes/QueueFake.php @@ -34,6 +34,56 @@ 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 $chain + * @param callable|null $callback + * @return void + */ + public function assertPushedWithChain($job, $chain = [], $callback = null) + { + PHPUnit::assertTrue( + $this->pushed($job, $callback)->count() > 0, + "The expected [{$job}] job was not pushed." + ); + + PHPUnit::assertTrue( + collect($chain)->count() > 0, + "The expected chain can not be empty." + ); + + $isChainOfObjects = collect($chain) + ->filter(function ($job) { + return is_object($job); + })->count() == collect($chain)->count(); + + if ($isChainOfObjects) + { + PHPUnit::assertEquals( + collect($chain)->map(function ($job) { return serialize($job); })->all(), + $this->pushed($job, $callback)->first()->chained, + "The expected chain was not pushed." + ); + + return; + } + + $chainedClasses = collect($this->pushed($job, $callback)->first()->chained) + ->map(function ($job) { + return unserialize($job); + })->map(function($job) { + return get_class($job); + })->all(); + + PHPUnit::assertEquals( + $chain, + $chainedClasses, + "The expected chain was not pushed." + ); + } + /** * Assert if a job was pushed a number of times. * @@ -160,7 +210,7 @@ public function push($job, $data = '', $queue = null) { $this->jobs[is_object($job) ? get_class($job) : $job][] = [ 'job' => $job, - 'queue' => $queue, + 'queue' => $queue ]; } diff --git a/tests/Support/SupportTestingQueueFakeTest.php b/tests/Support/SupportTestingQueueFakeTest.php index 495bb0684d3e..8b17bc973bf6 100644 --- a/tests/Support/SupportTestingQueueFakeTest.php +++ b/tests/Support/SupportTestingQueueFakeTest.php @@ -2,11 +2,12 @@ namespace Illuminate\Tests\Support; -use PHPUnit\Framework\TestCase; +use Illuminate\Bus\Queueable; use Illuminate\Foundation\Application; use Illuminate\Support\Testing\Fakes\QueueFake; -use PHPUnit\Framework\ExpectationFailedException; use PHPUnit\Framework\Constraint\ExceptionMessage; +use PHPUnit\Framework\ExpectationFailedException; +use PHPUnit\Framework\TestCase; class QueueFakeTest extends TestCase { @@ -101,6 +102,64 @@ public function testAssertPushedUsingBulk() $this->fake->assertPushedOn($queue, JobStub::class); $this->fake->assertPushed(JobStub::class, 2); } + + public function testAssertPushedWithChain() + { + $jobWithChain = new JobWithChainStub([ + new JobStub, + new JobWithParameterStub(0) + ]); + + $jobWithParameterAndChain = new JobWithParameterAndChainStub('first', [ + new JobStub, + ]); + $anotherJobWithParameterAndChain = new JobWithParameterAndChainStub('second', [ + new JobWithParameterStub(1) + ]); + + $this->fake->push($jobWithChain); + $this->fake->push($jobWithParameterAndChain); + $this->fake->push($anotherJobWithParameterAndChain); + + $this->fake->assertPushedWithChain(JobWithChainStub::class, [ + new JobStub, + new JobWithParameterStub(0) + ]); + + $this->fake->assertPushedWithChain(JobWithChainStub::class, [ + JobStub::class, + JobWithParameterStub::class + ]); + + 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 + ]); + $this->fail(); + } catch (ExpectationFailedException $e) { + $this->assertThat($e, new ExceptionMessage('The expected chain was not pushed')); + } + + $this->fake->assertPushedWithChain(JobWithParameterAndChainStub::class, [ + new JobStub + ], function ($job) { + return $job->parameter == 'first'; + }); + + try { + $this->fake->assertPushedWithChain(NotPushedJobStub::class, []); + $this->fail(); + } catch (ExpectationFailedException $e) { + $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\NotPushedJobStub] job was not pushed')); + } + } } class JobStub @@ -110,3 +169,59 @@ public function handle() // } } + +class JobWithParameterStub +{ + public $number; + + function __construct($number) + { + $this->number = $number; + } + + public function handle() + { + // + } +} + +class JobWithChainStub +{ + use Queueable; + + function __construct($chain) + { + $this->chain($chain); + } + + public function handle() + { + // + } +} + +class NotPushedJobStub +{ + public function handle() + { + // + } +} + +class JobWithParameterAndChainStub +{ + use Queueable; + + public $parameter; + + function __construct($parameter, $chain) + { + $this->parameter = $parameter; + $this->chain($chain); + } + + public function handle() + { + // + } +} \ No newline at end of file