From f82bd4c55e0cd1cfae7c4a0904265e24951dab37 Mon Sep 17 00:00:00 2001 From: Ruben Van Assche Date: Fri, 19 Jan 2024 13:02:11 +0100 Subject: [PATCH] Remove resolved partial --- composer.json | 2 +- src/Resolvers/VisibleDataFieldsResolver.php | 12 +- src/Support/Partials/Partial.php | 129 ++++++++++- src/Support/Partials/PartialsCollection.php | 14 +- src/Support/Partials/ResolvedPartial.php | 147 ------------- .../Partials/ResolvedPartialsCollection.php | 45 ---- src/Support/Transformation/DataContext.php | 12 +- .../Transformation/TransformationContext.php | 74 ++++--- .../TransformationContextFactory.php | 32 +-- .../VisibleDataFieldsResolverTest.php | 206 +++++++++--------- tests/Support/Partials/PartialTest.php | 141 ++++++++++++ .../Support/Partials/ResolvedPartialTest.php | 146 ------------- 12 files changed, 438 insertions(+), 522 deletions(-) delete mode 100644 src/Support/Partials/ResolvedPartial.php delete mode 100644 src/Support/Partials/ResolvedPartialsCollection.php delete mode 100644 tests/Support/Partials/ResolvedPartialTest.php diff --git a/composer.json b/composer.json index 53ac20b10..9d5ceaf08 100644 --- a/composer.json +++ b/composer.json @@ -56,7 +56,7 @@ "test" : "./vendor/bin/pest --no-coverage", "test-coverage" : "vendor/bin/pest --coverage-html coverage", "format" : "vendor/bin/php-cs-fixer fix --allow-risky=yes", - "benchmark" : "vendor/bin/phpbench run --report=default", + "benchmark" : "vendor/bin/phpbench run", "benchmark-profiled" : "vendor/bin/phpbench " }, "config" : { diff --git a/src/Resolvers/VisibleDataFieldsResolver.php b/src/Resolvers/VisibleDataFieldsResolver.php index 3e21e5b4e..71072d7c9 100644 --- a/src/Resolvers/VisibleDataFieldsResolver.php +++ b/src/Resolvers/VisibleDataFieldsResolver.php @@ -131,7 +131,7 @@ protected function performExcept( if ($nested = $exceptPartial->getNested()) { try { - $fields[$nested]->addExceptResolvedPartial($exceptPartial->next()); + $fields[$nested]->addExceptPartial($exceptPartial->next()); } catch (ErrorException $exception) { $this->handleNonExistingNestedField($exception, PartialType::Except, $nested, $dataClass, $transformationContext); } @@ -169,7 +169,7 @@ protected function performOnly( if ($nested = $onlyPartial->getNested()) { try { - $fields[$nested]->addOnlyResolvedPartial($onlyPartial->next()); + $fields[$nested]->addOnlyPartial($onlyPartial->next()); $onlyFields[] = $nested; } catch (ErrorException $exception) { $this->handleNonExistingNestedField($exception, PartialType::Only, $nested, $dataClass, $transformationContext); @@ -220,7 +220,7 @@ protected function resolveIncludedFields( foreach ($includedFields as $includedField) { // can be null when field is a non data object/collectable or array - $fields[$includedField]?->addIncludedResolvedPartial($includedPartial->next()); + $fields[$includedField]?->addIncludedPartial($includedPartial->next()); } break; @@ -228,7 +228,7 @@ protected function resolveIncludedFields( if ($nested = $includedPartial->getNested()) { try { - $fields[$nested]->addIncludedResolvedPartial($includedPartial->next()); + $fields[$nested]->addIncludedPartial($includedPartial->next()); $includedFields[] = $nested; } catch (ErrorException $exception) { $this->handleNonExistingNestedField($exception, PartialType::Include, $nested, $dataClass, $transformationContext); @@ -268,7 +268,7 @@ protected function resolveExcludedFields( ->all(); foreach ($excludedFields as $excludedField) { - $fields[$excludedField]?->addExcludedResolvedPartial($excludePartial->next()); + $fields[$excludedField]?->addExcludedPartial($excludePartial->next()); } break; @@ -276,7 +276,7 @@ protected function resolveExcludedFields( if ($nested = $excludePartial->getNested()) { try { - $fields[$nested]->addExcludedResolvedPartial($excludePartial->next()); + $fields[$nested]->addExcludedPartial($excludePartial->next()); } catch (ErrorException $exception) { $this->handleNonExistingNestedField($exception, PartialType::Exclude, $nested, $dataClass, $transformationContext); } diff --git a/src/Support/Partials/Partial.php b/src/Support/Partials/Partial.php index 97a567922..615844c0f 100644 --- a/src/Support/Partials/Partial.php +++ b/src/Support/Partials/Partial.php @@ -13,17 +13,23 @@ class Partial implements Stringable { - protected readonly ResolvedPartial $resolvedPartial; + protected int $segmentCount; + + protected bool $endsInAll; /** * @param array $segments */ public function __construct( public array $segments, - public bool $permanent, - public ?Closure $condition, + public bool $permanent = false, + public ?Closure $condition = null, + public int $pointer = 0, ) { - $this->resolvedPartial = new ResolvedPartial($segments); + $this->segmentCount = count($segments); + $this->endsInAll = $this->segmentCount === 0 + ? false + : $this->segments[$this->segmentCount - 1] instanceof AllPartialSegment; } public static function create( @@ -107,19 +113,120 @@ protected static function resolveSegmentsFromPath(string $path): array return $segments; } - public function resolve(BaseData|BaseDataCollectable $data): ?ResolvedPartial + public function isUndefined(): bool { - if ($this->condition === null) { - return $this->resolvedPartial->reset(); + return ! $this->endsInAll && $this->pointer >= $this->segmentCount; + } + + public function isAll(): bool + { + return $this->endsInAll && $this->pointer >= $this->segmentCount - 1; + } + + public function getNested(): ?string + { + $segment = $this->getCurrentSegment(); + + if ($segment === null) { + return null; + } + + if (! $segment instanceof NestedPartialSegment) { + return null; + } + + return $segment->field; + } + + public function getFields(): ?array + { + if ($this->isUndefined()) { + return null; + } + + $segment = $this->getCurrentSegment(); + + if ($segment === null) { + return null; } - if (($this->condition)($data)) { - return $this->resolvedPartial->reset(); + if (! $segment instanceof FieldsPartialSegment) { + return null; } - return null; + return $segment->fields; } + /** @return string[] */ + public function toLaravel(): array + { + /** @var array $segments */ + $segments = []; + + for ($i = $this->pointer; $i < $this->segmentCount; $i++) { + $segment = $this->segments[$i]; + + if ($segment instanceof AllPartialSegment) { + $segments[] = '*'; + + continue; + } + + if ($segment instanceof NestedPartialSegment) { + $segments[] = $segment->field; + + continue; + } + + if ($segment instanceof FieldsPartialSegment) { + $segmentsAsString = count($segments) === 0 + ? '' + : implode('.', $segments).'.'; + + return array_map( + fn (string $field) => "{$segmentsAsString}{$field}", + $segment->fields + ); + } + } + + return [implode('.', $segments)]; + } + + public function next(): self + { + $this->pointer++; + + return $this; + } + + public function rollbackWhenRequired(): void + { + $this->pointer--; + } + + public function reset(): self + { + $this->pointer = 0; + + return $this; + } + + protected function getCurrentSegment(): ?PartialSegment + { + return $this->segments[$this->pointer] ?? null; + } + + public function isRequired(BaseData|BaseDataCollectable $data): bool + { + if ($this->condition === null) { + return true; + } + + return ($this->condition)($data); + } + + public function toArray(): array { return [ @@ -131,6 +238,6 @@ public function toArray(): array public function __toString(): string { - return implode('.', $this->segments); + return implode('.', $this->segments)." (current: {$this->pointer})"; } } diff --git a/src/Support/Partials/PartialsCollection.php b/src/Support/Partials/PartialsCollection.php index 243696478..7540b6d88 100644 --- a/src/Support/Partials/PartialsCollection.php +++ b/src/Support/Partials/PartialsCollection.php @@ -3,11 +3,12 @@ namespace Spatie\LaravelData\Support\Partials; use SplObjectStorage; +use Stringable; /** * @extends SplObjectStorage */ -class PartialsCollection extends SplObjectStorage +class PartialsCollection extends SplObjectStorage implements Stringable { public static function create(Partial ...$partials): self { @@ -30,4 +31,15 @@ public function toArray(): array return $output; } + + public function __toString(): string + { + $output = ''; + + foreach ($this as $partial) { + $output .= " - {$partial}".PHP_EOL; + } + + return $output; + } } diff --git a/src/Support/Partials/ResolvedPartial.php b/src/Support/Partials/ResolvedPartial.php deleted file mode 100644 index f898c94df..000000000 --- a/src/Support/Partials/ResolvedPartial.php +++ /dev/null @@ -1,147 +0,0 @@ - $segments - * @param int $pointer - */ - public function __construct( - public array $segments, - public int $pointer = 0, - ) { - $this->segmentCount = count($segments); - $this->endsInAll = $this->segmentCount === 0 - ? false - : $this->segments[$this->segmentCount - 1] instanceof AllPartialSegment; - } - - public function isUndefined(): bool - { - return ! $this->endsInAll && $this->pointer >= $this->segmentCount; - } - - public function isAll(): bool - { - return $this->endsInAll && $this->pointer >= $this->segmentCount - 1; - } - - public function getNested(): ?string - { - $segment = $this->getCurrentSegment(); - - if ($segment === null) { - return null; - } - - if (! $segment instanceof NestedPartialSegment) { - return null; - } - - return $segment->field; - } - - public function getFields(): ?array - { - if ($this->isUndefined()) { - return null; - } - - $segment = $this->getCurrentSegment(); - - if ($segment === null) { - return null; - } - - if (! $segment instanceof FieldsPartialSegment) { - return null; - } - - return $segment->fields; - } - - /** @return string[] */ - public function toLaravel(): array - { - /** @var array $segments */ - $segments = []; - - for ($i = $this->pointer; $i < $this->segmentCount; $i++) { - $segment = $this->segments[$i]; - - if ($segment instanceof AllPartialSegment) { - $segments[] = '*'; - - continue; - } - - if ($segment instanceof NestedPartialSegment) { - $segments[] = $segment->field; - - continue; - } - - if ($segment instanceof FieldsPartialSegment) { - $segmentsAsString = count($segments) === 0 - ? '' - : implode('.', $segments).'.'; - - return array_map( - fn (string $field) => "{$segmentsAsString}{$field}", - $segment->fields - ); - } - } - - return [implode('.', $segments)]; - } - - public function toArray(): array - { - return [ - 'segments' => $this->segments, - 'pointer' => $this->pointer, - ]; - } - - public function next(): self - { - $this->pointer++; - - return $this; - } - - public function rollbackWhenRequired(): void - { - $this->pointer--; - } - - public function reset(): self - { - $this->pointer = 0; - - return $this; - } - - protected function getCurrentSegment(): ?PartialSegment - { - return $this->segments[$this->pointer] ?? null; - } - - public function __toString(): string - { - return implode('.', $this->segments)." (current: {$this->pointer})"; - } -} diff --git a/src/Support/Partials/ResolvedPartialsCollection.php b/src/Support/Partials/ResolvedPartialsCollection.php deleted file mode 100644 index c07224969..000000000 --- a/src/Support/Partials/ResolvedPartialsCollection.php +++ /dev/null @@ -1,45 +0,0 @@ - - */ -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 = ''; - - foreach ($this as $partial) { - $output .= " - {$partial}".PHP_EOL; - } - - return $output; - } -} diff --git a/src/Support/Transformation/DataContext.php b/src/Support/Transformation/DataContext.php index dcdab050f..59763594d 100644 --- a/src/Support/Transformation/DataContext.php +++ b/src/Support/Transformation/DataContext.php @@ -48,16 +48,16 @@ public function mergePartials(DataContext $dataContext): self return $this; } - public function getResolvedPartialsAndRemoveTemporaryOnes( + public function getRequiredPartialsAndRemoveTemporaryOnes( BaseData|BaseDataCollectable $data, PartialsCollection $partials, - ): ResolvedPartialsCollection { - $resolvedPartials = new ResolvedPartialsCollection(); + ): PartialsCollection { + $requiredPartials = new PartialsCollection(); $partialsToDetach = new PartialsCollection(); foreach ($partials as $partial) { - if ($resolved = $partial->resolve($data)) { - $resolvedPartials->attach($resolved); + if ($partial->isRequired($data)) { + $requiredPartials->attach($partial->reset()); } if (! $partial->permanent) { @@ -67,6 +67,6 @@ public function getResolvedPartialsAndRemoveTemporaryOnes( $partials->removeAll($partialsToDetach); - return $resolvedPartials; + return $requiredPartials; } } diff --git a/src/Support/Transformation/TransformationContext.php b/src/Support/Transformation/TransformationContext.php index cde37e622..21fbbb0e8 100644 --- a/src/Support/Transformation/TransformationContext.php +++ b/src/Support/Transformation/TransformationContext.php @@ -5,6 +5,8 @@ use Spatie\LaravelData\Contracts\BaseData; use Spatie\LaravelData\Contracts\BaseDataCollectable; use Spatie\LaravelData\Contracts\IncludeableData; +use Spatie\LaravelData\Support\Partials\Partial; +use Spatie\LaravelData\Support\Partials\PartialsCollection; use Spatie\LaravelData\Support\Partials\ResolvedPartial; use Spatie\LaravelData\Support\Partials\ResolvedPartialsCollection; use Spatie\LaravelData\Support\Wrapping\WrapExecutionType; @@ -20,10 +22,10 @@ public function __construct( public bool $mapPropertyNames = true, public WrapExecutionType $wrapExecutionType = WrapExecutionType::Disabled, public ?GlobalTransformersCollection $transformers = null, - public ?ResolvedPartialsCollection $includePartials = null, - public ?ResolvedPartialsCollection $excludePartials = null, - public ?ResolvedPartialsCollection $onlyPartials = null, - public ?ResolvedPartialsCollection $exceptPartials = null, + public ?PartialsCollection $includePartials = null, + public ?PartialsCollection $excludePartials = null, + public ?PartialsCollection $onlyPartials = null, + public ?PartialsCollection $exceptPartials = null, ) { } @@ -34,81 +36,81 @@ public function setWrapExecutionType(WrapExecutionType $wrapExecutionType): self return $this; } - public function addIncludedResolvedPartial(ResolvedPartial ...$resolvedPartials): void + public function addIncludedPartial(Partial ...$partials): void { if ($this->includePartials === null) { - $this->includePartials = new ResolvedPartialsCollection(); + $this->includePartials = new PartialsCollection(); } - foreach ($resolvedPartials as $resolvedPartial) { - $this->includePartials->attach($resolvedPartial); + foreach ($partials as $partial) { + $this->includePartials->attach($partial); } } - public function addExcludedResolvedPartial(ResolvedPartial ...$resolvedPartials): void + public function addExcludedPartial(Partial ...$partials): void { if ($this->excludePartials === null) { - $this->excludePartials = new ResolvedPartialsCollection(); + $this->excludePartials = new PartialsCollection(); } - foreach ($resolvedPartials as $resolvedPartial) { - $this->excludePartials->attach($resolvedPartial); + foreach ($partials as $partial) { + $this->excludePartials->attach($partial); } } - public function addOnlyResolvedPartial(ResolvedPartial ...$resolvedPartials): void + public function addOnlyPartial(Partial ...$partials): void { if ($this->onlyPartials === null) { - $this->onlyPartials = new ResolvedPartialsCollection(); + $this->onlyPartials = new PartialsCollection(); } - foreach ($resolvedPartials as $resolvedPartial) { - $this->onlyPartials->attach($resolvedPartial); + foreach ($partials as $partial) { + $this->onlyPartials->attach($partial); } } - public function addExceptResolvedPartial(ResolvedPartial ...$resolvedPartials): void + public function addExceptPartial(Partial ...$partials): void { if ($this->exceptPartials === null) { - $this->exceptPartials = new ResolvedPartialsCollection(); + $this->exceptPartials = new PartialsCollection(); } - foreach ($resolvedPartials as $resolvedPartial) { - $this->exceptPartials->attach($resolvedPartial); + foreach ($partials as $partial) { + $this->exceptPartials->attach($partial); } } - public function mergeIncludedResolvedPartials(ResolvedPartialsCollection $partials): void + public function mergeIncludedPartials(PartialsCollection $partials): void { if ($this->includePartials === null) { - $this->includePartials = new ResolvedPartialsCollection(); + $this->includePartials = new PartialsCollection(); } $this->includePartials->addAll($partials); } - public function mergeExcludedResolvedPartials(ResolvedPartialsCollection $partials): void + public function mergeExcludedPartials(PartialsCollection $partials): void { if ($this->excludePartials === null) { - $this->excludePartials = new ResolvedPartialsCollection(); + $this->excludePartials = new PartialsCollection(); } $this->excludePartials->addAll($partials); } - public function mergeOnlyResolvedPartials(ResolvedPartialsCollection $partials): void + public function mergeOnlyPartials(PartialsCollection $partials): void { if ($this->onlyPartials === null) { - $this->onlyPartials = new ResolvedPartialsCollection(); + $this->onlyPartials = new PartialsCollection(); } $this->onlyPartials->addAll($partials); } - public function mergeExceptResolvedPartials(ResolvedPartialsCollection $partials): void + public function mergeExceptPartials(PartialsCollection $partials): void { if ($this->exceptPartials === null) { - $this->exceptPartials = new ResolvedPartialsCollection(); + $this->exceptPartials = new PartialsCollection(); } $this->exceptPartials->addAll($partials); @@ -150,26 +152,26 @@ public function mergePartialsFromDataContext( $dataContext = $data->getDataContext(); if ($dataContext->includePartials && $dataContext->includePartials->count() > 0) { - $this->mergeIncludedResolvedPartials( - $dataContext->getResolvedPartialsAndRemoveTemporaryOnes($data, $dataContext->includePartials) + $this->mergeIncludedPartials( + $dataContext->getRequiredPartialsAndRemoveTemporaryOnes($data, $dataContext->includePartials) ); } if ($dataContext->excludePartials && $dataContext->excludePartials->count() > 0) { - $this->mergeExcludedResolvedPartials( - $dataContext->getResolvedPartialsAndRemoveTemporaryOnes($data, $dataContext->excludePartials) + $this->mergeExcludedPartials( + $dataContext->getRequiredPartialsAndRemoveTemporaryOnes($data, $dataContext->excludePartials) ); } if ($dataContext->onlyPartials && $dataContext->onlyPartials->count() > 0) { - $this->mergeOnlyResolvedPartials( - $dataContext->getResolvedPartialsAndRemoveTemporaryOnes($data, $dataContext->onlyPartials) + $this->mergeOnlyPartials( + $dataContext->getRequiredPartialsAndRemoveTemporaryOnes($data, $dataContext->onlyPartials) ); } if ($dataContext->exceptPartials && $dataContext->exceptPartials->count() > 0) { - $this->mergeExceptResolvedPartials( - $dataContext->getResolvedPartialsAndRemoveTemporaryOnes($data, $dataContext->exceptPartials) + $this->mergeExceptPartials( + $dataContext->getRequiredPartialsAndRemoveTemporaryOnes($data, $dataContext->exceptPartials) ); } diff --git a/src/Support/Transformation/TransformationContextFactory.php b/src/Support/Transformation/TransformationContextFactory.php index f53ce0670..7b276ce16 100644 --- a/src/Support/Transformation/TransformationContextFactory.php +++ b/src/Support/Transformation/TransformationContextFactory.php @@ -38,13 +38,11 @@ public function get( $includePartials = null; if ($this->includePartials) { - $includePartials = new ResolvedPartialsCollection(); + $includePartials = new PartialsCollection(); foreach ($this->includePartials as $include) { - $resolved = $include->resolve($data); - - if ($resolved) { - $includePartials->attach($resolved); + if ($include->isRequired($data)) { + $includePartials->attach($include->reset()); } } } @@ -52,13 +50,11 @@ public function get( $excludePartials = null; if ($this->excludePartials) { - $excludePartials = new ResolvedPartialsCollection(); + $excludePartials = new PartialsCollection(); foreach ($this->excludePartials as $exclude) { - $resolved = $exclude->resolve($data); - - if ($resolved) { - $excludePartials->attach($resolved); + if ($exclude->isRequired($data)) { + $excludePartials->attach($exclude->reset()); } } } @@ -66,13 +62,11 @@ public function get( $onlyPartials = null; if ($this->onlyPartials) { - $onlyPartials = new ResolvedPartialsCollection(); + $onlyPartials = new PartialsCollection(); foreach ($this->onlyPartials as $only) { - $resolved = $only->resolve($data); - - if ($resolved) { - $onlyPartials->attach($resolved); + if ($only->isRequired($data)) { + $onlyPartials->attach($only->reset()); } } } @@ -80,13 +74,11 @@ public function get( $exceptPartials = null; if ($this->exceptPartials) { - $exceptPartials = new ResolvedPartialsCollection(); + $exceptPartials = new PartialsCollection(); foreach ($this->exceptPartials as $except) { - $resolved = $except->resolve($data); - - if ($resolved) { - $exceptPartials->attach($resolved); + if ($except->isRequired($data)) { + $exceptPartials->attach($except->reset()); } } } diff --git a/tests/Resolvers/VisibleDataFieldsResolverTest.php b/tests/Resolvers/VisibleDataFieldsResolverTest.php index 3bba1b625..82f8083bd 100644 --- a/tests/Resolvers/VisibleDataFieldsResolverTest.php +++ b/tests/Resolvers/VisibleDataFieldsResolverTest.php @@ -11,8 +11,8 @@ use Spatie\LaravelData\Support\DataConfig; use Spatie\LaravelData\Support\Lazy\ClosureLazy; use Spatie\LaravelData\Support\Lazy\InertiaLazy; -use Spatie\LaravelData\Support\Partials\ResolvedPartial; -use Spatie\LaravelData\Support\Partials\ResolvedPartialsCollection; +use Spatie\LaravelData\Support\Partials\Partial; +use Spatie\LaravelData\Support\Partials\PartialsCollection; use Spatie\LaravelData\Support\Partials\Segments\AllPartialSegment; use Spatie\LaravelData\Support\Partials\Segments\FieldsPartialSegment; use Spatie\LaravelData\Support\Partials\Segments\NestedPartialSegment; @@ -396,8 +396,8 @@ public static function instance(): self ->except('nested.a'), 'fields' => [ 'nested' => new TransformationContext( - exceptPartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('nested'), new FieldsPartialSegment(['a'])], 1) + exceptPartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('nested'), new FieldsPartialSegment(['a'])], pointer: 1) ), ), ], @@ -414,8 +414,8 @@ public static function instance(): self ->except('nested.{a,b}'), 'fields' => [ 'nested' => new TransformationContext( - exceptPartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('nested'), new FieldsPartialSegment(['a', 'b'])], 1) + exceptPartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('nested'), new FieldsPartialSegment(['a', 'b'])], pointer: 1) ), ), ], @@ -430,8 +430,8 @@ public static function instance(): self ->except('nested.*'), 'fields' => [ 'nested' => new TransformationContext( - exceptPartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('nested'), new AllPartialSegment()], 1) + exceptPartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('nested'), new AllPartialSegment()], pointer: 1) ), ), ], @@ -446,8 +446,8 @@ public static function instance(): self ->except('collection.string'), 'fields' => [ 'collection' => new TransformationContext( - exceptPartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('collection'), new FieldsPartialSegment(['string'])], 1) + exceptPartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('collection'), new FieldsPartialSegment(['string'])], pointer: 1) ), ), ], @@ -465,8 +465,8 @@ public static function instance(): self ->except('collection.{string,int}'), 'fields' => [ 'collection' => new TransformationContext( - exceptPartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('collection'), new FieldsPartialSegment(['string', 'int'])], 1) + exceptPartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('collection'), new FieldsPartialSegment(['string', 'int'])], pointer: 1) ), ), ], @@ -484,8 +484,8 @@ public static function instance(): self ->except('collection.*'), 'fields' => [ 'collection' => new TransformationContext( - exceptPartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('collection'), new AllPartialSegment()], 1) + exceptPartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('collection'), new AllPartialSegment()], pointer: 1) ), ), ], @@ -504,18 +504,18 @@ public static function instance(): self ->except('nested.a.string'), 'fields' => [ 'single' => new TransformationContext( - exceptPartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('single'), new FieldsPartialSegment(['string'])], 1) + exceptPartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('single'), new FieldsPartialSegment(['string'])], pointer: 1) ), ), 'collection' => new TransformationContext( - exceptPartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('collection'), new FieldsPartialSegment(['string'])], 1) + exceptPartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('collection'), new FieldsPartialSegment(['string'])], pointer: 1) ), ), 'nested' => new TransformationContext( - exceptPartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('nested'), new NestedPartialSegment('a'), new FieldsPartialSegment(['string'])], 1) + exceptPartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('nested'), new NestedPartialSegment('a'), new FieldsPartialSegment(['string'])], pointer: 1) ), ), ], @@ -605,8 +605,8 @@ public static function instance(): self ->only('nested.a'), 'fields' => [ 'nested' => new TransformationContext( - onlyPartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('nested'), new FieldsPartialSegment(['a'])], 1) + onlyPartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('nested'), new FieldsPartialSegment(['a'])], pointer: 1) ), ), ], @@ -622,8 +622,8 @@ public static function instance(): self ->only('nested.{a,b}'), 'fields' => [ 'nested' => new TransformationContext( - onlyPartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('nested'), new FieldsPartialSegment(['a', 'b'])], 1) + onlyPartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('nested'), new FieldsPartialSegment(['a', 'b'])], pointer: 1) ), ), ], @@ -640,8 +640,8 @@ public static function instance(): self ->only('nested.*'), 'fields' => [ 'nested' => new TransformationContext( - onlyPartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('nested'), new AllPartialSegment()], 1) + onlyPartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('nested'), new AllPartialSegment()], pointer: 1) ), ), ], @@ -658,8 +658,8 @@ public static function instance(): self ->only('collection.string'), 'fields' => [ 'collection' => new TransformationContext( - onlyPartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('collection'), new FieldsPartialSegment(['string'])], 1) + onlyPartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('collection'), new FieldsPartialSegment(['string'])], pointer: 1) ), ), ], @@ -676,8 +676,8 @@ public static function instance(): self ->only('collection.{string,int}'), 'fields' => [ 'collection' => new TransformationContext( - onlyPartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('collection'), new FieldsPartialSegment(['string', 'int'])], 1) + onlyPartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('collection'), new FieldsPartialSegment(['string', 'int'])], pointer: 1) ), ), ], @@ -694,8 +694,8 @@ public static function instance(): self ->only('collection.*'), 'fields' => [ 'collection' => new TransformationContext( - onlyPartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('collection'), new AllPartialSegment()], 1) + onlyPartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('collection'), new AllPartialSegment()], pointer: 1) ), ), ], @@ -715,18 +715,18 @@ public static function instance(): self 'fields' => [ 'string' => null, 'single' => new TransformationContext( - onlyPartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('single'), new FieldsPartialSegment(['string'])], 1) + onlyPartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('single'), new FieldsPartialSegment(['string'])], pointer: 1) ), ), 'collection' => new TransformationContext( - onlyPartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('collection'), new FieldsPartialSegment(['string'])], 1) + onlyPartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('collection'), new FieldsPartialSegment(['string'])], pointer: 1) ), ), 'nested' => new TransformationContext( - onlyPartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('nested'), new NestedPartialSegment('a'), new FieldsPartialSegment(['string'])], 1) + onlyPartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('nested'), new NestedPartialSegment('a'), new FieldsPartialSegment(['string'])], pointer: 1) ), ), ], @@ -852,20 +852,20 @@ public static function instance(bool $includeByDefault): self ->include('*'), 'fields' => [ 'single' => new TransformationContext( - includePartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new AllPartialSegment()], 3) + includePartials: PartialsCollection::create( + new Partial([new AllPartialSegment()], pointer: 3) ), ), 'int' => null, 'string' => null, 'nested' => new TransformationContext( - includePartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new AllPartialSegment()], 3) + includePartials: PartialsCollection::create( + new Partial([new AllPartialSegment()], pointer: 3) ), ), 'collection' => new TransformationContext( - includePartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new AllPartialSegment()], 3) + includePartials: PartialsCollection::create( + new Partial([new AllPartialSegment()], pointer: 3) ), ), ], @@ -890,8 +890,8 @@ public static function instance(bool $includeByDefault): self ->include('nested.a'), 'fields' => [ 'nested' => new TransformationContext( - includePartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('nested'), new FieldsPartialSegment(['a'])], 1) + includePartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('nested'), new FieldsPartialSegment(['a'])], pointer: 1) ), ), ], @@ -908,8 +908,8 @@ public static function instance(bool $includeByDefault): self ->include('nested.{a,b}'), 'fields' => [ 'nested' => new TransformationContext( - includePartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('nested'), new FieldsPartialSegment(['a', 'b'])], 1) + includePartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('nested'), new FieldsPartialSegment(['a', 'b'])], pointer: 1) ), ), ], @@ -927,8 +927,8 @@ public static function instance(bool $includeByDefault): self ->include('nested.*'), 'fields' => [ 'nested' => new TransformationContext( - includePartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('nested'), new AllPartialSegment()], 1) + includePartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('nested'), new AllPartialSegment()], pointer: 1) ), ), ], @@ -946,9 +946,9 @@ public static function instance(bool $includeByDefault): self ->include('nested.a.string', 'nested.b.int'), 'fields' => [ 'nested' => new TransformationContext( - includePartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('nested'), new NestedPartialSegment('a'), new FieldsPartialSegment(['string'])], 1), - new ResolvedPartial([new NestedPartialSegment('nested'), new NestedPartialSegment('b'), new FieldsPartialSegment(['int'])], 1) + includePartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('nested'), new NestedPartialSegment('a'), new FieldsPartialSegment(['string'])], pointer: 1), + new Partial([new NestedPartialSegment('nested'), new NestedPartialSegment('b'), new FieldsPartialSegment(['int'])], pointer: 1) ), ), ], @@ -966,8 +966,8 @@ public static function instance(bool $includeByDefault): self ->include('collection.string'), 'fields' => [ 'collection' => new TransformationContext( - includePartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('collection'), new FieldsPartialSegment(['string'])], 1) + includePartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('collection'), new FieldsPartialSegment(['string'])], pointer: 1) ), ), ], @@ -985,8 +985,8 @@ public static function instance(bool $includeByDefault): self ->include('collection.{string,int}'), 'fields' => [ 'collection' => new TransformationContext( - includePartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('collection'), new FieldsPartialSegment(['string', 'int'])], 1) + includePartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('collection'), new FieldsPartialSegment(['string', 'int'])], pointer: 1) ), ), ], @@ -1004,8 +1004,8 @@ public static function instance(bool $includeByDefault): self ->include('collection.*'), 'fields' => [ 'collection' => new TransformationContext( - includePartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('collection'), new AllPartialSegment()], 1) + includePartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('collection'), new AllPartialSegment()], pointer: 1) ), ), ], @@ -1025,18 +1025,18 @@ public static function instance(bool $includeByDefault): self 'fields' => [ 'string' => null, 'single' => new TransformationContext( - includePartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('single'), new FieldsPartialSegment(['string'])], 1) + includePartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('single'), new FieldsPartialSegment(['string'])], pointer: 1) ), ), 'collection' => new TransformationContext( - includePartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('collection'), new FieldsPartialSegment(['string'])], 1) + includePartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('collection'), new FieldsPartialSegment(['string'])], pointer: 1) ), ), 'nested' => new TransformationContext( - includePartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('nested'), new NestedPartialSegment('a'), new FieldsPartialSegment(['string'])], 1) + includePartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('nested'), new NestedPartialSegment('a'), new FieldsPartialSegment(['string'])], pointer: 1) ), ), ], @@ -1125,8 +1125,8 @@ public static function instance(bool $includeByDefault): self ->exclude('nested.a'), 'fields' => [ 'nested' => new TransformationContext( - excludePartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('nested'), new FieldsPartialSegment(['a'])], 1) + excludePartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('nested'), new FieldsPartialSegment(['a'])], pointer: 1) ), ), ], @@ -1143,8 +1143,8 @@ public static function instance(bool $includeByDefault): self ->exclude('nested.{a,b}'), 'fields' => [ 'nested' => new TransformationContext( - excludePartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('nested'), new FieldsPartialSegment(['a', 'b'])], 1) + excludePartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('nested'), new FieldsPartialSegment(['a', 'b'])], pointer: 1) ), ), ], @@ -1159,8 +1159,8 @@ public static function instance(bool $includeByDefault): self ->exclude('nested.*'), 'fields' => [ 'nested' => new TransformationContext( - excludePartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('nested'), new AllPartialSegment()], 1) + excludePartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('nested'), new AllPartialSegment()], pointer: 1) ), ), ], @@ -1175,9 +1175,9 @@ public static function instance(bool $includeByDefault): self ->exclude('nested.a.string', 'nested.b.int'), 'fields' => [ 'nested' => new TransformationContext( - excludePartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('nested'), new NestedPartialSegment('a'), new FieldsPartialSegment(['string'])], 1), - new ResolvedPartial([new NestedPartialSegment('nested'), new NestedPartialSegment('b'), new FieldsPartialSegment(['int'])], 1) + excludePartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('nested'), new NestedPartialSegment('a'), new FieldsPartialSegment(['string'])], pointer: 1), + new Partial([new NestedPartialSegment('nested'), new NestedPartialSegment('b'), new FieldsPartialSegment(['int'])], pointer: 1) ), ), ], @@ -1195,8 +1195,8 @@ public static function instance(bool $includeByDefault): self ->exclude('collection.string'), 'fields' => [ 'collection' => new TransformationContext( - excludePartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('collection'), new FieldsPartialSegment(['string'])], 1) + excludePartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('collection'), new FieldsPartialSegment(['string'])], pointer: 1) ), ), ], @@ -1214,8 +1214,8 @@ public static function instance(bool $includeByDefault): self ->exclude('collection.{string,int}'), 'fields' => [ 'collection' => new TransformationContext( - excludePartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('collection'), new FieldsPartialSegment(['string', 'int'])], 1) + excludePartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('collection'), new FieldsPartialSegment(['string', 'int'])], pointer: 1) ), ), ], @@ -1233,8 +1233,8 @@ public static function instance(bool $includeByDefault): self ->exclude('collection.*'), 'fields' => [ 'collection' => new TransformationContext( - excludePartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('collection'), new AllPartialSegment()], 1) + excludePartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('collection'), new AllPartialSegment()], pointer: 1) ), ), ], @@ -1253,18 +1253,18 @@ public static function instance(bool $includeByDefault): self ->exclude('nested.a.string'), 'fields' => [ 'single' => new TransformationContext( - excludePartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('single'), new FieldsPartialSegment(['string'])], 1) + excludePartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('single'), new FieldsPartialSegment(['string'])], pointer: 1) ), ), 'collection' => new TransformationContext( - excludePartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('collection'), new FieldsPartialSegment(['string'])], 1) + excludePartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('collection'), new FieldsPartialSegment(['string'])], pointer: 1) ), ), 'nested' => new TransformationContext( - excludePartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('nested'), new NestedPartialSegment('a'), new FieldsPartialSegment(['string'])], 1) + excludePartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('nested'), new NestedPartialSegment('a'), new FieldsPartialSegment(['string'])], pointer: 1) ), ), 'int' => null, @@ -1304,34 +1304,34 @@ public static function instance(bool $includeByDefault): self $expectedVisibleFields = [ 'single' => new TransformationContext( - excludePartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('single'), new FieldsPartialSegment(['int'])], 1) + excludePartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('single'), new FieldsPartialSegment(['int'])], pointer: 1) ), - onlyPartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('single'), new AllPartialSegment()], 1) + onlyPartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('single'), new AllPartialSegment()], pointer: 1) ), ), 'nested' => new TransformationContext( - includePartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('nested'), new NestedPartialSegment('a'), new FieldsPartialSegment(['string'])], 1), - new ResolvedPartial([new NestedPartialSegment('nested'), new NestedPartialSegment('b'), new AllPartialSegment()], 1) + includePartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('nested'), new NestedPartialSegment('a'), new FieldsPartialSegment(['string'])], pointer: 1), + new Partial([new NestedPartialSegment('nested'), new NestedPartialSegment('b'), new AllPartialSegment()], pointer: 1) ), - onlyPartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('nested'), new AllPartialSegment()], 1) + onlyPartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('nested'), new AllPartialSegment()], pointer: 1) ), - exceptPartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('nested'), new NestedPartialSegment('b'), new FieldsPartialSegment(['int'])], 1) + exceptPartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('nested'), new NestedPartialSegment('b'), new FieldsPartialSegment(['int'])], pointer: 1) ), ), 'collection' => new TransformationContext( - includePartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('collection'), new FieldsPartialSegment(['string'])], 1) + includePartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('collection'), new FieldsPartialSegment(['string'])], pointer: 1) ), - onlyPartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('collection'), new AllPartialSegment()], 1) + onlyPartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('collection'), new AllPartialSegment()], pointer: 1) ), - exceptPartials: ResolvedPartialsCollection::create( - new ResolvedPartial([new NestedPartialSegment('collection'), new FieldsPartialSegment(['int'])], 1) + exceptPartials: PartialsCollection::create( + new Partial([new NestedPartialSegment('collection'), new FieldsPartialSegment(['int'])], pointer: 1) ), ), ]; diff --git a/tests/Support/Partials/PartialTest.php b/tests/Support/Partials/PartialTest.php index 51bcf23ea..764cec1de 100644 --- a/tests/Support/Partials/PartialTest.php +++ b/tests/Support/Partials/PartialTest.php @@ -66,3 +66,144 @@ function invalidPartialsProvider(): Generator 'expected' => [new FieldsPartialSegment(['name', 'age'])], ]; } + +it('can use the pointer system when ending in a field', function () { + $partial = new Partial([ + new NestedPartialSegment('struct'), + new FieldsPartialSegment(['name', 'age']), + ]); + + expect($partial->isUndefined())->toBeFalse(); + expect($partial->isAll())->toBeFalse(); + expect($partial->getNested())->toBe('struct'); + expect($partial->getFields())->toBe(null); + + $partial->next(); // level 1 + + expect($partial->isUndefined())->toBeFalse(); + expect($partial->isAll())->toBeFalse(); + expect($partial->getNested())->toBe(null); + expect($partial->getFields())->toBe(['name', 'age']); + + $partial->rollbackWhenRequired(); // level 0 + + expect($partial->isUndefined())->toBeFalse(); + expect($partial->isAll())->toBeFalse(); + expect($partial->getNested())->toBe('struct'); + expect($partial->getFields())->toBe(null); + + $partial->next(); // level 1 + $partial->next(); // level 2 - non existing + + expect($partial->isUndefined())->toBeTrue(); + expect($partial->isAll())->toBeFalse(); + expect($partial->getNested())->toBe(null); + expect($partial->getFields())->toBe(null); + + $partial->rollbackWhenRequired(); // level 1 + + expect($partial->isUndefined())->toBeFalse(); + expect($partial->isAll())->toBeFalse(); + expect($partial->getNested())->toBe(null); + expect($partial->getFields())->toBe(['name', 'age']); + + $partial->next(); // level 2 - non existing + $partial->next(); // level 3 - non existing + + expect($partial->isUndefined())->toBeTrue(); + expect($partial->isAll())->toBeFalse(); + expect($partial->getNested())->toBe(null); + expect($partial->getFields())->toBe(null); + + $partial->rollbackWhenRequired(); // level 2 - non existing + + expect($partial->isUndefined())->toBeTrue(); + expect($partial->isAll())->toBeFalse(); + expect($partial->getNested())->toBe(null); + expect($partial->getFields())->toBe(null); + + $partial->rollbackWhenRequired(); // level 1 + + expect($partial->isUndefined())->toBeFalse(); + expect($partial->isAll())->toBeFalse(); + expect($partial->getNested())->toBe(null); + expect($partial->getFields())->toBe(['name', 'age']); + + $partial->rollbackWhenRequired(); // level 0 + + expect($partial->isUndefined())->toBeFalse(); + expect($partial->isAll())->toBeFalse(); + expect($partial->getNested())->toBe('struct'); + expect($partial->getFields())->toBe(null); +}); + +it('can use the pointer system when ending in an all', function () { + $partial = new Partial([ + new NestedPartialSegment('struct'), + new AllPartialSegment(), + ]); + + expect($partial->isUndefined())->toBeFalse(); + expect($partial->isAll())->toBeFalse(); + expect($partial->getNested())->toBe('struct'); + expect($partial->getFields())->toBe(null); + + $partial->next(); // level 1 + + expect($partial->isUndefined())->toBeFalse(); + expect($partial->isAll())->toBeTrue(); + expect($partial->getNested())->toBe(null); + expect($partial->getFields())->toBe(null); + + $partial->rollbackWhenRequired(); // level 0 + + expect($partial->isUndefined())->toBeFalse(); + expect($partial->isAll())->toBeFalse(); + expect($partial->getNested())->toBe('struct'); + expect($partial->getFields())->toBe(null); + + $partial->next(); // level 1 + $partial->next(); // level 2 - non existing + + expect($partial->isUndefined())->toBeFalse(); + expect($partial->isAll())->toBeTrue(); + expect($partial->getNested())->toBe(null); + expect($partial->getFields())->toBe(null); + + $partial->rollbackWhenRequired(); // level 1 + + expect($partial->isUndefined())->toBeFalse(); + expect($partial->isAll())->toBeTrue(); + expect($partial->getNested())->toBe(null); + expect($partial->getFields())->toBe(null); + + $partial->next(); // level 2 - non existing + $partial->next(); // level 3 - non existing + + expect($partial->isUndefined())->toBeFalse(); + expect($partial->isAll())->toBeTrue(); + expect($partial->getNested())->toBe(null); + expect($partial->getFields())->toBe(null); + + $partial->rollbackWhenRequired(); // level 2 - non existing + + expect($partial->isUndefined())->toBeFalse(); + expect($partial->isAll())->toBeTrue(); + expect($partial->getNested())->toBe(null); + expect($partial->getFields())->toBe(null); + + $partial->rollbackWhenRequired(); // level 1 + + expect($partial->isUndefined())->toBeFalse(); + expect($partial->isAll())->toBeTrue(); + expect($partial->getNested())->toBe(null); + expect($partial->getFields())->toBe(null); + + $partial->rollbackWhenRequired(); // level 0 + + expect($partial->isUndefined())->toBeFalse(); + expect($partial->isAll())->toBeFalse(); + expect($partial->getNested())->toBe('struct'); + expect($partial->getFields())->toBe(null); +}); + diff --git a/tests/Support/Partials/ResolvedPartialTest.php b/tests/Support/Partials/ResolvedPartialTest.php deleted file mode 100644 index 6cfd88741..000000000 --- a/tests/Support/Partials/ResolvedPartialTest.php +++ /dev/null @@ -1,146 +0,0 @@ -isUndefined())->toBeFalse(); - expect($partial->isAll())->toBeFalse(); - expect($partial->getNested())->toBe('struct'); - expect($partial->getFields())->toBe(null); - - $partial->next(); // level 1 - - expect($partial->isUndefined())->toBeFalse(); - expect($partial->isAll())->toBeFalse(); - expect($partial->getNested())->toBe(null); - expect($partial->getFields())->toBe(['name', 'age']); - - $partial->rollbackWhenRequired(); // level 0 - - expect($partial->isUndefined())->toBeFalse(); - expect($partial->isAll())->toBeFalse(); - expect($partial->getNested())->toBe('struct'); - expect($partial->getFields())->toBe(null); - - $partial->next(); // level 1 - $partial->next(); // level 2 - non existing - - expect($partial->isUndefined())->toBeTrue(); - expect($partial->isAll())->toBeFalse(); - expect($partial->getNested())->toBe(null); - expect($partial->getFields())->toBe(null); - - $partial->rollbackWhenRequired(); // level 1 - - expect($partial->isUndefined())->toBeFalse(); - expect($partial->isAll())->toBeFalse(); - expect($partial->getNested())->toBe(null); - expect($partial->getFields())->toBe(['name', 'age']); - - $partial->next(); // level 2 - non existing - $partial->next(); // level 3 - non existing - - expect($partial->isUndefined())->toBeTrue(); - expect($partial->isAll())->toBeFalse(); - expect($partial->getNested())->toBe(null); - expect($partial->getFields())->toBe(null); - - $partial->rollbackWhenRequired(); // level 2 - non existing - - expect($partial->isUndefined())->toBeTrue(); - expect($partial->isAll())->toBeFalse(); - expect($partial->getNested())->toBe(null); - expect($partial->getFields())->toBe(null); - - $partial->rollbackWhenRequired(); // level 1 - - expect($partial->isUndefined())->toBeFalse(); - expect($partial->isAll())->toBeFalse(); - expect($partial->getNested())->toBe(null); - expect($partial->getFields())->toBe(['name', 'age']); - - $partial->rollbackWhenRequired(); // level 0 - - expect($partial->isUndefined())->toBeFalse(); - expect($partial->isAll())->toBeFalse(); - expect($partial->getNested())->toBe('struct'); - expect($partial->getFields())->toBe(null); -}); - -it('can use the pointer system when ending in an all', function () { - $partial = new ResolvedPartial([ - new NestedPartialSegment('struct'), - new AllPartialSegment(), - ]); - - expect($partial->isUndefined())->toBeFalse(); - expect($partial->isAll())->toBeFalse(); - expect($partial->getNested())->toBe('struct'); - expect($partial->getFields())->toBe(null); - - $partial->next(); // level 1 - - expect($partial->isUndefined())->toBeFalse(); - expect($partial->isAll())->toBeTrue(); - expect($partial->getNested())->toBe(null); - expect($partial->getFields())->toBe(null); - - $partial->rollbackWhenRequired(); // level 0 - - expect($partial->isUndefined())->toBeFalse(); - expect($partial->isAll())->toBeFalse(); - expect($partial->getNested())->toBe('struct'); - expect($partial->getFields())->toBe(null); - - $partial->next(); // level 1 - $partial->next(); // level 2 - non existing - - expect($partial->isUndefined())->toBeFalse(); - expect($partial->isAll())->toBeTrue(); - expect($partial->getNested())->toBe(null); - expect($partial->getFields())->toBe(null); - - $partial->rollbackWhenRequired(); // level 1 - - expect($partial->isUndefined())->toBeFalse(); - expect($partial->isAll())->toBeTrue(); - expect($partial->getNested())->toBe(null); - expect($partial->getFields())->toBe(null); - - $partial->next(); // level 2 - non existing - $partial->next(); // level 3 - non existing - - expect($partial->isUndefined())->toBeFalse(); - expect($partial->isAll())->toBeTrue(); - expect($partial->getNested())->toBe(null); - expect($partial->getFields())->toBe(null); - - $partial->rollbackWhenRequired(); // level 2 - non existing - - expect($partial->isUndefined())->toBeFalse(); - expect($partial->isAll())->toBeTrue(); - expect($partial->getNested())->toBe(null); - expect($partial->getFields())->toBe(null); - - $partial->rollbackWhenRequired(); // level 1 - - expect($partial->isUndefined())->toBeFalse(); - expect($partial->isAll())->toBeTrue(); - expect($partial->getNested())->toBe(null); - expect($partial->getFields())->toBe(null); - - $partial->rollbackWhenRequired(); // level 0 - - expect($partial->isUndefined())->toBeFalse(); - expect($partial->isAll())->toBeFalse(); - expect($partial->getNested())->toBe('struct'); - expect($partial->getFields())->toBe(null); -});