Mocking Queue Service for faster Behat Testing

Posted: 2014-11-23 19:36:57

As I wrote about here I am using behat to tests our APIs. I could have used Laravel's guzzle and phpunit integration to test the controllers but I decided to go with Behat. But this means when hitting those endpoints I need to find another way to mock services.

Since Laravel has you register Providers you can easily replace or override a provider. In this case the "'Illuminate\Queue\QueueServiceProvider'" provider with my own.

This allows me to test in my endpoints leaving the queue related classes to be tested during phpunit testing. Using the queue "sync" driver would help too but it would then cause this to not be an isolated tests since the queue would trigger another classes method. (@fire).

Setup a class to use as the new provider


namespace App\Providers;

use Illuminate\Queue\QueueServiceProvider;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider;
use Mockery as m;

class MockedQueue {
    public function push($args)
        return true;

class QueueMockProvider extends QueueServiceProvider {

     * Register the service provider.
     * @return void
    public function register()
        if(getenv('APP_ENV') == 'local' || getenv('APP_ENV') == 'testing')
            $this->app->bind('queue', function(){
                $queue = new MockedQueue();
                Log::info("Mock queue in use");
                return $queue;
        } else {

Register Mocked Queue over Real Queue

Instead of setting it for config/local/queue.php and config/testing/queue.php I set it in the main config/app.php. I would like to come back to using the config/app.php but had some issues.




That was it. From there on my queue related API tests did not trigger a queued job which in this case was supper helpful those where calling external APIs.


behat laravel php