JobEncryptionTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace Illuminate\Tests\Integration\Queue;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Encryption\DecryptException;
  5. use Illuminate\Contracts\Queue\ShouldBeEncrypted;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Database\Schema\Blueprint;
  8. use Illuminate\Foundation\Bus\Dispatchable;
  9. use Illuminate\Support\Facades\Bus;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Support\Facades\Queue;
  12. use Illuminate\Support\Facades\Schema;
  13. use Illuminate\Support\Str;
  14. use Illuminate\Tests\Integration\Database\DatabaseTestCase;
  15. class JobEncryptionTest extends DatabaseTestCase
  16. {
  17. protected function getEnvironmentSetUp($app)
  18. {
  19. parent::getEnvironmentSetUp($app);
  20. $app['config']->set('app.key', Str::random(32));
  21. $app['config']->set('queue.default', 'database');
  22. }
  23. protected function setUp(): void
  24. {
  25. parent::setUp();
  26. Schema::create('jobs', function (Blueprint $table) {
  27. $table->bigIncrements('id');
  28. $table->string('queue')->index();
  29. $table->longText('payload');
  30. $table->unsignedTinyInteger('attempts');
  31. $table->unsignedInteger('reserved_at')->nullable();
  32. $table->unsignedInteger('available_at');
  33. $table->unsignedInteger('created_at');
  34. });
  35. }
  36. protected function tearDown(): void
  37. {
  38. JobEncryptionTestEncryptedJob::$ran = false;
  39. JobEncryptionTestNonEncryptedJob::$ran = false;
  40. parent::tearDown();
  41. }
  42. public function testEncryptedJobPayloadIsStoredEncrypted()
  43. {
  44. Bus::dispatch(new JobEncryptionTestEncryptedJob);
  45. $this->assertNotEmpty(
  46. decrypt(json_decode(DB::table('jobs')->first()->payload)->data->command)
  47. );
  48. }
  49. public function testNonEncryptedJobPayloadIsStoredRaw()
  50. {
  51. Bus::dispatch(new JobEncryptionTestNonEncryptedJob);
  52. $this->expectException(DecryptException::class);
  53. $this->expectExceptionMessage('The payload is invalid');
  54. $this->assertInstanceOf(JobEncryptionTestNonEncryptedJob::class,
  55. unserialize(json_decode(DB::table('jobs')->first()->payload)->data->command)
  56. );
  57. decrypt(json_decode(DB::table('jobs')->first()->payload)->data->command);
  58. }
  59. public function testQueueCanProcessEncryptedJob()
  60. {
  61. Bus::dispatch(new JobEncryptionTestEncryptedJob);
  62. Queue::pop()->fire();
  63. $this->assertTrue(JobEncryptionTestEncryptedJob::$ran);
  64. }
  65. public function testQueueCanProcessUnEncryptedJob()
  66. {
  67. Bus::dispatch(new JobEncryptionTestNonEncryptedJob);
  68. Queue::pop()->fire();
  69. $this->assertTrue(JobEncryptionTestNonEncryptedJob::$ran);
  70. }
  71. }
  72. class JobEncryptionTestEncryptedJob implements ShouldQueue, ShouldBeEncrypted
  73. {
  74. use Dispatchable, Queueable;
  75. public static $ran = false;
  76. public function handle()
  77. {
  78. static::$ran = true;
  79. }
  80. }
  81. class JobEncryptionTestNonEncryptedJob implements ShouldQueue
  82. {
  83. use Dispatchable, Queueable;
  84. public static $ran = false;
  85. public function handle()
  86. {
  87. static::$ran = true;
  88. }
  89. }