Allow PHP-8.2 and up Compatibility instead of just PHP-8.4

This commit is contained in:
johnnyq
2026-06-12 17:06:10 -04:00
parent 2204bd52f4
commit d3a93652f3
220 changed files with 7198 additions and 2635 deletions

View File

@@ -137,7 +137,6 @@ class BusFake implements Fake, QueueingDispatcher
* Assert if a job was pushed exactly once.
*
* @param string|\Closure $command
* @param int $times
* @return void
*/
public function assertDispatchedOnce($command)
@@ -488,7 +487,7 @@ class BusFake implements Fake, QueueingDispatcher
/**
* Create a new assertion about a chained batch.
*
* @param \Closure $callback
* @param \Closure(\Illuminate\Bus\PendingBatch): bool $callback
* @return \Illuminate\Support\Testing\Fakes\ChainedBatchTruthTest
*/
public function chainedBatch(Closure $callback)
@@ -499,11 +498,13 @@ class BusFake implements Fake, QueueingDispatcher
/**
* Assert if a batch was dispatched based on a truth-test callback.
*
* @param callable $callback
* @param array|callable(\Illuminate\Bus\PendingBatch): bool $callback
* @return void
*/
public function assertBatched(callable $callback)
public function assertBatched(callable|array $callback)
{
$callback = is_array($callback) ? fn (PendingBatchFake $batch) => $batch->hasJobs($callback) : $callback;
PHPUnit::assertTrue(
$this->batched($callback)->count() > 0,
'The expected batch was not dispatched.'
@@ -606,8 +607,8 @@ class BusFake implements Fake, QueueingDispatcher
/**
* Get all of the pending batches matching a truth-test callback.
*
* @param callable $callback
* @return \Illuminate\Support\Collection
* @param callable(\Illuminate\Bus\PendingBatch): bool $callback
* @return \Illuminate\Support\Collection<int, \Illuminate\Bus\PendingBatch>
*/
public function batched(callable $callback)
{

View File

@@ -9,14 +9,14 @@ class ChainedBatchTruthTest
/**
* The underlying truth test.
*
* @var \Closure
* @var \Closure(\Illuminate\Bus\PendingBatch): bool
*/
protected $callback;
/**
* Create a new truth test instance.
*
* @param \Closure $callback
* @param \Closure(\Illuminate\Bus\PendingBatch): bool $callback
*/
public function __construct(Closure $callback)
{

View File

@@ -151,7 +151,6 @@ class EventFake implements Dispatcher, Fake
* Assert if an event was dispatched exactly once.
*
* @param string $event
* @param int $times
* @return void
*/
public function assertDispatchedOnce($event)

View File

@@ -113,7 +113,7 @@ class ExceptionHandlerFake implements ExceptionHandler, Fake
{
try {
$this->assertReported($exception);
} catch (ExpectationFailedException $e) {
} catch (ExpectationFailedException) {
return;
}

View File

@@ -102,7 +102,7 @@ class MailFake implements Factory, Fake, Mailer, MailQueue
* @param int $times
* @return void
*/
protected function assertSentTimes($mailable, $times = 1)
public function assertSentTimes($mailable, $times = 1)
{
$count = $this->sent($mailable)->count();

View File

@@ -195,7 +195,7 @@ class NotificationFake implements Fake, NotificationDispatcher, NotificationFact
}
PHPUnit::assertEmpty(
$this->notifications[get_class($notifiable)][$notifiable->getKey()] ?? [],
$this->notifications[get_class($notifiable)][$notifiable->getKey() ?? ''] ?? [],
'Notifications were sent unexpectedly.',
);
}
@@ -314,7 +314,7 @@ class NotificationFake implements Fake, NotificationDispatcher, NotificationFact
foreach ($notifiables as $notifiable) {
if (! $notification->id) {
$notification->id = Str::uuid()->toString();
$notification->id = (string) Str::uuid();
}
$notifiableChannels = $channels ?: $notification->via($notifiable);

View File

@@ -2,11 +2,15 @@
namespace Illuminate\Support\Testing\Fakes;
use Closure;
use Illuminate\Bus\PendingBatch;
use Illuminate\Support\Collection;
use Illuminate\Support\Traits\ReflectsClosures;
class PendingBatchFake extends PendingBatch
{
use ReflectsClosures;
/**
* The fake bus instance.
*
@@ -23,7 +27,7 @@ class PendingBatchFake extends PendingBatch
public function __construct(BusFake $bus, Collection $jobs)
{
$this->bus = $bus;
$this->jobs = $jobs;
$this->jobs = $jobs->filter()->values();
}
/**
@@ -45,4 +49,39 @@ class PendingBatchFake extends PendingBatch
{
return $this->bus->recordPendingBatch($this);
}
/**
* Determine if the jobs in the batch match the given jobs.
*
* @param array $expectedJobs
* @return bool
*/
public function hasJobs(array $expectedJobs)
{
if (count($this->jobs) !== count($expectedJobs)) {
return false;
}
foreach ($expectedJobs as $index => $expectedJob) {
if ($expectedJob instanceof Closure) {
$expectedType = $this->firstClosureParameterType($expectedJob);
if (! $this->jobs[$index] instanceof $expectedType) {
return false;
}
if (! $expectedJob($this->jobs[$index])) {
return false;
}
} elseif (is_string($expectedJob)) {
if ($expectedJob != get_class($this->jobs[$index])) {
return false;
}
} elseif (serialize($expectedJob) != serialize($this->jobs[$index])) {
return false;
}
}
return true;
}
}

View File

@@ -4,7 +4,10 @@ namespace Illuminate\Support\Testing\Fakes;
use BadMethodCallException;
use Closure;
use Illuminate\Bus\UniqueLock;
use Illuminate\Contracts\Cache\Repository as Cache;
use Illuminate\Contracts\Queue\Queue;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Events\CallQueuedListener;
use Illuminate\Queue\CallQueuedClosure;
use Illuminate\Queue\QueueManager;
@@ -55,6 +58,13 @@ class QueueFake extends QueueManager implements Fake, Queue
*/
protected $rawPushes = [];
/**
* All of the unique jobs that were pushed.
*
* @var array
*/
private $uniqueJobs = [];
/**
* Indicates if items should be serialized and restored when pushed to the queue.
*
@@ -121,7 +131,7 @@ class QueueFake extends QueueManager implements Fake, Queue
* @param int $times
* @return void
*/
protected function assertPushedTimes($job, $times = 1)
public function assertPushedTimes($job, $times = 1)
{
$count = $this->pushed($job)->count();
@@ -477,6 +487,10 @@ class QueueFake extends QueueManager implements Fake, Queue
'queue' => $queue,
'data' => $data,
];
if ($job instanceof ShouldBeUnique) {
$this->uniqueJobs[] = $job;
}
} else {
is_object($job) && isset($job->connection)
? $this->queue->connection($job->connection)->push($job, $data, $queue)
@@ -650,6 +664,22 @@ class QueueFake extends QueueManager implements Fake, Queue
return unserialize(serialize($job));
}
/**
* Release the locks for all unique jobs that were pushed.
*
* @return void
*/
public function releaseUniqueJobLocks()
{
$lock = new UniqueLock($this->app->make(Cache::class));
foreach ($this->uniqueJobs as $job) {
$lock->release($job);
}
$this->uniqueJobs = [];
}
/**
* Get the connection name for the queue.
*