Skip to content

Instantly share code, notes, and snippets.

@aeveltstra
Created October 15, 2023 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aeveltstra/81ec97eec2e9e20a35e537c0a1b62184 to your computer and use it in GitHub Desktop.
Save aeveltstra/81ec97eec2e9e20a35e537c0a1b62184 to your computer and use it in GitHub Desktop.
php-unit-testing-without-a-framework
<?php
/**
* We don't always have the ability to use Composer on every PHP server we use.
* We also don't always have the spoons to vet every library or module. And yet,
* we still need to perform unit tests.
*
* Here's one way to perform unit tests in PHP 7.4 and newer. Everything is
* contained in this one page. Import the scripts that have functions to test,
* add them to a test batch, and load this page to run the tests and see the
* results printed to screen.
*
* Only failed tests get printed. If no tests fail, the script prints that
* "All's well."
*
* @author A.E.Veltstra for OmegaJunior Consultancy, LLC
* @version 2.23.1015.0940
*/
declare(strict_types=1);
error_reporting(E_ALL);
/** Equalities holds values for equality tests. Equality tests are
* true if their values equal. Each row should have values for a
* distinct test. Each row should be defined as:
* - test index number: used during logging to identify the test,
* - expected value (string): to compare against the actual value,
* - actual value (string): the value derived from an operation,
* to compare against the expected value.
*/
$equalities = [];
/** Inequalities holds values for difference tests. Each row should have
* values for a distinct test. Each row should be defined the same way
* as equalities. Difference tests are true if their values differ.
*/
$inequalities = [];
/* Some example tests that show the unit testing script itself works */
$equalities[] = [1, true, true)];
$equalities[] = [2, 'phrase', 'phrase'];
$inequalities[] = [3, true, false];
/* calling module functions */
require_once $_SERVER['DOCUMENT_ROOT'] . '/folder/module_under_test_1.php';
$inequalities[] = [4, 'expectation', module_function('parameter')];
/* calling page functions */
require_once $_SERVER['DOCUMENT_ROOT'] . '/umpire/page_under_test_2.php';
$inequalities[] = [5, 0, count(page_function_returning_elements_array())];
/* ensuring encryption works */
$equalities[] = [ 6,
'363f38ff73db62541bdffccb7b2af662c4e6687ab03896b45a047c501bfe30e03dc6d73c65227cf05bf20e77ceece3c6e73cedac37a159676a7d22b580ef4ad8',
hash('sha512','da5d1e035d2ff80c27a068ba766cff98c645462add916883a31670f30b36621ea8ca83c22739732552ab5671b3178bc64e1982286f6fb50d2c230ad61d357795')
];
/** Equality test. Returns true if values equal.
* Use this as the array_filter function.
*/
$a_equals_b = function($test):bool {
$good = $test[1] === $test[2];
return $good;
};
/** Inequality test. Returns true if values differ.
* Use this as the array_filter function.
*/
$a_differs_from_b = function($test):bool {
$fail = !($test[1] === $test[2]);
return $fail;
};
/** Perform the tests. */
$equality_failures = array_filter($equalities, $a_differs_from_b);
$inequality_failures = array_filter($inequalities, $a_equals_b);
/** Interpret the difference test results.
* Use this as the array_map function.
*/
$make_inequality_failure_output = function($test):string {
$a = $test[1];
$b = $test[2];
$counter = $test[0];
$a_type = gettype($a);
$b_type = gettype($b);
return "Fail: difference test ${counter} expected value ≤${a}≥ of type ${a_type} to differ from actual value ≤${b}≥ of type {$b_type}, but they are the same.";
};
/** Interpret the equality test results.
* Use this as the array_map function.
*/
$make_equality_failure_output = function($test):string {
$a = $test[1];
$b = $test[2];
$counter = $test[0];
$a_type = gettype($a);
$b_type = gettype($b);
return "Fail: equality test ${counter} expected value ≤${a}≥ of type ${a_type} to be the same as actual value ≤${b}≥ of type {$b_type}, but they differ.";
};
/** Gather the test result interpretations. */
$output = array_map($make_equality_failure_output, $equality_failures);
$ouput[] = array_map($make_inequality_failure_output, $inequality_failures);
/** Show the outcome(s). */
if (empty($output)) {
echo "All's well.";
}
foreach($output as $fail) {
echo "$fail\r\n";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment