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.
test: add tests for json_encode exception trace with args
- Loading branch information
1 parent
fee5f63
commit df084e3
Showing
2 changed files
with
52 additions
and
0 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
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(); | ||
} | ||
}); |
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,11 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelData\Tests\Fakes; | ||
|
||
class NotExtendedDataClass | ||
{ | ||
public function __construct( | ||
public string $name | ||
) { | ||
} | ||
} |