From c75c736bb25dcc2c9897876e182864295bfa3e51 Mon Sep 17 00:00:00 2001 From: Keith Brink Date: Thu, 25 Aug 2022 10:24:40 -0400 Subject: [PATCH 1/6] Fix serialization of carbon objects --- composer.json | 1 + src/Serializers/Native.php | 6 ++++++ tests/SerializerTest.php | 13 +++++++++++++ 3 files changed, 20 insertions(+) diff --git a/composer.json b/composer.json index 215c9793..7142366b 100644 --- a/composer.json +++ b/composer.json @@ -21,6 +21,7 @@ "php": "^7.3|^8.0" }, "require-dev": { + "nesbot/carbon": "^2.61", "pestphp/pest": "^1.21.3", "phpstan/phpstan": "^1.8.2", "symfony/var-dumper": "^5.4.11" diff --git a/src/Serializers/Native.php b/src/Serializers/Native.php index c04ead03..e6a235a7 100644 --- a/src/Serializers/Native.php +++ b/src/Serializers/Native.php @@ -460,6 +460,12 @@ protected function mapByReference(&$data) $instance = $data; + if ($data instanceof \DateTime) { + $this->scope[$instance] = $data; + + return; + } + if ($data instanceof UnitEnum) { $this->scope[$instance] = $data; diff --git a/tests/SerializerTest.php b/tests/SerializerTest.php index 5b85fdcf..026a5150 100644 --- a/tests/SerializerTest.php +++ b/tests/SerializerTest.php @@ -401,6 +401,19 @@ function () { expect($f(new Model))->toBeInstanceOf(Model::class); })->with('serializers'); +test('serializes carbon objects', function () { + $carbon = new \Carbon\Carbon('now'); + + $closure = function () use ($carbon){ + return $carbon; + }; + + $u = s($closure); + $r = $u(); + + expect($r)->toEqual($carbon); +})->with('serializers'); + class A { protected static function aStaticProtected() From a482f11a6e1598a7f555a8e5229653e7e80fa0ff Mon Sep 17 00:00:00 2001 From: Keith Brink Date: Thu, 25 Aug 2022 10:29:48 -0400 Subject: [PATCH 2/6] Style fixes --- src/Serializers/Native.php | 2 +- tests/SerializerTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Serializers/Native.php b/src/Serializers/Native.php index e6a235a7..bd856d75 100644 --- a/src/Serializers/Native.php +++ b/src/Serializers/Native.php @@ -462,7 +462,7 @@ protected function mapByReference(&$data) if ($data instanceof \DateTime) { $this->scope[$instance] = $data; - + return; } diff --git a/tests/SerializerTest.php b/tests/SerializerTest.php index 026a5150..e8bcb211 100644 --- a/tests/SerializerTest.php +++ b/tests/SerializerTest.php @@ -404,7 +404,7 @@ function () { test('serializes carbon objects', function () { $carbon = new \Carbon\Carbon('now'); - $closure = function () use ($carbon){ + $closure = function () use ($carbon) { return $carbon; }; From 5b8e2f26c395626b904b5bccc51f3f4ea152c6a8 Mon Sep 17 00:00:00 2001 From: Keith Brink Date: Thu, 25 Aug 2022 10:36:25 -0400 Subject: [PATCH 3/6] DateTime -> DateTimeInterface --- src/Serializers/Native.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Serializers/Native.php b/src/Serializers/Native.php index bd856d75..01304164 100644 --- a/src/Serializers/Native.php +++ b/src/Serializers/Native.php @@ -9,6 +9,7 @@ use Laravel\SerializableClosure\Support\ClosureStream; use Laravel\SerializableClosure\Support\ReflectionClosure; use Laravel\SerializableClosure\Support\SelfReference; +use DateTimeInterface; use ReflectionObject; use UnitEnum; @@ -460,7 +461,7 @@ protected function mapByReference(&$data) $instance = $data; - if ($data instanceof \DateTime) { + if ($data instanceof DateTimeInterface) { $this->scope[$instance] = $data; return; From 212163700fd38d6f3fed9a506db965a9897906f3 Mon Sep 17 00:00:00 2001 From: Keith Brink Date: Thu, 25 Aug 2022 10:37:48 -0400 Subject: [PATCH 4/6] Style fixes --- src/Serializers/Native.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Serializers/Native.php b/src/Serializers/Native.php index 01304164..0ff8410c 100644 --- a/src/Serializers/Native.php +++ b/src/Serializers/Native.php @@ -3,13 +3,13 @@ namespace Laravel\SerializableClosure\Serializers; use Closure; +use DateTimeInterface; use Laravel\SerializableClosure\Contracts\Serializable; use Laravel\SerializableClosure\SerializableClosure; use Laravel\SerializableClosure\Support\ClosureScope; use Laravel\SerializableClosure\Support\ClosureStream; use Laravel\SerializableClosure\Support\ReflectionClosure; use Laravel\SerializableClosure\Support\SelfReference; -use DateTimeInterface; use ReflectionObject; use UnitEnum; From 15f4e3dcc0f266df639053268941db3139debccb Mon Sep 17 00:00:00 2001 From: Keith Brink Date: Thu, 25 Aug 2022 11:55:49 -0400 Subject: [PATCH 5/6] Add carbon immutable test --- tests/SerializerTest.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/SerializerTest.php b/tests/SerializerTest.php index e8bcb211..4e657991 100644 --- a/tests/SerializerTest.php +++ b/tests/SerializerTest.php @@ -414,6 +414,19 @@ function () { expect($r)->toEqual($carbon); })->with('serializers'); +test('serializes carbon immutable objects', function () { + $carbon = new \Carbon\CarbonImmutable('now'); + + $closure = function () use ($carbon) { + return $carbon; + }; + + $u = s($closure); + $r = $u(); + + expect($r)->toEqual($carbon); +})->with('serializers'); + class A { protected static function aStaticProtected() From 2d266930ef94741f7e7dbb2e97d961fe22b8d9d6 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Fri, 26 Aug 2022 10:58:33 +0100 Subject: [PATCH 6/6] Adds more tests --- tests/SerializerTest.php | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/tests/SerializerTest.php b/tests/SerializerTest.php index 4e657991..49445cf3 100644 --- a/tests/SerializerTest.php +++ b/tests/SerializerTest.php @@ -1,5 +1,7 @@ toBeInstanceOf(Model::class); })->with('serializers'); -test('serializes carbon objects', function () { - $carbon = new \Carbon\Carbon('now'); - - $closure = function () use ($carbon) { - return $carbon; +test('serializes used dates', function ($_, $date) { + $closure = function () use ($date) { + return $date; }; $u = s($closure); $r = $u(); - expect($r)->toEqual($carbon); -})->with('serializers'); + expect($r)->toEqual($date); +})->with('serializers')->with([ + new DateTime, + new DateTimeImmutable, + new Carbon, + new CarbonImmutable, +]); -test('serializes carbon immutable objects', function () { - $carbon = new \Carbon\CarbonImmutable('now'); +test('serializes with used object date properties', function ($_, $date) { + $obj = new ObjSelf; + $obj->o = $date; - $closure = function () use ($carbon) { - return $carbon; + $closure = function () use ($obj) { + return $obj; }; $u = s($closure); $r = $u(); - expect($r)->toEqual($carbon); -})->with('serializers'); + expect($r->o)->toEqual($date); +})->with('serializers')->with([ + new DateTime, + new DateTimeImmutable, + new Carbon, + new CarbonImmutable, +]); class A {