UniqueJobSchedulingTest.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace Illuminate\Tests\Integration\Console;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Console\Scheduling\Schedule;
  5. use Illuminate\Contracts\Queue\ShouldBeUnique;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Bus\Dispatchable;
  8. use Illuminate\Queue\InteractsWithQueue;
  9. use Illuminate\Support\Facades\Queue;
  10. use Orchestra\Testbench\TestCase;
  11. class UniqueJobSchedulingTest extends TestCase
  12. {
  13. public function testJobsPushedToQueue()
  14. {
  15. Queue::fake();
  16. $this->dispatch(
  17. TestJob::class,
  18. TestJob::class,
  19. TestJob::class,
  20. TestJob::class
  21. );
  22. Queue::assertPushed(TestJob::class, 4);
  23. }
  24. public function testUniqueJobsPushedToQueue()
  25. {
  26. Queue::fake();
  27. $this->dispatch(
  28. UniqueTestJob::class,
  29. UniqueTestJob::class,
  30. UniqueTestJob::class,
  31. UniqueTestJob::class
  32. );
  33. Queue::assertPushed(UniqueTestJob::class, 1);
  34. }
  35. private function dispatch(...$jobs)
  36. {
  37. /** @var \Illuminate\Console\Scheduling\Schedule $scheduler */
  38. $scheduler = $this->app->make(Schedule::class);
  39. foreach ($jobs as $job) {
  40. $scheduler->job($job)->name('')->everyMinute();
  41. }
  42. $events = $scheduler->events();
  43. foreach ($events as $event) {
  44. $event->run($this->app);
  45. }
  46. }
  47. }
  48. class TestJob implements ShouldQueue
  49. {
  50. use InteractsWithQueue, Queueable, Dispatchable;
  51. }
  52. class UniqueTestJob extends TestJob implements ShouldBeUnique
  53. {
  54. }