Skip to content

Commit

Permalink
Partial tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Jan 16, 2024
1 parent 3d00623 commit 06fcfe6
Show file tree
Hide file tree
Showing 12 changed files with 564 additions and 87 deletions.
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@
},
"autoload" : {
"psr-4" : {
"Spatie\\LaravelData\\" : "src",
"Spatie\\LaravelData\\Database\\Factories\\" : "database/factories"
"Spatie\\LaravelData\\" : "src/"
}
},
"autoload-dev" : {
"psr-4" : {
"Spatie\\LaravelData\\Tests\\" : "tests"
"Spatie\\LaravelData\\Tests\\" : "tests/"
}
},
"scripts" : {
Expand Down
6 changes: 4 additions & 2 deletions src/Concerns/BaseData.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,12 @@ public function getMorphClass(): string

public function __sleep(): array
{
return app(DataConfig::class)->getDataClass(static::class)
$dataClass = app(DataConfig::class)->getDataClass(static::class);

return $dataClass
->properties
->map(fn (DataProperty $property) => $property->name)
->push('_additional')
->when($dataClass->appendable, fn(Collection $properties) => $properties->push('_additional'))
->toArray();
}
}
4 changes: 3 additions & 1 deletion src/Support/Annotations/DataCollectableAnnotationReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
use Spatie\LaravelData\Contracts\BaseData;
use Spatie\LaravelData\Contracts\BaseDataCollectable;

