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] Implement except method for fake classes to define what should not be faked #44117

Merged
merged 5 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/Illuminate/Support/Testing/Fakes/QueueFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ class QueueFake extends QueueManager implements Queue
*/
protected $jobsToFake;

/**
* The job types that should be pushed to the queue and not intercepted.
*
* @var Collection
*/
protected $jobsToBeQueued;

/**
* All of the jobs that have been pushed.
*
Expand All @@ -48,6 +55,7 @@ public function __construct($app, $jobsToFake = [], $queue = null)
parent::__construct($app);

$this->jobsToFake = Collection::wrap($jobsToFake);
$this->jobsToBeQueued = Collection::make();
$this->queue = $queue;
}

Expand Down Expand Up @@ -323,6 +331,10 @@ public function push($job, $data = '', $queue = null)
*/
public function shouldFakeJob($job)
mateusjunges marked this conversation as resolved.
Show resolved Hide resolved
{
if ($this->shouldDispatchJob($job)) {
return false;
}

if ($this->jobsToFake->isEmpty()) {
return true;
}
Expand All @@ -332,6 +344,36 @@ public function shouldFakeJob($job)
);
}

/**
* Determine if a job should be pushed to the queue instead of faked.
*
* @param object $job
* @return bool
*/
public function shouldDispatchJob($job)
{
if ($this->jobsToBeQueued->isEmpty()) {
return false;
}

return $this->jobsToBeQueued->contains(
fn ($jobToQueue) => $job instanceof ((string) $jobToQueue)
);
}

/**
* Specify the jobs that should be queued instead of faked.
*
* @param array|string $jobsToBeQueued
* @return $this
*/
public function except($jobsToBeQueued)
{
$this->jobsToBeQueued = Collection::wrap($jobsToBeQueued)->merge($this->jobsToBeQueued);

return $this;
}

/**
* Push a raw payload onto the queue.
*
Expand Down
18 changes: 18 additions & 0 deletions tests/Support/SupportTestingQueueFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,24 @@ public function testCallUndefinedMethodErrorHandling()
)));
}
}

public function testItDoesntFakeJobsPassedViaExcept()
{
$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, [], $manager))->except(JobStub::class);

$fake->push($job);
$fake->push(new JobToFakeStub());

$fake->assertNotPushed(JobStub::class);
$fake->assertPushed(JobToFakeStub::class);
}
}

class JobStub
Expand Down