Skip to content

Commit

Permalink
Merge pull request #20 from kenjis/fix-type-errors
Browse files Browse the repository at this point in the history
fix: TypeError in QueueJobModel and RedisHandlerTest
  • Loading branch information
michalsn authored Dec 18, 2023
2 parents 3f04fa3 + 913c571 commit 85505f7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
25 changes: 22 additions & 3 deletions src/Models/QueueJobModel.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

declare(strict_types=1);

namespace CodeIgniter\Queue\Models;

use CodeIgniter\Database\BaseBuilder;
use CodeIgniter\Database\RawSql;
use CodeIgniter\I18n\Time;
use CodeIgniter\Model;
use CodeIgniter\Queue\Entities\QueueJob;
Expand Down Expand Up @@ -102,9 +103,27 @@ private function setPriority(BaseBuilder $builder, array $priority): BaseBuilder

if ($priority !== ['default']) {
if ($this->db->DBDriver === 'SQLite3') {
$builder->orderBy(new RawSql('CASE priority ' . implode(' ', array_map(static fn ($value, $key) => "WHEN '{$value}' THEN {$key}", $priority, array_keys($priority))) . ' END'));
$builder->orderBy(
'CASE priority '
. implode(
' ',
array_map(static fn ($value, $key) => "WHEN '{$value}' THEN {$key}", $priority, array_keys($priority))
)
. ' END',
'',
false
);
} else {
$builder->orderBy(new RawSql('FIELD(priority, ' . implode(',', array_map(static fn ($value) => "'{$value}'", $priority)) . ')'));
$builder->orderBy(
'FIELD(priority, '
. implode(
',',
array_map(static fn ($value) => "'{$value}'", $priority)
)
. ')',
'',
false
);
}
}

Expand Down
16 changes: 9 additions & 7 deletions tests/RedisHandlerTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Tests;

use CodeIgniter\I18n\Time;
Expand Down Expand Up @@ -112,7 +114,7 @@ public function testPop()
$redis = self::getPrivateProperty($handler, 'redis');
$this->assertSame(1_234_567_890_654_321, $result->id);
$this->assertSame(0, $redis->zCard('queues:queue1:default'));
$this->assertTrue($redis->hExists('queues:queue1::reserved', $result->id));
$this->assertTrue($redis->hExists('queues:queue1::reserved', (string) $result->id));
}

public function testPopEmpty()
Expand All @@ -129,13 +131,13 @@ public function testLater()
$queueJob = $handler->pop('queue1', ['default']);

$redis = self::getPrivateProperty($handler, 'redis');
$this->assertTrue($redis->hExists('queues:queue1::reserved', $queueJob->id));
$this->assertTrue($redis->hExists('queues:queue1::reserved', (string) $queueJob->id));
$this->assertSame(0, $redis->zCard('queues:queue1:default'));

$result = $handler->later($queueJob, 60);

$this->assertTrue($result);
$this->assertFalse($redis->hExists('queues:queue1::reserved', $queueJob->id));
$this->assertFalse($redis->hExists('queues:queue1::reserved', (string) $queueJob->id));
$this->assertSame(1, $redis->zCard('queues:queue1:default'));
}

Expand All @@ -150,7 +152,7 @@ public function testFailedAndKeepJob()
$redis = self::getPrivateProperty($handler, 'redis');

$this->assertTrue($result);
$this->assertFalse($redis->hExists('queues:queue1::reserved', $queueJob->id));
$this->assertFalse($redis->hExists('queues:queue1::reserved', (string) $queueJob->id));
$this->assertSame(0, $redis->zCard('queues:queue1:default'));

$this->seeInDatabase('queue_jobs_failed', [
Expand All @@ -171,7 +173,7 @@ public function testFailedAndDontKeepJob()
$redis = self::getPrivateProperty($handler, 'redis');

$this->assertTrue($result);
$this->assertFalse($redis->hExists('queues:queue1::reserved', $queueJob->id));
$this->assertFalse($redis->hExists('queues:queue1::reserved', (string) $queueJob->id));
$this->assertSame(0, $redis->zCard('queues:queue1:default'));

$this->dontSeeInDatabase('queue_jobs_failed', [
Expand All @@ -191,7 +193,7 @@ public function testDoneAndKeepJob()
$redis = self::getPrivateProperty($handler, 'redis');

$this->assertTrue($result);
$this->assertFalse($redis->hExists('queues:queue1::reserved', $queueJob->id));
$this->assertFalse($redis->hExists('queues:queue1::reserved', (string) $queueJob->id));
$this->assertSame(1, $redis->lLen('queues:queue1::done'));
}

Expand All @@ -206,7 +208,7 @@ public function testDoneAndDontKeepJob()
$result = $handler->done($queueJob, false);

$this->assertTrue($result);
$this->assertFalse($redis->hExists('queues:queue1::reserved', $queueJob->id));
$this->assertFalse($redis->hExists('queues:queue1::reserved', (string) $queueJob->id));
$this->assertSame(0, $redis->lLen('queues:queue1::done'));
}

Expand Down

0 comments on commit 85505f7

Please sign in to comment.