2017 © Pedro Peláez
 

library isolator

Dependency injection for global functions.

image

icecave/isolator

Dependency injection for global functions.

  • Friday, March 27, 2015
  • by jmalloc
  • Repository
  • 4 Watchers
  • 30 Stars
  • 788,702 Installations
  • PHP
  • 35 Dependents
  • 0 Suggesters
  • 4 Forks
  • 0 Open issues
  • 17 Versions
  • 2 % Grown

The README.md

Isolator

Build Status Code Coverage Latest Version, (*1)

Isolator simplifies testing of classes that make use of global functions by treating all global functions as methods on an "isolator" object., (*2)

composer require icecave/isolator

Rationale

A large number of PHP extensions (and the PHP core) implement their functionality as global functions. Testing classes that use these functions quickly becomes difficult due to the inability to replace them with test doubles., (*3)

Isolator endeavours to solve this problem by acting as a proxy between your class and global functions. An isolator instance is passed into your object as a dependency and used in place of any global function calls that you may want to replace when testing., (*4)

Example

The following class makes use of file_get_contents() to read the contents of a file., (*5)

class MyDocument
{
    public function __construct($filename)
    {
        $this->filename = $filename;
    }

    public function getContents()
    {
        return file_get_contents($this->filename);
    }

    protected $filename;
}

Despite the simplicity of the example, the class immediately becomes difficult to test due to it's reliance on the filesystem. In order to test this class you might be inclined to set up some static fixtures on disk, make a temporary directory when your test suite is set up or perhaps even use a virtual filesystem wrapper., (*6)

Isolator provides a fourth alternative. Given below is the same example rewritten using an Isolator instance., (*7)

use Icecave\Isolator\Isolator;

class MyDocument
{
    public function __construct($filename, Isolator $isolator = null)
    {
        $this->filename = $filename;
        $this->isolator = Isolator::get($isolator);
    }

    public function getContents()
    {
        return $this->isolator->file_get_contents($this->filename);
    }

    protected $filename;
    protected $isolator;
}

MyDocument now takes an instance of Isolator in it's constructor. It would be a pain - and unnecessary - to create a new Isolator instance every time you construct an object in your production code, so a shared instance is made accessible using the Isolator::get() method. If a non-null value is passed to Isolator::get() it is returned unchanged, allowing you to replace the isolator when necessary., (*8)

MyDocument::getContents() is also updated to use the isolator instance rather than calling the global function directly. The behavior of MyDocument remains unchanged but testing the class is easy, as will be shown in the example test suite below., (*9)

Note: The test below is written for the PHPUnit testing framework, using Phake for mocking. Phake provides a more flexible alternative to PHPUnit's built-in mock objects., (*10)

class MyDocumentTest extends PHPUnit\Framework\TestCase
{
    public function setUp()
    {
        // First a mocked isolator instance is created ...
        $this->isolator = Phake::mock('Icecave\Isolator\Isolator');

        // That isolator instance is given to the MyDocument instance
        // that is to be tested ...
        $this->myDocument = new MyDocument('foo.txt', $this->isolator);
    }

    public function testGetContents()
    {
        // Phake is used to configure the mocked isolator to return a known
        // string when file_get_contents() is called with a parameter equal
        // to 'foo.txt' ...
        Phake::when($this->isolator)
          ->file_get_contents('foo.txt')
          ->thenReturn('This is the file contents.');

        // MyDocument::getContents() is called, and it's result checked ...
        $contents = $this->myDocument->getContents();
        $this->assertEquals($contents, 'This is the file contents.');

        // Finally Phake is used to verify that a call to file_get_contents()
        // was made as expected ...
        Phake::verify($this->isolator)
          ->file_get_contents('foo.txt');
    }
}

The test verifies the behavior of the MyDocument class completely, without requiring any disk access., (*11)

Using an isolator is most helpful when testing code that uses functions which maintain global state or utilize external resources such as databases, filesystems, etc. It is usually unnecessary to mock out deterministic functions such as strlen(), for example., (*12)

Isolator Trait

In PHP 5.4 and later, it is also possible to use IsolatorTrait to bring an isolator into your class. The isolator instance is accessed using $this->isolator() and can be set via $this->setIsolator()., (*13)

use Icecave\Isolator\IsolatorTrait;

class MyDocument
{
    use IsolatorTrait;

    public function __construct($filename)
    {
        $this->filename = $filename;
    }

    public function getContents()
    {
        return $this->isolator()->file_get_contents($this->filename);
    }

