Skip to content

[12.x] Add Scoped Queue Fakes section to testing documentation #10551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions queues.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
- [Monitoring Your Queues](#monitoring-your-queues)
- [Testing](#testing)
- [Faking a Subset of Jobs](#faking-a-subset-of-jobs)
- [Scoped Queue Fakes](#scoped-queue-fakes)
- [Testing Job Chains](#testing-job-chains)
- [Testing Job Batches](#testing-job-batches)
- [Testing Job / Queue Interactions](#testing-job-queue-interactions)
Expand Down Expand Up @@ -2584,6 +2585,120 @@ Queue::fake()->except([
]);
```

<a name="scoped-queue-fakes"></a>
### Scoped Queue Fakes

If you only want to fake queued jobs for a portion of your test, you may use the `fakeFor` method:

```php tab=Pest
<?php

use App\Jobs\ShipOrder;
use Illuminate\Support\Facades\Queue;

test('orders can be shipped', function () {
// Perform some test setup...

Queue::fakeFor(function () {
Queue::push(new ShipOrder);

Queue::assertPushed(ShipOrder::class);
});

// Jobs are pushed to the queue as normal and will be processed...
Queue::push(new ShipOrder);
});
```

```php tab=PHPUnit
<?php

namespace Tests\Feature;

use App\Jobs\ShipOrder;
use Illuminate\Support\Facades\Queue;
use Tests\TestCase;

class ExampleTest extends TestCase
{
public function test_orders_can_be_shipped(): void
{
// Perform some test setup...

Queue::fakeFor(function () {
Queue::push(new ShipOrder);

Queue::assertPushed(ShipOrder::class);
});

// Jobs are pushed to the queue as normal and will be processed...
Queue::push(new ShipOrder);
}
}
```

If you only want to fake specific jobs during the execution of a callable, you may pass an array of job class names as the second argument to the `fakeFor` method:

```php tab=Pest
test('orders can be shipped', function () {
Queue::fakeFor(function () {
Queue::push(new ShipOrder);
Queue::push(new AnotherJob);

Queue::assertPushed(ShipOrder::class);
Queue::assertNotPushed(AnotherJob::class);
}, [
ShipOrder::class,
]);
});
```

```php tab=PHPUnit
public function test_orders_can_be_shipped(): void
{
Queue::fakeFor(function () {
Queue::push(new ShipOrder);
Queue::push(new AnotherJob);

Queue::assertPushed(ShipOrder::class);
Queue::assertNotPushed(AnotherJob::class);
}, [
ShipOrder::class,
]);
}
```

You may fake all jobs except for a set of specified jobs during the execution of a callable using the `fakeExceptFor` method:

```php tab=Pest
test('orders can be shipped', function () {
Queue::fakeExceptFor(function () {
Queue::push(new ShipOrder); // Queued normally
Queue::push(new AnotherJob); // Faked

Queue::assertNotPushed(ShipOrder::class);
Queue::assertPushed(AnotherJob::class);
}, [
ShipOrder::class, // This job will be queued normally
]);
});
```

```php tab=PHPUnit
public function test_orders_can_be_shipped(): void
{
Queue::fakeExceptFor(function () {
Queue::push(new ShipOrder); // Queued normally
Queue::push(new AnotherJob); // Faked

Queue::assertNotPushed(ShipOrder::class);
Queue::assertPushed(AnotherJob::class);
}, [
ShipOrder::class, // This job will be queued normally
]);
}
```

<a name="testing-job-chains"></a>
### Testing Job Chains

Expand Down