From 5aca920a05a2e79f4dc99e089acc92e365cb1437 Mon Sep 17 00:00:00 2001 From: eivydas Date: Tue, 18 Oct 2022 12:25:28 +0300 Subject: [PATCH] Throw exception on invalid job content --- composer.json | 1 + src/Queue/AbstractQueue.php | 2 +- tests/src/Queue/QueueTest.php | 12 ++++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index df4d07f..6b11543 100644 --- a/composer.json +++ b/composer.json @@ -33,6 +33,7 @@ ], "require": { "php": "~7.4.0 || ~8.0.0 || ~8.1.0", + "ext-json": "*", "laminas/laminas-eventmanager": "^3.4", "laminas/laminas-servicemanager": "^3.11", "laminas/laminas-stdlib": "^3.7.1", diff --git a/src/Queue/AbstractQueue.php b/src/Queue/AbstractQueue.php index 2bda028..26d2d4d 100644 --- a/src/Queue/AbstractQueue.php +++ b/src/Queue/AbstractQueue.php @@ -90,6 +90,6 @@ public function serializeJob(JobInterface $job): string $data['content'] = base64_encode($data['content']); } - return json_encode($data); + return json_encode($data, JSON_THROW_ON_ERROR); } } diff --git a/tests/src/Queue/QueueTest.php b/tests/src/Queue/QueueTest.php index 09a58ea..7ddcda7 100644 --- a/tests/src/Queue/QueueTest.php +++ b/tests/src/Queue/QueueTest.php @@ -2,6 +2,7 @@ namespace SlmQueueTest\Queue; +use JsonException; use Laminas\ServiceManager\ServiceManager; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -133,6 +134,17 @@ public function testCorrectlySerializeJobServiceName(): void static::assertEquals($expected, $actual); } + public function testInvalidSerializeJobContent(): void + { + $job = new SimpleJob(); + $job->setMetadata('__name__', 'SimpleJob'); + $job->setContent(chr(128)); + + $this->expectException(JsonException::class); + + $this->queue->serializeJob($job); + } + public function testCanCreateJobWithFQCN(): void { $this->jobPluginManager->expects($this->once())