Skip to content

Commit cbc7d71

Browse files
committed
update tests for SQLSRV
1 parent eacc271 commit cbc7d71

File tree

3 files changed

+96
-40
lines changed

3 files changed

+96
-40
lines changed

tests/DatabaseHandlerTest.php

+29-40
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace Tests;
1515

16-
use Closure;
1716
use CodeIgniter\I18n\Time;
1817
use CodeIgniter\Queue\Entities\QueueJob;
1918
use CodeIgniter\Queue\Enums\Status;
@@ -36,22 +35,12 @@ final class DatabaseHandlerTest extends TestCase
3635

3736
protected $seed = TestDatabaseQueueSeeder::class;
3837
private QueueConfig $config;
39-
private Closure $field;
4038

4139
protected function setUp(): void
4240
{
4341
parent::setUp();
4442

4543
$this->config = config(QueueConfig::class);
46-
47-
// handle filed custom type conversion for SQLSRV
48-
$this->field = function ($field) {
49-
if ($this->db->DBDriver === 'SQLSRV') {
50-
return "CONVERT(VARCHAR, {$field})";
51-
}
52-
53-
return $field;
54-
};
5544
}
5645

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

9988
$this->assertTrue($result);
100-
$this->seeInDatabase('queue_jobs', [
101-
'queue' => 'queue',
102-
($this->field)('payload') => json_encode(['job' => 'success', 'data' => ['key' => 'value']]),
103-
'available_at' => '1703859316',
89+
$this->seeInDatabaseExtended('queue_jobs', [
90+
'queue' => 'queue',
91+
$this->field('payload') => json_encode(['job' => 'success', 'data' => ['key' => 'value']]),
92+
'available_at' => '1703859316',
10493
]);
10594
}
10695

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

117106
$this->assertTrue($result);
118-
$this->seeInDatabase('queue_jobs', [
119-
'queue' => 'queue',
120-
($this->field)('payload') => json_encode(['job' => 'success', 'data' => ['key' => 'value']]),
121-
'priority' => 'high',
122-
'available_at' => '1703859316',
107+
$this->seeInDatabaseExtended('queue_jobs', [
108+
'queue' => 'queue',
109+
$this->field('payload') => json_encode(['job' => 'success', 'data' => ['key' => 'value']]),
110+
'priority' => 'high',
111+
'available_at' => '1703859316',
123112
]);
124113
}
125114

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

133122
$this->assertTrue($result);
134-
$this->seeInDatabase('queue_jobs', [
135-
'queue' => 'queue',
136-
($this->field)('payload') => json_encode(['job' => 'success', 'data' => ['key1' => 'value1']]),
137-
'priority' => 'low',
138-
'available_at' => '1703859316',
123+
$this->seeInDatabaseExtended('queue_jobs', [
124+
'queue' => 'queue',
125+
$this->field('payload') => json_encode(['job' => 'success', 'data' => ['key1' => 'value1']]),
126+
'priority' => 'low',
127+
'available_at' => '1703859316',
139128
]);
140129

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

143132
$this->assertTrue($result);
144-
$this->seeInDatabase('queue_jobs', [
145-
'queue' => 'queue',
146-
($this->field)('payload') => json_encode(['job' => 'success', 'data' => ['key2' => 'value2']]),
147-
'priority' => 'high',
148-
'available_at' => '1703859316',
133+
$this->seeInDatabaseExtended('queue_jobs', [
134+
'queue' => 'queue',
135+
$this->field('payload') => json_encode(['job' => 'success', 'data' => ['key2' => 'value2']]),
136+
'priority' => 'high',
137+
'available_at' => '1703859316',
149138
]);
150139

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

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

246-
$this->seeInDatabase('queue_jobs', [
235+
$this->seeInDatabaseExtended('queue_jobs', [
247236
'id' => 2,
248237
'status' => Status::RESERVED->value,
249238
]);
250239

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

253242
$this->assertTrue($result);
254-
$this->seeInDatabase('queue_jobs', [
243+
$this->seeInDatabaseExtended('queue_jobs', [
255244
'id' => 2,
256245
'status' => Status::PENDING->value,
257246
'available_at' => Time::now()->addSeconds(60)->timestamp,
@@ -275,7 +264,7 @@ public function testFailedAndKeepJob(): void
275264
$this->dontSeeInDatabase('queue_jobs', [
276265
'id' => 2,
277266
]);
278-
$this->seeInDatabase('queue_jobs_failed', [
267+
$this->seeInDatabaseExtended('queue_jobs_failed', [
279268
'id' => 2,
280269
'connection' => 'database',
281270
'queue' => 'queue1',
@@ -313,7 +302,7 @@ public function testDoneAndKeepJob(): void
313302
$result = $handler->done($queueJob, true);
314303

315304
$this->assertTrue($result);
316-
$this->seeInDatabase('queue_jobs', [
305+
$this->seeInDatabaseExtended('queue_jobs', [
317306
'id' => 2,
318307
'status' => Status::DONE->value,
319308
]);
@@ -357,10 +346,10 @@ public function testRetry(): void
357346

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

360-
$this->seeInDatabase('queue_jobs', [
361-
'id' => 3,
362-
'queue' => 'queue1',
363-
($this->field)('payload') => json_encode(['job' => 'failure', 'data' => []]),
349+
$this->seeInDatabaseExtended('queue_jobs', [
350+
'id' => 3,
351+
'queue' => 'queue1',
352+
$this->field('payload') => json_encode(['job' => 'failure', 'data' => []]),
364353
]);
365354
$this->dontSeeInDatabase('queue_jobs_failed', [
366355
'id' => 1,
@@ -407,7 +396,7 @@ public function testFlush(): void
407396
$this->dontSeeInDatabase('queue_jobs_failed', [
408397
'id' => 1,
409398
]);
410-
$this->seeInDatabase('queue_jobs_failed', [
399+
$this->seeInDatabaseExtended('queue_jobs_failed', [
411400
'id' => 2,
412401
]);
413402
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter Queue.
7+
*
8+
* (c) CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Tests\Support\Constraints;
15+
16+
use CodeIgniter\Test\Constraints\SeeInDatabase;
17+
18+
class SeeInDatabaseExtended extends SeeInDatabase
19+
{
20+
/**
21+
* Gets a string representation of the constraint
22+
*
23+
* @param int $options
24+
*/
25+
public function toString(bool $exportObjects = false, $options = 0): string
26+
{
27+
$this->data = array_combine(
28+
array_map(fn ($key) => $this->extractFieldName($key), array_keys($this->data)),
29+
$this->data
30+
);
31+
32+
return parent::toString($exportObjects, $options);
33+
}
34+
35+
/**
36+
* Extract field name from complex key
37+
*/
38+
protected function extractFieldName(string $input): string
39+
{
40+
$pattern = '/CONVERT\(\s*\w+,\s*(\w+)\s*\)/';
41+
42+
if (preg_match($pattern, $input, $matches)) {
43+
return $matches[1];
44+
}
45+
46+
return $input;
47+
}
48+
}

tests/_support/TestCase.php

+19
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use CodeIgniter\Test\CIUnitTestCase;
1818
use CodeIgniter\Test\DatabaseTestTrait;
1919
use Exception;
20+
use Tests\Support\Constraints\SeeInDatabaseExtended;
2021

2122
abstract class TestCase extends CIUnitTestCase
2223
{
@@ -41,4 +42,22 @@ protected function tearDown(): void
4142
// Reset the current time.
4243
Time::setTestNow();
4344
}
45+
46+
public function seeInDatabaseExtended(string $table, array $where): void
47+
{
48+
$constraint = new SeeInDatabaseExtended($this->db, $where);
49+
$this->assertThat($table, $constraint);
50+
}
51+
52+
/**
53+
* Handle custom field type conversion for SQLSRV
54+
*/
55+
public function field(string $name): string
56+
{
57+
if ($this->db->DBDriver === 'SQLSRV') {
58+
return "CONVERT(VARCHAR, {$name})";
59+
}
60+
61+
return $name;
62+
}
4463
}

0 commit comments

Comments
 (0)