-
Notifications
You must be signed in to change notification settings - Fork 7
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 #77 from pemudakoding/main
Refactor DTO using cuyz/valinor to better implementation
- Loading branch information
Showing
7 changed files
with
214 additions
and
15 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
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
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
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,77 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Arr; | ||
use Tests\Unit\DataTransferObjects\Fixtures\GenderEnum; | ||
use Tests\Unit\DataTransferObjects\Fixtures\RoleData; | ||
use Tests\Unit\DataTransferObjects\Fixtures\UserData; | ||
|
||
it(description: 'can resolve From Array') | ||
->tap(callable: function () { | ||
$data = UserData::resolveFromArray(data: [ | ||
'name' => 'Stiven Katuuk', | ||
'roles' => null, | ||
]); | ||
|
||
expect(value: $data->name)->toBe(expected: 'Stiven Katuuk'); | ||
}) | ||
->group('unit', 'dto'); | ||
|
||
it(description: 'can map to sub dto as object') | ||
->tap(callable: function () { | ||
$data = UserData::resolveFromArray(data: [ | ||
'name' => 'Stiven Katuuk', | ||
'main_role' => ['name' => 'Moderator'], | ||
]); | ||
|
||
expect(value: $data->mainRole)->toBeInstanceOf(class: RoleData::class); | ||
}) | ||
->group('unit', 'dto'); | ||
|
||
it(description: 'can map to sub dto as array') | ||
->tap(callable: function () { | ||
$data = UserData::resolveFromArray(data: [ | ||
'name' => 'Stiven Katuuk', | ||
'roles' => [ | ||
['name' => 'Admin'], | ||
], | ||
]); | ||
|
||
expect(value: Arr::first($data->roles))->toBeInstanceOf(class: RoleData::class); | ||
}) | ||
->group('unit', 'dto'); | ||
|
||
it(description: 'can map to int') | ||
->tap(callable: function () { | ||
$data = UserData::resolveFromArray(data: [ | ||
'age' => 19, | ||
]); | ||
|
||
expect(value: $data->age)->toBeInt(); | ||
}) | ||
->group('unit', 'dto'); | ||
|
||
it(description: 'can map to enum') | ||
->tap(callable: function () { | ||
$data = UserData::resolveFromArray(data: [ | ||
'gender' => GenderEnum::Female, | ||
]); | ||
|
||
expect(value: $data->gender)->toBe(expected: GenderEnum::Female); | ||
}) | ||
->group('unit', 'dto'); | ||
|
||
it(description: 'can map array and can resolve the key') | ||
->tap(callable: function () { | ||
$data = UserData::resolveFromArray(data: [ | ||
'addresses' => [ | ||
'main_address' => 'where', | ||
'main_address_1' => 'where', | ||
], | ||
]); | ||
|
||
expect(value: $data->addresses)->toMatchArray(array: [ | ||
'mainAddress' => 'where', | ||
'mainAddress1' => 'where', | ||
]); | ||
}) | ||
->group('unit', 'dto'); |
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,10 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\DataTransferObjects\Fixtures; | ||
|
||
enum GenderEnum | ||
{ | ||
case Female; | ||
|
||
case Male; | ||
} |
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 Tests\Unit\DataTransferObjects\Fixtures; | ||
|
||
class RoleData | ||
{ | ||
public function __construct( | ||
public string | null $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,20 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\DataTransferObjects\Fixtures; | ||
|
||
use KoalaFacade\DiamondConsole\Foundation\DataTransferObject; | ||
|
||
readonly class UserData extends DataTransferObject | ||
{ | ||
public function __construct( | ||
public string | null $name = null, | ||
/** @var array<int, RoleData> | null $roles */ | ||
public array | null $roles = null, | ||
public RoleData | null $mainRole = null, | ||
public int | null $age = null, | ||
public GenderEnum | null $gender = null, | ||
/** @var array<string, string> | null $address */ | ||
public array | null $addresses = null | ||
) { | ||
} | ||
} |