Skip to content

Commit

Permalink
Instantiate restored collection class
Browse files Browse the repository at this point in the history
  • Loading branch information
tw99 committed Jul 16, 2019
1 parent 489ae22 commit 130c5b7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ protected function restoreCollection($value)
}

$collection = $collection->keyBy->getKey();
$collectionClass = get_class($collection);

return new EloquentCollection(
return new $collectionClass(
collect($value->id)->map(function ($id) use ($collection) {
return $collection[$id];
})
})->all()
);
}

Expand Down
30 changes: 30 additions & 0 deletions tests/Integration/Queue/ModelSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,20 @@ public function test_it_serializes_a_collection_in_correct_order()
$this->assertEquals($unserialized->users->first()->email, 'taylor@laravel.com');
$this->assertEquals($unserialized->users->last()->email, 'mohamed@laravel.com');
}

public function test_it_can_unserialize_custom_collection()
{
ModelSerializationTestCustomUser::create(['email' => 'mohamed@laravel.com']);
ModelSerializationTestCustomUser::create(['email' => 'taylor@laravel.com']);

$serialized = serialize(new CollectionSerializationTestClass(
ModelSerializationTestCustomUser::all()
));

$unserialized = unserialize($serialized);

$this->assertInstanceOf(ModelSerializationTestCustomUserCollection::class, $unserialized->users);
}
}

class ModelSerializationTestUser extends Model
Expand All @@ -244,6 +258,22 @@ class ModelSerializationTestUser extends Model
public $timestamps = false;
}

class ModelSerializationTestCustomUserCollection extends Collection
{
}

class ModelSerializationTestCustomUser extends Model
{
public $table = 'users';
public $guarded = ['id'];
public $timestamps = false;

public function newCollection(array $models = [])
{
return new ModelSerializationTestCustomUserCollection($models);
}
}

class Order extends Model
{
public $guarded = ['id'];
Expand Down

0 comments on commit 130c5b7

Please sign in to comment.