Skip to content

Commit

Permalink
Merge pull request #35 from michalsn/fix/cs
Browse files Browse the repository at this point in the history
fix: phpstan issues
  • Loading branch information
michalsn committed Dec 29, 2023
2 parents 3a4c5d3 + 5aa8b3b commit d15dc13
Show file tree
Hide file tree
Showing 15 changed files with 62 additions and 47 deletions.
2 changes: 0 additions & 2 deletions phpstan.neon.dist
Expand Up @@ -15,8 +15,6 @@ parameters:
paths:
- src/Handlers/RedisHandler.php
- src/Handlers/PredisHandler.php
-
message: '#Access to an undefined property CodeIgniter\\I18n\\Time::\$timestamp.#'
-
message: '#Call to an undefined method CodeIgniter\\Queue\\Models\\QueueJobFailedModel::affectedRows\(\).#'
paths:
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/Generators/JobGenerator.php
Expand Up @@ -45,7 +45,7 @@ class JobGenerator extends BaseCommand
/**
* The Command's Arguments
*
* @var array
* @var array<string, string>
*/
protected $arguments = [
'name' => 'The job class name.',
Expand All @@ -54,7 +54,7 @@ class JobGenerator extends BaseCommand
/**
* The Command's Options
*
* @var array
* @var array<string, string>
*/
protected $options = [
'--namespace' => 'Set root namespace. Default: "APP_NAMESPACE".',
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/QueueClear.php
Expand Up @@ -40,7 +40,7 @@ class QueueClear extends BaseCommand
/**
* The Command's Arguments
*
* @var array
* @var array<string, string>
*/
protected $arguments = [
'queueName' => 'Name of the queue we will work with.',
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/QueueFailed.php
Expand Up @@ -41,7 +41,7 @@ class QueueFailed extends BaseCommand
/**
* The Command's Options
*
* @var array
* @var array<string, string>
*/
protected $options = [
'-queue' => 'Queue name.',
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/QueueFlush.php
Expand Up @@ -40,7 +40,7 @@ class QueueFlush extends BaseCommand
/**
* The Command's Options
*
* @var array
* @var array<string, string>
*/
protected $options = [
'-hours' => 'Number of hours.',
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/QueueForget.php
Expand Up @@ -40,7 +40,7 @@ class QueueForget extends BaseCommand
/**
* The Command's Arguments
*
* @var array
* @var array<string, string>
*/
protected $arguments = [
'id' => 'ID of the failed job.',
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/QueueRetry.php
Expand Up @@ -40,7 +40,7 @@ class QueueRetry extends BaseCommand
/**
* The Command's Arguments
*
* @var array
* @var array<string, string>
*/
protected $arguments = [
'id' => 'ID of the failed job or "all" for all failed jobs.',
Expand All @@ -49,7 +49,7 @@ class QueueRetry extends BaseCommand
/**
* The Command's Options
*
* @var array
* @var array<string, string>
*/
protected $options = [
'-queue' => 'Queue name.',
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/QueueStop.php
Expand Up @@ -40,7 +40,7 @@ class QueueStop extends BaseCommand
/**
* The Command's Arguments
*
* @var array
* @var array<string, string>
*/
protected $arguments = [
'queueName' => 'Name of the queue we will work with.',
Expand All @@ -49,7 +49,7 @@ class QueueStop extends BaseCommand
/**
* The Command's Options
*
* @var array
* @var array<string, string>
*/
protected $options = [
];
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/QueueWork.php
Expand Up @@ -44,7 +44,7 @@ class QueueWork extends BaseCommand
/**
* The Command's Arguments
*
* @var array
* @var array<string, string>
*/
protected $arguments = [
'queueName' => 'Name of the queue we will work with.',
Expand All @@ -53,7 +53,7 @@ class QueueWork extends BaseCommand
/**
* The Command's Options
*
* @var array
* @var array<string, string>
*/
protected $options = [
'-sleep' => 'Wait time between the next check for available job when the queue is empty. Default value: 10 (seconds).',
Expand Down
4 changes: 2 additions & 2 deletions src/Handlers/DatabaseHandler.php
Expand Up @@ -39,7 +39,7 @@ public function push(string $queue, string $job, array $data): bool
'priority' => $this->priority,
'status' => Status::PENDING->value,
'attempts' => 0,
'available_at' => Time::now()->timestamp,
'available_at' => Time::now(),
]);

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

return $this->jobModel->save($queueJob);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Handlers/PredisHandler.php
Expand Up @@ -45,7 +45,7 @@ public function push(string $queue, string $job, array $data): bool
'priority' => $this->priority,
'status' => Status::PENDING->value,
'attempts' => 0,
'available_at' => Time::now()->timestamp,
'available_at' => Time::now(),
]);

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

$result = $this->predis->zadd(
"queues:{$queueJob->queue}:{$queueJob->priority}",
Expand Down
6 changes: 3 additions & 3 deletions src/Handlers/RedisHandler.php
Expand Up @@ -62,7 +62,7 @@ public function push(string $queue, string $job, array $data): bool
'priority' => $this->priority,
'status' => Status::PENDING->value,
'attempts' => 0,
'available_at' => Time::now()->timestamp,
'available_at' => Time::now(),
]);

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

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

$result = (int) $this->redis->zAdd(
"queues:{$queueJob->queue}:{$queueJob->priority}",
Expand Down
43 changes: 30 additions & 13 deletions tests/DatabaseHandlerTest.php
Expand Up @@ -4,6 +4,7 @@

namespace Tests;

use CodeIgniter\I18n\Time;
use CodeIgniter\Queue\Entities\QueueJob;
use CodeIgniter\Queue\Enums\Status;
use CodeIgniter\Queue\Exceptions\QueueException;
Expand Down Expand Up @@ -70,13 +71,16 @@ public function testPriorityNameLengthException(): void
*/
public function testPush(): void
{
Time::setTestNow('2023-12-29 14:15:16');

$handler = new DatabaseHandler($this->config);
$result = $handler->push('queue', 'success', ['key' => 'value']);

$this->assertTrue($result);
$this->seeInDatabase('queue_jobs', [
'queue' => 'queue',
'payload' => json_encode(['job' => 'success', 'data' => ['key' => 'value']]),
'queue' => 'queue',
'payload' => json_encode(['job' => 'success', 'data' => ['key' => 'value']]),
'available_at' => '1703859316',
]);
}

Expand All @@ -85,36 +89,43 @@ public function testPush(): void
*/
public function testPushWithPriority(): void
{
Time::setTestNow('2023-12-29 14:15:16');

$handler = new DatabaseHandler($this->config);
$result = $handler->setPriority('high')->push('queue', 'success', ['key' => 'value']);

$this->assertTrue($result);
$this->seeInDatabase('queue_jobs', [
'queue' => 'queue',
'payload' => json_encode(['job' => 'success', 'data' => ['key' => 'value']]),
'priority' => 'high',
'queue' => 'queue',
'payload' => json_encode(['job' => 'success', 'data' => ['key' => 'value']]),
'priority' => 'high',
'available_at' => '1703859316',
]);
}

public function testPushAndPopWithPriority(): void
{
Time::setTestNow('2023-12-29 14:15:16');

$handler = new DatabaseHandler($this->config);
$result = $handler->push('queue', 'success', ['key1' => 'value1']);

$this->assertTrue($result);
$this->seeInDatabase('queue_jobs', [
'queue' => 'queue',
'payload' => json_encode(['job' => 'success', 'data' => ['key1' => 'value1']]),
'priority' => 'low',
'queue' => 'queue',
'payload' => json_encode(['job' => 'success', 'data' => ['key1' => 'value1']]),
'priority' => 'low',
'available_at' => '1703859316',
]);

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

$this->assertTrue($result);
$this->seeInDatabase('queue_jobs', [
'queue' => 'queue',
'payload' => json_encode(['job' => 'success', 'data' => ['key2' => 'value2']]),
'priority' => 'high',
'queue' => 'queue',
'payload' => json_encode(['job' => 'success', 'data' => ['key2' => 'value2']]),
'priority' => 'high',
'available_at' => '1703859316',
]);

$result = $handler->pop('queue', ['high', 'low']);
Expand Down Expand Up @@ -207,6 +218,8 @@ public function testPopEmpty(): void
*/
public function testLater(): void
{
Time::setTestNow('2023-12-29 14:15:16');

$handler = new DatabaseHandler($this->config);
$queueJob = $handler->pop('queue1', ['default']);

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

$this->assertTrue($result);
$this->seeInDatabase('queue_jobs', [
'id' => 2,
'status' => Status::PENDING->value,
'id' => 2,
'status' => Status::PENDING->value,
'available_at' => Time::now()->addSeconds(60)->timestamp,
]);
}

Expand All @@ -229,6 +243,8 @@ public function testLater(): void
*/
public function testFailedAndKeepJob(): void
{
Time::setTestNow('2023-12-29 14:15:16');

$handler = new DatabaseHandler($this->config);
$queueJob = $handler->pop('queue1', ['default']);

Expand All @@ -243,6 +259,7 @@ public function testFailedAndKeepJob(): void
'id' => 2,
'connection' => 'database',
'queue' => 'queue1',
'failed_at' => '1703859316',
]);
}

Expand Down
13 changes: 0 additions & 13 deletions tests/_support/CLITestCase.php
Expand Up @@ -5,27 +5,14 @@
namespace Tests\Support;

use CodeIgniter\CLI\CLI;
use CodeIgniter\I18n\Time;
use CodeIgniter\Test\ReflectionHelper;
use Exception;

abstract class CLITestCase extends TestCase
{
use ReflectionHelper;

private array $lines = [];

/**
* @throws Exception
*/
protected function tearDown(): void
{
parent::tearDown();

// Reset the current time.
Time::setTestNow();
}

protected function parseOutput(string $output): string
{
$this->lines = [];
Expand Down
13 changes: 13 additions & 0 deletions tests/_support/TestCase.php
Expand Up @@ -4,8 +4,10 @@

namespace Tests\Support;

use CodeIgniter\I18n\Time;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use Exception;

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

parent::setUp();
}

/**
* @throws Exception
*/
protected function tearDown(): void
{
parent::tearDown();

// Reset the current time.
Time::setTestNow();
}
}

0 comments on commit d15dc13

Please sign in to comment.