Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

DataTransferObjectError::invalidType : get actual type before mutating $value for the error message #81

Merged
merged 2 commits into from
Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/DataTransferObjectError.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public static function invalidType(
array $expectedTypes,
$value
): DataTransferObjectError {
$currentType = gettype($value);

if ($value === null) {
$value = 'null';
}
Expand All @@ -35,8 +37,6 @@ public static function invalidType(

$expectedTypes = implode(', ', $expectedTypes);

$currentType = gettype($value);

return new self("Invalid type: expected `{$class}::{$field}` to be of type `{$expectedTypes}`, instead got value `{$value}`, which is {$currentType}.");
}

Expand Down
9 changes: 9 additions & 0 deletions tests/DataTransferObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function only_the_type_hinted_type_may_be_passed()
$this->markTestSucceeded();

$this->expectException(DataTransferObjectError::class);
$this->expectExceptionMessageRegExp('/Invalid type: expected `class@anonymous[^:]+::foo` to be of type `string`, instead got value ``, which is boolean/');
brendt marked this conversation as resolved.
Show resolved Hide resolved

new class(['foo' => false]) extends DataTransferObject {
/** @var string */
Expand Down Expand Up @@ -79,6 +80,7 @@ public function default_values_are_supported()
public function null_is_allowed_only_if_explicitly_specified()
{
$this->expectException(DataTransferObjectError::class);
$this->expectExceptionMessageRegExp('/Invalid type: expected `class@anonymous[^:]+::foo` to be of type `string`, instead got value `null`, which is NULL/');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before this PR, this message ended in:

instead got value null, which is string

and now

instead got value null, which is NULL


new class(['foo' => null]) extends DataTransferObject {
/** @var string */
Expand All @@ -90,6 +92,7 @@ public function null_is_allowed_only_if_explicitly_specified()
public function unknown_properties_throw_an_error()
{
$this->expectException(DataTransferObjectError::class);
$this->expectExceptionMessageRegExp('/Public properties `bar` not found on class@anonymous/');

new class(['bar' => null]) extends DataTransferObject {
};
Expand Down Expand Up @@ -187,6 +190,7 @@ public function classes_are_supported()
$this->markTestSucceeded();

$this->expectException(DataTransferObjectError::class);
$this->expectExceptionMessageRegExp('/Invalid type: expected `class@anonymous[^:]+::foo` to be of type `\\\Spatie\\\DataTransferObject\\\Tests\\\TestClasses\\\DummyClass`, instead got value `class@anonymous[^`]+`, which is object/');

new class(['foo' => new class() {
},
Expand All @@ -208,6 +212,7 @@ public function generic_collections_are_supported()
$this->markTestSucceeded();

$this->expectException(DataTransferObjectError::class);
$this->expectExceptionMessageRegExp('/Invalid type: expected `class@anonymous[^:]+::foo` to be of type `\\\Spatie\\\DataTransferObject\\\Tests\\\TestClasses\\\DummyClass\[\]`, instead got value `array`, which is array/');

new class(['foo' => [new OtherClass()]]) extends DataTransferObject {
/** @var \Spatie\DataTransferObject\Tests\TestClasses\DummyClass[] */
Expand All @@ -219,6 +224,7 @@ public function generic_collections_are_supported()
public function an_exception_is_thrown_for_a_generic_collection_of_null()
{
$this->expectException(DataTransferObjectError::class);
$this->expectExceptionMessageRegExp('/Invalid type: expected `class@anonymous[^:]+::foo` to be of type `string\[\]`, instead got value `array`, which is array./');

new class(['foo' => [null]]) extends DataTransferObject {
/** @var string[] */
Expand All @@ -230,6 +236,7 @@ public function an_exception_is_thrown_for_a_generic_collection_of_null()
public function an_exception_is_thrown_when_property_was_not_initialised()
{
$this->expectException(DataTransferObjectError::class);
$this->expectExceptionMessageRegExp('/Invalid type: expected `class@anonymous[^:]+::foo` to be of type `string`, instead got value `null`, which is NULL/');

new class([]) extends DataTransferObject {
/** @var string */
Expand Down Expand Up @@ -343,6 +350,7 @@ public function nested_array_dtos_are_recursive_cast_to_arrays_of_dtos()
public function nested_array_dtos_cannot_cast_with_null()
{
$this->expectException(DataTransferObjectError::class);
$this->expectExceptionMessage('Invalid type: expected `Spatie\DataTransferObject\Tests\TestClasses\NestedParentOfMany::children` to be of type `\Spatie\DataTransferObject\Tests\TestClasses\NestedChild[]`, instead got value `null`, which is NULL.');

new NestedParentOfMany([
'name' => 'parent',
Expand Down Expand Up @@ -415,6 +423,7 @@ public function iterable_is_supported()
public function an_exception_is_thrown_for_incoherent_iterator_type()
{
$this->expectException(DataTransferObjectError::class);
$this->expectExceptionMessageRegExp('/Invalid type: expected `class@anonymous[^:]+::strings` to be of type `iterable<string>`, instead got value `array`, which is array./');

new class(['strings' => ['foo', 1]]) extends DataTransferObject {
/** @var iterable<string> */
Expand Down
1 change: 1 addition & 0 deletions tests/ImmutableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function immutable_values_cannot_be_overwritten()
$this->assertEquals(1, $dto->testProperty);

$this->expectException(DataTransferObjectError::class);
$this->expectExceptionMessage('Cannot change the value of property testProperty on an immutable data transfer object');

$dto->testProperty = 2;
}
Expand Down