|
| 1 | +<?php |
| 2 | + |
| 3 | +require_once __DIR__.'/../../../bootstrap/unit.php'; |
| 4 | + |
| 5 | +function removeTrailingSpaces(string $output): string |
| 6 | +{ |
| 7 | + return preg_replace("/ *\n/", "\n", $output); |
| 8 | +} |
| 9 | + |
| 10 | +function whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput) |
| 11 | +{ |
| 12 | + $test->diag($name); |
| 13 | + |
| 14 | + ob_start(); |
| 15 | + $exitCode = (new lime_harness())->executePhpFile(__DIR__.'/fixtures/'.$name.'.php'); |
| 16 | + $output = ob_get_clean(); |
| 17 | + |
| 18 | + $test->is($exitCode, $expectedStatusCode, 'with test '.$name.' will exit with status code '.$expectedStatusCode); |
| 19 | + |
| 20 | + $test->is(removeTrailingSpaces($output), $expectedOutput, 'test '.$name.' output'); |
| 21 | +} |
| 22 | + |
| 23 | +function whenExecuteHarnessWithFilesWillHaveResultAndOutput($test, $files, $expectedOverallSucceed, $expectedOutput, $message) |
| 24 | +{ |
| 25 | + $harness = new lime_harness(); |
| 26 | + $harness->output->colorizer = new lime_no_colorizer(); |
| 27 | + |
| 28 | + $harness->register($files); |
| 29 | + |
| 30 | + ob_start(); |
| 31 | + $allTestsSucceed = $harness->run(); |
| 32 | + $output = ob_get_clean(); |
| 33 | + |
| 34 | + $test->is($expectedOverallSucceed, $allTestsSucceed, $message); |
| 35 | + |
| 36 | + $test->is(removeTrailingSpaces($output), $expectedOutput, 'test harness result'); |
| 37 | +} |
| 38 | + |
| 39 | +class lime_no_colorizer extends lime_colorizer |
| 40 | +{ |
| 41 | + public function __construct() |
| 42 | + { |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +$test = new lime_test(16); |
| 47 | + |
| 48 | +$files = [ |
| 49 | + __DIR__.'/fixtures/failed.php', |
| 50 | + __DIR__.'/fixtures/failed_with_plan_less_than_total.php', |
| 51 | + __DIR__.'/fixtures/failed_with_plan_more_than_total.php', |
| 52 | + __DIR__.'/fixtures/pass.php', |
| 53 | + __DIR__.'/fixtures/pass_with_plan_less_than_total.php', |
| 54 | + __DIR__.'/fixtures/pass_with_plan_more_than_total.php', |
| 55 | +]; |
| 56 | +$expectedOverallSucceed = false; |
| 57 | +$expectedOutput = <<<'EOF' |
| 58 | +test/unit/vendor/lime/fixtures/failed................................not ok |
| 59 | + Failed tests: 1 |
| 60 | +test/unit/vendor/lime/fixtures/failed_with_plan_less_than_total......dubious |
| 61 | + Test returned status 255 |
| 62 | + Looks like you planned 1 test but ran 1 extra. |
| 63 | + Failed tests: 1 |
| 64 | +test/unit/vendor/lime/fixtures/failed_with_plan_more_than_total......dubious |
| 65 | + Test returned status 255 |
| 66 | + Looks like you planned 2 tests but only ran 1. |
| 67 | + Failed tests: 1 |
| 68 | +test/unit/vendor/lime/fixtures/pass..................................ok |
| 69 | +test/unit/vendor/lime/fixtures/pass_with_plan_less_than_total........dubious |
| 70 | + Test returned status 255 |
| 71 | + Looks like you planned 1 test but ran 1 extra. |
| 72 | +test/unit/vendor/lime/fixtures/pass_with_plan_more_than_total........dubious |
| 73 | + Test returned status 255 |
| 74 | + Looks like you planned 2 tests but only ran 1. |
| 75 | +Failed Test Stat Total Fail Errors List of Failed |
| 76 | +-------------------------------------------------------------------------- |
| 77 | +it/vendor/lime/fixtures/failed 0 1 1 0 1 |
| 78 | +iled_with_plan_less_than_total 255 2 1 0 1 |
| 79 | +iled_with_plan_more_than_total 255 1 1 0 1 |
| 80 | +pass_with_plan_less_than_total 255 2 0 0 |
| 81 | +pass_with_plan_more_than_total 255 1 0 0 |
| 82 | +Failed 5/6 test scripts, 16.67% okay. 5/10 subtests failed, 50.00% okay. |
| 83 | + |
| 84 | +EOF; |
| 85 | +$message = 'with at least one failed test file will fail the overall test suite'; |
| 86 | +whenExecuteHarnessWithFilesWillHaveResultAndOutput($test, $files, $expectedOverallSucceed, $expectedOutput, $message); |
| 87 | + |
| 88 | +$files = [__DIR__.'/fixtures/pass_with_plan_less_than_total.php']; |
| 89 | +$expectedOverallSucceed = false; |
| 90 | +$expectedOutput = <<<'EOF' |
| 91 | +test/unit/vendor/lime/fixtures/pass_with_plan_less_than_total........dubious |
| 92 | + Test returned status 255 |
| 93 | + Looks like you planned 1 test but ran 1 extra. |
| 94 | +Failed Test Stat Total Fail Errors List of Failed |
| 95 | +-------------------------------------------------------------------------- |
| 96 | +pass_with_plan_less_than_total 255 2 0 0 |
| 97 | +Failed 1/1 test scripts, 0.00% okay. 0/2 subtests failed, 100.00% okay. |
| 98 | + |
| 99 | +EOF; |
| 100 | +$message = 'with at least one test file that not follow the plan will fail the overall test suite'; |
| 101 | +whenExecuteHarnessWithFilesWillHaveResultAndOutput($test, $files, $expectedOverallSucceed, $expectedOutput, $message); |
| 102 | + |
| 103 | + |
| 104 | +$name = 'pass'; |
| 105 | +$expectedStatusCode = 0; |
| 106 | +$expectedOutput = <<<'EOF' |
| 107 | +ok 1 |
| 108 | +1..1 |
| 109 | +# Looks like everything went fine. |
| 110 | + |
| 111 | +EOF; |
| 112 | +whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput); |
| 113 | + |
| 114 | +$name = 'failed'; |
| 115 | +$expectedStatusCode = 0; |
| 116 | +$expectedOutput = <<<'EOF' |
| 117 | +not ok 1 |
| 118 | +# Failed test (./test/unit/vendor/lime/fixtures/failed.php at line 7) |
| 119 | +# got: false |
| 120 | +# expected: true |
| 121 | +1..1 |
| 122 | +# Looks like you failed 1 tests of 1. |
| 123 | + |
| 124 | +EOF; |
| 125 | +whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput); |
| 126 | + |
| 127 | +$name = 'failed_with_plan_less_than_total'; |
| 128 | +$expectedStatusCode = 0; |
| 129 | +$expectedOutput = <<<'EOF' |
| 130 | +1..1 |
| 131 | +not ok 1 |
| 132 | +# Failed test (./test/unit/vendor/lime/fixtures/failed_with_plan_less_than_total.php at line 7) |
| 133 | +# got: false |
| 134 | +# expected: true |
| 135 | +ok 2 |
| 136 | +# Looks like you planned 1 tests but ran 1 extra. |
| 137 | +# Looks like you failed 1 tests of 2. |
| 138 | + |
| 139 | +EOF; |
| 140 | +whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput); |
| 141 | + |
| 142 | +$name = 'failed_with_plan_more_than_total'; |
| 143 | +$expectedStatusCode = 0; |
| 144 | +$expectedOutput = <<<'EOF' |
| 145 | +1..2 |
| 146 | +not ok 1 |
| 147 | +# Failed test (./test/unit/vendor/lime/fixtures/failed_with_plan_more_than_total.php at line 7) |
| 148 | +# got: false |
| 149 | +# expected: true |
| 150 | +# Looks like you planned 2 tests but only ran 1. |
| 151 | +# Looks like you failed 1 tests of 1. |
| 152 | + |
| 153 | +EOF; |
| 154 | +whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput); |
| 155 | + |
| 156 | +$name = 'pass_with_plan_less_than_total'; |
| 157 | +$expectedStatusCode = 0; |
| 158 | +$expectedOutput = <<<'EOF' |
| 159 | +1..1 |
| 160 | +ok 1 |
| 161 | +ok 2 |
| 162 | +# Looks like you planned 1 tests but ran 1 extra. |
| 163 | + |
| 164 | +EOF; |
| 165 | +whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput); |
| 166 | + |
| 167 | +$name = 'pass_with_plan_more_than_total'; |
| 168 | +$expectedStatusCode = 0; |
| 169 | +$expectedOutput = <<<'EOF' |
| 170 | +1..2 |
| 171 | +ok 1 |
| 172 | +# Looks like you planned 2 tests but only ran 1. |
| 173 | + |
| 174 | +EOF; |
| 175 | +whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput); |
0 commit comments