Skip to content

Commit

Permalink
update tests for SQLSRV
Browse files Browse the repository at this point in the history
  • Loading branch information
michalsn committed Sep 19, 2024
1 parent eacc271 commit cbc7d71
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 40 deletions.
69 changes: 29 additions & 40 deletions tests/DatabaseHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace Tests;

use Closure;
use CodeIgniter\I18n\Time;
use CodeIgniter\Queue\Entities\QueueJob;
use CodeIgniter\Queue\Enums\Status;
Expand All @@ -36,22 +35,12 @@ final class DatabaseHandlerTest extends TestCase

protected $seed = TestDatabaseQueueSeeder::class;
private QueueConfig $config;
private Closure $field;

protected function setUp(): void
{
parent::setUp();

$this->config = config(QueueConfig::class);

// handle filed custom type conversion for SQLSRV
$this->field = function ($field) {
if ($this->db->DBDriver === 'SQLSRV') {
return "CONVERT(VARCHAR, {$field})";
}

return $field;
};
}

public function testDatabaseHandler(): void
Expand Down Expand Up @@ -97,10 +86,10 @@ public function testPush(): void
$result = $handler->push('queue', 'success', ['key' => 'value']);

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

Expand All @@ -115,11 +104,11 @@ public function testPushWithPriority(): void
$result = $handler->setPriority('high')->push('queue', 'success', ['key' => 'value']);

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

Expand All @@ -131,21 +120,21 @@ public function testPushAndPopWithPriority(): void
$result = $handler->push('queue', 'success', ['key1' => 'value1']);

$this->assertTrue($result);
$this->seeInDatabase('queue_jobs', [
'queue' => 'queue',
($this->field)('payload') => json_encode(['job' => 'success', 'data' => ['key1' => 'value1']]),
'priority' => 'low',
'available_at' => '1703859316',
$this->seeInDatabaseExtended('queue_jobs', [
'queue' => 'queue',
$this->field('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',
($this->field)('payload') => json_encode(['job' => 'success', 'data' => ['key2' => 'value2']]),
'priority' => 'high',
'available_at' => '1703859316',
$this->seeInDatabaseExtended('queue_jobs', [
'queue' => 'queue',
$this->field('payload') => json_encode(['job' => 'success', 'data' => ['key2' => 'value2']]),
'priority' => 'high',
'available_at' => '1703859316',
]);

$result = $handler->pop('queue', ['high', 'low']);
Expand Down Expand Up @@ -216,7 +205,7 @@ public function testPop(): void
$result = $handler->pop('queue1', ['default']);

$this->assertInstanceOf(QueueJob::class, $result);
$this->seeInDatabase('queue_jobs', [
$this->seeInDatabaseExtended('queue_jobs', [
'status' => Status::RESERVED->value,
'available_at' => 1_697_269_860,
]);
Expand All @@ -243,15 +232,15 @@ public function testLater(): void
$handler = new DatabaseHandler($this->config);
$queueJob = $handler->pop('queue1', ['default']);

$this->seeInDatabase('queue_jobs', [
$this->seeInDatabaseExtended('queue_jobs', [
'id' => 2,
'status' => Status::RESERVED->value,
]);

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

$this->assertTrue($result);
$this->seeInDatabase('queue_jobs', [
$this->seeInDatabaseExtended('queue_jobs', [
'id' => 2,
'status' => Status::PENDING->value,
'available_at' => Time::now()->addSeconds(60)->timestamp,
Expand All @@ -275,7 +264,7 @@ public function testFailedAndKeepJob(): void
$this->dontSeeInDatabase('queue_jobs', [
'id' => 2,
]);
$this->seeInDatabase('queue_jobs_failed', [
$this->seeInDatabaseExtended('queue_jobs_failed', [
'id' => 2,
'connection' => 'database',
'queue' => 'queue1',
Expand Down Expand Up @@ -313,7 +302,7 @@ public function testDoneAndKeepJob(): void
$result = $handler->done($queueJob, true);

$this->assertTrue($result);
$this->seeInDatabase('queue_jobs', [
$this->seeInDatabaseExtended('queue_jobs', [
'id' => 2,
'status' => Status::DONE->value,
]);
Expand Down Expand Up @@ -357,10 +346,10 @@ public function testRetry(): void

$this->assertSame($count, 1);

$this->seeInDatabase('queue_jobs', [
'id' => 3,
'queue' => 'queue1',
($this->field)('payload') => json_encode(['job' => 'failure', 'data' => []]),
$this->seeInDatabaseExtended('queue_jobs', [
'id' => 3,
'queue' => 'queue1',
$this->field('payload') => json_encode(['job' => 'failure', 'data' => []]),
]);
$this->dontSeeInDatabase('queue_jobs_failed', [
'id' => 1,
Expand Down Expand Up @@ -407,7 +396,7 @@ public function testFlush(): void
$this->dontSeeInDatabase('queue_jobs_failed', [
'id' => 1,
]);
$this->seeInDatabase('queue_jobs_failed', [
$this->seeInDatabaseExtended('queue_jobs_failed', [
'id' => 2,
]);
}
Expand Down
48 changes: 48 additions & 0 deletions tests/_support/Constraints/SeeInDatabaseExtended.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter Queue.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace Tests\Support\Constraints;

use CodeIgniter\Test\Constraints\SeeInDatabase;

class SeeInDatabaseExtended extends SeeInDatabase
{
/**
* Gets a string representation of the constraint
*
* @param int $options
*/
public function toString(bool $exportObjects = false, $options = 0): string
{
$this->data = array_combine(
array_map(fn ($key) => $this->extractFieldName($key), array_keys($this->data)),
$this->data
);

return parent::toString($exportObjects, $options);
}

/**
* Extract field name from complex key
*/
protected function extractFieldName(string $input): string
{
$pattern = '/CONVERT\(\s*\w+,\s*(\w+)\s*\)/';

if (preg_match($pattern, $input, $matches)) {
return $matches[1];
}

return $input;
}
}
19 changes: 19 additions & 0 deletions tests/_support/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use Exception;
use Tests\Support\Constraints\SeeInDatabaseExtended;

abstract class TestCase extends CIUnitTestCase
{
Expand All @@ -41,4 +42,22 @@ protected function tearDown(): void
// Reset the current time.
Time::setTestNow();
}

public function seeInDatabaseExtended(string $table, array $where): void
{
$constraint = new SeeInDatabaseExtended($this->db, $where);
$this->assertThat($table, $constraint);
}

/**
* Handle custom field type conversion for SQLSRV
*/
public function field(string $name): string
{
if ($this->db->DBDriver === 'SQLSRV') {
return "CONVERT(VARCHAR, {$name})";
}

return $name;
}
}

0 comments on commit cbc7d71

Please sign in to comment.