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

[11.x] Allow for assertions against QueueFake::pushRaw() #54703

Merged
merged 2 commits into from
Feb 19, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion src/Illuminate/Support/Testing/Fakes/QueueFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use Illuminate\Support\Traits\ReflectsClosures;
use PHPUnit\Framework\Assert as PHPUnit;

/**
* @phpstan-type RawPushType array{"payload": string, "queue": string|null, "options": array<array-key, mixed>}
*/
class QueueFake extends QueueManager implements Fake, Queue
{
use ReflectsClosures;
Expand Down Expand Up @@ -43,6 +46,13 @@ class QueueFake extends QueueManager implements Fake, Queue
*/
protected $jobs = [];

/**
* All of the payloads that have been raw pushed.
*
* @var list<RawPushType>
*/
protected $rawPushes = [];

/**
* Indicates if items should be serialized and restored when pushed to the queue.
*
Expand Down Expand Up @@ -326,6 +336,19 @@ public function pushed($job, $callback = null)
)->pluck('job');
}

/**
* Get all of the raw pushes matching a truth-test callback.
*
* @param null|\Closure(string, ?string, array): bool $callback
* @return \Illuminate\Support\Collection<int, RawPushType>
*/
public function pushedRaw($callback = null)
{
$callback ??= static fn () => true;

return (new Collection($this->rawPushes))->filter(fn ($data) => $callback($data['payload'], $data['queue'], $data['options']));
}

/**
* Determine if there are any stored jobs for a given class.
*
Expand Down Expand Up @@ -436,7 +459,11 @@ protected function shouldDispatchJob($job)
*/
public function pushRaw($payload, $queue = null, array $options = [])
{
//
$this->rawPushes[] = [
'payload' => $payload,
'queue' => $queue,
'options' => $options,
];
}

/**
Expand Down Expand Up @@ -516,6 +543,16 @@ public function pushedJobs()
return $this->jobs;
}

/**
* Get the payloads that were pushed raw.
*
* @return list<RawPushType>
*/
public function rawPushes()
{
return $this->rawPushes;
}

/**
* Specify if jobs should be serialized and restored when being "pushed" to the queue.
*
Expand Down
38 changes: 38 additions & 0 deletions tests/Support/SupportTestingQueueFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,44 @@ public function testAssertChainErrorHandling()
$this->assertStringContainsString('The job has chained jobs.', $e->getMessage());
}
}

public function testGetRawPushes()
{
$this->fake->pushRaw('some-payload', null, ['options' => 'yeah']);
$this->fake->pushRaw('some-other-payload', 'my-queue', ['options' => 'also yeah']);

$actualPushedRaw = $this->fake->rawPushes();

$this->assertEqualsCanonicalizing([
['payload' => 'some-payload', 'queue' => null, 'options' => ['options' => 'yeah']],
['payload' => 'some-other-payload', 'queue' => 'my-queue', 'options' => ['options' => 'also yeah']],
], $actualPushedRaw);
}

public function testPushedRaw()
{
$this->fake->pushRaw('some-payload', null, ['options' => 'yeah']);
$this->fake->pushRaw('some-other-payload', 'my-queue', ['options' => 'also yeah']);

$this->assertCount(2, $this->fake->pushedRaw());

$pushedRaw = $this->fake->pushedRaw(fn ($payload) => $payload === 'some-payload');
$this->assertCount(1, $pushedRaw);
$this->assertEqualsCanonicalizing(
['payload' => 'some-payload', 'queue' => null, 'options' => ['options' => 'yeah']],
$pushedRaw[0]
);

$pushedRaw = $this->fake->pushedRaw(
fn ($payload, $queue, $options) => $payload === 'some-other-payload'
&& $queue === 'my-queue'
&& $options['options'] === 'also yeah'
);
$this->assertCount(1, $pushedRaw);

$pushedRaw = $this->fake->pushedRaw(fn ($payload, $queue, $options) => $options === []);
$this->assertCount(0, $pushedRaw);
}
}

class JobStub
Expand Down
Loading