Skip to content

Commit cc76f20

Browse files
committed
Inline fake classes and implement suggested fix for validation rules resolver
1 parent 17be5be commit cc76f20

10 files changed

+316
-240
lines changed

src/Resolvers/DataValidationRulesResolver.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ public function execute(
3939
/**
4040
* @var class-string<PropertyMorphableData> $class
4141
*/
42-
$payload = $path->isRoot() ? $fullPayload : Arr::get($fullPayload, $path->get(), []);
43-
$class = $class::morph($payload) ?? $class;
44-
$dataClass = $this->dataConfig->getDataClass($class);
42+
$morphedClass = $class::morph(
43+
$path->isRoot() ? $fullPayload : Arr::get($fullPayload, $path->get(), [])
44+
);
45+
46+
$dataClass = $this->dataConfig->getDataClass($morphedClass ?? $class);
4547
}
4648

4749
$withoutValidationProperties = [];

tests/CreationTest.php

+106-66
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Spatie\LaravelData\Casts\Uncastable;
2323
use Spatie\LaravelData\Concerns\WithDeprecatedCollectionMethod;
2424
use Spatie\LaravelData\Contracts\DeprecatedData as DeprecatedDataContract;
25+
use Spatie\LaravelData\Contracts\PropertyMorphableData;
2526
use Spatie\LaravelData\Data;
2627
use Spatie\LaravelData\DataCollection;
2728
use Spatie\LaravelData\DataPipeline;
@@ -53,10 +54,6 @@
5354
use Spatie\LaravelData\Tests\Fakes\NestedLazyData;
5455
use Spatie\LaravelData\Tests\Fakes\NestedModelCollectionData;
5556
use Spatie\LaravelData\Tests\Fakes\NestedModelData;
56-
use Spatie\LaravelData\Tests\Fakes\PropertyMorphableData\AbstractPropertyMorphableData;
57-
use Spatie\LaravelData\Tests\Fakes\PropertyMorphableData\NestedPropertyMorphableData;
58-
use Spatie\LaravelData\Tests\Fakes\PropertyMorphableData\PropertyMorphableDataA;
59-
use Spatie\LaravelData\Tests\Fakes\PropertyMorphableData\PropertyMorphableDataB;
6057
use Spatie\LaravelData\Tests\Fakes\SimpleData;
6158
use Spatie\LaravelData\Tests\Fakes\SimpleDataWithoutConstructor;
6259

@@ -1232,78 +1229,121 @@ public static function pipeline(): DataPipeline
12321229
);
12331230
})->todo();
12341231

