Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.7] Fix QueueableCollection serialization of Eloquent Models when using Binary IDs #27271

Merged
merged 2 commits into from
Jan 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Contracts\Queue\QueueableEntity;
use Illuminate\Contracts\Queue\QueueableCollection;
use Illuminate\Support\Collection as BaseCollection;

Expand Down Expand Up @@ -541,7 +541,7 @@ public function getQueueableIds()
return [];
}

return $this->first() instanceof Pivot
return $this->first() instanceof QueueableEntity
? $this->map->getQueueableId()->all()
: $this->modelKeys();
}
Expand Down
68 changes: 68 additions & 0 deletions tests/Database/DatabaseEloquentCollectionQueueableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Illuminate\Tests\Database;

use Mockery;
use PHPUnit\Framework\TestCase;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\Pivot;

class DatabaseEloquentCollectionQueueableTest extends TestCase
{
public function tearDown()
{
Mockery::close();
}

public function testSerializesPivotsEntitiesId()
{
$spy = Mockery::spy(Pivot::class);

$c = new Collection([$spy]);

$c->getQueueableIds();

$spy->shouldHaveReceived()
->getQueueableId()
->once();
}

public function testSerializesModelEntitiesById()
{
$spy = Mockery::spy(Model::class);

$c = new Collection([$spy]);

$c->getQueueableIds();

$spy->shouldHaveReceived()
->getQueueableId()
->once();
}

/**
* @throws \Exception
*/
public function testJsonSerializationOfCollectionQueueableIdsWorks()
{
// When the ID of a Model is binary instead of int or string, the Collection
// serialization + JSON encoding breaks because of UTF-8 issues. Encoding
// of a QueueableCollection must favor QueueableEntity::queueableId().
$mock = Mockery::mock(Model::class, [
'getKey' => random_bytes(10),
'getQueueableId' => 'mocked',
]);

$c = new Collection([$mock]);

$payload = [
'ids' => $c->getQueueableIds(),
];

$this->assertTrue(
json_encode($payload) !== false,
'EloquentCollection is not using the QueueableEntity::getQueueableId() method.'
);
}
}