Welcome to Abdul Malik Ikhsan's Blog

Testing Hard Dependency with AspectMock

Posted in Teknologi, Tutorial PHP by samsonasik on January 28, 2016

This is another testing legacy application post. Don’t tell your client/other people to refactor, if your job is only to make tests for it, as at some situations, there are reasons to not refactor. You may have situation to test hard dependency that impossible to be mocked and stubbed. There is a library named AspectMock for it, that you can use in PHPUnit, for example.

So, to have it, you can require via composer:

composer require "codeception/aspect-mock:^0.5.5" --dev

For example, you have the following class:

namespace App;

class MyController
    public function save()
        $user = new User();
        if (! $user->save()) {
            echo 'not saved';

        echo 'saved';

That bad, huh! Ok, let’s deal to tests it even you don’t really like it. First, setup your phpunit.xml to have ‘backupGlobals=”false”‘ config:

<?xml version="1.0" encoding="UTF-8"?>

        <testsuite name="AppTest">
            <directory suffix=".php">./test</directory>
        <whitelist addUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./src</directory>

Now, based on config above, you can create bootstrap.php:

include 'vendor/autoload.php';

use AspectMock\Kernel;

$kernel = Kernel::getInstance();
    'debug' => true,
    'cacheDir' => __DIR__ . '/data/cache',
    'includePaths' => [__DIR__.'/src'],

Assumption: You have ‘./data/cache’ for saving cache and ‘src/’ for your source code directory, if you use your own autoloader, you can add:

// ...

as the AspectMock documentation mentioned.

Now, time to write the tests:

  1. Preparation
    namespace AppTest;
    use PHPUnit_Framework_TestCase;
    use App\MyController;
    use AspectMock\Test as test;
    class MyControllerTest extends PHPUnit_Framework_TestCase
        private $myController;
        protected function setUp()
            $this->myController = new MyController;
        protected function tearDown()
            test::clean(); // remove all registered test doubles

  2. write the test cases

class MyControllerTest extends PHPUnit_Framework_TestCase
    // ...
    public function provideSave()
        return [
            [true, 'saved'],
            [false, 'not saved'],

     * @dataProvider provideSave
    public function testSave($succeed, $echoed)
        // mock
        $userMock = test::double('App\User', ['save' => $succeed]);

        $content = ob_get_clean();
        $this->assertEquals($echoed, $content);

        // stub
    // ...



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: