From cbc7d71443232e077a325cba5753ef58fa141570 Mon Sep 17 00:00:00 2001 From: michalsn Date: Thu, 19 Sep 2024 14:50:58 +0200 Subject: [PATCH] update tests for SQLSRV --- tests/DatabaseHandlerTest.php | 69 ++++++++----------- .../Constraints/SeeInDatabaseExtended.php | 48 +++++++++++++ tests/_support/TestCase.php | 19 +++++ 3 files changed, 96 insertions(+), 40 deletions(-) create mode 100644 tests/_support/Constraints/SeeInDatabaseExtended.php diff --git a/tests/DatabaseHandlerTest.php b/tests/DatabaseHandlerTest.php index 51cf4ba..40cb4c6 100644 --- a/tests/DatabaseHandlerTest.php +++ b/tests/DatabaseHandlerTest.php @@ -13,7 +13,6 @@ namespace Tests; -use Closure; use CodeIgniter\I18n\Time; use CodeIgniter\Queue\Entities\QueueJob; use CodeIgniter\Queue\Enums\Status; @@ -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 @@ -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', ]); } @@ -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', ]); } @@ -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']); @@ -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, ]); @@ -243,7 +232,7 @@ 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, ]); @@ -251,7 +240,7 @@ public function testLater(): void $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, @@ -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', @@ -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, ]); @@ -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, @@ -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, ]); } diff --git a/tests/_support/Constraints/SeeInDatabaseExtended.php b/tests/_support/Constraints/SeeInDatabaseExtended.php new file mode 100644 index 0000000..96397da --- /dev/null +++ b/tests/_support/Constraints/SeeInDatabaseExtended.php @@ -0,0 +1,48 @@ + + * + * 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; + } +} diff --git a/tests/_support/TestCase.php b/tests/_support/TestCase.php index 0b956db..2718d9d 100644 --- a/tests/_support/TestCase.php +++ b/tests/_support/TestCase.php @@ -17,6 +17,7 @@ use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\DatabaseTestTrait; use Exception; +use Tests\Support\Constraints\SeeInDatabaseExtended; abstract class TestCase extends CIUnitTestCase { @@ -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; + } }