CallQueuedHandlerTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace Illuminate\Tests\Integration\Queue;
  3. use Illuminate\Bus\Dispatcher;
  4. use Illuminate\Bus\Queueable;
  5. use Illuminate\Contracts\Queue\Job;
  6. use Illuminate\Database\Eloquent\ModelNotFoundException;
  7. use Illuminate\Queue\CallQueuedHandler;
  8. use Illuminate\Queue\Events\JobFailed;
  9. use Illuminate\Queue\InteractsWithQueue;
  10. use Illuminate\Support\Facades\Event;
  11. use Mockery as m;
  12. use Orchestra\Testbench\TestCase;
  13. class CallQueuedHandlerTest extends TestCase
  14. {
  15. protected function tearDown(): void
  16. {
  17. parent::tearDown();
  18. m::close();
  19. }
  20. public function testJobCanBeDispatched()
  21. {
  22. CallQueuedHandlerTestJob::$handled = false;
  23. $instance = new CallQueuedHandler(new Dispatcher($this->app), $this->app);
  24. $job = m::mock(Job::class);
  25. $job->shouldReceive('hasFailed')->andReturn(false);
  26. $job->shouldReceive('isDeleted')->andReturn(false);
  27. $job->shouldReceive('isReleased')->andReturn(false);
  28. $job->shouldReceive('isDeletedOrReleased')->andReturn(false);
  29. $job->shouldReceive('delete')->once();
  30. $instance->call($job, [
  31. 'command' => serialize(new CallQueuedHandlerTestJob),
  32. ]);
  33. $this->assertTrue(CallQueuedHandlerTestJob::$handled);
  34. }
  35. public function testJobCanBeDispatchedThroughMiddleware()
  36. {
  37. CallQueuedHandlerTestJobWithMiddleware::$handled = false;
  38. CallQueuedHandlerTestJobWithMiddleware::$middlewareCommand = null;
  39. $instance = new CallQueuedHandler(new Dispatcher($this->app), $this->app);
  40. $job = m::mock(Job::class);
  41. $job->shouldReceive('hasFailed')->andReturn(false);
  42. $job->shouldReceive('isDeleted')->andReturn(false);
  43. $job->shouldReceive('isReleased')->andReturn(false);
  44. $job->shouldReceive('isDeletedOrReleased')->andReturn(false);
  45. $job->shouldReceive('delete')->once();
  46. $instance->call($job, [
  47. 'command' => serialize($command = new CallQueuedHandlerTestJobWithMiddleware),
  48. ]);
  49. $this->assertInstanceOf(CallQueuedHandlerTestJobWithMiddleware::class, CallQueuedHandlerTestJobWithMiddleware::$middlewareCommand);
  50. $this->assertTrue(CallQueuedHandlerTestJobWithMiddleware::$handled);
  51. }
  52. public function testJobCanBeDispatchedThroughMiddlewareOnDispatch()
  53. {
  54. $_SERVER['__test.dispatchMiddleware'] = false;
  55. CallQueuedHandlerTestJobWithMiddleware::$handled = false;
  56. CallQueuedHandlerTestJobWithMiddleware::$middlewareCommand = null;
  57. $instance = new CallQueuedHandler(new Dispatcher($this->app), $this->app);
  58. $job = m::mock(Job::class);
  59. $job->shouldReceive('hasFailed')->andReturn(false);
  60. $job->shouldReceive('isDeleted')->andReturn(false);
  61. $job->shouldReceive('isReleased')->andReturn(false);
  62. $job->shouldReceive('isDeletedOrReleased')->andReturn(false);
  63. $job->shouldReceive('delete')->once();
  64. $command = $command = new CallQueuedHandlerTestJobWithMiddleware;
  65. $command->through([new TestJobMiddleware]);
  66. $instance->call($job, [
  67. 'command' => serialize($command),
  68. ]);
  69. $this->assertInstanceOf(CallQueuedHandlerTestJobWithMiddleware::class, CallQueuedHandlerTestJobWithMiddleware::$middlewareCommand);
  70. $this->assertTrue(CallQueuedHandlerTestJobWithMiddleware::$handled);
  71. $this->assertTrue($_SERVER['__test.dispatchMiddleware']);
  72. }
  73. public function testJobIsMarkedAsFailedIfModelNotFoundExceptionIsThrown()
  74. {
  75. $instance = new CallQueuedHandler(new Dispatcher($this->app), $this->app);
  76. $job = m::mock(Job::class);
  77. $job->shouldReceive('resolveName')->andReturn(__CLASS__);
  78. $job->shouldReceive('fail')->once();
  79. $instance->call($job, [
  80. 'command' => serialize(new CallQueuedHandlerExceptionThrower),
  81. ]);
  82. }
  83. public function testJobIsDeletedIfHasDeleteProperty()
  84. {
  85. Event::fake();
  86. $instance = new CallQueuedHandler(new Dispatcher($this->app), $this->app);
  87. $job = m::mock(Job::class);
  88. $job->shouldReceive('getConnectionName')->andReturn('connection');
  89. $job->shouldReceive('resolveName')->andReturn(CallQueuedHandlerExceptionThrower::class);
  90. $job->shouldReceive('markAsFailed')->never();
  91. $job->shouldReceive('isDeleted')->andReturn(false);
  92. $job->shouldReceive('delete')->once();
  93. $job->shouldReceive('failed')->never();
  94. $instance->call($job, [
  95. 'command' => serialize(new CallQueuedHandlerExceptionThrower),
  96. ]);
  97. Event::assertNotDispatched(JobFailed::class);
  98. }
  99. }
  100. class CallQueuedHandlerTestJob
  101. {
  102. use InteractsWithQueue;
  103. public static $handled = false;
  104. public function handle()
  105. {
  106. static::$handled = true;
  107. }
  108. }
  109. /** This exists to test that middleware can also be defined in base classes */
  110. abstract class AbstractCallQueuedHandlerTestJobWithMiddleware
  111. {
  112. public static $middlewareCommand;
  113. public function middleware()
  114. {
  115. return [
  116. new class
  117. {
  118. public function handle($command, $next)
  119. {
  120. AbstractCallQueuedHandlerTestJobWithMiddleware::$middlewareCommand = $command;
  121. return $next($command);
  122. }
  123. },
  124. ];
  125. }
  126. }
  127. class CallQueuedHandlerTestJobWithMiddleware extends AbstractCallQueuedHandlerTestJobWithMiddleware
  128. {
  129. use InteractsWithQueue, Queueable;
  130. public static $handled = false;
  131. public function handle()
  132. {
  133. static::$handled = true;
  134. }
  135. }
  136. class CallQueuedHandlerExceptionThrower
  137. {
  138. public $deleteWhenMissingModels = true;
  139. public function handle()
  140. {
  141. //
  142. }
  143. public function __wakeup()
  144. {
  145. throw new ModelNotFoundException('Foo');
  146. }
  147. }
  148. class TestJobMiddleware
  149. {
  150. public function handle($command, $next)
  151. {
  152. $_SERVER['__test.dispatchMiddleware'] = true;
  153. return $next($command);
  154. }
  155. }