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] Adds partial queue faking #41344

Closed
wants to merge 9 commits into from
Closed
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
4 changes: 2 additions & 2 deletions src/Illuminate/Bus/BusServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class BusServiceProvider extends ServiceProvider implements DeferrableProvider
public function register()
{
$this->app->singleton(Dispatcher::class, function ($app) {
return new Dispatcher($app, function ($connection = null) use ($app) {
return $app[QueueFactoryContract::class]->connection($connection);
return new Dispatcher($app, function ($connection = null, $job = null) use ($app) {
return $app[QueueFactoryContract::class]->connection($connection, $job);
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Bus/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public function dispatchToQueue($command)
{
$connection = $command->connection ?? null;

$queue = call_user_func($this->queueResolver, $connection);
$queue = call_user_func($this->queueResolver, $connection, $command);

if (! $queue instanceof Queue) {
throw new RuntimeException('Queue resolver did not return a Queue implementation.');
Expand Down
5 changes: 3 additions & 2 deletions src/Illuminate/Support/Facades/Queue.php
Original file line number Diff line number Diff line change
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
65 changes: 61 additions & 4 deletions src/Illuminate/Support/Testing/Fakes/QueueFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,52 @@
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\Queue\QueueManager|null
*/
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 @@ -248,12 +280,16 @@ public function hasPushed($job)
/**
* Resolve a queue connection instance.
*
* @param mixed $value
* @param string|null $name
* @return \Illuminate\Contracts\Queue\Queue
*/
public function connection($value = null)
public function connection($name = null, $job = null)
{
return $this;
if ($this->shouldFakeJob($job)) {
return $this;
}

return $this->queue->connection($name);
}

/**
Expand All @@ -275,7 +311,7 @@ public function size($queue = null)
* @param string|object $job
* @param mixed $data
* @param string|null $queue
* @return mixed
* @return void
*/
public function push($job, $data = '', $queue = null)
{
Expand Down Expand Up @@ -375,6 +411,27 @@ public function pushedJobs()
return $this->jobs;
}

/**
* 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;
}

if (is_null($this->queue)) {
return true;
}

return $this->jobsToFake->contains(function ($jobToFake) use ($job) {
return get_class($job) === $jobToFake;
});
}

/**
* Get the connection name for the queue.
*
Expand Down
43 changes: 43 additions & 0 deletions tests/Support/SupportTestingQueueFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
use BadMethodCallException;
use Illuminate\Bus\Queueable;
use Illuminate\Foundation\Application;
use Illuminate\Queue\DatabaseQueue;
use Illuminate\Queue\QueueManager;
use Illuminate\Queue\SyncQueue;
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 +33,12 @@ protected function setUp(): void
$this->job = new JobStub;
}

protected function tearDown(): void
{
parent::tearDown();
m::close();
}

public function testAssertPushed()
{
try {
Expand All @@ -52,6 +62,31 @@ public function testAssertPushedWithClosure()
});
}

public function testConnectionWithFakedJobs()
{
$manager = m::mock(QueueManager::class);
$manager->shouldReceive('connection')->once()->andReturn(new SyncQueue);

$fake = new QueueFake(new Application, JobToFakeStub::class, $manager);

$this->assertInstanceOf(SyncQueue::class, $fake->connection(null, new JobStub));
$this->assertInstanceOf(QueueFake::class, $fake->connection(null, new JobToFakeStub));
}

public function testConnectionWithFakedJobsAndCustomConnection()
{
$manager = m::mock(QueueManager::class);
$manager->shouldReceive('connection')
->once()
->with('database')
->andReturn(m::mock(DatabaseQueue::class));

$fake = new QueueFake(new Application, JobToFakeStub::class, $manager);

$this->assertInstanceOf(DatabaseQueue::class, $fake->connection('database', new JobStub));
$this->assertInstanceOf(QueueFake::class, $fake->connection(null, new JobToFakeStub));
}

public function testQueueSize()
{
$this->assertEquals(0, $this->fake->size());
Expand Down Expand Up @@ -297,6 +332,14 @@ public function handle()
}
}

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

class JobWithChainStub
{
use Queueable;
Expand Down