forked from laravel/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBusPendingBatchTest.php
More file actions
82 lines (60 loc) · 2.57 KB
/
Copy pathBusPendingBatchTest.php
File metadata and controls
82 lines (60 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
namespace Illuminate\Tests\Bus;
use Illuminate\Bus\Batch;
use Illuminate\Bus\Batchable;
use Illuminate\Bus\BatchRepository;
use Illuminate\Bus\PendingBatch;
use Illuminate\Container\Container;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Collection;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use stdClass;
class BusPendingBatchTest extends TestCase
{
protected function tearDown(): void
{
m::close();
}
public function test_pending_batch_may_be_configured_and_dispatched()
{
$container = new Container;
$eventDispatcher = m::mock(Dispatcher::class);
$eventDispatcher->shouldReceive('dispatch')->once();
$container->instance(Dispatcher::class, $eventDispatcher);
$pendingBatch = new PendingBatch($container, new Collection([$job = new class {
use Batchable;
}]));
$pendingBatch = $pendingBatch->then(function () {
//
})->catch(function () {
//
})->allowFailures()->onConnection('test-connection')->onQueue('test-queue');
$this->assertSame('test-connection', $pendingBatch->connection());
$this->assertSame('test-queue', $pendingBatch->queue());
$this->assertCount(1, $pendingBatch->thenCallbacks());
$this->assertCount(1, $pendingBatch->catchCallbacks());
$repository = m::mock(BatchRepository::class);
$repository->shouldReceive('store')->once()->with($pendingBatch)->andReturn($batch = m::mock(stdClass::class));
$batch->shouldReceive('add')->once()->with(m::type(Collection::class))->andReturn($batch = m::mock(Batch::class));
$container->instance(BatchRepository::class, $repository);
$pendingBatch->dispatch();
}
public function test_batch_is_deleted_from_storage_if_exception_thrown_during_batching()
{
$this->expectException(RuntimeException::class);
$container = new Container;
$pendingBatch = new PendingBatch($container, new Collection([new class {
}]));
$repository = m::mock(BatchRepository::class);
$repository->shouldReceive('store')->once()->with($pendingBatch)->andReturn($batch = m::mock(stdClass::class));
$batch->id = 'test-id';
$batch->shouldReceive('add')->once()->andReturnUsing(function () {
throw new RuntimeException('Failed to add jobs...');
});
$repository->shouldReceive('delete')->once()->with('test-id');
$container->instance(BatchRepository::class, $repository);
$pendingBatch->dispatch();
}
}