BusPendingBatchTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace Illuminate\Tests\Bus;
  3. use Illuminate\Bus\Batch;
  4. use Illuminate\Bus\Batchable;
  5. use Illuminate\Bus\BatchRepository;
  6. use Illuminate\Bus\PendingBatch;
  7. use Illuminate\Container\Container;
  8. use Illuminate\Contracts\Events\Dispatcher;
  9. use Illuminate\Support\Collection;
  10. use Mockery as m;
  11. use PHPUnit\Framework\TestCase;
  12. use RuntimeException;
  13. use stdClass;
  14. class BusPendingBatchTest extends TestCase
  15. {
  16. protected function tearDown(): void
  17. {
  18. m::close();
  19. }
  20. public function test_pending_batch_may_be_configured_and_dispatched()
  21. {
  22. $container = new Container;
  23. $eventDispatcher = m::mock(Dispatcher::class);
  24. $eventDispatcher->shouldReceive('dispatch')->once();
  25. $container->instance(Dispatcher::class, $eventDispatcher);
  26. $job = new class
  27. {
  28. use Batchable;
  29. };
  30. $pendingBatch = new PendingBatch($container, new Collection([$job]));
  31. $pendingBatch = $pendingBatch->then(function () {
  32. //
  33. })->catch(function () {
  34. //
  35. })->allowFailures()->onConnection('test-connection')->onQueue('test-queue')->withOption('extra-option', 123);
  36. $this->assertSame('test-connection', $pendingBatch->connection());
  37. $this->assertSame('test-queue', $pendingBatch->queue());
  38. $this->assertCount(1, $pendingBatch->thenCallbacks());
  39. $this->assertCount(1, $pendingBatch->catchCallbacks());
  40. $this->assertArrayHasKey('extra-option', $pendingBatch->options);
  41. $this->assertSame(123, $pendingBatch->options['extra-option']);
  42. $repository = m::mock(BatchRepository::class);
  43. $repository->shouldReceive('store')->once()->with($pendingBatch)->andReturn($batch = m::mock(stdClass::class));
  44. $batch->shouldReceive('add')->once()->with(m::type(Collection::class))->andReturn($batch = m::mock(Batch::class));
  45. $container->instance(BatchRepository::class, $repository);
  46. $pendingBatch->dispatch();
  47. }
  48. public function test_batch_is_deleted_from_storage_if_exception_thrown_during_batching()
  49. {
  50. $this->expectException(RuntimeException::class);
  51. $container = new Container;
  52. $job = new class
  53. {
  54. };
  55. $pendingBatch = new PendingBatch($container, new Collection([$job]));
  56. $repository = m::mock(BatchRepository::class);
  57. $repository->shouldReceive('store')->once()->with($pendingBatch)->andReturn($batch = m::mock(stdClass::class));
  58. $batch->id = 'test-id';
  59. $batch->shouldReceive('add')->once()->andReturnUsing(function () {
  60. throw new RuntimeException('Failed to add jobs...');
  61. });
  62. $repository->shouldReceive('delete')->once()->with('test-id');
  63. $container->instance(BatchRepository::class, $repository);
  64. $pendingBatch->dispatch();
  65. }
  66. }