Skip to content

Commit

Permalink
Copy partial trees when using array access on a collection
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Sep 15, 2023
1 parent 8f8e6f3 commit bc33d56
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
9 changes: 8 additions & 1 deletion src/DataCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Spatie\LaravelData\Concerns\WrappableData;
use Spatie\LaravelData\Contracts\BaseData;
use Spatie\LaravelData\Contracts\DataCollectable;
use Spatie\LaravelData\Contracts\IncludeableData as IncludeableDataContract;
use Spatie\LaravelData\Exceptions\CannotCastData;
use Spatie\LaravelData\Exceptions\InvalidDataCollectionOperation;
use Spatie\LaravelData\Support\EloquentCasts\DataCollectionEloquentCast;
Expand Down Expand Up @@ -101,7 +102,13 @@ public function offsetGet($offset): mixed
throw InvalidDataCollectionOperation::create();
}

return $this->items->offsetGet($offset);
$data = $this->items->offsetGet($offset);

if($data instanceof IncludeableDataContract){
$data->withPartialTrees($this->getPartialTrees());
}

return $data;
}

/**
Expand Down
32 changes: 22 additions & 10 deletions tests/DataCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
use Spatie\LaravelData\DataCollection;
use Spatie\LaravelData\PaginatedDataCollection;
use Spatie\LaravelData\Support\PartialTrees;
use Spatie\LaravelData\Tests\Fakes\ComplicatedData;
use Spatie\LaravelData\Tests\Fakes\DataCollections\CustomDataCollection;
use Spatie\LaravelData\Tests\Fakes\DataCollections\CustomPaginatedDataCollection;
use Spatie\LaravelData\Tests\Fakes\DefaultLazyData;
use Spatie\LaravelData\Tests\Fakes\LazyData;
use Spatie\LaravelData\Tests\Fakes\MultiData;
use Spatie\LaravelData\Tests\Fakes\SimpleData;

use function Spatie\Snapshots\assertMatchesJsonSnapshot;
use function Spatie\Snapshots\assertMatchesSnapshot;

Expand Down Expand Up @@ -116,7 +117,11 @@
expect($letters)->toMatchArray(['A', 'B', 'C', 'D']);
});

it('has array access', function (DataCollection $collection) {
it('has array access', function () {
$collection = SimpleData::collection([
'A', 'B', SimpleData::from('C'), SimpleData::from('D'),
]);

// Count
expect($collection)->toHaveCount(4);

Expand All @@ -126,9 +131,9 @@
expect(empty($collection[5]))->toBeTrue();

// Offset get
expect(SimpleData::from('A'))->toEqual($collection[0]);
expect($collection[0])->toEqual(SimpleData::from('A')->withPartialTrees(new PartialTrees()));

expect(SimpleData::from('D'))->toEqual($collection[3]);
expect($collection[3])->toEqual(SimpleData::from('D')->withPartialTrees(new PartialTrees()));

if ($collection->items() instanceof AbstractPaginator || $collection->items() instanceof CursorPaginator) {
return;
Expand All @@ -138,17 +143,24 @@
$collection[2] = 'And now something completely different';
$collection[4] = 'E';

expect(
SimpleData::from('And now something completely different')
)
->toEqual($collection[2]);
expect(SimpleData::from('E'))->toEqual($collection[4]);
expect($collection[2])->toEqual(
SimpleData::from('And now something completely different')->withPartialTrees(new PartialTrees())
);
expect($collection[4])->toEqual(SimpleData::from('E')->withPartialTrees(new PartialTrees()));

// Offset unset
unset($collection[4]);

expect($collection)->toHaveCount(4);
})->with('array-access-collections');
});

it('has array access and will replicate partialtrees', function () {
$collection = MultiData::collection([
new MultiData('first', 'second'),
])->only('second');

expect($collection[0]->toArray())->toEqual(['second' => 'second']);
});

it('can dynamically include data based upon the request', function () {
LazyData::$allowedIncludes = [''];
Expand Down
2 changes: 1 addition & 1 deletion tests/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
use Spatie\LaravelData\Tests\Fakes\UnionData;
use Spatie\LaravelData\Transformers\DateTimeInterfaceTransformer;
use Spatie\LaravelData\WithData;

use function Spatie\Snapshots\assertMatchesSnapshot;

it('can create a resource', function () {
Expand Down Expand Up @@ -2461,3 +2460,4 @@ public function __construct(
yield 'no params' => [[], 'Could not create `Spatie\LaravelData\Tests\Fakes\MultiData`: the constructor requires 2 parameters, 0 given. Parameters missing: first, second.'],
yield 'one param' => [['first' => 'First'], 'Could not create `Spatie\LaravelData\Tests\Fakes\MultiData`: the constructor requires 2 parameters, 1 given. Parameters given: first. Parameters missing: second.'],
]);

13 changes: 0 additions & 13 deletions tests/Datasets/DataCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,6 @@

use Spatie\LaravelData\Tests\Fakes\SimpleData;

dataset('array-access-collections', function () {
yield "array" => [
fn () => SimpleData::collection([
'A', 'B', SimpleData::from('C'), SimpleData::from('D'),
]),
];

yield "collection" => [
fn () => SimpleData::collection([
'A', 'B', SimpleData::from('C'), SimpleData::from('D'),
]),
];
});

dataset('collection-operations', function () {
yield [
Expand Down

0 comments on commit bc33d56

Please sign in to comment.