Skip to content

Batch Assertions

Use the Execution facade to assert batch dispatches:

php
use Havn\Executable\Testing\Facades\Execution;

it("dispatches a batch of imports", function () {
    Bus::fake();

    $service->importAllStores();

    Execution::assertBatchCount(1);

    Execution::assertBatched(function (PushedBatch $batch) {
        return $batch->hasCount(3) && $batch->contains(ImportProducts::class);
    });
});

it("configures the batch correctly", function () {
    Bus::fake();

    $service->importAllStores();

    // Boolean style: return true/false
    Execution::assertBatched(function (PushedBatch $batch) {
        return $batch->hasName("daily-import") && $batch->isOnQueue("imports") && $batch->allowsFailures();
    });
});

Chainable Assert Methods

Use chainable assert* methods for descriptive failure messages. This only works when exactly one batch is dispatched, because assertBatched iterates all batches looking for a match. If an assert throws, it stops checking and fails immediately.

php
it("configures the batch correctly", function () {
    Bus::fake();

    $service->importAllStores();

    Execution::assertBatched(function (PushedBatch $batch) {
        return (bool) $batch
            ->assertHasName("daily-import")
            ->assertIsOnQueue("imports")
            ->assertAllowsFailures()
            ->assertHasCount(3)
            ->assertContains(ImportProducts::class)
            ->assertContainsThenCallback()
            ->assertContainsCatchCallback();
    });
});

it("dispatches no batches when there are no stores", function () {
    Bus::fake();

    $service->importAllStores(); // no stores exist

    Execution::assertNothingBatched();
});

Exact Contents

Use containsExactly() to verify a batch contains exactly the given executables, no more, no less:

php
Execution::assertBatched(function (PushedBatch $batch) {
    return $batch->containsExactly([ImportProducts::class, ProcessOrder::class]);
});

assertBatched() also accepts an array directly as a shorthand:

php
Execution::assertBatched([ImportProducts::class, ProcessOrder::class]);

Retrieving Batches

Use Execution::batched() to get a Collection of PushedBatch objects for programmatic inspection:

php
$batches = Execution::batched();
$filtered = Execution::batched(fn(PushedBatch $b) => $b->hasName("daily"));

expect($filtered)->toHaveCount(1);

Available PushedBatch Checks

CheckAssert variantWhat it checks
hasName(string)assertHasName()Batch name
hasCount(int)assertHasCount()Number of jobs in batch
isOnQueue(string)assertIsOnQueue()Queue name
isOnConnection(string)assertIsOnConnection()Connection name
allowsFailures()assertAllowsFailures()Whether failures are allowed
contains(string|Closure)assertContains()Batch contains a specific job
containsExactly(array)assertContainsExactly()Exact batch contents
containsProgressCallback()assertContainsProgressCallback()Has a progress callback
containsBeforeCallback()assertContainsBeforeCallback()Has a before callback
containsThenCallback()assertContainsThenCallback()Has a then callback
containsCatchCallback()assertContainsCatchCallback()Has a catch callback
containsFinallyCallback()assertContainsFinallyCallback()Has a finally callback
hasOption(string, ?callable)assertHasOption()Custom batch option

All callback-checking methods (containsThenCallback, containsCatchCallback, etc.) accept an optional callable to validate a specific callback, not just its presence:

php
Execution::assertBatched(function (PushedBatch $batch) {
    return $batch->containsThenCallback(fn ($callback) => /* validate */);
});