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.
- Root Folder ("/")
-
Test Page
For example "test.php" - you can secure your page using whatever mechanism you normally use! - Enhance Folder ("/enhance/")
-
Stick the EnhanceTestFramework.php file in here
- Test Folder ("/test/")
-
Test Fixture classes
Put your test fixture classes in here, normally you would have one test fixture class per production class.
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.
- The reference to EnhanceTestFramework.php
- The reference to a class (or classes) that will be tested
- The reference to a class (or classes) that contain the tests
- 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.