|
| 1 | +<?php |
| 2 | + |
| 3 | +require_once __DIR__.'/../../../bootstrap/unit.php'; |
| 4 | +require_once __DIR__.'/tools/TestCase.php'; |
| 5 | +require_once __DIR__.'/tools/lime_no_colorizer.php'; |
| 6 | + |
| 7 | +class lime_harnessTest extends TestCase |
| 8 | +{ |
| 9 | + private function makeHarness(): lime_harness |
| 10 | + { |
| 11 | + $harness = new lime_harness(); |
| 12 | + $harness->output->colorizer = new lime_no_colorizer(); |
| 13 | + |
| 14 | + return $harness; |
| 15 | + } |
| 16 | + |
| 17 | + private function whenExecuteHarnessWithOneFileWillHaveStatusCodeAndOutput($name, $expectedOverallSucceed, $expectedOutput) |
| 18 | + { |
| 19 | + $this->info($name); |
| 20 | + |
| 21 | + $harness = $this->makeHarness(); |
| 22 | + |
| 23 | + $harness->register([ |
| 24 | + __DIR__."/fixtures/$name.php", |
| 25 | + ]); |
| 26 | + |
| 27 | + $this->assertHarnessWithExitStatusAndOutput($harness, $expectedOverallSucceed, $expectedOutput); |
| 28 | + } |
| 29 | + |
| 30 | + private function assertHarnessWithExitStatusAndOutput(lime_harness $harness, $expectedOverallSucceed, string $expectedOutput) |
| 31 | + { |
| 32 | + ob_start(); |
| 33 | + $allTestsSucceed = $harness->run(); |
| 34 | + $output = ob_get_clean(); |
| 35 | + |
| 36 | + $this->is($expectedOverallSucceed, $allTestsSucceed, 'expect overall test '.($expectedOverallSucceed ? 'succeed' : 'failed')); |
| 37 | + |
| 38 | + $this->is($this->removeTrailingSpaces($output), $expectedOutput, 'test harness result output'); |
| 39 | + } |
| 40 | + |
| 41 | + private function removeTrailingSpaces(string $output): string |
| 42 | + { |
| 43 | + return preg_replace("/ *\n/", "\n", $output); |
| 44 | + } |
| 45 | + |
| 46 | + public function testOnlyOneTestFile(): void |
| 47 | + { |
| 48 | + foreach ($this->provideOnlyOneTestFile() as $parameters) { |
| 49 | + $this->whenExecuteHarnessWithOneFileWillHaveStatusCodeAndOutput(...$parameters); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private function provideOnlyOneTestFile(): iterable |
| 54 | + { |
| 55 | + yield [ |
| 56 | + /* name */ 'pass', |
| 57 | + /* expectedOverallSucceed */ true, |
| 58 | + /* expectedOutput */ <<<'EOF' |
| 59 | +test/unit/vendor/lime/fixtures/pass..................................ok |
| 60 | + All tests successful. |
| 61 | + Files=1, Tests=1 |
| 62 | + |
| 63 | +EOF |
| 64 | + ]; |
| 65 | + |
| 66 | + yield [ |
| 67 | + /* name */ 'pass_with_plan_less_than_total', |
| 68 | + /* expectedOverallSucceed */ false, |
| 69 | + /* expectedOutput */ <<<'EOF' |
| 70 | +test/unit/vendor/lime/fixtures/pass_with_plan_less_than_total........dubious |
| 71 | + Test returned status 255 |
| 72 | + Looks like you planned 1 test but ran 1 extra. |
| 73 | +Failed Test Stat Total Fail Errors List of Failed |
| 74 | +-------------------------------------------------------------------------- |
| 75 | +pass_with_plan_less_than_total 255 2 0 0 |
| 76 | +Failed 1/1 test scripts, 0.00% okay. 0/2 subtests failed, 100.00% okay. |
| 77 | + |
| 78 | +EOF |
| 79 | + ]; |
| 80 | + |
| 81 | + yield [ |
| 82 | + /* name */ 'pass_with_plan_more_than_total', |
| 83 | + /* expectedOverallSucceed */ false, |
| 84 | + /* expectedOutput */ <<<'EOF' |
| 85 | +test/unit/vendor/lime/fixtures/pass_with_plan_more_than_total........dubious |
| 86 | + Test returned status 255 |
| 87 | + Looks like you planned 2 tests but only ran 1. |
| 88 | +Failed Test Stat Total Fail Errors List of Failed |
| 89 | +-------------------------------------------------------------------------- |
| 90 | +pass_with_plan_more_than_total 255 1 0 0 |
| 91 | +Failed 1/1 test scripts, 0.00% okay. 1/2 subtests failed, 50.00% okay. |
| 92 | + |
| 93 | +EOF |
| 94 | + ]; |
| 95 | + |
| 96 | + yield [ |
| 97 | + /* name */ 'pass_with_one_error', |
| 98 | + /* expectedOverallSucceed */ false, |
| 99 | + /* expectedOutput */ <<<'EOF' |
| 100 | +test/unit/vendor/lime/fixtures/pass_with_one_error...................errors |
| 101 | + Errors: |
| 102 | + - Notice: some user error message |
| 103 | +Failed Test Stat Total Fail Errors List of Failed |
| 104 | +-------------------------------------------------------------------------- |
| 105 | +e/fixtures/pass_with_one_error 0 1 0 1 |
| 106 | +Failed 1/1 test scripts, 0.00% okay. 0/1 subtests failed, 100.00% okay. |
| 107 | + |
| 108 | +EOF |
| 109 | + ]; |
| 110 | + |
| 111 | + yield [ |
| 112 | + /* name */ 'pass_with_one_throw_exception', |
| 113 | + /* expectedOverallSucceed */ false, |
| 114 | + /* expectedOutput */ <<<'EOF' |
| 115 | +test/unit/vendor/lime/fixtures/pass_with_one_throw_exception.........errors |
| 116 | + Errors: |
| 117 | + - LogicException: some exception message |
| 118 | +Failed Test Stat Total Fail Errors List of Failed |
| 119 | +-------------------------------------------------------------------------- |
| 120 | +/pass_with_one_throw_exception 0 0 0 1 |
| 121 | +Failed 1/1 test scripts, 0.00% okay. 0/0 subtests failed, 0.00% okay. |
| 122 | + |
| 123 | +EOF |
| 124 | + ]; |
| 125 | + |
| 126 | + yield [ |
| 127 | + /* name */ 'pass_with_one_parse_error', |
| 128 | + /* expectedOverallSucceed */ false, |
| 129 | + /* expectedOutput */ <<<'EOF' |
| 130 | +test/unit/vendor/lime/fixtures/pass_with_one_parse_error.............dubious |
| 131 | + Test returned status 255 |
| 132 | + Failed tests: 0 |
| 133 | +Failed Test Stat Total Fail Errors List of Failed |
| 134 | +-------------------------------------------------------------------------- |
| 135 | +ures/pass_with_one_parse_error 255 1 1 0 0 |
| 136 | +Failed 1/1 test scripts, 0.00% okay. 1/0 subtests failed, 0.00% okay. |
| 137 | + |
| 138 | +EOF |
| 139 | + ]; |
| 140 | + |
| 141 | + yield [ |
| 142 | + /* name */ 'skip', |
| 143 | + /* expectedOverallSucceed */ true, |
| 144 | + /* expectedOutput */ <<<'EOF' |
| 145 | +test/unit/vendor/lime/fixtures/skip..................................ok |
| 146 | + All tests successful. |
| 147 | + Files=1, Tests=1 |
| 148 | + |
| 149 | +EOF |
| 150 | + ]; |
| 151 | + |
| 152 | + yield [ |
| 153 | + /* name */ 'skip_with_plan', |
| 154 | + /* expectedOverallSucceed */ true, |
| 155 | + /* expectedOutput */ <<<'EOF' |
| 156 | +test/unit/vendor/lime/fixtures/skip_with_plan........................ok |
| 157 | + All tests successful. |
| 158 | + Files=1, Tests=2 |
| 159 | + |
| 160 | +EOF |
| 161 | + ]; |
| 162 | + |
| 163 | + yield [ |
| 164 | + /* name */ 'failed', |
| 165 | + /* expectedOverallSucceed */ false, |
| 166 | + /* expectedOutput */ <<<'EOF' |
| 167 | +test/unit/vendor/lime/fixtures/failed................................not ok |
| 168 | + Failed tests: 1 |
| 169 | +Failed Test Stat Total Fail Errors List of Failed |
| 170 | +-------------------------------------------------------------------------- |
| 171 | +it/vendor/lime/fixtures/failed 0 1 1 0 1 |
| 172 | +Failed 1/1 test scripts, 0.00% okay. 1/1 subtests failed, 0.00% okay. |
| 173 | + |
| 174 | +EOF |
| 175 | + ]; |
| 176 | + |
| 177 | + yield [ |
| 178 | + /* name */ 'failed_with_plan_less_than_total', |
| 179 | + /* expectedOverallSucceed */ false, |
| 180 | + /* expectedOutput */ <<<'EOF' |
| 181 | +test/unit/vendor/lime/fixtures/failed_with_plan_less_than_total......dubious |
| 182 | + Test returned status 255 |
| 183 | + Looks like you planned 1 test but ran 1 extra. |
| 184 | + Failed tests: 1 |
| 185 | +Failed Test Stat Total Fail Errors List of Failed |
| 186 | +-------------------------------------------------------------------------- |
| 187 | +iled_with_plan_less_than_total 255 2 1 0 1 |
| 188 | +Failed 1/1 test scripts, 0.00% okay. 1/2 subtests failed, 50.00% okay. |
| 189 | + |
| 190 | +EOF |
| 191 | + ]; |
| 192 | + |
| 193 | + yield [ |
| 194 | + /* name */ 'failed_with_plan_more_than_total', |
| 195 | + /* expectedOverallSucceed */ false, |
| 196 | + /* expectedOutput */ <<<'EOF' |
| 197 | +test/unit/vendor/lime/fixtures/failed_with_plan_more_than_total......dubious |
| 198 | + Test returned status 255 |
| 199 | + Looks like you planned 2 tests but only ran 1. |
| 200 | + Failed tests: 1 |
| 201 | +Failed Test Stat Total Fail Errors List of Failed |
| 202 | +-------------------------------------------------------------------------- |
| 203 | +iled_with_plan_more_than_total 255 1 1 0 1 |
| 204 | +Failed 1/1 test scripts, 0.00% okay. 2/2 subtests failed, 0.00% okay. |
| 205 | + |
| 206 | +EOF |
| 207 | + ]; |
| 208 | + } |
| 209 | + |
| 210 | + public function test_registerFilesWithGlob_thenRunThemAll(): void |
| 211 | + { |
| 212 | + $harness = $this->makeHarness(); |
| 213 | + |
| 214 | + $harness->register_glob(__DIR__."/fixtures/*.php"); |
| 215 | + |
| 216 | + $this->assertHarnessWithExitStatusAndOutput($harness, false, <<<'EOF' |
| 217 | +test/unit/vendor/lime/fixtures/failed................................not ok |
| 218 | + Failed tests: 1 |
| 219 | +test/unit/vendor/lime/fixtures/failed_with_plan_less_than_total......dubious |
| 220 | + Test returned status 255 |
| 221 | + Looks like you planned 1 test but ran 1 extra. |
| 222 | + Failed tests: 1 |
| 223 | +test/unit/vendor/lime/fixtures/failed_with_plan_more_than_total......dubious |
| 224 | + Test returned status 255 |
| 225 | + Looks like you planned 2 tests but only ran 1. |
| 226 | + Failed tests: 1 |
| 227 | +test/unit/vendor/lime/fixtures/pass..................................ok |
| 228 | +test/unit/vendor/lime/fixtures/pass_with_one_error...................errors |
| 229 | + Errors: |
| 230 | + - Notice: some user error message |
| 231 | +test/unit/vendor/lime/fixtures/pass_with_one_parse_error.............dubious |
| 232 | + Test returned status 255 |
| 233 | + Failed tests: 0 |
| 234 | +test/unit/vendor/lime/fixtures/pass_with_one_throw_exception.........errors |
| 235 | + Errors: |
| 236 | + - LogicException: some exception message |
| 237 | +test/unit/vendor/lime/fixtures/pass_with_plan_less_than_total........dubious |
| 238 | + Test returned status 255 |
| 239 | + Looks like you planned 1 test but ran 1 extra. |
| 240 | +test/unit/vendor/lime/fixtures/pass_with_plan_more_than_total........dubious |
| 241 | + Test returned status 255 |
| 242 | + Looks like you planned 2 tests but only ran 1. |
| 243 | +test/unit/vendor/lime/fixtures/skip..................................ok |
| 244 | +test/unit/vendor/lime/fixtures/skip_with_plan........................ok |
| 245 | +Failed Test Stat Total Fail Errors List of Failed |
| 246 | +-------------------------------------------------------------------------- |
| 247 | +it/vendor/lime/fixtures/failed 0 1 1 0 1 |
| 248 | +iled_with_plan_less_than_total 255 2 1 0 1 |
| 249 | +iled_with_plan_more_than_total 255 1 1 0 1 |
| 250 | +e/fixtures/pass_with_one_error 0 1 0 1 |
| 251 | +ures/pass_with_one_parse_error 255 1 1 0 0 |
| 252 | +/pass_with_one_throw_exception 0 0 0 1 |
| 253 | +pass_with_plan_less_than_total 255 2 0 0 |
| 254 | +pass_with_plan_more_than_total 255 1 0 0 |
| 255 | +Failed 8/11 test scripts, 27.27% okay. 6/14 subtests failed, 57.14% okay. |
| 256 | + |
| 257 | +EOF); |
| 258 | + } |
| 259 | +} |
| 260 | + |
| 261 | +(new lime_harnessTest())->run(); |
0 commit comments