Skip to content

Commit 5aa8b3b

Browse files
committed
fix phpstan issues and add more tests
1 parent 746d6e5 commit 5aa8b3b

15 files changed

+62
-47
lines changed

phpstan.neon.dist

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ parameters:
1515
paths:
1616
- src/Handlers/RedisHandler.php
1717
- src/Handlers/PredisHandler.php
18-
-
19-
message: '#Access to an undefined property CodeIgniter\\I18n\\Time::\$timestamp.#'
2018
-
2119
message: '#Call to an undefined method CodeIgniter\\Queue\\Models\\QueueJobFailedModel::affectedRows\(\).#'
2220
paths:

src/Commands/Generators/JobGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class JobGenerator extends BaseCommand
4545
/**
4646
* The Command's Arguments
4747
*
48-
* @var array
48+
* @var array<string, string>
4949
*/
5050
protected $arguments = [
5151
'name' => 'The job class name.',
@@ -54,7 +54,7 @@ class JobGenerator extends BaseCommand
5454
/**
5555
* The Command's Options
5656
*
57-
* @var array
57+
* @var array<string, string>
5858
*/
5959
protected $options = [
6060
'--namespace' => 'Set root namespace. Default: "APP_NAMESPACE".',

src/Commands/QueueClear.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class QueueClear extends BaseCommand
4040
/**
4141
* The Command's Arguments
4242
*
43-
* @var array
43+
* @var array<string, string>
4444
*/
4545
protected $arguments = [
4646
'queueName' => 'Name of the queue we will work with.',

src/Commands/QueueFailed.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class QueueFailed extends BaseCommand
4141
/**
4242
* The Command's Options
4343
*
44-
* @var array
44+
* @var array<string, string>
4545
*/
4646
protected $options = [
4747
'-queue' => 'Queue name.',

src/Commands/QueueFlush.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class QueueFlush extends BaseCommand
4040
/**
4141
* The Command's Options
4242
*
43-
* @var array
43+
* @var array<string, string>
4444
*/
4545
protected $options = [
4646
'-hours' => 'Number of hours.',

src/Commands/QueueForget.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class QueueForget extends BaseCommand
4040
/**
4141
* The Command's Arguments
4242
*
43-
* @var array
43+
* @var array<string, string>
4444
*/
4545
protected $arguments = [
4646
'id' => 'ID of the failed job.',

src/Commands/QueueRetry.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class QueueRetry extends BaseCommand
4040
/**
4141
* The Command's Arguments
4242
*
43-
* @var array
43+
* @var array<string, string>
4444
*/
4545
protected $arguments = [
4646
'id' => 'ID of the failed job or "all" for all failed jobs.',
@@ -49,7 +49,7 @@ class QueueRetry extends BaseCommand
4949
/**
5050
* The Command's Options
5151
*
52-
* @var array
52+
* @var array<string, string>
5353
*/
5454
protected $options = [
5555
'-queue' => 'Queue name.',

src/Commands/QueueStop.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class QueueStop extends BaseCommand
4040
/**
4141
* The Command's Arguments
4242
*
43-
* @var array
43+
* @var array<string, string>
4444
*/
4545
protected $arguments = [
4646
'queueName' => 'Name of the queue we will work with.',
@@ -49,7 +49,7 @@ class QueueStop extends BaseCommand
4949
/**
5050
* The Command's Options
5151
*
52-
* @var array
52+
* @var array<string, string>
5353
*/
5454
protected $options = [
5555
];

src/Commands/QueueWork.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class QueueWork extends BaseCommand
4444
/**
4545
* The Command's Arguments
4646
*
47-
* @var array
47+
* @var array<string, string>
4848
*/
4949
protected $arguments = [
5050
'queueName' => 'Name of the queue we will work with.',
@@ -53,7 +53,7 @@ class QueueWork extends BaseCommand
5353
/**
5454
* The Command's Options
5555
*
56-
* @var array
56+
* @var array<string, string>
5757
*/
5858
protected $options = [
5959
'-sleep' => 'Wait time between the next check for available job when the queue is empty. Default value: 10 (seconds).',

src/Handlers/DatabaseHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function push(string $queue, string $job, array $data): bool
3939
'priority' => $this->priority,
4040
'status' => Status::PENDING->value,
4141
'attempts' => 0,
42-
'available_at' => Time::now()->timestamp,
42+
'available_at' => Time::now(),
4343
]);
4444

4545
$this->priority = null;
@@ -75,7 +75,7 @@ public function pop(string $queue, array $priorities): ?QueueJob
7575
public function later(QueueJob $queueJob, int $seconds): bool
7676
{
7777
$queueJob->status = Status::PENDING->value;
78-
$queueJob->available_at = Time::now()->addSeconds($seconds)->timestamp;
78+
$queueJob->available_at = Time::now()->addSeconds($seconds);
7979

8080
return $this->jobModel->save($queueJob);
8181
}

src/Handlers/PredisHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function push(string $queue, string $job, array $data): bool
4545
'priority' => $this->priority,
4646
'status' => Status::PENDING->value,
4747
'attempts' => 0,
48-
'available_at' => Time::now()->timestamp,
48+
'available_at' => Time::now(),
4949
]);
5050

5151
$result = $this->predis->zadd("queues:{$queue}:{$this->priority}", [json_encode($queueJob) => Time::now()->timestamp]);
@@ -100,7 +100,7 @@ public function pop(string $queue, array $priorities): ?QueueJob
100100
public function later(QueueJob $queueJob, int $seconds): bool
101101
{
102102
$queueJob->status = Status::PENDING->value;
103-
$queueJob->available_at = Time::now()->addSeconds($seconds)->timestamp;
103+
$queueJob->available_at = Time::now()->addSeconds($seconds);
104104

105105
$result = $this->predis->zadd(
106106
"queues:{$queueJob->queue}:{$queueJob->priority}",

src/Handlers/RedisHandler.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function push(string $queue, string $job, array $data): bool
6262
'priority' => $this->priority,
6363
'status' => Status::PENDING->value,
6464
'attempts' => 0,
65-
'available_at' => Time::now()->timestamp,
65+
'available_at' => Time::now(),
6666
]);
6767

6868
$result = (int) $this->redis->zAdd("queues:{$queue}:{$this->priority}", Time::now()->timestamp, json_encode($queueJob));
@@ -83,7 +83,7 @@ public function pop(string $queue, array $priorities): ?QueueJob
8383
$now = Time::now()->timestamp;
8484

8585
foreach ($priorities as $priority) {
86-
if ($tasks = $this->redis->zRangeByScore("queues:{$queue}:{$priority}", '-inf', $now, ['limit' => [0, 1]])) {
86+
if ($tasks = $this->redis->zRangeByScore("queues:{$queue}:{$priority}", '-inf', (string) $now, ['limit' => [0, 1]])) {
8787
if ($this->redis->zRem("queues:{$queue}:{$priority}", ...$tasks)) {
8888
break;
8989
}
@@ -114,7 +114,7 @@ public function pop(string $queue, array $priorities): ?QueueJob
114114
public function later(QueueJob $queueJob, int $seconds): bool
115115
{
116116
$queueJob->status = Status::PENDING->value;
117-
$queueJob->available_at = Time::now()->addSeconds($seconds)->timestamp;
117+
$queueJob->available_at = Time::now()->addSeconds($seconds);
118118

119119
$result = (int) $this->redis->zAdd(
120120
"queues:{$queueJob->queue}:{$queueJob->priority}",

tests/DatabaseHandlerTest.php

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Tests;
66

7+
use CodeIgniter\I18n\Time;
78
use CodeIgniter\Queue\Entities\QueueJob;
89
use CodeIgniter\Queue\Enums\Status;
910
use CodeIgniter\Queue\Exceptions\QueueException;
@@ -70,13 +71,16 @@ public function testPriorityNameLengthException(): void
7071
*/
7172
public function testPush(): void
7273
{
74+
Time::setTestNow('2023-12-29 14:15:16');
75+
7376
$handler = new DatabaseHandler($this->config);
7477
$result = $handler->push('queue', 'success', ['key' => 'value']);
7578

7679
$this->assertTrue($result);
7780
$this->seeInDatabase('queue_jobs', [
78-
'queue' => 'queue',
79-
'payload' => json_encode(['job' => 'success', 'data' => ['key' => 'value']]),
81+
'queue' => 'queue',
82+
'payload' => json_encode(['job' => 'success', 'data' => ['key' => 'value']]),
83+
'available_at' => '1703859316',
8084
]);
8185
}
8286

@@ -85,36 +89,43 @@ public function testPush(): void
8589
*/
8690
public function testPushWithPriority(): void
8791
{
92+
Time::setTestNow('2023-12-29 14:15:16');
93+
8894
$handler = new DatabaseHandler($this->config);
8995
$result = $handler->setPriority('high')->push('queue', 'success', ['key' => 'value']);
9096

9197
$this->assertTrue($result);
9298
$this->seeInDatabase('queue_jobs', [
93-
'queue' => 'queue',
94-
'payload' => json_encode(['job' => 'success', 'data' => ['key' => 'value']]),
95-
'priority' => 'high',
99+
'queue' => 'queue',
100+
'payload' => json_encode(['job' => 'success', 'data' => ['key' => 'value']]),
101+
'priority' => 'high',
102+
'available_at' => '1703859316',
96103
]);
97104
}
98105

99106
public function testPushAndPopWithPriority(): void
100107
{
108+
Time::setTestNow('2023-12-29 14:15:16');
109+
101110
$handler = new DatabaseHandler($this->config);
102111
$result = $handler->push('queue', 'success', ['key1' => 'value1']);
103112

104113
$this->assertTrue($result);
105114
$this->seeInDatabase('queue_jobs', [
106-
'queue' => 'queue',
107-
'payload' => json_encode(['job' => 'success', 'data' => ['key1' => 'value1']]),
108-
'priority' => 'low',
115+
'queue' => 'queue',
116+
'payload' => json_encode(['job' => 'success', 'data' => ['key1' => 'value1']]),
117+
'priority' => 'low',
118+
'available_at' => '1703859316',
109119
]);
110120

111121
$result = $handler->setPriority('high')->push('queue', 'success', ['key2' => 'value2']);
112122

113123
$this->assertTrue($result);
114124
$this->seeInDatabase('queue_jobs', [
115-
'queue' => 'queue',
116-
'payload' => json_encode(['job' => 'success', 'data' => ['key2' => 'value2']]),
117-
'priority' => 'high',
125+
'queue' => 'queue',
126+
'payload' => json_encode(['job' => 'success', 'data' => ['key2' => 'value2']]),
127+
'priority' => 'high',
128+
'available_at' => '1703859316',
118129
]);
119130

120131
$result = $handler->pop('queue', ['high', 'low']);
@@ -207,6 +218,8 @@ public function testPopEmpty(): void
207218
*/
208219
public function testLater(): void
209220
{
221+
Time::setTestNow('2023-12-29 14:15:16');
222+
210223
$handler = new DatabaseHandler($this->config);
211224
$queueJob = $handler->pop('queue1', ['default']);
212225

@@ -219,8 +232,9 @@ public function testLater(): void
219232

220233
$this->assertTrue($result);
221234
$this->seeInDatabase('queue_jobs', [
222-
'id' => 2,
223-
'status' => Status::PENDING->value,
235+
'id' => 2,
236+
'status' => Status::PENDING->value,
237+
'available_at' => Time::now()->addSeconds(60)->timestamp,
224238
]);
225239
}
226240

@@ -229,6 +243,8 @@ public function testLater(): void
229243
*/
230244
public function testFailedAndKeepJob(): void
231245
{
246+
Time::setTestNow('2023-12-29 14:15:16');
247+
232248
$handler = new DatabaseHandler($this->config);
233249
$queueJob = $handler->pop('queue1', ['default']);
234250

@@ -243,6 +259,7 @@ public function testFailedAndKeepJob(): void
243259
'id' => 2,
244260
'connection' => 'database',
245261
'queue' => 'queue1',
262+
'failed_at' => '1703859316',
246263
]);
247264
}
248265

tests/_support/CLITestCase.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,14 @@
55
namespace Tests\Support;
66

77
use CodeIgniter\CLI\CLI;
8-
use CodeIgniter\I18n\Time;
98
use CodeIgniter\Test\ReflectionHelper;
10-
use Exception;
119

1210
abstract class CLITestCase extends TestCase
1311
{
1412
use ReflectionHelper;
1513

1614
private array $lines = [];
1715

18-
/**
19-
* @throws Exception
20-
*/
21-
protected function tearDown(): void
22-
{
23-
parent::tearDown();
24-
25-
// Reset the current time.
26-
Time::setTestNow();
27-
}
28-
2916
protected function parseOutput(string $output): string
3017
{
3118
$this->lines = [];

tests/_support/TestCase.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
namespace Tests\Support;
66

7+
use CodeIgniter\I18n\Time;
78
use CodeIgniter\Test\CIUnitTestCase;
89
use CodeIgniter\Test\DatabaseTestTrait;
10+
use Exception;
911

1012
abstract class TestCase extends CIUnitTestCase
1113
{
@@ -19,4 +21,15 @@ protected function setUp(): void
1921

2022
parent::setUp();
2123
}
24+
25+
/**
26+
* @throws Exception
27+
*/
28+
protected function tearDown(): void
29+
{
30+
parent::tearDown();
31+
32+
// Reset the current time.
33+
Time::setTestNow();
34+
}
2235
}

0 commit comments

Comments
 (0)