VOOZH about

URL: https://deepwiki.com/hypervel/support/10.5-queue-testing

⇱ Queue Testing | hypervel/support | DeepWiki


Loading...
Menu

Queue Testing

This document explains how to test code that pushes jobs to queues without actually executing them. The Queue::fake() method replaces the real queue manager with a test double that records all pushed jobs and provides assertion methods for verifying queue interactions.

For general testing patterns and concepts, see Testing Overview. For testing other facades, see Event Testing and HTTP Client Testing.


Purpose and Architecture

The queue testing infrastructure allows you to verify that your application pushes the correct jobs to queues without actually dispatching them to queue workers. This enables fast, isolated unit tests that validate queue interaction logic.

System Architecture


Sources: src/Facades/Queue.php1-114


Activating Queue Faking

Basic Fake Activation

The Queue::fake() method replaces the queue manager with a test double that intercepts all queue operations:


The fake is implemented in src/Facades/Queue.php91-104 which creates a QueueFake instance and swaps it into the service container using the swap() method.

Selective Job Faking

You can fake only specific job types while allowing others to execute normally:


Alternatively, use the except() method to fake all jobs except specific ones:


Sources: src/Facades/Queue.php91-104 src/Facades/Queue.php58


Assertion Methods

The QueueFake provides comprehensive assertion methods for verifying queue interactions. All assertions are available as static methods on the Queue facade after calling fake().

assertPushed

Verifies that a specific job type was pushed to any queue:


The method signature from src/Facades/Queue.php59:


assertPushedOn

Verifies that a job was pushed to a specific queue:


Sources: src/Facades/Queue.php60

assertPushedWithChain

Verifies that a job was pushed with a specific chain of subsequent jobs:


Sources: src/Facades/Queue.php61

assertPushedWithoutChain

Verifies that a job was pushed without any chained jobs:


Sources: src/Facades/Queue.php62

assertClosurePushed / assertClosureNotPushed

Verifies that closure-based jobs were or were not pushed:


Sources: src/Facades/Queue.php63-64

assertNotPushed

Verifies that a specific job type was never pushed:


Sources: src/Facades/Queue.php65

assertCount

Verifies the total number of jobs pushed across all queues:


Sources: src/Facades/Queue.php66

assertNothingPushed

Verifies that no jobs were pushed to any queue:


Sources: src/Facades/Queue.php67


Job Inspection Methods

In addition to assertions, QueueFake provides methods for inspecting pushed jobs programmatically.

Assertion Method Reference

MethodPurposeParameters
assertPushed()Verify job was pushedJob class, optional callback/count
assertPushedOn()Verify job pushed to specific queueQueue name, job class, optional callback
assertPushedWithChain()Verify job has specific chainJob class, chain array, optional callback
assertPushedWithoutChain()Verify job has no chainJob class, optional callback
assertClosurePushed()Verify closure job pushedOptional callback/count
assertClosureNotPushed()Verify no closure jobsOptional callback
assertNotPushed()Verify job not pushedJob class, optional callback
assertCount()Verify total job countExpected count
assertNothingPushed()Verify no jobs pushedNone

pushed

Returns a collection of all pushed jobs of a specific type:


Sources: src/Facades/Queue.php68

hasPushed

Checks if any jobs of a specific type were pushed:


Sources: src/Facades/Queue.php69

pushedJobs

Returns all pushed jobs organized by job class:


Sources: src/Facades/Queue.php71


Advanced Features

Serialization Testing

Use serializeAndRestore() to test that jobs can be properly serialized and unserialized, mimicking the actual queue behavior:


This ensures your jobs handle serialization correctly, catching issues with closures, resources, or other non-serializable data.

Sources: src/Facades/Queue.php72

Checking Fake Status

Determine if job faking should apply to a specific job instance:


Sources: src/Facades/Queue.php70


Testing Workflow

Complete Testing Flow Diagram


Sources: src/Facades/Queue.php91-104

Common Testing Patterns

Pattern 1: Basic Job Verification


Pattern 2: Queue Priority Testing


Pattern 3: Job Chain Verification


Pattern 4: Negative Assertions


Pattern 5: Selective Faking



Integration with Queue Operations

Supported Queue Methods

The QueueFake intercepts all standard queue methods:

MethodDescriptionUsage in Tests
push()Push job immediatelyQueue::push(new Job())
pushOn()Push to specific queueQueue::pushOn('high', new Job())
pushRaw()Push raw payloadQueue::pushRaw($payload)
later()Push with delayQueue::later(60, new Job())
laterOn()Push delayed to queueQueue::laterOn('low', 60, new Job())
bulk()Push multiple jobsQueue::bulk([new Job1(), new Job2()])

All of these methods are recorded by QueueFake and can be verified with assertions.

Sources: src/Facades/Queue.php41-46

Queue Method Mapping


Sources: src/Facades/Queue.php41-46 src/Facades/Queue.php59-71


Implementation Details

QueueFake Class Structure

The QueueFake class, referenced in src/Facades/Queue.php10 implements the queue contract while recording all operations:

Hypervel\Support\Testing\Fakes\QueueFake
├── Constructor receives:
│ ├── ContainerInterface $container
│ ├── array|string $jobsToFake
│ └── QueueManager $actualQueue (for non-faked jobs)
├── Records job pushes with:
│ ├── Job instance
│ ├── Queue name
│ ├── Delay timing
│ └── Chained jobs
└── Provides assertion/inspection methods

Facade Integration

The Queue::fake() method src/Facades/Queue.php91-104 performs the following steps:

  1. Checks if already faked (via isFake())
  2. Retrieves the actual queue manager reference
  3. Creates QueueFake instance with dependencies
  4. Uses tap() helper for fluent interface
  5. Calls swap() to replace facade root
  6. Returns the fake instance for method chaining

Sources: src/Facades/Queue.php91-104 src/Facades/Queue.php10


Related Testing Features

For comprehensive testing of asynchronous operations:

  • Event Testing [#10.3]: Test event dispatching with Event::fake()
  • HTTP Client Testing [#10.4]: Test external API calls with Http::fake()
  • Facade Mocking [#10.2]: Mock facade methods with shouldReceive()
  • Testing Overview [#10.1]: General testing patterns and strategies

Sources: src/Facades/Queue.php1-114

Refresh this wiki

On this page