From d943f485ebe235e98847064427cd97ce4da4d8ed Mon Sep 17 00:00:00 2001 From: Alexandre Quercia Date: Sun, 31 Mar 2024 23:20:49 +0200 Subject: [PATCH] fixup! fix(test): exit code of lime test --- test/unit/vendor/lime/lime_harnessTest.php | 226 ++++++++++++--------- test/unit/vendor/lime/lime_testTest.php | 146 +++++++------ 2 files changed, 220 insertions(+), 152 deletions(-) diff --git a/test/unit/vendor/lime/lime_harnessTest.php b/test/unit/vendor/lime/lime_harnessTest.php index d3ee8606f..a72213ca3 100644 --- a/test/unit/vendor/lime/lime_harnessTest.php +++ b/test/unit/vendor/lime/lime_harnessTest.php @@ -2,78 +2,82 @@ require_once __DIR__.'/../../../bootstrap/unit.php'; -function removeTrailingSpaces(string $output): string +class lime_no_colorizer extends lime_colorizer { - return preg_replace("/ *\n/", "\n", $output); + public function __construct() + { + } } -function whenExecuteHarnessWithFilesWillHaveResultAndOutput($test, $files, $expectedOverallSucceed, $expectedOutput, $message) +class lime_harnessTest { - $harness = new lime_harness(); - $harness->output->colorizer = new lime_no_colorizer(); + private $test; - $harness->register($files); + public function __construct() + { + $this->test = new lime_test(); + } - ob_start(); - $allTestsSucceed = $harness->run(); - $output = ob_get_clean(); + private function whenExecuteHarnessWithFilesWillHaveResultAndOutput($message, $files, $expectedOverallSucceed, $expectedOutput) + { + $this->test->info($message); - $test->is($expectedOverallSucceed, $allTestsSucceed, $message); + $harness = $this->makeHarnessWithFiles($files); - $test->is(removeTrailingSpaces($output), $expectedOutput, 'test harness result output'); -} + ob_start(); + $allTestsSucceed = $harness->run(); + $output = ob_get_clean(); -class lime_no_colorizer extends lime_colorizer -{ - public function __construct() + $this->test->is($expectedOverallSucceed, $allTestsSucceed, 'overall test '.($expectedOverallSucceed ? 'succeed' : 'failed')); + + $this->test->is($this->removeTrailingSpaces($output), $expectedOutput, 'test harness result output'); + } + + private function makeHarnessWithFiles($files): lime_harness { + $harness = new lime_harness(); + $harness->output->colorizer = new lime_no_colorizer(); + + $harness->register($files); + + return $harness; } -} -$test = new lime_test(12); - -$files = [ - __DIR__.'/fixtures/failed.php', - __DIR__.'/fixtures/failed_with_plan_less_than_total.php', - __DIR__.'/fixtures/failed_with_plan_more_than_total.php', - __DIR__.'/fixtures/pass.php', - __DIR__.'/fixtures/pass_with_plan_less_than_total.php', - __DIR__.'/fixtures/pass_with_plan_more_than_total.php', -]; -$expectedOverallSucceed = false; -$expectedOutput = <<<'EOF' -test/unit/vendor/lime/fixtures/failed................................not ok - Failed tests: 1 -test/unit/vendor/lime/fixtures/failed_with_plan_less_than_total......not ok - Looks like you planned 1 test but ran 1 extra. - Failed tests: 1 -test/unit/vendor/lime/fixtures/failed_with_plan_more_than_total......not ok - Looks like you planned 2 tests but only ran 1. - Failed tests: 1 -test/unit/vendor/lime/fixtures/pass..................................ok -test/unit/vendor/lime/fixtures/pass_with_plan_less_than_total........dubious - Test returned status 255 - Looks like you planned 1 test but ran 1 extra. -test/unit/vendor/lime/fixtures/pass_with_plan_more_than_total........dubious - Test returned status 255 - Looks like you planned 2 tests but only ran 1. -Failed Test Stat Total Fail Errors List of Failed --------------------------------------------------------------------------- -it/vendor/lime/fixtures/failed 1 1 1 0 1 -iled_with_plan_less_than_total 1 2 1 0 1 -iled_with_plan_more_than_total 1 1 1 0 1 -pass_with_plan_less_than_total 255 2 0 0 -pass_with_plan_more_than_total 255 1 0 0 -Failed 5/6 test scripts, 16.67% okay. 5/10 subtests failed, 50.00% okay. + private function removeTrailingSpaces(string $output): string + { + return preg_replace("/ *\n/", "\n", $output); + } -EOF; -$message = 'with at least one failed test file will fail the overall test suite'; -whenExecuteHarnessWithFilesWillHaveResultAndOutput($test, $files, $expectedOverallSucceed, $expectedOutput, $message); + public function run(): void + { + foreach ($this->provideTestCases() as $parameters) { + $this->whenExecuteHarnessWithFilesWillHaveResultAndOutput(...$parameters); + } + } + private function provideTestCases() + { + yield [ + /* name */ 'with all tests passes without error and exception will succeed the overall test suite', + /* files */ [ + __DIR__.'/fixtures/pass.php', + ], + /* expectedOverallSucceed */ true, + /* expectedOutput */ <<<'EOF' +test/unit/vendor/lime/fixtures/pass..................................ok + All tests successful. + Files=1, Tests=1 -$files = [__DIR__.'/fixtures/pass_with_plan_less_than_total.php']; -$expectedOverallSucceed = false; -$expectedOutput = <<<'EOF' +EOF + ]; + + yield [ + /* name */ 'with at least one test file that not follow the plan will fail the overall test suite', + /* files */ [ + __DIR__.'/fixtures/pass_with_plan_less_than_total.php', + ], + /* expectedOverallSucceed */ false, + /* expectedOutput */ <<<'EOF' test/unit/vendor/lime/fixtures/pass_with_plan_less_than_total........dubious Test returned status 255 Looks like you planned 1 test but ran 1 extra. @@ -82,14 +86,16 @@ public function __construct() pass_with_plan_less_than_total 255 2 0 0 Failed 1/1 test scripts, 0.00% okay. 0/2 subtests failed, 100.00% okay. -EOF; -$message = 'with at least one test file that not follow the plan will fail the overall test suite'; -whenExecuteHarnessWithFilesWillHaveResultAndOutput($test, $files, $expectedOverallSucceed, $expectedOutput, $message); - +EOF + ]; -$files = [__DIR__.'/fixtures/pass_with_one_error.php']; -$expectedOverallSucceed = false; -$expectedOutput = <<<'EOF' + yield [ + /* name */ 'with at least one error will fail the overall test suite', + /* files */ [ + __DIR__.'/fixtures/pass_with_one_error.php', + ], + /* expectedOverallSucceed */ false, + /* expectedOutput */ <<<'EOF' test/unit/vendor/lime/fixtures/pass_with_one_error...................errors Errors: - Notice: some user error message @@ -98,14 +104,16 @@ public function __construct() e/fixtures/pass_with_one_error 1 1 0 1 Failed 1/1 test scripts, 0.00% okay. 0/1 subtests failed, 100.00% okay. -EOF; -$message = 'with at least one error will fail the overall test suite'; -whenExecuteHarnessWithFilesWillHaveResultAndOutput($test, $files, $expectedOverallSucceed, $expectedOutput, $message); - +EOF + ]; -$files = [__DIR__.'/fixtures/pass_with_one_throw_exception.php']; -$expectedOverallSucceed = false; -$expectedOutput = <<<'EOF' + yield [ + /* name */ 'with at least one thrown Exception will fail the overall test suite', + /* files */ [ + __DIR__.'/fixtures/pass_with_one_throw_exception.php', + ], + /* expectedOverallSucceed */ false, + /* expectedOutput */ <<<'EOF' test/unit/vendor/lime/fixtures/pass_with_one_throw_exception.........errors Errors: - LogicException: some exception message @@ -114,26 +122,16 @@ public function __construct() /pass_with_one_throw_exception 1 0 0 1 Failed 1/1 test scripts, 0.00% okay. 0/0 subtests failed, 0.00% okay. -EOF; -$message = 'with at least one thrown Exception will fail the overall test suite'; -whenExecuteHarnessWithFilesWillHaveResultAndOutput($test, $files, $expectedOverallSucceed, $expectedOutput, $message); - - -$files = [__DIR__.'/fixtures/pass.php']; -$expectedOverallSucceed = true; -$expectedOutput = <<<'EOF' -test/unit/vendor/lime/fixtures/pass..................................ok - All tests successful. - Files=1, Tests=1 +EOF + ]; -EOF; -$message = 'with all tests passes without error and exception will succeed the overall test suite'; -whenExecuteHarnessWithFilesWillHaveResultAndOutput($test, $files, $expectedOverallSucceed, $expectedOutput, $message); - - -$files = [__DIR__.'/fixtures/pass_with_one_parse_error.php']; -$expectedOverallSucceed = false; -$expectedOutput = <<<'EOF' + yield [ + /* name */ 'with parse error will fail the overall test suite', + /* files */ [ + __DIR__.'/fixtures/pass_with_one_parse_error.php', + ], + /* expectedOverallSucceed */ false, + /* expectedOutput */ <<<'EOF' test/unit/vendor/lime/fixtures/pass_with_one_parse_error.............errors Errors: - Missing test report. It is probably due to a Parse error. @@ -142,6 +140,48 @@ public function __construct() ures/pass_with_one_parse_error 255 0 0 1 Failed 1/1 test scripts, 0.00% okay. 0/0 subtests failed, 0.00% okay. -EOF; -$message = 'with parse error will fail the overall test suite'; -whenExecuteHarnessWithFilesWillHaveResultAndOutput($test, $files, $expectedOverallSucceed, $expectedOutput, $message); +EOF + ]; + + yield [ + /* name */ 'with at least one failed test file will fail the overall test suite', + /* files */ [ + __DIR__.'/fixtures/failed.php', + __DIR__.'/fixtures/failed_with_plan_less_than_total.php', + __DIR__.'/fixtures/failed_with_plan_more_than_total.php', + __DIR__.'/fixtures/pass.php', + __DIR__.'/fixtures/pass_with_plan_less_than_total.php', + __DIR__.'/fixtures/pass_with_plan_more_than_total.php', + ], + /* expectedOverallSucceed */ false, + /* expectedOutput */ <<<'EOF' +test/unit/vendor/lime/fixtures/failed................................not ok + Failed tests: 1 +test/unit/vendor/lime/fixtures/failed_with_plan_less_than_total......not ok + Looks like you planned 1 test but ran 1 extra. + Failed tests: 1 +test/unit/vendor/lime/fixtures/failed_with_plan_more_than_total......not ok + Looks like you planned 2 tests but only ran 1. + Failed tests: 1 +test/unit/vendor/lime/fixtures/pass..................................ok +test/unit/vendor/lime/fixtures/pass_with_plan_less_than_total........dubious + Test returned status 255 + Looks like you planned 1 test but ran 1 extra. +test/unit/vendor/lime/fixtures/pass_with_plan_more_than_total........dubious + Test returned status 255 + Looks like you planned 2 tests but only ran 1. +Failed Test Stat Total Fail Errors List of Failed +-------------------------------------------------------------------------- +it/vendor/lime/fixtures/failed 1 1 1 0 1 +iled_with_plan_less_than_total 1 2 1 0 1 +iled_with_plan_more_than_total 1 1 1 0 1 +pass_with_plan_less_than_total 255 2 0 0 +pass_with_plan_more_than_total 255 1 0 0 +Failed 5/6 test scripts, 16.67% okay. 5/10 subtests failed, 50.00% okay. + +EOF + ]; + } +} + +(new lime_harnessTest())->run(); diff --git a/test/unit/vendor/lime/lime_testTest.php b/test/unit/vendor/lime/lime_testTest.php index 99491be41..a43dd159c 100644 --- a/test/unit/vendor/lime/lime_testTest.php +++ b/test/unit/vendor/lime/lime_testTest.php @@ -2,39 +2,57 @@ require_once __DIR__.'/../../../bootstrap/unit.php'; -function removeTrailingSpaces(string $output): string +class lime_testTest { - return preg_replace("/ *\n/", "\n", $output); -} - -function whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput) -{ - $test->diag($name); - - ob_start(); - $exitCode = (new lime_harness())->executePhpFile(__DIR__.'/fixtures/'.$name.'.php'); - $output = ob_get_clean(); - - $test->is($exitCode, $expectedStatusCode, 'with test '.$name.' will exit with status code '.$expectedStatusCode); - - $test->is(removeTrailingSpaces($output), $expectedOutput, 'test '.$name.' output'); -} - -$test = new lime_test(16); - -$name = 'pass'; -$expectedStatusCode = 0; -$expectedOutput = <<<'EOF' + private $test; + + public function __construct() + { + $this->test = new lime_test(); + } + + private function removeTrailingSpaces(string $output): string + { + return preg_replace("/ *\n/", "\n", $output); + } + + private function whenExecutePhpFileWillHaveStatusCodeAndOutput($name, $expectedStatusCode, $expectedOutput) + { + $this->test->info($name); + + ob_start(); + $exitCode = (new lime_harness())->executePhpFile(__DIR__.'/fixtures/'.$name.'.php'); + $output = ob_get_clean(); + + $this->test->is($exitCode, $expectedStatusCode, 'with test '.$name.' will exit with status code '.$expectedStatusCode); + + $this->test->is($this->removeTrailingSpaces($output), $expectedOutput, 'test '.$name.' output'); + } + + public function run() + { + foreach ($this->provideTestCases() as $parameters) { + $this->whenExecutePhpFileWillHaveStatusCodeAndOutput(...$parameters); + } + } + + private function provideTestCases() + { + yield [ + /* name */ 'pass', + /* expectedStatusCode*/ 0, + /* expectedOutput */ <<<'EOF' ok 1 1..1 # Looks like everything went fine. -EOF; -whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput); +EOF + ]; -$name = 'failed'; -$expectedStatusCode = 1; -$expectedOutput = <<<'EOF' + yield [ + /* name */ 'failed', + /* expectedStatusCode*/ 1, + /* expectedOutput */ <<<'EOF' not ok 1 # Failed test (./test/unit/vendor/lime/fixtures/failed.php at line 7) # got: false @@ -42,12 +60,13 @@ function whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $ 1..1 # Looks like you failed 1 tests of 1. -EOF; -whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput); +EOF + ]; -$name = 'failed_with_plan_less_than_total'; -$expectedStatusCode = 1; -$expectedOutput = <<<'EOF' + yield [ + /* name */ 'failed_with_plan_less_than_total', + /* expectedStatusCode*/ 1, + /* expectedOutput */ <<<'EOF' 1..1 not ok 1 # Failed test (./test/unit/vendor/lime/fixtures/failed_with_plan_less_than_total.php at line 7) @@ -57,12 +76,13 @@ function whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $ # Looks like you planned 1 tests but ran 1 extra. # Looks like you failed 1 tests of 2. -EOF; -whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput); +EOF + ]; -$name = 'failed_with_plan_more_than_total'; -$expectedStatusCode = 1; -$expectedOutput = <<<'EOF' + yield [ + /* name */ 'failed_with_plan_more_than_total', + /* expectedStatusCode*/ 1, + /* expectedOutput */ <<<'EOF' 1..2 not ok 1 # Failed test (./test/unit/vendor/lime/fixtures/failed_with_plan_more_than_total.php at line 7) @@ -71,33 +91,36 @@ function whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $ # Looks like you planned 2 tests but only ran 1. # Looks like you failed 1 tests of 1. -EOF; -whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput); +EOF + ]; -$name = 'pass_with_plan_less_than_total'; -$expectedStatusCode = 255; -$expectedOutput = <<<'EOF' + yield [ + /* name */ 'pass_with_plan_less_than_total', + /* expectedStatusCode*/ 255, + /* expectedOutput */ <<<'EOF' 1..1 ok 1 ok 2 # Looks like you planned 1 tests but ran 1 extra. -EOF; -whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput); +EOF + ]; -$name = 'pass_with_plan_more_than_total'; -$expectedStatusCode = 255; -$expectedOutput = <<<'EOF' + yield [ + /* name */ 'pass_with_plan_more_than_total', + /* expectedStatusCode*/ 255, + /* expectedOutput */ <<<'EOF' 1..2 ok 1 # Looks like you planned 2 tests but only ran 1. -EOF; -whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput); +EOF + ]; -$name = 'pass_with_one_error'; -$expectedStatusCode = 1; -$expectedOutput = <<<'EOF' + yield [ + /* name */ 'pass_with_one_error', + /* expectedStatusCode*/ 1, + /* expectedOutput */ <<<'EOF' Notice: some user error message @@ -112,12 +135,13 @@ function whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $ ok 1 1..1 -EOF; -whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput); +EOF + ]; -$name = 'pass_with_one_throw_exception'; -$expectedStatusCode = 1; -$expectedOutput = <<<'EOF' + yield [ + /* name */ 'pass_with_one_throw_exception', + /* expectedStatusCode*/ 1, + /* expectedOutput */ <<<'EOF' LogicException: some exception message @@ -128,5 +152,9 @@ function whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $ 1..0 -EOF; -whenExecutePhpFileWillHaveStatusCodeAndOutput($harness, $test, $name, $expectedStatusCode, $expectedOutput); +EOF + ]; + } +} + +(new lime_testTest())->run();