enqueue/null
Null transport for Enqueue/Queue Interop: a no-op queue implementation that doesn’t send messages anywhere. Useful as a mock transport for tests and local development, while keeping the same messaging APIs and configuration style.
Installation Add the package via Composer:
composer require enqueue/null
Register the service provider in config/app.php (if not auto-discovered):
'providers' => [
// ...
Enqueue\Null\NullTransport::class,
],
Basic Usage
The enqueue/null package provides a null transport for the PHP-Enqueue library, allowing you to mock or bypass message queues entirely. Initialize it in your Laravel app:
use Enqueue\Null\NullTransport;
use Enqueue\Client\Producer;
$transport = new NullTransport();
$producer = new Producer($transport);
First Use Case Replace a real queue (e.g., RabbitMQ, Redis) with a null transport for testing, debugging, or development where queue operations are unnecessary:
$producer->send(new Message('Hello, Null Transport!'));
// No actual message is sent; this is a no-op.
Use in Tests
Replace real transports with NullTransport in unit/integration tests to avoid external dependencies:
$this->app->bind(Producer::class, function ($app) {
return new Producer(new NullTransport());
});
Conditional Queue Handling Dynamically switch between real and null transports based on environment:
$transport = config('queue.transport') === 'null'
? new NullTransport()
: new RabbitMqTransport(config('queue.rabbitmq'));
Consumers & Contexts The null transport supports the full PHP-Enqueue API, including:
$consumer = new Consumer($transport, 'test-queue', new NullContext());
$consumer->consume(); // No actual consumption occurs.
Error Handling Mimic real queue behavior by throwing exceptions (if needed) or silently ignoring operations.
Queue Workers
Disable queue workers entirely in config/queue.php:
'connections' => [
'null' => [
'driver' => 'enqueue-null',
'transport' => Enqueue\Null\NullTransport::class,
],
],
Then dispatch jobs to the null connection:
dispatch(new Job())->onConnection('null');
Event Queues Use for testing event listeners that rely on queues:
Event::listen(JobDispatched::class, function () {
// This will not actually queue anything.
dispatch(new ProcessPodcast());
});
No Persistence All operations are in-memory and discarded immediately. Use only for testing or development.
// ❌ Won't work for production:
$producer->send(new Message('Critical task!')); // Lost forever.
Consumer Behavior Consumers will appear to run but do nothing. Avoid using them for real workloads:
$consumer->consume(); // Runs forever but processes no messages.
Thread Safety The null transport is not thread-safe (like all in-memory transports). Not an issue for Laravel’s single-process workers, but beware in multi-process setups.
Verify No Side Effects
Use Log::debug() to confirm messages aren’t being sent:
$producer->send(new Message('Test'));
Log::debug('Message sent to null transport'); // Only this log appears.
Check for Misconfigurations Ensure the transport is correctly bound in Laravel’s service container:
php artisan config:clear
Custom Null Transport
Extend NullTransport to log dropped messages:
class LoggingNullTransport extends NullTransport {
public function send(Message $message) {
Log::info('Dropped message: ' . $message->getBody());
}
}
Hybrid Testing Combine with other transports for partial mocking:
$transport = new CompositeTransport([
new NullTransport(), // Drops messages
new RedisTransport(), // Logs to Redis for inspection
]);
Performance Testing Use to benchmark queue overhead by comparing against real transports.
How can I help you explore Laravel packages today?