komex/influence

Mocking any objects and classes


Keywords
php, testing, test, mock, stub, unittest, unit
License
CC-BY-SA-4.0

Documentation

Build Status Code Coverage Scrutinizer Code Quality Latest Stable Version License Gitter

Influence

Influence is the PHP library gives you ability to mock any objects and static classes. Works only if you use Composer autoloader. This library very useful for testing your code.

Requirements

  • PHP 5.4 or higher
  • SPL extension
  • Tokenizer extension
  • Composer loader

Installation

To add Influence as a local, per-project dependency to your project, simply add a dependency on komex/influence to your project's composer.json file. Here is a minimal example of a composer.json file that just defines a develop dependency on Influence:

{
    "require-dev": {
        "komex/influence": "1.1.*"
    }
}

Usage

Influence must be injected as early as possible. If you are using unit test framework like unteist or PHPUnit the best way to do this is include autoload and influence in bootstrap file.

require 'vendor/autoload.php';
Influence\Influence::affect();

Since this moment you are able to mock any objects and static classes. By default, all objects behave as usual. You need to configure behavior of each object or class you need to control.

Manage objects

Let imagine we have a simple class A:

class A
{
    public function sum($a)
    {
        return $a + $this->rand(0, $a);
    }
    private fuction rand($min, $max)
    {
        return rand($min, $max);
    }
}

Custom method behavior.

So, if we create an object of class A we can invoke only sum() method and control only $a and never know result of our operation.

$a = new A();
echo $a->sum(1); // ??
echo $a->sum(7); // ??

But with Influence you can simply test this code. Just specify the behavior of sum() like this:

$a = new A();
$method = Influence\RemoteControl::controlObject($a)->get('rand');
$method->setValue(new Value(1));
echo $a->sum(1); // 2
echo $a->sum(7); // 8
$method->setValue();
echo $a->sum(1); // ??
echo $a->sum(7); // ??

Log method calls

If you don't need to set custom method behavior, but want to know how many times method was called and with what arguments.

$a = new A();
$method = Influence\RemoteControl::controlObject($a)->get('rand');
$method->setLog(true);
echo $a->sum(1); // ??
echo $a->sum(7); // ??
var_dump($method->getLogs()); // [ [0, 1], [0, 7] ]

License

Influence by Andrey Kolchenko is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Creative Commons License