-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27271 from tonysm/fix/queueable-collection-serial…
…ization [5.7] Fix QueueableCollection serialization of Eloquent Models when using Binary IDs
- Loading branch information
Showing
2 changed files
with
70 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
tests/Database/DatabaseEloquentCollectionQueueableTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.' | ||
); | ||
} | ||
} |