Skip to content
Open
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
28 changes: 28 additions & 0 deletions tests/Helpers/CollectionWrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Tests\Helpers;

use Illuminate\Support\Collection;
use YouCanShop\Cereal\Cereal;
use YouCanShop\Cereal\Contracts\Serializable;

class CollectionWrapper implements Serializable
{
use Cereal;

/** @var Collection<array-key, Something> */
public Collection $things;

/**
* @param Collection<array-key, Something> $things
*/
public function __construct(Collection $things)
{
$this->things = $things;
}

public function serializes(): array
{
return ['things'];
}
}
13 changes: 13 additions & 0 deletions tests/Helpers/Something.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
class Something
{
private string $id;
private bool $processed = false;

public function __construct(string $id)
{
Expand All @@ -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;
}
}
2 changes: 1 addition & 1 deletion tests/Helpers/SomethingHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
28 changes: 28 additions & 0 deletions tests/Unit/Laravel/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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();
});