DatabaseEloquentCollectionQueueableTest.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace Illuminate\Tests\Database;
  3. use Illuminate\Database\Eloquent\Collection;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\Relations\Pivot;
  6. use Mockery;
  7. use PHPUnit\Framework\TestCase;
  8. class DatabaseEloquentCollectionQueueableTest extends TestCase
  9. {
  10. protected function tearDown(): void
  11. {
  12. Mockery::close();
  13. }
  14. public function testSerializesPivotsEntitiesId()
  15. {
  16. $spy = Mockery::spy(Pivot::class);
  17. $c = new Collection([$spy]);
  18. $c->getQueueableIds();
  19. $spy->shouldHaveReceived()
  20. ->getQueueableId()
  21. ->once();
  22. }
  23. public function testSerializesModelEntitiesById()
  24. {
  25. $spy = Mockery::spy(Model::class);
  26. $c = new Collection([$spy]);
  27. $c->getQueueableIds();
  28. $spy->shouldHaveReceived()
  29. ->getQueueableId()
  30. ->once();
  31. }
  32. /**
  33. * @throws \Exception
  34. */
  35. public function testJsonSerializationOfCollectionQueueableIdsWorks()
  36. {
  37. // When the ID of a Model is binary instead of int or string, the Collection
  38. // serialization + JSON encoding breaks because of UTF-8 issues. Encoding
  39. // of a QueueableCollection must favor QueueableEntity::queueableId().
  40. $mock = Mockery::mock(Model::class, [
  41. 'getKey' => random_bytes(10),
  42. 'getQueueableId' => 'mocked',
  43. ]);
  44. $c = new Collection([$mock]);
  45. $payload = [
  46. 'ids' => $c->getQueueableIds(),
  47. ];
  48. $this->assertNotFalse(
  49. json_encode($payload),
  50. 'EloquentCollection is not using the QueueableEntity::getQueueableId() method.'
  51. );
  52. }
  53. }