JobSchedulingTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace Illuminate\Tests\Integration\Console;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Console\Scheduling\Schedule;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Queue\InteractsWithQueue;
  7. use Illuminate\Support\Facades\Queue;
  8. use Orchestra\Testbench\TestCase;
  9. class JobSchedulingTest extends TestCase
  10. {
  11. public function testJobQueuingRespectsJobQueue()
  12. {
  13. Queue::fake();
  14. /** @var \Illuminate\Console\Scheduling\Schedule $scheduler */
  15. $scheduler = $this->app->make(Schedule::class);
  16. // all job names were set to an empty string so that the registered shutdown function in CallbackEvent does nothing
  17. // that function would in this test environment fire after everything was run, including the tearDown method
  18. // (which flushes the entire container) which would then result in a ReflectionException when the container would try
  19. // to resolve the config service (which is needed in order to resolve the cache store for the mutex that is being cleared)
  20. $scheduler->job(JobWithDefaultQueue::class)->name('')->everyMinute();
  21. $scheduler->job(JobWithDefaultQueueTwo::class, 'another-queue')->name('')->everyMinute();
  22. $scheduler->job(JobWithoutDefaultQueue::class)->name('')->everyMinute();
  23. $events = $scheduler->events();
  24. foreach ($events as $event) {
  25. $event->run($this->app);
  26. }
  27. Queue::assertPushedOn('test-queue', JobWithDefaultQueue::class);
  28. Queue::assertPushedOn('another-queue', JobWithDefaultQueueTwo::class);
  29. Queue::assertPushedOn(null, JobWithoutDefaultQueue::class);
  30. $this->assertTrue(Queue::pushed(JobWithDefaultQueueTwo::class, function ($job, $pushedQueue) {
  31. return $pushedQueue === 'test-queue-two';
  32. })->isEmpty());
  33. }
  34. public function testJobQueuingRespectsJobConnection()
  35. {
  36. Queue::fake();
  37. /** @var \Illuminate\Console\Scheduling\Schedule $scheduler */
  38. $scheduler = $this->app->make(Schedule::class);
  39. // all job names were set to an empty string so that the registered shutdown function in CallbackEvent does nothing
  40. // that function would in this test environment fire after everything was run, including the tearDown method
  41. // (which flushes the entire container) which would then result in a ReflectionException when the container would try
  42. // to resolve the config service (which is needed in order to resolve the cache store for the mutex that is being cleared)
  43. $scheduler->job(JobWithDefaultConnection::class)->name('')->everyMinute();
  44. $scheduler->job(JobWithDefaultConnection::class, null, 'foo')->name('')->everyMinute();
  45. $scheduler->job(JobWithoutDefaultConnection::class)->name('')->everyMinute();
  46. $scheduler->job(JobWithoutDefaultConnection::class, null, 'bar')->name('')->everyMinute();
  47. $events = $scheduler->events();
  48. foreach ($events as $event) {
  49. $event->run($this->app);
  50. }
  51. $this->assertSame(1, Queue::pushed(JobWithDefaultConnection::class, function (JobWithDefaultConnection $job, $pushedQueue) {
  52. return $job->connection === 'test-connection';
  53. })->count());
  54. $this->assertSame(1, Queue::pushed(JobWithDefaultConnection::class, function (JobWithDefaultConnection $job, $pushedQueue) {
  55. return $job->connection === 'foo';
  56. })->count());
  57. $this->assertSame(0, Queue::pushed(JobWithDefaultConnection::class, function (JobWithDefaultConnection $job, $pushedQueue) {
  58. return $job->connection === null;
  59. })->count());
  60. $this->assertSame(1, Queue::pushed(JobWithoutDefaultConnection::class, function (JobWithoutDefaultConnection $job, $pushedQueue) {
  61. return $job->connection === null;
  62. })->count());
  63. $this->assertSame(1, Queue::pushed(JobWithoutDefaultConnection::class, function (JobWithoutDefaultConnection $job, $pushedQueue) {
  64. return $job->connection === 'bar';
  65. })->count());
  66. }
  67. }
  68. class JobWithDefaultQueue implements ShouldQueue
  69. {
  70. use Queueable, InteractsWithQueue;
  71. public function __construct()
  72. {
  73. $this->onQueue('test-queue');
  74. }
  75. }
  76. class JobWithDefaultQueueTwo implements ShouldQueue
  77. {
  78. use Queueable, InteractsWithQueue;
  79. public function __construct()
  80. {
  81. $this->onQueue('test-queue-two');
  82. }
  83. }
  84. class JobWithoutDefaultQueue implements ShouldQueue
  85. {
  86. use Queueable, InteractsWithQueue;
  87. }
  88. class JobWithDefaultConnection implements ShouldQueue
  89. {
  90. use Queueable, InteractsWithQueue;
  91. public function __construct()
  92. {
  93. $this->onConnection('test-connection');
  94. }
  95. }
  96. class JobWithoutDefaultConnection implements ShouldQueue
  97. {
  98. use Queueable, InteractsWithQueue;
  99. }