-
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.
fix: ensure nested bags are cast correctly with Eloquent
fixes #70
- Loading branch information
Showing
6 changed files
with
174 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,9 @@ | |
use Tests\Fixtures\Models\CastedModel; | ||
use Tests\Fixtures\Models\CastedModelLegacy; | ||
use Tests\Fixtures\Values\BagWithCollection; | ||
use Tests\Fixtures\Values\CastedModelValues; | ||
use Tests\Fixtures\Values\HiddenParametersBag; | ||
use Tests\Fixtures\Values\OptionalPropertiesBag; | ||
use Tests\Fixtures\Values\TestBag; | ||
|
||
covers(WithEloquentCasting::class, AsBag::class, AsBagCollection::class); | ||
|
@@ -158,6 +160,74 @@ | |
->and($model->hidden_bag->age)->toBe(40) | ||
->and($model->hidden_bag->email)->toBe('[email protected]'); | ||
}); | ||
|
||
test('it stores bags with toArray on Laravel 11+', function () { | ||
$model = CastedModel::create(CastedModelValues::from( | ||
[ | ||
'bag' => TestBag::from(['name' => 'Davey Shafik', 'age' => 40, 'email' => '[email protected]']) | ||
] | ||
)->toArray()); | ||
|
||
expect($model->bag)->toBeInstanceOf(TestBag::class) | ||
->and($model->bag->name)->toBe('Davey Shafik') | ||
->and($model->bag->age)->toBe(40) | ||
->and($model->bag->email)->toBe('[email protected]'); | ||
|
||
assertDatabaseHas('testing', ['bag' => '{"name":"Davey Shafik","age":40,"email":"[email protected]"}']); | ||
}); | ||
|
||
test('it stores nested bags with toArray on Laravel 11+', function () { | ||
$model = CastedModel::create(CastedModelValues::from( | ||
[ | ||
'optionalsBag' => OptionalPropertiesBag::from([ | ||
'bag' => TestBag::from(['name' => 'Davey Shafik', 'age' => 40, 'email' => '[email protected]']) | ||
]), | ||
] | ||
)->toArray()); | ||
|
||
expect($model->optionals_bag) | ||
->toBeInstanceOf(OptionalPropertiesBag::class) | ||
->and($model->optionals_bag->bag)->toBeInstanceOf(TestBag::class) | ||
->and($model->optionals_bag->bag->name)->toBe('Davey Shafik') | ||
->and($model->optionals_bag->bag->age)->toBe(40) | ||
->and($model->optionals_bag->bag->email)->toBe('[email protected]'); | ||
|
||
assertDatabaseHas('testing', ['optionals_bag' => '{"name":null,"age":null,"email":null,"bag":{"name":"Davey Shafik","age":40,"email":"[email protected]"}}']); | ||
}); | ||
|
||
test('it retrieves bags stored with toArray on Laravel 11+', function () { | ||
CastedModel::create(CastedModelValues::from( | ||
[ | ||
'bag' => TestBag::from(['name' => 'Davey Shafik', 'age' => 40, 'email' => '[email protected]']) | ||
] | ||
)->toArray()); | ||
|
||
$model = CastedModel::first(); | ||
|
||
expect($model->bag)->toBeInstanceOf(TestBag::class) | ||
->and($model->bag->name)->toBe('Davey Shafik') | ||
->and($model->bag->age)->toBe(40) | ||
->and($model->bag->email)->toBe('[email protected]'); | ||
}); | ||
|
||
test('it retrieves nested bags stored with toArray on Laravel 11+', function () { | ||
CastedModel::create(CastedModelValues::from( | ||
[ | ||
'optionalsBag' => OptionalPropertiesBag::from([ | ||
'bag' => TestBag::from(['name' => 'Davey Shafik', 'age' => 40, 'email' => '[email protected]']) | ||
]), | ||
] | ||
)->toArray()); | ||
|
||
$model = CastedModel::first(); | ||
|
||
expect($model->optionals_bag) | ||
->toBeInstanceOf(OptionalPropertiesBag::class) | ||
->and($model->optionals_bag->bag)->toBeInstanceOf(TestBag::class) | ||
->and($model->optionals_bag->bag->name)->toBe('Davey Shafik') | ||
->and($model->optionals_bag->bag->age)->toBe(40) | ||
->and($model->optionals_bag->bag->email)->toBe('[email protected]'); | ||
}); | ||
})->skip(fn () => !version_compare(Application::VERSION, '11.0.0', '>='), 'Requires Laravel 11+'); | ||
|
||
describe('Laravel 10+', function () { | ||
|
@@ -302,4 +372,72 @@ | |
->and($model->hidden_bag->age)->toBe(40) | ||
->and($model->hidden_bag->email)->toBe('[email protected]'); | ||
}); | ||
|
||
test('it stores bags with toArray on Laravel 10+', function () { | ||
$model = CastedModelLegacy::create(CastedModelValues::from( | ||
[ | ||
'bag' => TestBag::from(['name' => 'Davey Shafik', 'age' => 40, 'email' => '[email protected]']) | ||
] | ||
)->toArray()); | ||
|
||
expect($model->bag)->toBeInstanceOf(TestBag::class) | ||
->and($model->bag->name)->toBe('Davey Shafik') | ||
->and($model->bag->age)->toBe(40) | ||
->and($model->bag->email)->toBe('[email protected]'); | ||
|
||
assertDatabaseHas('testing', ['bag' => '{"name":"Davey Shafik","age":40,"email":"[email protected]"}']); | ||
}); | ||
|
||
test('it stores nested bags with toArray on Laravel 10+', function () { | ||
$model = CastedModelLegacy::create(CastedModelValues::from( | ||
[ | ||
'optionalsBag' => OptionalPropertiesBag::from([ | ||
'bag' => TestBag::from(['name' => 'Davey Shafik', 'age' => 40, 'email' => '[email protected]']) | ||
]), | ||
] | ||
)->toArray()); | ||
|
||
expect($model->optionals_bag) | ||
->toBeInstanceOf(OptionalPropertiesBag::class) | ||
->and($model->optionals_bag->bag)->toBeInstanceOf(TestBag::class) | ||
->and($model->optionals_bag->bag->name)->toBe('Davey Shafik') | ||
->and($model->optionals_bag->bag->age)->toBe(40) | ||
->and($model->optionals_bag->bag->email)->toBe('[email protected]'); | ||
|
||
assertDatabaseHas('testing', ['optionals_bag' => '{"name":null,"age":null,"email":null,"bag":{"name":"Davey Shafik","age":40,"email":"[email protected]"}}']); | ||
}); | ||
|
||
test('it retrieves bags stored with toArray on Laravel 10+', function () { | ||
CastedModelLegacy::create(CastedModelValues::from( | ||
[ | ||
'bag' => TestBag::from(['name' => 'Davey Shafik', 'age' => 40, 'email' => '[email protected]']) | ||
] | ||
)->toArray()); | ||
|
||
$model = CastedModelLegacy::first(); | ||
|
||
expect($model->bag)->toBeInstanceOf(TestBag::class) | ||
->and($model->bag->name)->toBe('Davey Shafik') | ||
->and($model->bag->age)->toBe(40) | ||
->and($model->bag->email)->toBe('[email protected]'); | ||
}); | ||
|
||
test('it retrieves nested bags stored with toArray on Laravel 10+', function () { | ||
CastedModelLegacy::create(CastedModelValues::from( | ||
[ | ||
'optionalsBag' => OptionalPropertiesBag::from([ | ||
'bag' => TestBag::from(['name' => 'Davey Shafik', 'age' => 40, 'email' => '[email protected]']) | ||
]), | ||
] | ||
)->toArray()); | ||
|
||
$model = CastedModelLegacy::first(); | ||
|
||
expect($model->optionals_bag) | ||
->toBeInstanceOf(OptionalPropertiesBag::class) | ||
->and($model->optionals_bag->bag)->toBeInstanceOf(TestBag::class) | ||
->and($model->optionals_bag->bag->name)->toBe('Davey Shafik') | ||
->and($model->optionals_bag->bag->age)->toBe(40) | ||
->and($model->optionals_bag->bag->email)->toBe('[email protected]'); | ||
}); | ||
}); |
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,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Fixtures\Values; | ||
|
||
use Bag\Attributes\MapName; | ||
use Bag\Bag; | ||
use Bag\Mappers\SnakeCase; | ||
|
||
#[MapName(SnakeCase::class, SnakeCase::class)] | ||
readonly class CastedModelValues extends Bag | ||
{ | ||
public function __construct( | ||
public ?string $name = null, | ||
public ?int $age = null, | ||
public ?string $email = null, | ||
public ?TestBag $bag = null, | ||
public ?OptionalPropertiesBag $optionalsBag = null, | ||
) { | ||
} | ||
} |
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