This is legacy material. Now I recommend PHP testing using my Easy Testing of PHP system.
Download PHP Tester from GitHub.
Install using Node Package Manager PHP Tester on NPM
The script will be accessed by it's URL on a server that is running PHP. We may add this URL to the Karma configuration file in the list of files to load in the browser.
If there are any errors or warnings due to bugs in the PHP code, we will see an unhelpful script error message in the test runner output. So we want to trap these errors and wrap them in a test that displays the error in the test runner output.
Even if our initial tests pass, modifications to PHP modules that are being tested later may well cause PHP errors. So we must trap these errors and report them to the test runner without breaking the flow.
The PHP test specs are not discoverable by the use of wild-cards (*) in the Karma configuration file since they are URLs on the server. So we need to list them inpidually, insert code into the Karma configuration file that generates the list of URLs (if this is possible), or write a PHP script that acts as a middleman to provide the combined test code.
The latter option seems best since it automates the discovery of the test specs and avoids adding custom behaviour to the Karma files.
See the example below which shows neatly indented code that folds nicely to expand and collapse in the Sublime Text Editor.
We include before.php to insert the Tester Class and configure the script.
We include the code file that we are testing.
We include mocks (that simulate functionality) of any code that is required for our unit under test to function. We don't include any other files, that are themselves subject to tests, because they may have bugs of their own.
Then we have a suite of tests built using the Tester Class methods.
Finally, we provide callback functions to exercise the code under test, and return test results.
We will write our test specs in PHP and store each one in it's own file with a name ending in Spec such as: myClassSpec.php.
Three functions (static methods Tester::method()) are used to create a test suite:
This function takes 2 or 3 parameters.
Parameter 1 is a text string to describe a group of tests.
Parameter 2 is a string that is returned from a describe function or a chain of concatenated it functions.
Parameter 3 is a boolean which should be set to true in the outermost describe function to cause the text to be displayed rather than returned.
This function takes 3 parameters.
Parameter 1 is a text string to describe a test.
Parameter 2 is a string that is returned from a callback function.
Parameter 3 is the expected result (number/boolean/string).
Note that a boolean value is displayed as a 1 for true or blank for false, so when a test fails, the result may be confusing.
bool_to_string method is provided with the Tester Class which converts a boolean value to it's text representation of
This function takes 1 parameter, the name of a function to call. The called function returns the test result.
Your development code is placed in a src directory. PHP test Specs are put in tests/php/specs/ and should be named as: *Spec.php where * is the name of the test suite.
The test setup is built from an NPM install of php-tester
The package.json file includes a test command which runs the karma test runner.
This URL is specified in the karma.conf.js file. You can test this URL in a web browser to ensure that it is working and produces test output results.
Once everything is set up, you may run the tests from the command line with: npm test from the root of your test environment.