diff --git a/tests/Helpers/CollectionWrapper.php b/tests/Helpers/CollectionWrapper.php new file mode 100644 index 0000000..4d5d5e2 --- /dev/null +++ b/tests/Helpers/CollectionWrapper.php @@ -0,0 +1,28 @@ + */ + public Collection $things; + + /** + * @param Collection $things + */ + public function __construct(Collection $things) + { + $this->things = $things; + } + + public function serializes(): array + { + return ['things']; + } +} diff --git a/tests/Helpers/Something.php b/tests/Helpers/Something.php index c29b438..1620aae 100644 --- a/tests/Helpers/Something.php +++ b/tests/Helpers/Something.php @@ -5,6 +5,7 @@ class Something { private string $id; + private bool $processed = false; public function __construct(string $id) { @@ -15,4 +16,16 @@ public function getId(): string { return $this->id; } + + public function isProcessed(): bool + { + return $this->processed; + } + + public function setProcessed(bool $processed): Something + { + $this->processed = $processed; + + return $this; + } } diff --git a/tests/Helpers/SomethingHandler.php b/tests/Helpers/SomethingHandler.php index f49ae69..a1c3769 100644 --- a/tests/Helpers/SomethingHandler.php +++ b/tests/Helpers/SomethingHandler.php @@ -37,6 +37,6 @@ public function serialize(Serializable $serializable, $value): string */ public function deserialize(Serializable $serializable, $value): Something { - return $this->table[$value]; + return $this->table[$value]->setProcessed(true); } } diff --git a/tests/Unit/Laravel/SerializerTest.php b/tests/Unit/Laravel/SerializerTest.php index d583d0b..2cf487d 100644 --- a/tests/Unit/Laravel/SerializerTest.php +++ b/tests/Unit/Laravel/SerializerTest.php @@ -4,9 +4,13 @@ use Illuminate\Database\Capsule\Manager as Capsule; use Illuminate\Database\Eloquent\Model; +use Tests\Helpers\CollectionWrapper; +use Tests\Helpers\Something; +use Tests\Helpers\SomethingHandler; use YouCanShop\Cereal\Contracts\Serializable; use YouCanShop\Cereal\Laravel\Cereal; use YouCanShop\Cereal\Laravel\SerializationHandlerFactory as LaravelSerializationHandlerFactory; +use YouCanShop\Cereal\SerializationHandlerFactory; use YouCanShop\Cereal\SerializationHandlerFactory as BaseSerializationHandlerFactory; it('serializes eloquent models', function () { @@ -81,3 +85,27 @@ public function serializes(): array expect($deserializedEmail->user->id)->toBe($user->id); expect($deserializedEmail->user->full_name)->toBe($user->full_name); }); + +it('serializes collections', function () { + $table = [ + 'one' => new Something('one'), + 'two' => new Something('two'), + ]; + + SerializationHandlerFactory::getInstance() + ->addHandler( + Something::class, + new SomethingHandler($table) + ); + + $wrapper = new CollectionWrapper(collect($table)); + + $s = serialize($wrapper); + + /** @var CollectionWrapper $u */ + $u = unserialize($s); + + expect($u->things) + ->every(fn(Something $t) => $t->isProcessed()) + ->toBeTrue(); +});