generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #735 from spatie/data-serialization
Add better support for serializing data
- Loading branch information
Showing
13 changed files
with
183 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
|
||
use Spatie\LaravelData\DataCollection; | ||
use Spatie\LaravelData\Lazy; | ||
use Spatie\LaravelData\Support\Lazy\DefaultLazy; | ||
use Spatie\LaravelData\Tests\Fakes\LazyData; | ||
use Spatie\LaravelData\Tests\Fakes\SimpleData; | ||
|
||
use function Spatie\Snapshots\assertMatchesSnapshot; | ||
|
||
it('can serialize and unserialize a data object', function () { | ||
$object = SimpleData::from('Hello world'); | ||
|
||
$serialized = serialize($object); | ||
|
||
assertMatchesSnapshot($serialized); | ||
|
||
$unserialized = unserialize($serialized); | ||
|
||
expect($unserialized)->toBeInstanceOf(SimpleData::class); | ||
expect($unserialized->string)->toEqual('Hello world'); | ||
}); | ||
|
||
it('can serialize and unserialize a data object with additional data', function () { | ||
$object = SimpleData::from('Hello world')->additional([ | ||
'int' => 69, | ||
]); | ||
|
||
$serialized = serialize($object); | ||
|
||
assertMatchesSnapshot($serialized); | ||
|
||
$unserialized = unserialize($serialized); | ||
|
||
expect($unserialized)->toBeInstanceOf(SimpleData::class); | ||
expect($unserialized->string)->toEqual('Hello world'); | ||
expect($unserialized->getAdditionalData())->toEqual(['int' => 69]); | ||
}); | ||
|
||
it('can serialize and unserialize a data collection', function () { | ||
$collection = new DataCollection(SimpleData::class, ['A', 'B']); | ||
|
||
$serialized = serialize($collection); | ||
|
||
assertMatchesSnapshot($serialized); | ||
|
||
$unserialized = unserialize($serialized); | ||
|
||
expect($unserialized)->toBeInstanceOf(DataCollection::class); | ||
expect($unserialized)->toEqual(new DataCollection(SimpleData::class, ['A', 'B'])); | ||
}); | ||
|
||
it('will keep context attached to data when serialized', function () { | ||
$object = LazyData::from('Hello world')->include('name'); | ||
|
||
$unserialized = unserialize(serialize($object)); | ||
|
||
expect($unserialized)->toBeInstanceOf(LazyData::class); | ||
expect($unserialized->toArray())->toMatchArray(['name' => 'Hello world']); | ||
}); | ||
|
||
it('is possible to add partials with closures and serialize them', function () { | ||
$object = LazyData::from('Hello world')->includeWhen( | ||
'name', | ||
fn (LazyData $data) => $data->name instanceof DefaultLazy | ||
); | ||
|
||
$unserialized = unserialize(serialize($object)); | ||
|
||
expect($unserialized)->toBeInstanceOf(LazyData::class); | ||
expect($unserialized->toArray())->toMatchArray(['name' => 'Hello world']); | ||
}); | ||
|
||
it('is possible to serialize conditional lazy properties', function () { | ||
$object = new LazyData(Lazy::when( | ||
fn () => true, | ||
fn () => 'Hello world' | ||
)); | ||
|
||
$unserialized = unserialize(serialize($object)); | ||
|
||
expect($unserialized)->toBeInstanceOf(LazyData::class); | ||
expect($unserialized->toArray())->toMatchArray(['name' => 'Hello world']); | ||
}); |
Binary file added
BIN
+489 Bytes
..._snapshots__/SerializeableTest__it_can_serialize_and_unserialize_a_data_collection__1.txt
Binary file not shown.
Binary file added
BIN
+138 Bytes
tests/__snapshots__/SerializeableTest__it_can_serialize_and_unserialize_a_data_object__1.txt
Binary file not shown.
Binary file added
BIN
+153 Bytes
...alizeableTest__it_can_serialize_and_unserialize_a_data_object_with_additional_data__1.txt
Binary file not shown.