From b8ee5372b7f26c051fca0bc4c4c8faab5eea2144 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 10 Mar 2022 12:43:58 -0600 Subject: [PATCH 1/2] add partial queue faking --- src/Illuminate/Support/Facades/Queue.php | 5 +- .../Support/Testing/Fakes/QueueFake.php | 62 +++++++++++++++++-- tests/Support/SupportTestingQueueFakeTest.php | 35 +++++++++++ 3 files changed, 96 insertions(+), 6 deletions(-) diff --git a/src/Illuminate/Support/Facades/Queue.php b/src/Illuminate/Support/Facades/Queue.php index 5af1329e0356..4b67884fd5ab 100755 --- a/src/Illuminate/Support/Facades/Queue.php +++ b/src/Illuminate/Support/Facades/Queue.php @@ -42,11 +42,12 @@ public static function popUsing($workerName, $callback) /** * Replace the bound instance with a fake. * + * @param array|string $jobsToFake * @return \Illuminate\Support\Testing\Fakes\QueueFake */ - public static function fake() + public static function fake($jobsToFake = []) { - static::swap($fake = new QueueFake(static::getFacadeApplication())); + static::swap($fake = new QueueFake(static::getFacadeApplication(), $jobsToFake, static::getFacadeRoot())); return $fake; } diff --git a/src/Illuminate/Support/Testing/Fakes/QueueFake.php b/src/Illuminate/Support/Testing/Fakes/QueueFake.php index cb34915b13cf..3e2444ae9e5f 100644 --- a/src/Illuminate/Support/Testing/Fakes/QueueFake.php +++ b/src/Illuminate/Support/Testing/Fakes/QueueFake.php @@ -6,6 +6,7 @@ use Closure; use Illuminate\Contracts\Queue\Queue; use Illuminate\Queue\QueueManager; +use Illuminate\Support\Collection; use Illuminate\Support\Traits\ReflectsClosures; use PHPUnit\Framework\Assert as PHPUnit; @@ -13,6 +14,20 @@ class QueueFake extends QueueManager implements Queue { use ReflectsClosures; + /** + * The original queue manager. + * + * @var \Illuminate\Contracts\Queue\Queue + */ + protected $queue; + + /** + * The job types that should be intercepted instead of pushed to the queue. + * + * @var array + */ + protected $jobsToFake; + /** * All of the jobs that have been pushed. * @@ -20,6 +35,22 @@ class QueueFake extends QueueManager implements Queue */ protected $jobs = []; + /** + * Create a new fake queue instance. + * + * @param \Illuminate\Contracts\Foundation\Application $app + * @param array $jobsToFake + * @param \Illuminate\Queue\QueueManager|null $queue + * @return void + */ + public function __construct($app, $jobsToFake = [], $queue = null) + { + parent::__construct($app); + + $this->jobsToFake = Collection::wrap($jobsToFake); + $this->queue = $queue; + } + /** * Assert if a job was pushed based on a truth-test callback. * @@ -279,10 +310,33 @@ public function size($queue = null) */ public function push($job, $data = '', $queue = null) { - $this->jobs[is_object($job) ? get_class($job) : $job][] = [ - 'job' => $job, - 'queue' => $queue, - ]; + if ($this->shouldFakeJob($job)) { + $this->jobs[is_object($job) ? get_class($job) : $job][] = [ + 'job' => $job, + 'queue' => $queue, + ]; + } else { + is_object($job) && isset($job->connection) + ? $this->queue->connection($job->connection)->push($job, $data, $queue) + : $this->queue->push($job, $data, $queue); + } + } + + /** + * Determine if a job should be faked or actually dispatched. + * + * @param object $job + * @return bool + */ + public function shouldFakeJob($job) + { + if ($this->jobsToFake->isEmpty()) { + return true; + } + + return $this->jobsToFake->contains(function ($jobToFake) use ($job) { + return $job instanceof ((string) $jobToFake); + }); } /** diff --git a/tests/Support/SupportTestingQueueFakeTest.php b/tests/Support/SupportTestingQueueFakeTest.php index cf22717cf29b..42b198c8f8a4 100644 --- a/tests/Support/SupportTestingQueueFakeTest.php +++ b/tests/Support/SupportTestingQueueFakeTest.php @@ -5,7 +5,9 @@ use BadMethodCallException; use Illuminate\Bus\Queueable; use Illuminate\Foundation\Application; +use Illuminate\Queue\QueueManager; use Illuminate\Support\Testing\Fakes\QueueFake; +use Mockery as m; use PHPUnit\Framework\Constraint\ExceptionMessage; use PHPUnit\Framework\ExpectationFailedException; use PHPUnit\Framework\TestCase; @@ -29,6 +31,13 @@ protected function setUp(): void $this->job = new JobStub; } + protected function tearDown(): void + { + parent::tearDown(); + + m::close(); + } + public function testAssertPushed() { try { @@ -43,6 +52,24 @@ public function testAssertPushed() $this->fake->assertPushed(JobStub::class); } + public function testAssertPushedWithIgnore() + { + $job = new JobStub; + + $manager = m::mock(QueueManager::class); + $manager->shouldReceive('push')->once()->withArgs(function($passedJob) use ($job){ + return $passedJob === $job; + }); + + $fake = new QueueFake(new Application, JobToFakeStub::class, $manager); + + $fake->push($job); + $fake->push(new JobToFakeStub()); + + $fake->assertNotPushed(JobStub::class); + $fake->assertPushed(JobToFakeStub::class); + } + public function testAssertPushedWithClosure() { $this->fake->push($this->job); @@ -297,6 +324,14 @@ public function handle() } } +class JobToFakeStub +{ + public function handle() + { + // + } +} + class JobWithChainStub { use Queueable; From 621d5e1e7f1aed9af28208cea96f6e3924cadbdd Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 10 Mar 2022 18:46:43 +0000 Subject: [PATCH 2/2] Apply fixes from StyleCI --- tests/Support/SupportTestingQueueFakeTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Support/SupportTestingQueueFakeTest.php b/tests/Support/SupportTestingQueueFakeTest.php index 42b198c8f8a4..049482452aaa 100644 --- a/tests/Support/SupportTestingQueueFakeTest.php +++ b/tests/Support/SupportTestingQueueFakeTest.php @@ -57,7 +57,7 @@ public function testAssertPushedWithIgnore() $job = new JobStub; $manager = m::mock(QueueManager::class); - $manager->shouldReceive('push')->once()->withArgs(function($passedJob) use ($job){ + $manager->shouldReceive('push')->once()->withArgs(function ($passedJob) use ($job) { return $passedJob === $job; });