Getting started

Creating a compatible test case

In order to run PhpUnit test cases with Doublit, your test class should extend the Doublit\TestCase class. This class extends the PHPUnit\Framework\TestCase class so you can still write your tests like you would normally write them in PhpUnit. You should also import the \Doublit\Doublit method that you are going to need to create test doubles. Here is what your test class should look like :

use \Doublit\Doublit;
use \Doublit\TestCase;

class MyTestClass extends TestCase {

    function testMyFirstTest(){
        // your test here ...
    }


}

This will make sure the Doublit\Doublit::close() method is executed at the end of all your tests. Now we have a first test ready but it doesn't test anything. The next step is to create class doubles which can then be tested.

Creating a double

A double is called a "dummy" when all the methods of the original class are overwritten to return null. If you are not sure what doubles of type "dummy" are, you can get more details on the introduction page. To get a "dummy" double instance, use the dummy_instance method :

use \Doublit\Doublit;
use \Doublit\TestCase;

class MyTestClass extends TestCase {

    function testMyFirstTest(){
        // Get a double instance of type "dummy" for class "Foo"
        $double = Doublit::dummy_instance(Foo::class);
    }

}

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. If you are not sure what doubles of type "mock" are, you can get more details on the introduction page. To get a "mock" double instance, use the mock_instance method :

use \Doublit\Doublit;
use \Doublit\TestCase;

class MyTestClass extends TestCase {

    function testMyFirstTest(){
        // Get a double instance of type "mock" for class "Foo"
        $double = Doublit::mock_instance(Foo::class);
    }

}

For more details : Read the doc on creating doubles

Testing a double

First use the Doublit::_method method to tell doublit which method you would like to test. Then use the following methods to define your tests :

  • If you want to test how many times a double method is called, use the count method.
  • If you want to test the values of the arguments passed to a double method, use the args method.
  • If you want to change the return value of a method, use the stub method.

Here is a full working example :

use \Doublit\Doublit;
use \Doublit\TestCase;

class MyTestClass extends TestCase {

    function testMyFirstTest(){

        /* Get a double instance of type "dummy" for class "Foo" */
        $double = Doublit::dummy_instance(Foo::class);

        /* Set double test expectations for method "myMethod" */
        $double::_method('myMethod')
            ->count('>=1') // Test that the method "myMethod" is called a least one time
            ->args(['value1', 'value2']); // Test that the arguments passed to method "myMethod" are "value1" and "value2"
            ->stub('hello'); // Make method return "hello"

        /* Run the "methodToTest" method of the "ClassToTest" class with $double */
        $class_to_test = new ClassToTest($double);
        $result = $class_to_test->methodToTest();

        /* Test the result with PhpUnit */
        $this->assertEquals('result', $result);

    }
}

For more details : Read the doc on testing doubles