generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
443439d
commit 0fc8124
Showing
34 changed files
with
2,656 additions
and
2,506 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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
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,86 @@ | ||
<?php | ||
|
||
use Spatie\LaravelData\Data; | ||
|
||
it('can append data via method overwriting', function () { | ||
$data = new class ('Freek') extends Data { | ||
public function __construct(public string $name) | ||
{ | ||
} | ||
|
||
public function with(): array | ||
{ | ||
return ['alt_name' => "{$this->name} from Spatie"]; | ||
} | ||
}; | ||
|
||
expect($data->toArray())->toMatchArray([ | ||
'name' => 'Freek', | ||
'alt_name' => 'Freek from Spatie', | ||
]); | ||
}); | ||
|
||
it('can append data via method overwriting with closures', function () { | ||
$data = new class ('Freek') extends Data { | ||
public function __construct(public string $name) | ||
{ | ||
} | ||
|
||
public function with(): array | ||
{ | ||
return [ | ||
'alt_name' => static function (self $data) { | ||
return $data->name.' from Spatie via closure'; | ||
}, | ||
]; | ||
} | ||
}; | ||
|
||
expect($data->toArray())->toMatchArray([ | ||
'name' => 'Freek', | ||
'alt_name' => 'Freek from Spatie via closure', | ||
]); | ||
}); | ||
|
||
it('can append data via method call', function () { | ||
$data = new class ('Freek') extends Data { | ||
public function __construct(public string $name) | ||
{ | ||
} | ||
}; | ||
|
||
$transformed = $data->additional([ | ||
'company' => 'Spatie', | ||
'alt_name' => fn (Data $data) => "{$data->name} from Spatie", | ||
])->toArray(); | ||
|
||
expect($transformed)->toMatchArray([ | ||
'name' => 'Freek', | ||
'company' => 'Spatie', | ||
'alt_name' => 'Freek from Spatie', | ||
]); | ||
}); | ||
|
||
|
||
test('when using additional method and with method the additional method will be prioritized', function () { | ||
$data = new class ('Freek') extends Data { | ||
public function __construct(public string $name) | ||
{ | ||
} | ||
|
||
public function with(): array | ||
{ | ||
return [ | ||
'alt_name' => static function (self $data) { | ||
return $data->name.' from Spatie via closure'; | ||
}, | ||
]; | ||
} | ||
}; | ||
|
||
expect($data->additional(['alt_name' => 'I m Freek from additional'])->toArray())->toMatchArray([ | ||
'name' => 'Freek', | ||
'alt_name' => 'I m Freek from additional', | ||
]); | ||
}); | ||
|
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
Oops, something went wrong.