From 81e322e6e5d2c21122e1983a2ba1bbd241c0d87f Mon Sep 17 00:00:00 2001 From: asantibanez Date: Sat, 3 Mar 2018 22:41:58 -0500 Subject: [PATCH 1/5] Added test helper to assert that a Job has been queued with a Chain --- .../Support/Testing/Fakes/QueueFake.php | 41 ++++++++++++++ tests/Support/SupportTestingQueueFakeTest.php | 54 +++++++++++++++++++ 2 files changed, 95 insertions(+) diff --git a/src/Illuminate/Support/Testing/Fakes/QueueFake.php b/src/Illuminate/Support/Testing/Fakes/QueueFake.php index 66d191e90c95..251c894485f9 100644 --- a/src/Illuminate/Support/Testing/Fakes/QueueFake.php +++ b/src/Illuminate/Support/Testing/Fakes/QueueFake.php @@ -34,6 +34,27 @@ public function assertPushed($job, $callback = null) ); } + /** + * Assert if a job was pushed with chain. + * + * @param string $job + * @param array $chain + * @return void + */ + public function assertPushedWithChain($job, $chain = []) + { + PHPUnit::assertTrue( + $this->pushed($job)->count() > 0, + "The expected [{$job}] job was not pushed." + ); + + PHPUnit::assertEquals( + collect($chain)->map(function ($job) { return serialize($job); })->all(), + $this->pushed($job)->first()->chained, + "The expected chain was not pushed." + ); + } + /** * Assert if a job was pushed a number of times. * @@ -161,9 +182,29 @@ public function push($job, $data = '', $queue = null) $this->jobs[is_object($job) ? get_class($job) : $job][] = [ 'job' => $job, 'queue' => $queue, + 'chain' => $this->getJobChain($job) ]; } + /** + * @param mixed $job + * @return array + */ + private function getJobChain($job) + { + if (is_object($job)) + return []; + + dd($job->chained); + + return collect($job->chained) + ->map(function ($shackle) { + return unserialize($shackle); + })->map(function ($shackle) { + return get_class($shackle); + })->toArray(); + } + /** * Push a raw payload onto the queue. * diff --git a/tests/Support/SupportTestingQueueFakeTest.php b/tests/Support/SupportTestingQueueFakeTest.php index 495bb0684d3e..77bf1ca9b36b 100644 --- a/tests/Support/SupportTestingQueueFakeTest.php +++ b/tests/Support/SupportTestingQueueFakeTest.php @@ -2,6 +2,9 @@ namespace Illuminate\Tests\Support; +use Illuminate\Bus\Queueable; +use Illuminate\Queue\SerializesAndRestoresModelIdentifiers; +use Illuminate\Queue\SerializesModels; use PHPUnit\Framework\TestCase; use Illuminate\Foundation\Application; use Illuminate\Support\Testing\Fakes\QueueFake; @@ -101,6 +104,27 @@ public function testAssertPushedUsingBulk() $this->fake->assertPushedOn($queue, JobStub::class); $this->fake->assertPushed(JobStub::class, 2); } + + public function testAssertPushedWithChain() + { + $chain = [ + new JobStub, + new JobWithParameterStub(100) + ]; + + $jobWithChain = new JobWithChainStub($chain); + + $this->fake->push($jobWithChain); + $this->fake->assertPushed(JobWithChainStub::class); + $this->fake->assertPushedWithChain(JobWithChainStub::class, $chain); + + try { + $this->fake->assertPushedWithChain(JobWithChainStub::class, []); + $this->fail(); + } catch (ExpectationFailedException $e) { + $this->assertThat($e, new ExceptionMessage('The expected chain was not pushed.')); + } + } } class JobStub @@ -110,3 +134,33 @@ 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() + { + // + } +} \ No newline at end of file From ce927ff9ed38adac06dc5553bebd11d21e950353 Mon Sep 17 00:00:00 2001 From: asantibanez Date: Sat, 3 Mar 2018 22:50:59 -0500 Subject: [PATCH 2/5] Removed previous implementation dead code --- .../Support/Testing/Fakes/QueueFake.php | 22 +------------------ 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/src/Illuminate/Support/Testing/Fakes/QueueFake.php b/src/Illuminate/Support/Testing/Fakes/QueueFake.php index 251c894485f9..c411486c0ded 100644 --- a/src/Illuminate/Support/Testing/Fakes/QueueFake.php +++ b/src/Illuminate/Support/Testing/Fakes/QueueFake.php @@ -181,30 +181,10 @@ public function push($job, $data = '', $queue = null) { $this->jobs[is_object($job) ? get_class($job) : $job][] = [ 'job' => $job, - 'queue' => $queue, - 'chain' => $this->getJobChain($job) + 'queue' => $queue ]; } - /** - * @param mixed $job - * @return array - */ - private function getJobChain($job) - { - if (is_object($job)) - return []; - - dd($job->chained); - - return collect($job->chained) - ->map(function ($shackle) { - return unserialize($shackle); - })->map(function ($shackle) { - return get_class($shackle); - })->toArray(); - } - /** * Push a raw payload onto the queue. * From eeb582690eca79b024676eeab649cbc69b228c87 Mon Sep 17 00:00:00 2001 From: asantibanez Date: Fri, 9 Mar 2018 11:10:00 -0500 Subject: [PATCH 3/5] Added support to check for chained jobs using class names - Added callback to support identifying particular jobs - Added tests --- .../Support/Testing/Fakes/QueueFake.php | 39 ++++++++-- tests/Support/SupportTestingQueueFakeTest.php | 77 +++++++++++++++++-- 2 files changed, 104 insertions(+), 12 deletions(-) diff --git a/src/Illuminate/Support/Testing/Fakes/QueueFake.php b/src/Illuminate/Support/Testing/Fakes/QueueFake.php index c411486c0ded..ee11f9e93673 100644 --- a/src/Illuminate/Support/Testing/Fakes/QueueFake.php +++ b/src/Illuminate/Support/Testing/Fakes/QueueFake.php @@ -35,22 +35,51 @@ public function assertPushed($job, $callback = null) } /** - * Assert if a job was pushed with chain. + * 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 = []) + public function assertPushedWithChain($job, $chain = [], $callback = null) { PHPUnit::assertTrue( - $this->pushed($job)->count() > 0, + $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( - collect($chain)->map(function ($job) { return serialize($job); })->all(), - $this->pushed($job)->first()->chained, + $chain, + $chainedClasses, "The expected chain was not pushed." ); } diff --git a/tests/Support/SupportTestingQueueFakeTest.php b/tests/Support/SupportTestingQueueFakeTest.php index 77bf1ca9b36b..28c26afeec71 100644 --- a/tests/Support/SupportTestingQueueFakeTest.php +++ b/tests/Support/SupportTestingQueueFakeTest.php @@ -107,22 +107,59 @@ public function testAssertPushedUsingBulk() public function testAssertPushedWithChain() { - $chain = [ + $jobWithChain = new JobWithChainStub([ new JobStub, - new JobWithParameterStub(100) - ]; + new JobWithParameterStub(0) + ]); - $jobWithChain = new JobWithChainStub($chain); + $jobWithParameterAndChain = new JobWithParameterAndChainStub('first', [ + new JobStub, + ]); + $anotherJobWithParameterAndChain = new JobWithParameterAndChainStub('second', [ + new JobWithParameterStub(1) + ]); $this->fake->push($jobWithChain); - $this->fake->assertPushed(JobWithChainStub::class); - $this->fake->assertPushedWithChain(JobWithChainStub::class, $chain); + $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 was not pushed.')); + $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')); } } } @@ -159,6 +196,32 @@ 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() { // From 0d9933b3c953142e90c05d5e2ed5152c82fc42c2 Mon Sep 17 00:00:00 2001 From: asantibanez Date: Fri, 9 Mar 2018 11:17:43 -0500 Subject: [PATCH 4/5] Removed unused dependencies --- tests/Support/SupportTestingQueueFakeTest.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/Support/SupportTestingQueueFakeTest.php b/tests/Support/SupportTestingQueueFakeTest.php index 28c26afeec71..8b17bc973bf6 100644 --- a/tests/Support/SupportTestingQueueFakeTest.php +++ b/tests/Support/SupportTestingQueueFakeTest.php @@ -3,13 +3,11 @@ namespace Illuminate\Tests\Support; use Illuminate\Bus\Queueable; -use Illuminate\Queue\SerializesAndRestoresModelIdentifiers; -use Illuminate\Queue\SerializesModels; -use PHPUnit\Framework\TestCase; 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 { From 46ab9beb00bc807b035559ae0dd9df181d324195 Mon Sep 17 00:00:00 2001 From: asantibanez Date: Mon, 12 Mar 2018 13:58:12 -0500 Subject: [PATCH 5/5] Fixed implementation to take into account same job with different chains - Separated assertPushedWithChain tests for each specific case - Removed unused stub classes --- .../Support/Testing/Fakes/QueueFake.php | 53 ++++---- tests/Support/SupportTestingQueueFakeTest.php | 120 +++++++++++------- 2 files changed, 101 insertions(+), 72 deletions(-) diff --git a/src/Illuminate/Support/Testing/Fakes/QueueFake.php b/src/Illuminate/Support/Testing/Fakes/QueueFake.php index ee11f9e93673..c1d1f6a7d79b 100644 --- a/src/Illuminate/Support/Testing/Fakes/QueueFake.php +++ b/src/Illuminate/Support/Testing/Fakes/QueueFake.php @@ -2,8 +2,8 @@ namespace Illuminate\Support\Testing\Fakes; -use Illuminate\Queue\QueueManager; use Illuminate\Contracts\Queue\Queue; +use Illuminate\Queue\QueueManager; use PHPUnit\Framework\Assert as PHPUnit; class QueueFake extends QueueManager implements Queue @@ -37,12 +37,12 @@ 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 + * @param string $job + * @param array $expectedChain + * @param callable|null $callback * @return void */ - public function assertPushedWithChain($job, $chain = [], $callback = null) + public function assertPushedWithChain($job, $expectedChain = [], $callback = null) { PHPUnit::assertTrue( $this->pushed($job, $callback)->count() > 0, @@ -50,37 +50,40 @@ public function assertPushedWithChain($job, $chain = [], $callback = null) ); PHPUnit::assertTrue( - collect($chain)->count() > 0, + collect($expectedChain)->count() > 0, "The expected chain can not be empty." ); - $isChainOfObjects = collect($chain) - ->filter(function ($job) { - return is_object($job); - })->count() == collect($chain)->count(); + $isExpectedChainOfObjects = collect($expectedChain) + ->filter(function ($job) { return is_object($job); }) + ->count() == collect($expectedChain)->count(); - if ($isChainOfObjects) + if ($isExpectedChainOfObjects) { - PHPUnit::assertEquals( - collect($chain)->map(function ($job) { return serialize($job); })->all(), - $this->pushed($job, $callback)->first()->chained, + $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; } - $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." + 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' ); } diff --git a/tests/Support/SupportTestingQueueFakeTest.php b/tests/Support/SupportTestingQueueFakeTest.php index 8b17bc973bf6..01081c341fa8 100644 --- a/tests/Support/SupportTestingQueueFakeTest.php +++ b/tests/Support/SupportTestingQueueFakeTest.php @@ -103,33 +103,83 @@ public function testAssertPushedUsingBulk() $this->fake->assertPushed(JobStub::class, 2); } - public function testAssertPushedWithChain() + public function testAssertPushedWithChainUsingClassesOrObjectsArray() { - $jobWithChain = new JobWithChainStub([ - new JobStub, - new JobWithParameterStub(0) - ]); + $this->fake->push(new JobWithChainStub([ + new JobStub + ])); - $jobWithParameterAndChain = new JobWithParameterAndChainStub('first', [ - new JobStub, + $this->fake->assertPushedWithChain(JobWithChainStub::class, [ + JobStub::class ]); - $anotherJobWithParameterAndChain = new JobWithParameterAndChainStub('second', [ - new JobWithParameterStub(1) + + $this->fake->assertPushedWithChain(JobWithChainStub::class, [ + new JobStub ]); + } - $this->fake->push($jobWithChain); - $this->fake->push($jobWithParameterAndChain); - $this->fake->push($anotherJobWithParameterAndChain); + public function testAssertPushedWithChainSameJobDifferentChains() + { + $this->fake->push(new JobWithChainStub([ + new JobStub + ])); + $this->fake->push(new JobWithChainStub([ + new JobStub, + new JobStub + ])); $this->fake->assertPushedWithChain(JobWithChainStub::class, [ - new JobStub, - new JobWithParameterStub(0) + JobStub::class ]); $this->fake->assertPushedWithChain(JobWithChainStub::class, [ JobStub::class, - JobWithParameterStub::class + JobStub::class ]); + } + + public function testAssertPushedWithChainUsingCallback() + { + $this->fake->push(new JobWithChainAndParameterStub('first', [ + new JobStub, + new JobStub + ])); + + $this->fake->push(new JobWithChainAndParameterStub('second', [ + new JobStub + ])); + + $this->fake->assertPushedWithChain(JobWithChainAndParameterStub::class, [ + JobStub::class + ], function ($job) { + return $job->parameter == 'second'; + }); + + try { + $this->fake->assertPushedWithChain(JobWithChainAndParameterStub::class, [ + JobStub::class, + JobStub::class, + ], function ($job) { + return $job->parameter == 'second'; + }); + $this->fail(); + } catch (ExpectationFailedException $e) { + $this->assertThat($e, new ExceptionMessage('The expected chain was not pushed')); + } + } + + public function testAssertPushedWithChainErrorHandling() + { + try { + $this->fake->assertPushedWithChain(JobWithChainStub::class, []); + $this->fail(); + } catch (ExpectationFailedException $e) { + $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\JobWithChainStub] job was not pushed')); + } + + $this->fake->push(new JobWithChainStub([ + new JobStub + ])); try { $this->fake->assertPushedWithChain(JobWithChainStub::class, []); @@ -140,6 +190,7 @@ public function testAssertPushedWithChain() try { $this->fake->assertPushedWithChain(JobWithChainStub::class, [ + new JobStub, new JobStub ]); $this->fail(); @@ -147,38 +198,21 @@ public function testAssertPushedWithChain() $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->fake->assertPushedWithChain(JobWithChainStub::class, [ + JobStub::class, + JobStub::class + ]); $this->fail(); } catch (ExpectationFailedException $e) { - $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\NotPushedJobStub] job was not pushed')); + $this->assertThat($e, new ExceptionMessage('The expected chain was not pushed')); } } -} -class JobStub -{ - public function handle() - { - // - } } -class JobWithParameterStub +class JobStub { - public $number; - - function __construct($number) - { - $this->number = $number; - } - public function handle() { // @@ -200,15 +234,7 @@ public function handle() } } -class NotPushedJobStub -{ - public function handle() - { - // - } -} - -class JobWithParameterAndChainStub +class JobWithChainAndParameterStub { use Queueable;