1235-
it('will allow property-morphable data to be created', function () {
1236-
$dataA = AbstractPropertyMorphableData::from([
1237-
'variant' => 'a',
1238-
'a' => 'foo',
1239-
'enum' => 'foo',
1240-
]);
1232+
describe('property-morphable creation tests', function () {
1233+
abstract class TestAbstractPropertyMorphableData extends Data implements PropertyMorphableData
1234+
{
1235+
public function __construct(public string $variant)
1236+
{
1237+
}
12411238

1242-
expect($dataA)
1243-
->toBeInstanceOf(PropertyMorphableDataA::class)
1244-
->variant->toEqual('a')
1245-
->a->toEqual('foo')
1246-
->enum->toEqual(DummyBackedEnum::FOO);
1239+
public static function morph(array $properties): ?string
1240+
{
1241+
return match ($properties['variant'] ?? null) {
1242+
'a' => TestPropertyMorphableDataA::class,
1243+
'b' => TestPropertyMorphableDataB::class,
1244+
default => null,
1245+
};
1246+
}
1247+
}
12471248

1248-
$dataB = AbstractPropertyMorphableData::from([
1249-
'variant' => 'b',
1250-
'b' => 'bar',
1251-
]);
1249+
class TestPropertyMorphableDataA extends TestAbstractPropertyMorphableData
1250+
{
1251+
public function __construct(public string $a, public DummyBackedEnum $enum)
1252+
{
1253+
parent::__construct('a');
1254+
}
1255+
}
12521256

1253-
expect($dataB)
1254-
->toBeInstanceOf(PropertyMorphableDataB::class)
1255-
->variant->toEqual('b')
1256-
->b->toEqual('bar');
1257-
});
1257+
class TestPropertyMorphableDataB extends TestAbstractPropertyMorphableData
1258+
{
1259+
public function __construct(public string $b)
1260+
{
1261+
parent::__construct('b');
1262+
}
1263+
}
12581264

1259-
it('will allow property-morphable data to be created from concrete', function () {
1260-
$dataA = PropertyMorphableDataA::from([
1261-
'a' => 'foo',
1262-
'enum' => 'foo',
1263-
]);
1265+
it('will allow property-morphable data to be created', function () {
1266+
$dataA = TestAbstractPropertyMorphableData::from([
1267+
'variant' => 'a',
1268+
'a' => 'foo',
1269+
'enum' => 'foo',
1270+
]);
12641271

1265-
expect($dataA)
1266-
->toBeInstanceOf(PropertyMorphableDataA::class)
1267-
->variant->toEqual('a')
1268-
->a->toEqual('foo')
1269-
->enum->toEqual(DummyBackedEnum::FOO);
1270-
});
1272+
expect($dataA)
1273+
->toBeInstanceOf(TestPropertyMorphableDataA::class)
1274+
->variant->toEqual('a')
1275+
->a->toEqual('foo')
1276+
->enum->toEqual(DummyBackedEnum::FOO);
12711277

1272-
it('will allow property-morphable data to be created from a nested collection', function () {
1273-
$data = NestedPropertyMorphableData::from([
1274-
'nestedCollection' => [
1275-
['variant' => 'a', 'a' => 'foo', 'enum' => 'foo'],
1276-
['variant' => 'b', 'b' => 'bar'],
1277-
],
1278-
]);
1278+
$dataB = TestAbstractPropertyMorphableData::from([
1279+
'variant' => 'b',
1280+
'b' => 'bar',
1281+
]);
12791282

1280-
expect($data->nestedCollection[0])
1281-
->toBeInstanceOf(PropertyMorphableDataA::class)
1282-
->variant->toEqual('a')
1283-
->a->toEqual('foo')
1284-
->enum->toEqual(DummyBackedEnum::FOO);
1283+
expect($dataB)
1284+
->toBeInstanceOf(TestPropertyMorphableDataB::class)
1285+
->variant->toEqual('b')
1286+
->b->toEqual('bar');
1287+
});
12851288

1286-
expect($data->nestedCollection[1])
1287-
->toBeInstanceOf(PropertyMorphableDataB::class)
1288-
->variant->toEqual('b')
1289-
->b->toEqual('bar');
1290-
});
1289+
it('will allow property-morphable data to be created from concrete', function () {
1290+
$dataA = TestPropertyMorphableDataA::from([
1291+
'a' => 'foo',
1292+
'enum' => 'foo',
1293+
]);
12911294

1295+
expect($dataA)
1296+
->toBeInstanceOf(TestPropertyMorphableDataA::class)
1297+
->variant->toEqual('a')
1298+
->a->toEqual('foo')
1299+
->enum->toEqual(DummyBackedEnum::FOO);
1300+
});
12921301

1293-
it('will allow property-morphable data to be created as a collection', function () {
1294-
$collection = AbstractPropertyMorphableData::collect([
1295-
['variant' => 'a', 'a' => 'foo', 'enum' => DummyBackedEnum::FOO->value],
1296-
['variant' => 'b', 'b' => 'bar'],
1297-
]);
1302+
it('will allow property-morphable data to be created from a nested collection', function () {
1303+
class NestedPropertyMorphableData extends Data
1304+
{
1305+
public function __construct(
1306+
/** @var TestAbstractPropertyMorphableData[] */
1307+
public ?DataCollection $nestedCollection,
1308+
) {
1309+
}
1310+
}
1311+
1312+
$data = NestedPropertyMorphableData::from([
1313+
'nestedCollection' => [
1314+
['variant' => 'a', 'a' => 'foo', 'enum' => 'foo'],
1315+
['variant' => 'b', 'b' => 'bar'],
1316+
],
1317+
]);
12981318

1299-
expect($collection[0])
1300-
->toBeInstanceOf(PropertyMorphableDataA::class)
1301-
->variant->toEqual('a')
1302-
->a->toEqual('foo')
1303-
->enum->toEqual(DummyBackedEnum::FOO);
1319+
expect($data->nestedCollection[0])
1320+
->toBeInstanceOf(TestPropertyMorphableDataA::class)
1321+
->variant->toEqual('a')
1322+
->a->toEqual('foo')
1323+
->enum->toEqual(DummyBackedEnum::FOO);
1324+
1325+
expect($data->nestedCollection[1])
1326+
->toBeInstanceOf(TestPropertyMorphableDataB::class)
1327+
->variant->toEqual('b')
1328+
->b->toEqual('bar');
1329+
});
1330+
1331+
1332+
it('will allow property-morphable data to be created as a collection', function () {
1333+
$collection = TestAbstractPropertyMorphableData::collect([
1334+
['variant' => 'a', 'a' => 'foo', 'enum' => DummyBackedEnum::FOO->value],
1335+
['variant' => 'b', 'b' => 'bar'],
1336+
]);
13041337

1305-
expect($collection[1])
1306-
->toBeInstanceOf(PropertyMorphableDataB::class)
1307-
->variant->toEqual('b')
1308-
->b->toEqual('bar');
1338+
expect($collection[0])
1339+
->toBeInstanceOf(TestPropertyMorphableDataA::class)
1340+
->variant->toEqual('a')
1341+
->a->toEqual('foo')
1342+
->enum->toEqual(DummyBackedEnum::FOO);
1343+
1344+
expect($collection[1])
1345+
->toBeInstanceOf(TestPropertyMorphableDataB::class)
1346+
->variant->toEqual('b')
1347+
->b->toEqual('bar');
1348+
});
13091349
});

tests/Fakes/Models/DummyModelWithPropertyMorphableCast.php

-19
This file was deleted.

tests/Fakes/PropertyMorphableData/AbstractPropertyMorphableData.php

-25
This file was deleted.

tests/Fakes/PropertyMorphableData/NestedPropertyMorphableData.php

-15
This file was deleted.

tests/Fakes/PropertyMorphableData/PropertyMorphableDataA.php

-15
This file was deleted.

tests/Fakes/PropertyMorphableData/PropertyMorphableDataB.php

-12
This file was deleted.

0 commit comments

Comments
 (0)