    protected $filename;
}

Language Constructs

Isolator can also be used to invoke the following function-like language constructs:, (*14)

  • include, include_once, require and require_once
  • exit and die
  • echo
  • eval
  • new

Peculiarities

Several of PHP's core global functions have some peculiarities and inconsitencies in the way they are defined. Isolator attempts to accomodate such inconsistencies when possible, but may have issues with some native C functions for which parameter reflection information is non-standard or incorrect. These issues seem to be largely rectified as of PHP 5.6., (*15)

The Versions

27/03 2015

dev-develop

dev-develop https://github.com/IcecaveStudios/isolator

Dependency injection for global functions.

  Sources   Download

MIT

The Requires

  • php >=5.3

 

The Development Requires

test mock stub phpunit unit double fake

27/03 2015

dev-master

9999999-dev https://github.com/IcecaveStudios/isolator

Dependency injection for global functions.

  Sources   Download

MIT

The Requires

  • php >=5.3

 

The Development Requires

test mock stub phpunit unit double fake

27/03 2015

3.0.3

3.0.3.0 https://github.com/IcecaveStudios/isolator

Dependency injection for global functions.

  Sources   Download

MIT

The Requires

  • php >=5.3

 

The Development Requires

test mock stub phpunit unit double fake

23/03 2015

3.0.2

3.0.2.0 https://github.com/IcecaveStudios/isolator

Dependency injection for global functions.

  Sources   Download

MIT

The Requires

  • php >=5.3

 

The Development Requires

test mock stub phpunit unit double fake

21/10 2014

3.0.1

3.0.1.0 https://github.com/IcecaveStudios/isolator

Dependency injection for global functions.

  Sources   Download

MIT

The Requires

  • php >=5.3

 

The Development Requires

test mock stub phpunit unit double fake

08/10 2014

3.0.0

3.0.0.0 https://github.com/IcecaveStudios/isolator

Dependency injection for global functions.

  Sources   Download

MIT

The Requires

  • php >=5.3

 

The Development Requires

test mock stub phpunit unit double fake

08/10 2014

dev-release/3.0.0

dev-release/3.0.0 https://github.com/IcecaveStudios/isolator

Dependency injection for global functions.

  Sources   Download

MIT

The Requires

  • php >=5.3

 

The Development Requires

test mock stub phpunit unit double fake

08/10 2014

dev-feature/codegen

dev-feature/codegen https://github.com/IcecaveStudios/isolator

Dependency injection for global functions.

  Sources   Download

MIT

The Requires

  • php >=5.3

 

The Development Requires

test mock stub phpunit unit double fake

12/08 2014

2.3.0

2.3.0.0 https://github.com/IcecaveStudios/isolator

Dependency injection for global functions.

  Sources   Download

MIT

The Requires

  • php >=5.3

 

The Development Requires

test mock phpunit unit fake phake

27/02 2014

2.2.1

2.2.1.0 https://github.com/IcecaveStudios/isolator

Dependency injection for global functions.

  Sources   Download

MIT

The Requires

  • php >=5.3

 

The Development Requires

test mock phpunit unit fake phake

04/06 2013

2.2.0

2.2.0.0 https://github.com/IcecaveStudios/isolator

Isolate PHP tests from the global environment.

  Sources   Download

MIT

The Requires

  • php >=5.3

 

The Development Requires

test mock phpunit unit fake phake

05/04 2013

2.1.2

2.1.2.0 https://github.com/IcecaveStudios/isolator

Isolate PHP tests from the global environment.

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

test mock phpunit unit fake phake

14/01 2013

2.1.1

2.1.1.0 https://github.com/IcecaveStudios/isolator

Mock out calls to built-in PHP functions.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

  • icecave/testing 1.*

test mock phpunit unit fake phake

12/08 2012

2.1.0

2.1.0.0 https://github.com/IcecaveStudios/isolator

Mock out calls to built-in PHP functions.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

test mock phpunit unit fake phake

05/08 2012

2.0.0

2.0.0.0 https://github.com/IcecaveStudios/isolator

Mock out calls to built-in PHP functions.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

test mock phpunit unit fake phake

30/07 2012

1.0.1

1.0.1.0 https://github.com/IcecaveStudios/isolator

Mock out calls to built-in PHP functions.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

test mock phpunit unit fake phake

10/07 2012

1.0.0

1.0.0.0 https://gihub.com/IcecaveStudios/isolator

Mock out calls to built-in PHP functions.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

test mock phpunit unit fake phake