BusDispatcherTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace Illuminate\Tests\Bus;
  3. use Illuminate\Bus\Dispatcher;
  4. use Illuminate\Bus\Queueable;
  5. use Illuminate\Config\Repository as Config;
  6. use Illuminate\Container\Container;
  7. use Illuminate\Contracts\Queue\Queue;
  8. use Illuminate\Contracts\Queue\ShouldQueue;
  9. use Illuminate\Queue\InteractsWithQueue;
  10. use Mockery as m;
  11. use PHPUnit\Framework\TestCase;
  12. use RuntimeException;
  13. class BusDispatcherTest extends TestCase
  14. {
  15. protected function tearDown(): void
  16. {
  17. m::close();
  18. }
  19. public function testCommandsThatShouldQueueIsQueued()
  20. {
  21. $container = new Container;
  22. $dispatcher = new Dispatcher($container, function () {
  23. $mock = m::mock(Queue::class);
  24. $mock->shouldReceive('push')->once();
  25. return $mock;
  26. });
  27. $dispatcher->dispatch(m::mock(ShouldQueue::class));
  28. }
  29. public function testCommandsThatShouldQueueIsQueuedUsingCustomHandler()
  30. {
  31. $container = new Container;
  32. $dispatcher = new Dispatcher($container, function () {
  33. $mock = m::mock(Queue::class);
  34. $mock->shouldReceive('push')->once();
  35. return $mock;
  36. });
  37. $dispatcher->dispatch(new BusDispatcherTestCustomQueueCommand);
  38. }
  39. public function testCommandsThatShouldQueueIsQueuedUsingCustomQueueAndDelay()
  40. {
  41. $container = new Container;
  42. $dispatcher = new Dispatcher($container, function () {
  43. $mock = m::mock(Queue::class);
  44. $mock->shouldReceive('laterOn')->once()->with('foo', 10, m::type(BusDispatcherTestSpecificQueueAndDelayCommand::class));
  45. return $mock;
  46. });
  47. $dispatcher->dispatch(new BusDispatcherTestSpecificQueueAndDelayCommand);
  48. }
  49. public function testDispatchNowShouldNeverQueue()
  50. {
  51. $container = new Container;
  52. $mock = m::mock(Queue::class);
  53. $mock->shouldReceive('push')->never();
  54. $dispatcher = new Dispatcher($container, function () use ($mock) {
  55. return $mock;
  56. });
  57. $dispatcher->dispatch(new BusDispatcherBasicCommand);
  58. }
  59. public function testDispatcherCanDispatchStandAloneHandler()
  60. {
  61. $container = new Container;
  62. $mock = m::mock(Queue::class);
  63. $dispatcher = new Dispatcher($container, function () use ($mock) {
  64. return $mock;
  65. });
  66. $dispatcher->map([StandAloneCommand::class => StandAloneHandler::class]);
  67. $response = $dispatcher->dispatch(new StandAloneCommand);
  68. $this->assertInstanceOf(StandAloneCommand::class, $response);
  69. }
  70. public function testOnConnectionOnJobWhenDispatching()
  71. {
  72. $container = new Container;
  73. $container->singleton('config', function () {
  74. return new Config([
  75. 'queue' => [
  76. 'default' => 'null',
  77. 'connections' => [
  78. 'null' => ['driver' => 'null'],
  79. ],
  80. ],
  81. ]);
  82. });
  83. $dispatcher = new Dispatcher($container, function () {
  84. $mock = m::mock(Queue::class);
  85. $mock->shouldReceive('push')->once();
  86. return $mock;
  87. });
  88. $job = (new ShouldNotBeDispatched)->onConnection('null');
  89. $dispatcher->dispatch($job);
  90. }
  91. }
  92. class BusInjectionStub
  93. {
  94. //
  95. }
  96. class BusDispatcherBasicCommand
  97. {
  98. public $name;
  99. public function __construct($name = null)
  100. {
  101. $this->name = $name;
  102. }
  103. public function handle(BusInjectionStub $stub)
  104. {
  105. //
  106. }
  107. }
  108. class BusDispatcherTestCustomQueueCommand implements ShouldQueue
  109. {
  110. public function queue($queue, $command)
  111. {
  112. $queue->push($command);
  113. }
  114. }
  115. class BusDispatcherTestSpecificQueueAndDelayCommand implements ShouldQueue
  116. {
  117. public $queue = 'foo';
  118. public $delay = 10;
  119. }
  120. class StandAloneCommand
  121. {
  122. //
  123. }
  124. class StandAloneHandler
  125. {
  126. public function handle(StandAloneCommand $command)
  127. {
  128. return $command;
  129. }
  130. }
  131. class ShouldNotBeDispatched implements ShouldQueue
  132. {
  133. use InteractsWithQueue, Queueable;
  134. public function handle()
  135. {
  136. throw new RuntimeException('This should not be run');
  137. }
  138. }