Using Mockery with Codeception and Laravel 4

I have a new client who came to me wanting to build out some new features on an existing Laravel 4.2 application. Unfortunately the codebase did not have any tests, wich complicates the process of implementing the new changes. As a sort of stop-gap measure, we have agreed to add some "retro-active" acceptance tests to make sure that the existing functionality isn't inadvertently broken.

Being on 4.2, it does not have access to all of the great testing conveniences found in Laravel 5.*, which is unfortunate because it means I didn't have access to any of Laravel 5's convenient test helpers. Codeception is a great alternative testing package that provides a wealth of functionality and it is already compatible with Laravel 4 - this seemed like the best choice for us. So far, it has been working quite well for us, but I did run into one particularly frustrating problem: Binding mocks of service classes to the IoC does not work "out of the box". This is frustrating if you have a service class that hits a third-party API and you don't want to have to hit that API each time you run your tests.

As it turns out, this problem is due to the Laravel4 Module re-initializing the Application object on each request. While it looks like this should work, it actually doesn't:

$I = new FunctionalTester($scenario);
$I->wantTo('upgrade a subscription plan');
$I->amActingAs('testuser@example.com'); // This is a custom helper method which sets the active user

// Establish the necessary mocks
$mockBillingManager = Mockery::mock('Acme\Billing\StripeBillingManager');
// Make sure to set the expectations of the mock object before binding it to the IoC
$I->getApplication()->bind('Acme\Billing\StripeBillingManager', $mockBillingManager);

$I->amOnRoute('billing.upgrade.form');
$I->submitForm('subscription-form', [
    'plan' => 'premium',
    'token' => csrf_token()
]);
$I->seeInDatabase('users', ['email' => 'testuser@example.com', 'plan' => 'premium');

When the amOnRoute() method is executed, the Application is refreshed and all of your custom bindings are lost in favor of the bindings you establish in your service provider(s). This is triggerd by the doRequest() method on the Codeception\Lib\Connector\Laravel4 class, which is used by default in the Laravel4 module. The solution that worked for me was to extend the Laravel4 module and its connector class. This allowed me to remove the offfending line from the doRequest() method. Also, the Laravel4 module does not provide any methods for helping with temporary binding; creating a custom module allowed me to add some convenient methods to help with this.

Jordan Eldredge has a great article about writing custom Codeception modules. Using the methods he describes in his post we can create a custom Codeception Module and a new Connector library which is identical to the existing Laravel4 connector library. Only one change is required to the existing connector library, on line 47:

// New class based on Codeception\Lib\Connector\Laravel4

class CustomLaravel4Connector {

    // Most everything remains the same...

    /**
     * @param Request $request
     * @return Response
     */
    protected function doRequest($request)
    {
        //$this->initialize();  -- Remove this line

        return $this->kernel->handle($request);
    }

    //  Don't change anything else

}

Now we can create a custom module that extends the Laravel4 module and add some new functionality:

namespace Codeception\Module;

use CustomLaravel4Connector;

class CustomLaravel4 extends \Codeception\Module\Laravel4
{
    /**
     * Initialize hook.
     */
    public function _initialize()
    {
        $this->checkStartFileExists();
        $this->registerAutoloaders();
        $this->revertErrorHandler();
        $this->client = new CustomeLaravel4Connector($this);
    }

    /**
     * Allow the Codeception Actor to add a binding to the Laravel IOC
     *
     * @return \Illuminate\Foundation\Application
     */
    public function bindService($abstract, $instance, $shared = false)
    {
        $this->app->bind($abstract, $instance, $shared = false);
    }

    /**
     * Allow the Codeception Actor to bind an instantiated object to the Laravel IOC
     *
     * @return \Illuminate\Foundation\Application
     */
    public function bindInstance($abstract, $instance)
    {
        $this->app->instance($abstract, $instance);
    }
}

Presto! That is all there is to it. Now your mock objects can be bound appropriately and will remain in place for the duration of the test. Our new acceptance test looks like this:

$I = new FunctionalTester($scenario);
$I->wantTo('upgrade a subscription plan');
$I->amActingAs('testuser@example.com'); // This is a custom helper method which sets the active user

// Establish the necessary mocks
$I->bindService('Acme\Billing\BillingInterface', function(){
    return Mockery::mock('Acme\Billing\StripeBillingManager');
});

$I->amOnRoute('billing.upgrade.form');
$I->submitForm('subscription-form', [
    'plan' => 'premium',
    'token' => csrf_token()
]);
$I->seeInDatabase('users', ['email' => 'testuser@example.com', 'plan' => 'premium');

I have added my custom module and connector to my Laravel-Testing-Utilities package if you would like to make use of them in your own Laravel 4.2 application. Currently they are only on the 1.* branch, but if needed I will add them to the other versions of that package down the road.

comments powered by Disqus