Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix serialization of non-closures #233

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
- { }
Loading