Enhance PHP Unit Testing And Mocking Framework improve the quality

Test Classes

If you have been using PHP for ages, you probably have a firm idea of how you want to organise your code. If you want some ideas though, here is our recommended structure. You don't have to do things this way, this is just how the Enhance PHP team organise things.

All of the examples are based on this structure. If you use a different structure just adjust the paths of the include files.

Test Page

The test page only needs four things.

  1. The reference to EnhanceTestFramework.php
  2. The reference to a class (or classes) that will be tested
  3. The reference to a class (or classes) that contain the tests
  4. The "RunTests" call

Here is an example test page:

<?php
// Include the test framework
include('../EnhanceTestFramework.php');
// Find the tests - '.' is the current folder
\Enhance\Core::discoverTests('.');
// Run the tests
\Enhance\Core::runTests();
?>

Optionally, you can opt for discoverTests to only run within the specified folder, not any sub-folders. You can do this by telling the discovery to be non-recursive:

\Enhance\Core::discoverTests('.', false);

You can also run auto discovery against multiple folders.

\Enhance\Core::discoverTests('tests/', false);
\Enhance\Core::discoverTests('integrationtests/', false);

Test Fixture

Each test fixture must extend \Enhance\TestFixture - when this is the case, all public methods except for setUp() and tearDown() will be treated as tests.

This example test fixture contains two tests that verify the behaviour of a simple addition function.

class ExampleClassTests extends \Enhance\TestFixture
{
        private $target;

        public function setUp()
        {
                $this->target = \Enhance\Core::getCodeCoverageWrapper('ExampleClass');
        }

        public function addTwoNumbersWith3and2Expect5()
        {
                $result = $this->target->addTwoNumbers(3, 2);
                \Enhance\Assert::areIdentical(5, $result);
        }
        
        public function addTwoNumbersWith4and2Expect6()
        {
                $result = $this->target->addTwoNumbers(4, 2);
                \Enhance\Assert::areIdentical(6, $result);
        }
}

The tests use the classic "Triple-A" syntax - Arrange, Act, Assert.