/**
* @note To myself, always use the fully qualified class names in pest tests when using anonymous classes
*/
class DataCollectableAnnotationReader
{
/** @var array<string, Context> */
Expand Down Expand Up @@ -220,7 +223,6 @@ protected function resolveFcqn(
return ltrim((string) $type, '\\');
}


protected function getContext(ReflectionProperty|ReflectionClass|ReflectionMethod $reflection): Context
{
$reflectionClass = $reflection instanceof ReflectionProperty || $reflection instanceof ReflectionMethod
Expand Down
10 changes: 5 additions & 5 deletions src/Support/Creation/CreationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ class CreationContext
*/
public function __construct(
public string $dataClass,
public ValidationType $validationType,
public bool $mapPropertyNames,
public bool $withoutMagicalCreation,
public ?array $ignoredMagicalMethods,
public ?GlobalCastsCollection $casts,
public readonly ValidationType $validationType,
public readonly bool $mapPropertyNames,
public readonly bool $withoutMagicalCreation,
public readonly ?array $ignoredMagicalMethods,
public readonly ?GlobalCastsCollection $casts,
) {
}

Expand Down
17 changes: 15 additions & 2 deletions src/Support/Creation/CreationContextFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ public static function createFromConfig(
);
}

public static function createFromContext(
CreationContext $context
) {
return new self(
dataClass: $context->dataClass,
validationType: $context->validationType,
mapPropertyNames: $context->mapPropertyNames,
withoutMagicalCreation: $context->withoutMagicalCreation,
ignoredMagicalMethods: $context->ignoredMagicalMethods,
casts: $context->casts,
);
}

public function validationType(ValidationType $validationType): self
{
$this->validationType = $validationType;
Expand Down Expand Up @@ -112,7 +125,7 @@ public function ignoreMagicalMethod(string ...$methods): self
*/
public function withCast(
string $castable,
Cast|string $cast,
Cast | string $cast,
): self {
$cast = is_string($cast) ? app($cast) : $cast;

Expand Down Expand Up @@ -174,7 +187,7 @@ public function from(mixed ...$payloads): BaseData
public function collect(
mixed $items,
?string $into = null
): array|DataCollection|PaginatedDataCollection|CursorPaginatedDataCollection|Enumerable|AbstractPaginator|PaginatorContract|AbstractCursorPaginator|CursorPaginatorContract|LazyCollection|Collection {
): array | DataCollection | PaginatedDataCollection | CursorPaginatedDataCollection | Enumerable | AbstractPaginator | PaginatorContract | AbstractCursorPaginator | CursorPaginatorContract | LazyCollection | Collection {
return DataContainer::get()->dataCollectableFromSomethingResolver()->execute(
$this->dataClass,
$this->get(),
Expand Down
8 changes: 8 additions & 0 deletions src/Support/Partials/ResolvedPartial.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ public function toLaravel(): array
return [implode('.', $segments)];
}

public function toArray(): array
{
return [
'segments' => $this->segments,
'pointer' => $this->pointer,
];
}

public function next(): self
{
$this->pointer++;
Expand Down
22 changes: 22 additions & 0 deletions src/Support/Partials/ResolvedPartialsCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@
*/
class ResolvedPartialsCollection extends SplObjectStorage implements Stringable
{
public static function create(ResolvedPartial ...$resolvedPartials): self
{
$collection = new self();

foreach ($resolvedPartials as $resolvedPartial) {
$collection->attach($resolvedPartial);
}

return $collection;
}

public function toArray(): array
{
$output = [];

foreach ($this as $resolvedPartial) {
$output[] = $resolvedPartial->toArray();
}

return $output;
}

public function __toString(): string
{
$output = "- excludedPartials:".PHP_EOL;
Expand Down
14 changes: 14 additions & 0 deletions src/Support/Transformation/TransformationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,20 @@ public function mergePartialsFromDataContext(
return $this;
}

public function toArray(): array
{
return [
'transformValues' => $this->transformValues,
'mapPropertyNames' => $this->mapPropertyNames,
'wrapExecutionType' => $this->wrapExecutionType,
'transformers' => $this->transformers !== null ? iterator_to_array($this->transformers) : null,
'includedPartials' => $this->includedPartials?->toArray(),
'excludedPartials' => $this->excludedPartials?->toArray(),
'onlyPartials' => $this->onlyPartials?->toArray(),
'exceptPartials' => $this->exceptPartials?->toArray(),
];
}

public function __clone(): void
{
if ($this->includedPartials !== null) {
Expand Down
33 changes: 19 additions & 14 deletions tests/Fakes/CollectionAnnotationsData.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,27 @@ class CollectionAnnotationsData
public array $propertyT;

/**
* @param \Spatie\LaravelData\Tests\Fakes\SimpleData[]|null $propertyA
* @param null|\Spatie\LaravelData\Tests\Fakes\SimpleData[] $propertyB
* @param ?\Spatie\LaravelData\Tests\Fakes\SimpleData[] $propertyC
* @param ?\Spatie\LaravelData\Tests\Fakes\SimpleData[] $propertyD
* @param \Spatie\LaravelData\DataCollection<\Spatie\LaravelData\Tests\Fakes\SimpleData> $propertyE
* @param ?\Spatie\LaravelData\DataCollection<\Spatie\LaravelData\Tests\Fakes\SimpleData> $propertyF
* @param SimpleData[] $propertyG
* @param \Spatie\LaravelData\Tests\Fakes\SimpleData[]|null $paramA
* @param null|\Spatie\LaravelData\Tests\Fakes\SimpleData[] $paramB
* @param ?\Spatie\LaravelData\Tests\Fakes\SimpleData[] $paramC
* @param ?\Spatie\LaravelData\Tests\Fakes\SimpleData[] $paramD
* @param \Spatie\LaravelData\DataCollection<\Spatie\LaravelData\Tests\Fakes\SimpleData> $paramE
* @param ?\Spatie\LaravelData\DataCollection<\Spatie\LaravelData\Tests\Fakes\SimpleData> $paramF
* @param SimpleData[] $paramG
* @param array<SimpleData> $paramH
* @param array<int,SimpleData> $paramJ
* @param array<int, SimpleData> $paramI
*/
public function method(
array $propertyA,
?array $propertyB,
?array $propertyC,
array $propertyD,
DataCollection $propertyE,
?DataCollection $propertyF,
array $propertyG,
array $paramA,
?array $paramB,
?array $paramC,
array $paramD,
DataCollection $paramE,
?DataCollection $paramF,
array $paramG,
array $paramJ,
array $paramI,
) {

}
Expand Down
Loading

0 comments on commit 06fcfe6

Please sign in to comment.