Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Add partial queue faking #41425

Merged
merged 2 commits into from Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Illuminate/Support/Facades/Queue.php
Expand Up @@ -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;
}
Expand Down
62 changes: 58 additions & 4 deletions src/Illuminate/Support/Testing/Fakes/QueueFake.php
Expand Up @@ -6,20 +6,51 @@
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;

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.
*
* @var array
*/
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.
*
Expand Down Expand Up @@ -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);
});
}

/**
Expand Down
35 changes: 35 additions & 0 deletions tests/Support/SupportTestingQueueFakeTest.php
Expand Up @@ -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;
Expand All @@ -29,6 +31,13 @@ protected function setUp(): void
$this->job = new JobStub;
}

protected function tearDown(): void
{
parent::tearDown();

m::close();
}

public function testAssertPushed()
{
try {
Expand All @@ -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);
Expand Down Expand Up @@ -297,6 +324,14 @@ public function handle()
}
}

class JobToFakeStub
{
public function handle()
{
//
}
}

class JobWithChainStub
{
use Queueable;
Expand Down