Testing without frameworks
A test does not need a framework: one helper that compares a value with what you expect is enough to start. The examples below are plain PHP scripts, and every OK line on the page came from a run.
Hand asserts
A test can be one tiny helper: compare expected with actual, print OK or FAIL, stop on the first lie. No framework is needed to learn the core loop of arrange, act, and check.
<?php function check(bool $cond, string $label): void { if (!$cond) { echo "FAIL: " . $label . "\n"; exit(1); } echo "OK: " . $label . "\n"; } check(2 + 2 === 4, "math works");
php check.php
OK: math works
The helper prints one line per passing check and leaves the script with status 1 on the first failure, so a runner of any kind can read the result.
Table cases
One example proves little, so pack inputs and wanted outputs into a table and loop over it. Each row becomes a named case, and adding an edge case means adding one line.
<?php function check(bool $cond, string $label): void { if (!$cond) { echo "FAIL: " . $label . "\n"; exit(1); } echo "OK: " . $label . "\n"; } $cases = [ ["in" => 2, "want" => 4], ["in" => 5, "want" => 10], ["in" => 0, "want" => 0], ]; foreach ($cases as $c) { check($c["in"] * 2 === $c["want"], "doubles " . $c["in"]); }
php cases.php
OK: doubles 2
OK: doubles 5
OK: doubles 0
The label carries the input, so a failure names the row that broke without a second look at the table.
Run script
Keep every check in one plain script and run it with the PHP binary before each commit. A green run takes a blink, and a red run points at the exact line that broke the promise.
The file tests/check.php holds the helper and both sets of checks:
<?php function check(bool $cond, string $label): void { if (!$cond) { echo "FAIL: " . $label . "\n"; exit(1); } echo "OK: " . $label . "\n"; } check(2 + 2 === 4, "math works"); $cases = [ ["in" => 2, "want" => 4], ["in" => 5, "want" => 10], ["in" => 0, "want" => 0], ]; foreach ($cases as $c) { check($c["in"] * 2 === $c["want"], "doubles " . $c["in"]); }
php tests/check.php
OK: math works
OK: doubles 2
OK: doubles 5
OK: doubles 0
The four lines are the whole report of the file, and a single failing row would replace them with one FAIL line and status 1.
Red-green habit
Write the failing check first and watch it turn red, then write the smallest code that turns it green. The red step proves the test can catch the bug, and the green step proves the fix is real.
<?php function check(bool $cond, string $label): void { if (!$cond) { echo "FAIL: " . $label . "\n"; exit(1); } echo "OK: " . $label . "\n"; } check(strlen("hello") === 5, "length of hello"); check(trim(" hi ") === "hi", "trim spaces");
php green.php
OK: length of hello
OK: trim spaces
Both checks exercise a real function and pass, which is the green half of the habit; changing one expected value turns the same file red at that line.
Exercises
- Add a failing check to the run script and read its status with echo $?.
- Add a row to the table with a wrong expected value and read the label of the failure.
- Add a third check to the green example for strtoupper().
- Count the checks in the run script with a global counter and print the total.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What is the smallest useful test helper?
Answer
A function that compares a condition with a label and reports the result
The helper only needs to decide pass or fail and say which check it was.
-
Why does a table of cases beat one example?
Answer
It covers edge cases with one line each
Every row is a named case, so an empty input or a boundary value is cheap to add.
-
What should a check do when it fails?
Answer
Print the label and leave with a non-zero status
The label says what broke, and the status is what a runner or a CI step reads.
-
Why write a failing check first?
Answer
It proves the check can catch the bug
A check that has never failed may be testing nothing at all.
-
What does this program print?
<?php function check(bool $cond, string $label): void { if (!$cond) { echo "FAIL: " . $label . "\n"; exit(1); } echo "OK: " . $label . "\n"; } check(2 + 2 === 5, "math works");Answer
FAIL: math works
The condition is false, so the helper prints the failure label and stops.
-
What does this program print?
<?php function check(bool $cond, string $label): void { if (!$cond) { echo "FAIL: " . $label . "\n"; exit(1); } echo "OK: " . $label . "\n"; } check(2 + 2 === 4, "math works"); check("5" === 5, "strict comparison");Answer
OK: math works / FAIL: strict comparison
The first check passes and the second one compares a string with an integer under
===, so it fails. -
What does this program print?
<?php $cases = [ ["in" => 2, "want" => 4], ["in" => 5, "want" => 10], ]; $passed = 0; foreach ($cases as $c) { if ($c["in"] * 2 === $c["want"]) { $passed++; } } echo $passed;Answer
2
Both rows double correctly, so the counter ends at two.
-
What does this program print?
<?php function check(bool $cond, string $label): void { if (!$cond) { echo "FAIL: " . $label . "\n"; exit(1); } echo "OK: " . $label . "\n"; } check(strlen("hello") === 5, "length"); check(trim(" hi ") === "hi", "trim");Answer
OK: length / OK: trim
Both calls exercise the real library calls and both comparisons hold.
-
A test file prints
OKfor every case but never fails when the code breaks. What is the likely cause?Answer
The helper prints OK without checking the condition
A helper that does not branch on its argument reports a healthy suite that proves nothing.
-
Why run the same script before every commit instead of once a week?
Answer
A break is found while the change is still small
A failing check right after the change points at one small edit instead of a week of them.
The examples call strlen(), trim() and exit(). The programs on this page were run with PHP 8.5.13 (php -v) with the commands shown above.
Article author: Arthur Isaev