Skip to content

Commit

Permalink
test: add tests for json_encode exception trace with args
Browse files Browse the repository at this point in the history
  • Loading branch information
dont-know-php committed Oct 29, 2024
1 parent fee5f63 commit df084e3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
41 changes: 41 additions & 0 deletions tests/ExceptionTraceArgumentsJsonSerializationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

use Spatie\LaravelData\Data;
use Spatie\LaravelData\Exceptions\CannotCreateData;
use Spatie\LaravelData\Tests\Fakes\NotExtendedDataClass;
use Spatie\LaravelData\Tests\Fakes\SimpleData;

// enable args in exception trace
beforeEach(function () {
ini_set('zend.exception_ignore_args', 'Off');
});

it('can json_encode exception trace with args when not all required properties passed', function () {
try {
SimpleData::from([]);
} catch (CannotCreateData $e) {
$trace = $e->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();
}
});
11 changes: 11 additions & 0 deletions tests/Fakes/NotExtendedDataClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Spatie\LaravelData\Tests\Fakes;

class NotExtendedDataClass
{
public function __construct(
public string $name
) {
}
}

0 comments on commit df084e3

Please sign in to comment.