PHPT scripts are unit and integration tests for the PHP interpreter and user applications.
The PHP Quality Assurance Team uses these scripts to test the engine, verify bug fixes, and maintain a stable build. Everyone is encouraged to write and submit tests to the PHP QA team.
They can also be used to test your own command-line and web applications. You can simulate HTTP requests, file uploads, and terminal input and validate script output and HTTP responses.
Writing Tests
The .phpt
file combines section markers, raw text, and PHP code to direct the test runner. Most sections are optional, and most tests will require only a small subset based on the type of test being run. Some of the more common sections include
--TEST--
and--DESCRIPTION--
to describe the test--GET--
,--POST--
, and--PUT--
to simulate an HTTP request--STDIN--
to provide raw input to the script--FILE--
to contain the test script itself--EXPECT--
and--EXPECTREGEX--
to validate the output--CLEAN--
to remove any artifacts created while running the test
Here is a simple example that tests processing standard input using stream_get_contents
.
--TEST--
STDIN input
--FILE--
<?php
var_dump(stream_get_contents(STDIN));
?>
--STDIN--
fooBar
use this to input some thing to the php script
--EXPECT--
string(54) "fooBar
use this to input some thing to the php script
"
Running Tests
Tests are run from the command-line using run-tests.php
or PEAR's run-tests
command.
$ pear run-tests stdin.phpt
Running 1 tests
PASS STDIN input[stdin.phpt]
TOTAL TIME: 00:00
1 PASSED TESTS
0 SKIPPED TESTS
They can also be integrated with PHPUnit using PhptTestCase
and PhptTestSuite
or run directly.
$ phpunit stdin.phpt
PHPUnit 3.6.12 by Sebastian Bergmann.
.
Time: 0 seconds, Memory: 6.25Mb
OK (1 test, 1 assertion)