Skip to content

Commit

Permalink
Merge pull request #233 from Riley19280/serializable-closure-bug
Browse files Browse the repository at this point in the history
Fix serialization of non-closures
  • Loading branch information
freekmurze authored Jul 15, 2024
2 parents 7ad6563 + 3dc7759 commit 7b075e9
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 11 deletions.
20 changes: 14 additions & 6 deletions src/Checks/Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
}
21 changes: 17 additions & 4 deletions tests/Checks/QueueCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
});

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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";}}
Original file line number Diff line number Diff line change
@@ -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";}}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ queues:
label: Queue
name: Queue
runConditions:
- true
- { }

0 comments on commit 7b075e9

Please sign in to comment.