2017 © Pedro Peláez
 

library influence

Mocking any objects and classes

image

komex/influence

Mocking any objects and classes

  • Wednesday, December 3, 2014
  • by komex
  • Repository
  • 2 Watchers
  • 0 Stars
  • 17 Installations
  • PHP
  • 1 Dependents
  • 1 Suggesters
  • 1 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Build Status Code Coverage Scrutinizer Code Quality Latest Stable Version License ![Gitter](https://badges.gitter.im/Join Chat.svg), (*1)

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., (*2)

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:, (*3)

{
    "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., (*4)

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., (*5)

Manage objects

Let imagine we have a simple class A:, (*6)

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., (*7)

$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:

```php
$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., (*8)

$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., (*9)

Creative Commons License, (*10)

The Versions

03/12 2014

dev-develop

dev-develop

Mocking any objects and classes

  Sources   Download

CC-BY-SA-4.0

The Requires

  • php >=5.4
  • ext-spl *
  • ext-tokenizer *

 

The Development Requires

by Andrey Kolchenko

test mock stub testing php unittest unit

11/11 2014

dev-master

9999999-dev

Mocking any objects and classes

  Sources   Download

CC BY-SA 4.0

The Requires

  • php >=5.4
  • ext-spl *
  • ext-tokenizer *

 

The Development Requires

by Andrey Kolchenko

test mock stub testing php unittest unit

11/11 2014

v1.1.0

1.1.0.0

Mocking any objects and classes

  Sources   Download

CC BY-SA 4.0

The Requires

  • php >=5.4
  • ext-spl *
  • ext-tokenizer *

 

The Development Requires

by Andrey Kolchenko

test mock stub testing php unittest unit

21/10 2014

v1.0.0

1.0.0.0

Mocking any objects and classes

  Sources   Download

CC BY-SA 4.0

The Requires

  • php >=5.4
  • ext-spl *
  • ext-tokenizer *

 

The Development Requires

by Andrey Kolchenko

test mock stub testing php unittest unit