Skip to content

Commit 9faf948

Browse files
ZaiatsDmytrobest-usernameclaude
authored
[SN-31237] Fix compatibility with Laravel 11+ (#32)
- Fix QlessJob::attempts() to return actual attempts made instead of max retries - Replace deprecated getMockBuilder()->disableOriginalConstructor() with createMock() - Update testAttempts to verify correct attempts-so-far calculation - Add php ^8.3 constraint to composer.json require - Remove leading whitespace from infection/infection constraint Co-authored-by: Andrii Yarosh <yarosh.andrey2001@gmail.com> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 7d4ef53 commit 9faf948

6 files changed

Lines changed: 56 additions & 67 deletions

File tree

composer.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@
1515
}
1616
],
1717
"require": {
18+
"php": "^8.3",
1819
"ext-json": "*",
1920
"pdffiller/qless-php": "^3.23"
2021
},
2122
"require-dev": {
22-
"illuminate/events": "5.6.*|5.7.*|^6.20",
23-
"infection/infection": "^0.12.0",
24-
"orchestra/testbench": "~3.0",
25-
"phpstan/phpstan": "^0.11.1",
26-
"phpunit/phpunit": ">=5.3 <8.0 | ^8.2",
23+
"illuminate/contracts": "^11.0 | ^12.0",
24+
"illuminate/events": "^11.0 | ^12.0",
25+
"infection/infection": "^0.33.0",
26+
"orchestra/testbench": "^9.0 | ^10.0",
27+
"phpstan/phpstan": "^2.2",
28+
"phpunit/phpunit": "^12.5",
2729
"squizlabs/php_codesniffer": "^3.4"
2830
},
2931
"autoload": {
@@ -42,7 +44,8 @@
4244
"preferred-install": "dist",
4345
"sort-packages": true,
4446
"allow-plugins": {
45-
"ocramius/package-versions": true
47+
"ocramius/package-versions": true,
48+
"infection/extension-installer": true
4649
}
4750
}
4851
}

phpunit.xml

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit
3-
bootstrap="tests/bootstrap.php"
4-
backupGlobals="false"
5-
backupStaticAttributes="false"
6-
colors="true"
7-
verbose="true"
8-
convertErrorsToExceptions="true"
9-
convertNoticesToExceptions="true"
10-
convertWarningsToExceptions="true"
11-
processIsolation="false"
12-
stopOnFailure="false">
13-
<testsuites>
14-
<testsuite name="Laravel Qless test suite">
15-
<directory>tests</directory>
16-
</testsuite>
17-
</testsuites>
18-
<filter>
19-
<whitelist>
20-
<directory suffix=".php">src/</directory>
21-
</whitelist>
22-
</filter>
23-
</phpunit>
2+
<phpunit bootstrap="tests/bootstrap.php" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
3+
<testsuites>
4+
<testsuite name="Laravel Qless test suite">
5+
<directory>tests</directory>
6+
</testsuite>
7+
</testsuites>
8+
<source>
9+
<include>
10+
<directory suffix=".php">src/</directory>
11+
</include>
12+
</source>
13+
</phpunit>

src/Job/QlessJob.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public function delete()
133133
*/
134134
public function attempts()
135135
{
136-
return $this->job->getRetries();
136+
return max(($this->job->getRetries() - $this->job->getRemaining()) + 1, 1);
137137
}
138138

