|
| 1 | +<?php |
| 2 | + |
| 3 | + |
| 4 | +use Spatie\LaravelData\DataCollection; |
| 5 | +use Spatie\LaravelData\Lazy; |
| 6 | +use Spatie\LaravelData\Support\Lazy\DefaultLazy; |
| 7 | +use Spatie\LaravelData\Tests\Fakes\LazyData; |
| 8 | +use Spatie\LaravelData\Tests\Fakes\SimpleData; |
| 9 | + |
| 10 | +use function Spatie\Snapshots\assertMatchesSnapshot; |
| 11 | + |
| 12 | +it('can serialize and unserialize a data object', function () { |
| 13 | + $object = SimpleData::from('Hello world'); |
| 14 | + |
| 15 | + $serialized = serialize($object); |
| 16 | + |
| 17 | + assertMatchesSnapshot($serialized); |
| 18 | + |
| 19 | + $unserialized = unserialize($serialized); |
| 20 | + |
| 21 | + expect($unserialized)->toBeInstanceOf(SimpleData::class); |
| 22 | + expect($unserialized->string)->toEqual('Hello world'); |
| 23 | +}); |
| 24 | + |
| 25 | +it('can serialize and unserialize a data object with additional data', function () { |
| 26 | + $object = SimpleData::from('Hello world')->additional([ |
| 27 | + 'int' => 69, |
| 28 | + ]); |
| 29 | + |
| 30 | + $serialized = serialize($object); |
| 31 | + |
| 32 | + assertMatchesSnapshot($serialized); |
| 33 | + |
| 34 | + $unserialized = unserialize($serialized); |
| 35 | + |
| 36 | + expect($unserialized)->toBeInstanceOf(SimpleData::class); |
| 37 | + expect($unserialized->string)->toEqual('Hello world'); |
| 38 | + expect($unserialized->getAdditionalData())->toEqual(['int' => 69]); |
| 39 | +}); |
| 40 | + |
| 41 | +it('can serialize and unserialize a data collection', function () { |
| 42 | + $collection = new DataCollection(SimpleData::class, ['A', 'B']); |
| 43 | + |
| 44 | + $serialized = serialize($collection); |
| 45 | + |
| 46 | + assertMatchesSnapshot($serialized); |
| 47 | + |
| 48 | + $unserialized = unserialize($serialized); |
| 49 | + |
| 50 | + expect($unserialized)->toBeInstanceOf(DataCollection::class); |
| 51 | + expect($unserialized)->toEqual(new DataCollection(SimpleData::class, ['A', 'B'])); |
| 52 | +}); |
| 53 | + |
| 54 | +it('will keep context attached to data when serialized', function () { |
| 55 | + $object = LazyData::from('Hello world')->include('name'); |
| 56 | + |
| 57 | + $unserialized = unserialize(serialize($object)); |
| 58 | + |
| 59 | + expect($unserialized)->toBeInstanceOf(LazyData::class); |
| 60 | + expect($unserialized->toArray())->toMatchArray(['name' => 'Hello world']); |
| 61 | +}); |
| 62 | + |
| 63 | +it('is possible to add partials with closures and serialize them', function () { |
| 64 | + $object = LazyData::from('Hello world')->includeWhen( |
| 65 | + 'name', |
| 66 | + fn (LazyData $data) => $data->name instanceof DefaultLazy |
| 67 | + ); |
| 68 | + |
| 69 | + $unserialized = unserialize(serialize($object)); |
| 70 | + |
| 71 | + expect($unserialized)->toBeInstanceOf(LazyData::class); |
| 72 | + expect($unserialized->toArray())->toMatchArray(['name' => 'Hello world']); |
| 73 | +}); |
| 74 | + |
| 75 | +it('is possible to serialize conditional lazy properties', function () { |
| 76 | + $object = new LazyData(Lazy::when( |
| 77 | + fn () => true, |
| 78 | + fn () => 'Hello world' |
| 79 | + )); |
| 80 | + |
| 81 | + $unserialized = unserialize(serialize($object)); |
| 82 | + |
| 83 | + expect($unserialized)->toBeInstanceOf(LazyData::class); |
| 84 | + expect($unserialized->toArray())->toMatchArray(['name' => 'Hello world']); |
| 85 | +}); |
0 commit comments