From 3dc77590bdb8e7d62d705c6cfcc4dbd3799d84f8 Mon Sep 17 00:00:00 2001 From: Riley Aven Date: Thu, 4 Jul 2024 14:52:45 -0400 Subject: [PATCH] Fix serialization of non-closures --- src/Checks/Check.php | 20 ++++++++++++------ tests/Checks/QueueCheckTest.php | 21 +++++++++++++++---- ...ueueCheckTest__it_can_be_serialized__1.txt | 1 - ...heckTest__it_can_serialize_closures__1.txt | 1 + ...Test__it_can_serialize_non_closures__1.txt | 1 + ...QueueCheckTest__it_can_unserialize__1.yml} | 1 + 6 files changed, 34 insertions(+), 11 deletions(-) delete mode 100644 tests/__snapshots__/QueueCheckTest__it_can_be_serialized__1.txt create mode 100644 tests/__snapshots__/QueueCheckTest__it_can_serialize_closures__1.txt create mode 100644 tests/__snapshots__/QueueCheckTest__it_can_serialize_non_closures__1.txt rename tests/__snapshots__/{QueueCheckTest__it_can_be_unserialized__1.yml => QueueCheckTest__it_can_unserialize__1.yml} (89%) diff --git a/src/Checks/Check.php b/src/Checks/Check.php index e818e897..2ce18076 100755 --- a/src/Checks/Check.php +++ b/src/Checks/Check.php @@ -126,12 +126,16 @@ public function __serialize(): array { $vars = get_object_vars($this); - $serializableClosures = []; + $serializedShouldRun = []; foreach ($vars['shouldRun'] as $shouldRun) { - $serializableClosures[] = new SerializableClosure($shouldRun); + if ($shouldRun instanceof \Closure) { + $serializedShouldRun[] = new SerializableClosure($shouldRun); + } else { + $serializedShouldRun[] = $shouldRun; + } } - $vars['shouldRun'] = $serializableClosures; + $vars['shouldRun'] = $serializedShouldRun; return $vars; } @@ -142,12 +146,16 @@ public function __unserialize(array $data): void $this->$property = $value; } - $unwrappedClosures = []; + $deserializedShouldRun = []; foreach ($this->shouldRun as $shouldRun) { - $unwrappedClosures[] = $shouldRun->getClosure(); + if ($shouldRun instanceof SerializableClosure) { + $deserializedShouldRun[] = $shouldRun->getClosure(); + } else { + $deserializedShouldRun[] = $shouldRun; + } } - $this->shouldRun = $unwrappedClosures; + $this->shouldRun = $deserializedShouldRun; } } diff --git a/tests/Checks/QueueCheckTest.php b/tests/Checks/QueueCheckTest.php index 0bc09ecb..808a8ad9 100644 --- a/tests/Checks/QueueCheckTest.php +++ b/tests/Checks/QueueCheckTest.php @@ -10,6 +10,7 @@ use function Pest\Laravel\artisan; use function PHPUnit\Framework\assertCount; use function PHPUnit\Framework\assertInstanceOf; +use function PHPUnit\Framework\assertIsBool; use function Spatie\PestPluginTestTime\testTime; use function Spatie\Snapshots\assertMatchesObjectSnapshot; use function Spatie\Snapshots\assertMatchesSnapshot; @@ -114,7 +115,7 @@ expect($this->queueCheck->getQueues())->toBe([$queueName]); }); -it('can be serialized', function () { +it('can serialize closures', function () { $check = QueueCheck::new() ->onQueue('sync') ->if(fn () => false); @@ -126,15 +127,27 @@ assertMatchesSnapshot($result); }); -it('can be unserialized', function () { +it('can serialize non closures', function () { $check = QueueCheck::new() ->onQueue('sync') + ->if(true); + + $result = serialize($check); + + assertMatchesSnapshot($result); +}); + +it('can unserialize', function () { + $check = QueueCheck::new() + ->onQueue('sync') + ->if(true) ->if(fn () => false); $result = unserialize(serialize($check)); - assertCount(1, $result->getRunConditions()); - assertInstanceOf(Closure::class, $result->getRunConditions()[0]); + assertCount(2, $result->getRunConditions()); + assertIsBool($result->getRunConditions()[0]); + assertInstanceOf(Closure::class, $result->getRunConditions()[1]); assertMatchesObjectSnapshot($result); }); diff --git a/tests/__snapshots__/QueueCheckTest__it_can_be_serialized__1.txt b/tests/__snapshots__/QueueCheckTest__it_can_be_serialized__1.txt deleted file mode 100644 index fdf635ae..00000000 --- a/tests/__snapshots__/QueueCheckTest__it_can_be_serialized__1.txt +++ /dev/null @@ -1 +0,0 @@ -O:38:"Spatie\Health\Checks\Checks\QueueCheck":8:{s:10:"expression";s:9:"* * * * *";s:4:"name";N;s:5:"label";N;s:9:"shouldRun";a:1:{i:0;O:47:"Laravel\SerializableClosure\SerializableClosure":1:{s:12:"serializable";O:46:"Laravel\SerializableClosure\Serializers\Native":5:{s:3:"use";a:0:{}s:8:"function";s:13:"fn() => false";s:5:"scope";s:29:"P\Tests\Checks\QueueCheckTest";s:4:"this";N;s:4:"self";s:32:"0000000000000000000000000000000000000000";}}}s:8:"cacheKey";s:37:"health:checks:queue:latestHeartbeatAt";s:14:"cacheStoreName";N;s:37:"failWhenTestJobTakesLongerThanMinutes";i:5;s:8:"onQueues";a:1:{i:0;s:4:"sync";}} \ No newline at end of file diff --git a/tests/__snapshots__/QueueCheckTest__it_can_serialize_closures__1.txt b/tests/__snapshots__/QueueCheckTest__it_can_serialize_closures__1.txt new file mode 100644 index 00000000..1fb2977b --- /dev/null +++ b/tests/__snapshots__/QueueCheckTest__it_can_serialize_closures__1.txt @@ -0,0 +1 @@ +O:38:"Spatie\Health\Checks\Checks\QueueCheck":8:{s:10:"expression";s:9:"* * * * *";s:4:"name";N;s:5:"label";N;s:9:"shouldRun";a:1:{i:0;O:47:"Laravel\SerializableClosure\SerializableClosure":1:{s:12:"serializable";O:46:"Laravel\SerializableClosure\Serializers\Native":5:{s:3:"use";a:0:{}s:8:"function";s:14:"fn () => false";s:5:"scope";s:29:"P\Tests\Checks\QueueCheckTest";s:4:"this";N;s:4:"self";s:32:"0000000000000000000000000000000000000000";}}}s:8:"cacheKey";s:37:"health:checks:queue:latestHeartbeatAt";s:14:"cacheStoreName";N;s:37:"failWhenTestJobTakesLongerThanMinutes";i:5;s:8:"onQueues";a:1:{i:0;s:4:"sync";}} \ No newline at end of file diff --git a/tests/__snapshots__/QueueCheckTest__it_can_serialize_non_closures__1.txt b/tests/__snapshots__/QueueCheckTest__it_can_serialize_non_closures__1.txt new file mode 100644 index 00000000..bceb4aac --- /dev/null +++ b/tests/__snapshots__/QueueCheckTest__it_can_serialize_non_closures__1.txt @@ -0,0 +1 @@ +O:38:"Spatie\Health\Checks\Checks\QueueCheck":8:{s:10:"expression";s:9:"* * * * *";s:4:"name";N;s:5:"label";N;s:9:"shouldRun";a:1:{i:0;b:1;}s:8:"cacheKey";s:37:"health:checks:queue:latestHeartbeatAt";s:14:"cacheStoreName";N;s:37:"failWhenTestJobTakesLongerThanMinutes";i:5;s:8:"onQueues";a:1:{i:0;s:4:"sync";}} \ No newline at end of file diff --git a/tests/__snapshots__/QueueCheckTest__it_can_be_unserialized__1.yml b/tests/__snapshots__/QueueCheckTest__it_can_unserialize__1.yml similarity index 89% rename from tests/__snapshots__/QueueCheckTest__it_can_be_unserialized__1.yml rename to tests/__snapshots__/QueueCheckTest__it_can_unserialize__1.yml index 949b6eea..c5e8aec8 100644 --- a/tests/__snapshots__/QueueCheckTest__it_can_be_unserialized__1.yml +++ b/tests/__snapshots__/QueueCheckTest__it_can_unserialize__1.yml @@ -4,4 +4,5 @@ queues: label: Queue name: Queue runConditions: + - true - { }