139139
/**

src/Queue/QlessQueue.php

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace LaravelQless\Queue;
44

5+
use Illuminate\Contracts\Container\BindingResolutionException;
56
use Illuminate\Contracts\Queue\Queue as QueueContract;
67
use Illuminate\Queue\InvalidPayloadException;
78
use Illuminate\Queue\Queue;
@@ -62,23 +63,24 @@ public function size($queue = null): int
6263
* Push a raw payload onto the queue.
6364
*
6465
* @param string $payload
65-
* @param string $queueName
66+
* @param string $queue
6667
* @param array $options
68+
*
6769
* @return mixed
6870
*/
69-
public function pushRaw($payload, $queueName = null, array $options = [])
71+
public function pushRaw($payload, $queue = null, array $options = [])
7072
{
7173
$payloadData = array_merge(json_decode($payload, true), $options);
7274

73-
$queueName = $queueName ?? $this->defaultQueue;
75+
$queue = $queue ?? $this->defaultQueue;
7476

75-
$queue = $this->getRandomConnection()->queues[$queueName];
77+
$queueObj = $this->getRandomConnection()->queues[$queue];
7678

7779
$qlessOptions = $payloadData['data'][self::JOB_OPTIONS_KEY] ?? [];
7880

7981
$options = array_merge($qlessOptions, $options);
8082

81-
return $queue->put(
83+
return $queueObj->put(
8284
$payloadData['job'],
8385
$payloadData['data'],
8486
$options['jid'] ?? null,
@@ -95,12 +97,13 @@ public function pushRaw($payload, $queueName = null, array $options = [])
9597
*
9698
* @param string|object $job
9799
* @param mixed $data
98-
* @param string $queueName
100+
* @param string $queue
101+
*
99102
* @return mixed
100103
*/
101-
public function push($job, $data = '', $queueName = null)
104+
public function push($job, $data = '', $queue = null)
102105
{
103-
return $this->pushRaw($this->makePayload($job, (array)$data), $queueName);
106+
return $this->pushRaw($this->makePayload($job, (array)$data), $queue);
104107
}
105108

106109
/**
@@ -109,17 +112,18 @@ public function push($job, $data = '', $queueName = null)
109112
* @param \DateTimeInterface|\DateInterval|int $delay
110113
* @param string|object $job
111114
* @param mixed $data
112-
* @param string $queueName
115+
* @param string $queue
116+
*
113117
* @return mixed
114118
*/
115-
public function later($delay, $job, $data = '', $queueName = null)
119+
public function later($delay, $job, $data = '', $queue = null)
116120
{
117121
$options = $data[self::JOB_OPTIONS_KEY] ?? [];
118122
$options = array_merge($options, ['timeout' => $delay]);
119123

120124
return $this->pushRaw(
121125
$this->makePayload($job, $data, $options),
122-
$queueName,
126+
$queue,
123127
$options
124128
);
125129
}
@@ -130,18 +134,17 @@ public function later($delay, $job, $data = '', $queueName = null)
130134
* @param int $interval
131135
* @param string $job
132136
* @param array $data
133-
* @param string $queueName
137+
* @param string|null $queue
134138
* @return string
135139
*/
136-
public function recur(int $interval, string $job, array $data, ?string $queueName = null): string
140+
public function recur(int $interval, string $job, array $data, ?string $queue = null): string
137141
{
138-
/** @var \Qless\Queues\Queue $queue */
139-
$queue = $this->getNextConnection()->queues[$queueName];
142+
$queueObj = $this->getNextConnection()->queues[$queue];
140143

141144
$options = $data[self::JOB_OPTIONS_KEY] ?? [];
142145
$options = array_merge($options, ['interval' => $interval]);
143146

144-
return $queue->recur(
147+
return $queueObj->recur(
145148
$job,
146149
$data,
147150
$options['interval'],
@@ -157,21 +160,23 @@ public function recur(int $interval, string $job, array $data, ?string $queueNam
157160
/**
158161
* Pop the next job off of the queue.
159162
*
160-
* @param string $queueName
163+
* @param string $queue
164+
*
161165
* @return QlessJob|null
166+
* @throws BindingResolutionException
162167
*/
163-
public function pop($queueName = null)
168+
public function pop($queue = null)
164169
{
165170
$connectionCount = $this->getClientCount();
166171

167172
for ($i = 0; $i < $connectionCount; $i++) {
168173
$connection = $this->getNextConnection();
169174

170-
/** @var \Qless\Queues\Queue $queue */
171-
$queue = $connection->queues[$queueName];
175+
/** @var \Qless\Queues\Queue $queueObj */
176+
$queueObj = $connection->queues[$queue];
172177

173178
/** @var \Qless\Jobs\BaseJob $job */
174-
$job = $queue->pop(self::WORKER_PREFIX . $connection->getWorkerName());
179+
$job = $queueObj->pop(self::WORKER_PREFIX . $connection->getWorkerName());
175180

176181
if ($job) {
177182
break;

tests/Job/QlessJobTest.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,16 @@ public function testDelete(): void
155155
public function testAttempts(): void
156156
{
157157
$job = $this->getJob();
158+
$job->expects(self::once())
159+
->method('getRetries')
160+
->willReturn(5);
158161
$job->expects(self::once())
159162
->method('getRemaining')
160163
->willReturn(3);
161164

162165
$job = (new QlessJob($this->getContainer(), $this->getQueue(), $this->getJobHandler(), $job, ''));
163166

164-
self::assertEquals($job->attempts(), 3);
167+
self::assertEquals(3, $job->attempts());
165168
}
166169

167170
public function testMaxTries(): void
@@ -234,39 +237,31 @@ public function testGetRawBody(): void
234237
*/
235238
private function getContainer()
236239
{
237-
return $this->getMockBuilder(Container::class)
238-
->disableOriginalConstructor()
239-
->getMock();
240+
return $this->createMock(Container::class);
240241
}
241242

242243
/**
243244
* @return QlessQueue|\PHPUnit\Framework\MockObject\MockObject
244245
*/
245246
private function getQueue()
246247
{
247-
return $this->getMockBuilder(QlessQueue::class)
248-
->disableOriginalConstructor()
249-
->getMock();
248+
return $this->createMock(QlessQueue::class);
250249
}
251250

252251
/**
253252
* @return JobHandler|\PHPUnit\Framework\MockObject\MockObject
254253
*/
255254
private function getJobHandler()
256255
{
257-
return $this->getMockBuilder(JobHandler::class)
258-
->disableOriginalConstructor()
259-
->getMock();
256+
return $this->createMock(JobHandler::class);
260257
}
261258

262259
/**
263260
* @return BaseJob|\PHPUnit\Framework\MockObject\MockObject
264261
*/
265262
private function getJob()
266263
{
267-
return $this->getMockBuilder(BaseJob::class)
268-
->disableOriginalConstructor()
269-
->getMock();
264+
return $this->createMock(BaseJob::class);
270265
}
271266

272267
}

tests/ServiceProviderTest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace LaravelQless\Tests;
44

5-
use Illuminate\Container\Container;
65
use Illuminate\Queue\QueueManager;
7-
use Illuminate\Support\Facades\Event;
86
use Illuminate\Support\ServiceProvider;
97
use LaravelQless\LaravelQlessServiceProvider;
108
use LaravelQless\Queue\QlessConnector;
@@ -34,8 +32,6 @@ public function testBoot()
3432
$app = $this->app;
3533
$app['queue'] = $queueMock;
3634

37-
$app['events'] = $this->createMock(Event::class);
38-
3935
$providerMock = new LaravelQlessServiceProvider($app);
4036
$providerMock->boot();
4137
}

0 commit comments

Comments
 (0)