Skip to content

Commit

Permalink
Merge pull request #89 from kevariable/2x
Browse files Browse the repository at this point in the history
Conditionable Data Transfer Object Feature
  • Loading branch information
kevariable authored Mar 26, 2023
2 parents 3a28ec4 + ac7e60d commit 6a259b7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Foundation/DataTransferObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Conditionable;
use Illuminate\Support\Traits\Tappable;
use KoalaFacade\DiamondConsole\Foundation\DataTransferObject\HasResolvable;
use Spatie\Cloneable\Cloneable;
Expand All @@ -14,6 +15,7 @@
use HasResolvable;
use Tappable;
use Cloneable;
use Conditionable;

/**
* Prevent properties to included on create
Expand Down
37 changes: 37 additions & 0 deletions tests/Unit/DataTransferObjects/DataTransferObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,41 @@
->tap(callback: function (UserData $data) use ($addresses) {
expect($data->addresses)->toMatchArray(array: $addresses);
});
});

it(description: 'can recycle the data with conditional')
->group('unit', 'dto')
->skip(
conditionOrMessage: InstalledVersions::getVersion(packageName: 'spatie/php-cloneable') === '1.0.0.0'
)
->tap(callable: function () {
$data = UserData::resolve(data: [
'name' => 'Kevin'
]);

$roleData = new RoleData(name: 'Maintainer');

$addresses = [
'main_address' => 'where',
'main_address_1' => 'where',
];

$data
->when(
value: true,
callback: fn (UserData $data) => $data->with(addresses: $addresses)
)
->tap(
callback: fn (UserData $data) => expect($data->addresses)
->toMatchArray(array: $addresses)
);

$data
->unless(
value: false,
callback: fn (UserData $data) => $data->with(mainRole: $roleData)
)
->tap(
callback: fn (UserData $data) => expect($roleData->name)->toBe($roleData->name)
);
});

0 comments on commit 6a259b7

Please sign in to comment.