Like most people who have written unit tests for PHP code, I used PHPUnit. It is likely the most widely used testing library for PHP.

I recently came across Enhance PHP Unit Testing for PHP, so I thought I would give it a try.

Lets start with the traditional super basic Math class:

class Math {
    public function add_2_numbers( $num_1, $num_2 ) {
        return $num_1 + $num_2;
    }

    public function subtract_2_numbers( $num_1, $num_2 ) {
        return $num1 - $num2;
    }

    public function multiply_2_numbers( $num_1, $num_2 ) {
        return $num_1 * $num_2;
    }

    public function divide_2_numbers( $num_1, $num_2 ) {
        return $num_1 / $num_2;
    }
}

There are a few ways to test this with Enhance PHP, I eventually settled on the method coverage approach:

class Math_Test extends EnhanceTestFixture {
    public function add_2_numbers() {
        $target = EnhanceCore::getCodeCoverageWrapper( 'Math' );
        $scenario = EnhanceCore::getScenario( $target, 'add_2_numbers' );

        $scenario->with( 2, 2 )->expect( 4 );
        $scenario->with( 0, 2 )->expect( 2 );
        $scenario->with( 3, 2 )->expect( 5 );

        $scenario->VerifyExpectations();
    }
}

EnhanceCore::runTests();

The syntax is easy enough, we define a $target for the class we are going to test then use $scenario to make multiple calls to the same method. In this case we’ll call add_2_numbers with three different sets of parameters and indicating what the expected result is. After defining our scenarios calling VerifyExpectations takes care of checking each one.

Running that test returns the following results:

Test Passed

add_2_numbers - Passed

Math->add_2_numbers:3
Math->subtract_2_numbers:0
Math->multiply_2_numbers:0
Math->divide_2_numbers:0

Test Passed
Test run took 0.0001370906829834 seconds

The code coverage results are only at the method level. You get a report indicating how many tests there are for each method. If you want granular line by line code coverage analysis you’ll still want to look at PHPUnit + Xdebug.

To see what a failure looks like we’ll add these lines to the Math_Test class:

        // for sufficiently large values of 2
        $scenario->with( 2, 2 )->expect( 5 ); 

Reports:

Test Failed

[Line 983 in file EnhanceTestFramework.php] add_2_numbers - Failed - Expected 5 but was 4

Math->add_2_numbers:4
Math->subtract_2_numbers:0
Math->multiply_2_numbers:0
Math->divide_2_numbers:0

Test Failed
Test run took 0.00020480155944824 seconds

There is a trade off in detail level using the scenario approach. On the whole I like the tighter syntax of scenario, so I consider this level of detail good enough.

I haven’t used Enhance PHP for enough tests to know for sure that I’d use it over PHPUnit, but I’m leaning that direction. I really, really like having a single 56k file that handles the testing infrastructure. That means I can easily distribute it along with my tests.

If you are already heavily invested in using PHPUnit, switching to something else is likely not going to be worth it. If you are writing tests for something new then Enhance PHP is worth trying.