This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #168 from pikant/master
Added tests for automatic casting of nullable nested array DTOs
- Loading branch information
Showing
3 changed files
with
161 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,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spatie\DataTransferObject\Tests; | ||
|
||
use Spatie\DataTransferObject\DataTransferObject; | ||
|
||
class NestedArrayDataTransferObjectTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_can_validate_nested_arrays() | ||
{ | ||
$a = new WrapperDtoPropertyType([ | ||
'arr' => [ | ||
['name' => 'test',], | ||
['name' => 'test2',] | ||
], | ||
]); | ||
|
||
$this->assertEquals('test', $a->arr[0]->name); | ||
$this->assertEquals('test2', $a->arr[1]->name); | ||
$this->assertEquals( | ||
NestedArrayDtoPropertyType::class, | ||
get_class($a->arr[0]) | ||
); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_handle_empty_arrays() | ||
{ | ||
$a = new WrapperDtoPropertyType([ | ||
'arr' => [], | ||
]); | ||
|
||
$this->assertIsArray($a->arr); | ||
$this->assertEmpty($a->arr); | ||
} | ||
} | ||
|
||
class WrapperDtoPropertyType extends DataTransferObject | ||
{ | ||
/** @var \Spatie\DataTransferObject\Tests\NestedArrayDtoPropertyType[] */ | ||
public array $arr; | ||
} | ||
|
||
class NestedArrayDtoPropertyType extends DataTransferObject | ||
{ | ||
public string $name; | ||
} |
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,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spatie\DataTransferObject\Tests; | ||
|
||
use Spatie\DataTransferObject\DataTransferObject; | ||
|
||
class NestedNullableArrayDataTransferObjectTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_can_validate_nested_arrays() | ||
{ | ||
$a = new WrapperWithNullableArrayDtoPropertyType([ | ||
'arr' => [ | ||
['name' => 'test',], | ||
['name' => 'test2',] | ||
], | ||
]); | ||
|
||
$this->assertEquals('test', $a->arr[0]->name); | ||
$this->assertEquals('test2', $a->arr[1]->name); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_handle_empty_arrays() | ||
{ | ||
$a = new WrapperWithNullableArrayDtoPropertyType([ | ||
'arr' => [], | ||
]); | ||
|
||
$this->assertIsArray($a->arr); | ||
$this->assertEmpty($a->arr); | ||
} | ||
|
||
/** @test */ | ||
public function it_fails_on_null() | ||
{ | ||
$a = new WrapperWithNullableArrayDtoPropertyType([ | ||
'arr' => null, | ||
]); | ||
|
||
$this->assertNull($a->arr); | ||
} | ||
} | ||
|
||
class WrapperWithNullableArrayDtoPropertyType extends DataTransferObject | ||
{ | ||
/** @var \Spatie\DataTransferObject\Tests\NestedArrayDtoPropertyType[]|null */ | ||
public ?array $arr; | ||
} |
60 changes: 60 additions & 0 deletions
60
tests/NestedNullableArrayFlexibleDataTransferObjectTest.php
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,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spatie\DataTransferObject\Tests; | ||
|
||
use Spatie\DataTransferObject\DataTransferObject; | ||
use Spatie\DataTransferObject\FlexibleDataTransferObject; | ||
|
||
class NestedNullableArrayFlexibleDataTransferObjectTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_can_validate_nested_arrays() | ||
{ | ||
$a = new FlexibleWrapperWithNullableArrayDtoPropertyType([ | ||
'arr' => [ | ||
['name' => 'test',], | ||
['name' => 'test2',] | ||
], | ||
]); | ||
|
||
$this->assertEquals('test', $a->arr[0]->name); | ||
$this->assertEquals('test2', $a->arr[1]->name); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_handle_empty_arrays() | ||
{ | ||
$a = new FlexibleWrapperWithNullableArrayDtoPropertyType([ | ||
'arr' => [], | ||
]); | ||
|
||
$this->assertIsArray($a->arr); | ||
$this->assertEmpty($a->arr); | ||
} | ||
|
||
/** @test */ | ||
public function it_fails_on_null() | ||
{ | ||
$a = new FlexibleWrapperWithNullableArrayDtoPropertyType([ | ||
'arr' => null, | ||
]); | ||
|
||
$this->assertNull($a->arr); | ||
} | ||
|
||
/** @test */ | ||
public function it_fails_on_missing() | ||
{ | ||
$a = new FlexibleWrapperWithNullableArrayDtoPropertyType([]); | ||
|
||
$this->assertNull($a->arr); | ||
} | ||
} | ||
|
||
class FlexibleWrapperWithNullableArrayDtoPropertyType extends FlexibleDataTransferObject | ||
{ | ||
/** @var \Spatie\DataTransferObject\Tests\NestedArrayDtoPropertyType[]|null */ | ||
public ?array $arr; | ||
} |