|
| 1 | +<?php |
| 2 | +namespace Coralys\Tests; |
| 3 | + |
| 4 | +/** |
| 5 | + * A simple utility to pretty-up your test suites by simply |
| 6 | + * and neatly printing out the PASS/Fail result with comment and checkmark. |
| 7 | + * The user only has to call AnnounceCase() at the beginning of the test case |
| 8 | + * and then PrintResult() to test the condition which internally takes care |
| 9 | + * of updating the tally. Internally a shutdown function is registered so that |
| 10 | + * when the test suite file terminates, a summary of passed/failed is presented. |
| 11 | + * |
| 12 | + * Example: |
| 13 | + * use Coralys\Tests; |
| 14 | + * function testCaseOne { |
| 15 | + * AnnounceCase("Testing constructor"); |
| 16 | + * : do the test |
| 17 | + * PrintResult($got == $obtained, 'Constructor with parameters'); |
| 18 | + * } |
| 19 | + */ |
| 20 | + |
| 21 | +ini_set('display_errors', '1'); |
| 22 | +ini_set('display_startup_errors', '1'); |
| 23 | +require_once('functions.php'); |
| 24 | + |
| 25 | +/* |
| 26 | + * G L O B A L S |
| 27 | + */ |
| 28 | +$Ctx = new TestContext(); // But no need to access it outside. PrintResult takes care of it. |
| 29 | + |
| 30 | +/* |
| 31 | + * C L A S S E S |
| 32 | + */ |
| 33 | + |
| 34 | +/** |
| 35 | + * A Test Context is a unique instance for every file that runs one or |
| 36 | + * more tests. It must be instantiated in the file that has the test cases. |
| 37 | + * Example: |
| 38 | + * $Ctx = new Coralys\Tests\TestContext(); |
| 39 | + */ |
| 40 | +class TestContext { |
| 41 | + public $Passed; |
| 42 | + public $Failed; |
| 43 | + public $SuiteCount; |
| 44 | + public $TotalPassed; |
| 45 | + public $TotalFailed; |
| 46 | + public $CurrentSuite; |
| 47 | + |
| 48 | + public function Pass() { |
| 49 | + $this->Passed += 1; |
| 50 | + } |
| 51 | + |
| 52 | + public function Fail() { |
| 53 | + $this->Failed += 1; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Usually invoked by AnnounceTestSuite() and saves the |
| 58 | + * current tally to the total (accumulative) and resets |
| 59 | + * the current count for the new test suite. |
| 60 | + * @see AnnounceTestSuite() |
| 61 | + */ |
| 62 | + public function Accumulate(string $currentSuite) { |
| 63 | + $this->TotalPassed += $this->Passed; |
| 64 | + $this->TotalFailed += $this->Failed; |
| 65 | + $this->Passed = 0; |
| 66 | + $this->Failed = 0; |
| 67 | + $this->SuiteCount += 1; |
| 68 | + $this->CurrentSuite = $currentSuite; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Resets both the current test suite tally as well as the |
| 73 | + * totals. |
| 74 | + */ |
| 75 | + public function Reset() { |
| 76 | + $this->Passed = 0; |
| 77 | + $this->Failed = 0; |
| 78 | + $this->TotalPassed = 0; |
| 79 | + $this->TotalFailed = 0; |
| 80 | + } |
| 81 | + |
| 82 | + private function SubTotal(): int { |
| 83 | + return $this->Passed + $this->Failed; |
| 84 | + } |
| 85 | + |
| 86 | + private function Total(): int { |
| 87 | + return $this->TotalPassed + $this->TotalFailed; |
| 88 | + } |
| 89 | + |
| 90 | + private function SubPercentPassed(): int { |
| 91 | + if ($this->SubTotal() == 0) { |
| 92 | + return 0; |
| 93 | + } |
| 94 | + return intval(100 * $this->Passed / $this->SubTotal()); |
| 95 | + } |
| 96 | + |
| 97 | + private function TotalPercentPassed(): int { |
| 98 | + return intval(100 * $this->Passed / $this->Total()); |
| 99 | + } |
| 100 | + |
| 101 | + public function String() { |
| 102 | + printf("\n\n\tUnit Test Summary\n"); |
| 103 | + printf("\t(%s)\n", $this->CurrentSuite); |
| 104 | + if ($this->SuiteCount > 1) { |
| 105 | + |
| 106 | + } |
| 107 | + $pctPassed = $this->SubPercentPassed(); |
| 108 | + $pctFailed = 100 - $pctPassed; |
| 109 | + printf("\t============================\n\t\u{2705} Passed: %3d / %-4d (%d%%)\n", |
| 110 | + $this->Passed, $this->SubTotal(), $pctPassed |
| 111 | + ); |
| 112 | + if ($this->Failed > 0) { |
| 113 | + printf("\t\u{274c} Failed: %3d / %-4d (%d%%)\n", |
| 114 | + $this->Failed, $this->SubTotal(), $pctFailed |
| 115 | + ); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | + |
| 121 | +function shutdown() |
| 122 | +{ |
| 123 | + // This is our shutdown function, in |
| 124 | + // here we can do any last operations |
| 125 | + // before the script is complete. |
| 126 | + |
| 127 | + //echo 'Script executed with success', PHP_EOL; |
| 128 | + global $Ctx; |
| 129 | + $Ctx->String(); |
| 130 | +} |
| 131 | + |
| 132 | +register_shutdown_function('Coralys\Tests\shutdown'); |
| 133 | + |
| 134 | +/** |
| 135 | + * Convert a boolean to OK or FAILED. |
| 136 | + * @see PrintResult() |
| 137 | + */ |
| 138 | +function Result($condition) { |
| 139 | + global $Ctx; |
| 140 | + if ($condition) { |
| 141 | + $Ctx->Pass(); |
| 142 | + } else { |
| 143 | + $Ctx->Fail(); |
| 144 | + } |
| 145 | + return $condition ? 'OK' : 'FAILED'; |
| 146 | +} |
| 147 | + |
| 148 | + |
| 149 | +?> |
| 150 | + |
0 commit comments