Skip to content

Commit

Permalink
Merge pull request #87 from kevariable/2x
Browse files Browse the repository at this point in the history
Abilities to orchestrate the Data
  • Loading branch information
kevariable authored Mar 26, 2023
2 parents 25856d5 + 8d5314a commit 3a28ec4
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 7 deletions.
23 changes: 22 additions & 1 deletion src/Foundation/DataTransferObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace KoalaFacade\DiamondConsole\Foundation;

use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Tappable;
Expand All @@ -25,7 +26,7 @@ protected function toExcludedPropertiesOnCreate(): array
}

/**
* Prevent properties to included on create
* Prevent properties to included on update
*
* @return array<empty>
*/
Expand Down Expand Up @@ -73,4 +74,24 @@ public function dd(): never
{
dd($this);
}

/**
* Abilities to orchestrate the Data
*
* @param mixed ...$values
* @return static
*/
public function recycle(mixed ...$values): static
{
$callback = Arr::first($values);

if (Collection::make($values)->containsOneItem() && $callback instanceof \Closure) {
/** @var static $instance */
$instance = call_user_func($callback, $this);
} else {
$instance = $this->with(...$values);
}

return $instance;
}
}
6 changes: 0 additions & 6 deletions tests/Feature/Commands/Domain/ModelMakeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use KoalaFacade\DiamondConsole\Exceptions\FileAlreadyExistException;

it(description: 'can generate new Model class')
->skip()
->tap(function () {
$fileName = '/Shared/User/Models/User.php';

Expand All @@ -28,7 +27,6 @@
->group(groups: 'commands');

it(description: 'can generate new Model class with separator')
->skip()
->tap(function () {
$fileName = '/Shared/User/Models/Foo/bar.php';

Expand All @@ -48,7 +46,6 @@
->group(groups: 'commands');

it(description: 'can force generate exists Model class')
->skip()
->tap(function () {
$fileName = '/Shared/User/Models/User.php';

Expand All @@ -70,7 +67,6 @@
->group(groups: 'commands');

it(description: 'can generate new Model class with Migration')
->skip()
->tap(function () {
$fileName = '/Shared/User/Models/User.php';

Expand Down Expand Up @@ -104,7 +100,6 @@
->group(groups: 'commands');

it(description: 'can force generate exists Model class with Migration')
->skip()
->tap(function () {
$fileName = '/Shared/User/Models/User.php';

Expand Down Expand Up @@ -182,7 +177,6 @@
->group(groups: 'commands');

it(description: 'cannot generate the Model, if the Model already exists')
->skip()
->tap(function () {
$fileName = '/Shared/User/Models/User.php';

Expand Down
49 changes: 49 additions & 0 deletions tests/Unit/DataTransferObjects/DataTransferObjectTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Composer\InstalledVersions;
use Illuminate\Support\Arr;
use Tests\Unit\DataTransferObjects\Fixtures\GenderEnum;
use Tests\Unit\DataTransferObjects\Fixtures\RoleData;
Expand Down Expand Up @@ -75,3 +76,51 @@
]);
})
->group('unit', 'dto');

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

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

$data
->recycle(
addresses: $addresses
)
->tap(callback: function (UserData $data) use ($addresses) {
expect($data->addresses)->toMatchArray(array: $addresses);
});
});

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

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

$data
->recycle(function (UserData $data) use ($addresses): UserData {
return $data->with(addresses: $addresses);
})
->tap(callback: function (UserData $data) use ($addresses) {
expect($data->addresses)->toMatchArray(array: $addresses);
});
});

0 comments on commit 3a28ec4

Please sign in to comment.