PHP Classes

Doubles: Create mock objects by doubling a given class

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 54 All time: 10,612 This week: 455Up
Version License PHP version Categories
doubles 1.0MIT/X Consortium ...5PHP 5, Language, Testing
Description 

Author

This package can be used to create mock objects by doubling a given class.

It can take a given class reference as parameter and creates a dummy object of that class that can be used to test as a mock object. The mock object provides useful features like:

- Counting how many times an object method is called
- Check if the mock object is called with given values
- Make a given class function of the mock object return a specific value

Innovation Award
PHP Programming Innovation award nominee
July 2019
Number 5
One way to test applications based on classes that implement important behaviors is to create mock objects.

These are objects that behave like objects of the classes that you want to test but you can change them to make them behave according to the aspects that you want to test about the class features.

This package can be used to create mock objects by creating similar classes that have additional features that are useful for testing purposes like counting how many times among other features.

Manuel Lemos
Picture of Alexandre
  Performance   Level  
Name: Alexandre <contact>
Classes: 6 packages by
Country: France France
Age: ???
All time rank: 355392 in France France
Week rank: 164 Up7 in France France Up
Innovation award
Innovation award
Nominee: 2x

Documentation

SitPHP/Doubles

Build Status

The "sitphp/doubles" library can help you to test your PHP classes by generating doubles that look like the original classes but can be manipulated and tested (sort of a copy of a class). These doubles then can then be used instead of the original classes for your test. This library can create doubles of any kind of class, interface or trait.

See full documentation : here

Installation

Add the line "sitphp/doubles": "2.2.*" in the "require-dev" section of your composer.json file :

{
    "require-dev": {
        "sitphp/doubles": "2.2.*"
    }
}

And run the following command :

composer update

This will install the latest version of the "sitphp/doubles" library with the required PhpUnit package.

Creating a double

A double is called a "dummy" when all the methods of the original class are overwritten to return null. To get a "dummy" double instance, use the dummy method :

// Get a double instance of type "dummy" for class "MyClass"
$my_double = Double::dummy(MyClass::class)->getInstance();

A double is called a "mock" when all the methods of the original class are overwritten to behave the same as in the original class. To get a "mock" double instance, use the mock method :

// Get a double instance of type "mock" for class "MyClass"
$my_double = Double::mock(MyClass::class)->getInstance();

For more details : Read the doc on creating doubles

Testing a double

To test how many times a double method is called, use the count method :


// Test that the method "myMethod" is called a least one time
$double::_method('myMethod')->count('>=1');

To test the values of the arguments passed to a double method, use the args method :

// Test that the arguments passed to method "myMethod" are "value1" and "value2"
$double::_method('myMethod')->args(['value1', 'value2']);

To change the return value of a method, use the stub method. :


// Make method "myMethod" return "hello"
$my_double::_method('myMethod')->return('hello');

For more details : Read the doc on testing doubles

Configuration

You define the configuration for a specific double using the 2nd argument of the dummy and mock methods :</p>

// Get double instance with config
$my_double = Double::dummy(MyClass::class, [
    'allow_final_doubles' => true,
    'allow_non_existent_classes' => true
])->getInstance();

Here is a list of all available config parameters :

  • `allow_final_doubles` : Set this parameter to `false` to stop trying to make doubles of final classes/methods.
  • `allow_protected_methods` : Set this parameter to `false` to disallow testing protected methods.
  • `allow_non_existent_classes` : Set this parameter to `false` to disallow alias doubles of non existent classes.
  • `test_unexpected_methods` : Set this parameter to `true` to automatically receive an assertion error whenever an unexpected method is called.

For more details : Read the doc on configuration

About

License

This library is licensed under the MIT license.

Author

Alexandre Geiswiller - alexandre.geiswiller@gmail.com.


  Files folder image Files (40)  
File Role Description
Files folder imagedoc (6 files)
Files folder imagesrc (6 files, 3 directories)
Files folder imagetests (5 files)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file readme.md Doc. Documentation

  Files folder image Files (40)  /  doc  
File Role Description
  Accessible without login Plain text file 0_intro.md Data Auxiliary data
  Accessible without login Plain text file 1_quick_start.md Data Auxiliary data
  Accessible without login Plain text file 2_creating_doubles.md Data Auxiliary data
  Accessible without login Plain text file 3_testing_doubles.md Data Auxiliary data
  Accessible without login Plain text file 4_configuration.md Data Auxiliary data
  Accessible without login Plain text file 5_about.md Data Auxiliary data

  Files folder image Files (40)  /  src  
File Role Description
Files folder imageExceptions (5 files)
Files folder imageLib (6 files)
Files folder imageStubs (7 files)
  Plain text file Constraints.php Class Class source
  Plain text file Double.php Class Class source
  Plain text file Stubs.php Class Class source
  Plain text file TestCase.php Class Class source
  Plain text file TestCasePHPUnit7AndLower.php Class Class source
  Plain text file TestCasePHPUnit8AndHigher.php Class Class source

  Files folder image Files (40)  /  src  /  Exceptions  
File Role Description
  Plain text file BadMethodCallException.php Class Class source
  Plain text file DoublesException.php Class Class source
  Plain text file InvalidArgumentException.php Class Class source
  Plain text file LogicException.php Class Class source
  Plain text file RuntimeException.php Class Class source

  Files folder image Files (40)  /  src  /  Lib  
File Role Description
  Plain text file ClassManager.php Class Class source
  Plain text file DoubleInterface.php Class Class source
  Plain text file DoubleStub.stub Class Class source
  Plain text file EvalLoader.php Class Class source
  Plain text file Expectation.php Class Class source
  Plain text file ExpectationCollection.php Class Class source

  Files folder image Files (40)  /  src  /  Stubs  
File Role Description
  Plain text file ReturnArgumentStub.php Class Class source
  Plain text file ReturnCallbackStub.php Class Class source
  Plain text file ReturnSelfStub.php Class Class source
  Plain text file ReturnValueMapStub.php Class Class source
  Plain text file ReturnValueStub.php Class Class source
  Plain text file StubInterface.php Class Class source
  Plain text file ThrowExceptionStub.php Class Class source

  Files folder image Files (40)  /  tests  
File Role Description
  Plain text file AssertionTest.php Class Class source
  Accessible without login Plain text file bootstrap.php Example Example script
  Plain text file ConstraintTest.php Class Class source
  Plain text file DoubleTest.php Class Class source
  Plain text file StubTest.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:54
This week:0
All time:10,612
This week:455Up