How to Write a PHPT File and Run One PHP Source Test
Write a minimal PHPT file, run a selected test from the PHP source tree, and check its output against the EXPECT section.
A PHPT file puts PHP code and its expected output in the same test. To try one selected test from the PHP source tree, write echo_basic.phpt with TEST, FILE, and EXPECT sections, then run make test TESTS=echo_basic.phpt. The question to ask afterward is whether the output produced by FILE matches EXPECT—not merely whether the PHP code looks plausible.
Write the smallest useful test
The PHP QA PHPT guide says a test must contain TEST, FILE, and either EXPECT or EXPECTF. Use EXPECT when you can specify the standard output exactly. This example has one fixed line of output, so there is no need for a more flexible expectation.
Save this example as echo_basic.phpt:
--TEST--
Echoes a fixed line
--FILE--
<?php
echo "Hello from PHPT\n";
?>
--EXPECT--
Hello from PHPT
The section markers divide one file into three parts. TEST gives this example a short name. FILE contains the PHP code to run; the <?php tag marks the start of that code. EXPECT gives the standard output that the code is supposed to produce. It is not another piece of PHP code, so the expected line has no echo statement or PHP tag.
Notice the \n in the PHP string. The example is intended to print Hello from PHPT followed by a line break, and the expected line is written on its own line in EXPECT. Keeping both sides short makes the comparison easy to inspect. The PHP Internals Book’s PHPT structure guide describes EXPECT as containing exactly the expected standard output.
Run the selected file
From the PHP source tree, use the documented selected-test command:
make test TESTS=echo_basic.phpt
Here, TESTS identifies the PHPT file you want to run. This is a command for the PHP source test workflow, not a command to paste into the FILE section. The PHP Internals Book shows this command for a selected test. Use the filename of your own selected test when it differs from the example.
Check what the result means
Read the two relevant pieces side by side. The FILE section above prints this line to standard output:
Hello from PHPT
The EXPECT section specifies the same line. That is the match this example is designed to check. If the code instead printed Hello from PHPT!, the extra exclamation mark would make its output different from the written expectation. Likewise, changing EXPECT alone would change the test’s demand without changing what FILE prints.
A mismatch means the test did not produce its specified output. It does not, by itself, tell you which side expresses your intention: inspect the PHP code and the expected text before editing either one. If the code should keep printing the original line, correct the expectation; if the intended output changed, update the code or expectation to describe that intended behavior. Do not treat the expected text as a way to conceal an output you have not examined.
EXPECTF can replace EXPECT in the required structure, but this fixed-output example needs only EXPECT. For a first PHPT file, a single visible line is easier to assess than a long transcript; the PHP QA guide suggests no more than ten lines of output as a useful guide while allowing exceptions.
