Skip to content

Commit

Permalink
Merge pull request #27271 from tonysm/fix/queueable-collection-serial…
Browse files Browse the repository at this point in the history
…ization

[5.7] Fix QueueableCollection serialization of Eloquent Models when using Binary IDs
  • Loading branch information
taylorotwell authored Jan 23, 2019
2 parents 18a6ec7 + f7d34f8 commit d58d20f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
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.'
);
}
}

0 comments on commit d58d20f

Please sign in to comment.