From df084e34e66ece47acb89af30d82e0295f5f09d0 Mon Sep 17 00:00:00 2001 From: Vladyslav Martynenko Date: Tue, 29 Oct 2024 19:43:50 +0200 Subject: [PATCH 1/2] test: add tests for json_encode exception trace with args --- ...ionTraceArgumentsJsonSerializationTest.php | 41 +++++++++++++++++++ tests/Fakes/NotExtendedDataClass.php | 11 +++++ 2 files changed, 52 insertions(+) create mode 100644 tests/ExceptionTraceArgumentsJsonSerializationTest.php create mode 100644 tests/Fakes/NotExtendedDataClass.php diff --git a/tests/ExceptionTraceArgumentsJsonSerializationTest.php b/tests/ExceptionTraceArgumentsJsonSerializationTest.php new file mode 100644 index 00000000..4e4093a0 --- /dev/null +++ b/tests/ExceptionTraceArgumentsJsonSerializationTest.php @@ -0,0 +1,41 @@ +getTrace(); + $encodedJson = json_encode($trace, JSON_THROW_ON_ERROR); + + expect($encodedJson)->toBeJson(); + } +}); + +it('can json_encode exception trace with args when nested property not extends Data or Dto class', function () { + $dataClass = new class ('', new NotExtendedDataClass('')) extends Data { + public function __construct( + public string $string, + public NotExtendedDataClass $nested + ) { + } + }; + + try { + $dataClass::from(['string' => '', 'nested' => ['name' => '']]); + } catch (TypeError $e) { + $trace = $e->getTrace(); + $encodedJson = json_encode($trace, JSON_THROW_ON_ERROR); + + expect($encodedJson)->toBeJson(); + } +}); diff --git a/tests/Fakes/NotExtendedDataClass.php b/tests/Fakes/NotExtendedDataClass.php new file mode 100644 index 00000000..9c9dbecb --- /dev/null +++ b/tests/Fakes/NotExtendedDataClass.php @@ -0,0 +1,11 @@ + Date: Tue, 29 Oct 2024 19:48:28 +0200 Subject: [PATCH 2/2] feature: make enums backed for correct json serialization --- src/Enums/CustomCreationMethodType.php | 8 ++++---- src/Enums/DataCollectableType.php | 12 +++++------ src/Enums/DataTypeKind.php | 28 +++++++++++++------------- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/Enums/CustomCreationMethodType.php b/src/Enums/CustomCreationMethodType.php index 74af7544..ceacb8f1 100644 --- a/src/Enums/CustomCreationMethodType.php +++ b/src/Enums/CustomCreationMethodType.php @@ -2,9 +2,9 @@ namespace Spatie\LaravelData\Enums; -enum CustomCreationMethodType +enum CustomCreationMethodType: string { - case None; - case Object; - case Collection; + case None = 'None'; + case Object = 'Object'; + case Collection = 'Collection'; } diff --git a/src/Enums/DataCollectableType.php b/src/Enums/DataCollectableType.php index 6e25bc99..d1415e9e 100644 --- a/src/Enums/DataCollectableType.php +++ b/src/Enums/DataCollectableType.php @@ -2,11 +2,11 @@ namespace Spatie\LaravelData\Enums; -enum DataCollectableType +enum DataCollectableType: string { - case Default; - case Array; - case Collection; - case Paginated; - case CursorPaginated; + case Default = 'Default'; + case Array = 'Array'; + case Collection = 'Collection'; + case Paginated = 'Paginated'; + case CursorPaginated = 'CursorPaginated'; } diff --git a/src/Enums/DataTypeKind.php b/src/Enums/DataTypeKind.php index 0c1e3c44..cb5f072f 100644 --- a/src/Enums/DataTypeKind.php +++ b/src/Enums/DataTypeKind.php @@ -4,21 +4,21 @@ use Exception; -enum DataTypeKind +enum DataTypeKind: string { - case Default; - case Array; - case Enumerable; - case Paginator; - case CursorPaginator; - case DataObject; - case DataCollection; - case DataPaginatedCollection; - case DataCursorPaginatedCollection; - case DataArray; - case DataEnumerable; - case DataPaginator; - case DataCursorPaginator; + case Default = 'Default'; + case Array = 'Array'; + case Enumerable = 'Enumerable'; + case Paginator = 'Paginator'; + case CursorPaginator = 'CursorPaginator'; + case DataObject = 'DataObject'; + case DataCollection = 'DataCollection'; + case DataPaginatedCollection = 'DataPaginatedCollection'; + case DataCursorPaginatedCollection = 'DataCursorPaginatedCollection'; + case DataArray = 'DataArray'; + case DataEnumerable = 'DataEnumerable'; + case DataPaginator = 'DataPaginator'; + case DataCursorPaginator = 'DataCursorPaginator'; public function isDataObject(): bool {