From c03eb6d141d9ae69cb2f86e92884da0e7cbdf7b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Barto=C5=A1?= Date: Mon, 11 Mar 2024 09:36:53 +0100 Subject: [PATCH] Tests: extract command output normalization to CommandOutputHelper class --- tests/Helpers/CommandOutputHelper.php | 27 +++++++ tests/Unit/Command/ExplainCommandTest.php | 46 ++---------- tests/Unit/Command/ListCommandTest.php | 88 ++++------------------- tests/Unit/Command/RunCommandTest.php | 30 +++----- tests/Unit/Command/RunJobCommandTest.php | 87 +++------------------- tests/Unit/Command/WorkerCommandTest.php | 29 ++------ tools/phpstan.baseline.neon | 2 +- 7 files changed, 69 insertions(+), 240 deletions(-) create mode 100644 tests/Helpers/CommandOutputHelper.php diff --git a/tests/Helpers/CommandOutputHelper.php b/tests/Helpers/CommandOutputHelper.php new file mode 100644 index 0000000..15ed4fc --- /dev/null +++ b/tests/Helpers/CommandOutputHelper.php @@ -0,0 +1,27 @@ + rtrim($s), + explode(PHP_EOL, preg_replace('~\R~u', PHP_EOL, $tester->getDisplay())), + ), + ); + } + +} diff --git a/tests/Unit/Command/ExplainCommandTest.php b/tests/Unit/Command/ExplainCommandTest.php index 991bac9..20ea5af 100644 --- a/tests/Unit/Command/ExplainCommandTest.php +++ b/tests/Unit/Command/ExplainCommandTest.php @@ -12,11 +12,7 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Console\Tester\CommandTester; use Tests\Orisai\Scheduler\Doubles\CallbackList; -use function array_map; -use function explode; -use function implode; -use function rtrim; -use const PHP_EOL; +use Tests\Orisai\Scheduler\Helpers\CommandOutputHelper; final class ExplainCommandTest extends TestCase { @@ -71,13 +67,7 @@ public function testBasicExplain(): void - timezone - run only when cron expression matches within given timezone MSG, - implode( - PHP_EOL, - array_map( - static fn (string $s): string => rtrim($s), - explode(PHP_EOL, $tester->getDisplay()), - ), - ), + CommandOutputHelper::getCommandOutput($tester), ); self::assertSame($command::SUCCESS, $code); } @@ -121,13 +111,7 @@ public function testExplainId(): void Job with id 'non-existent' does not exist. MSG, - implode( - PHP_EOL, - array_map( - static fn (string $s): string => rtrim($s), - explode(PHP_EOL, $tester->getDisplay()), - ), - ), + CommandOutputHelper::getCommandOutput($tester), ); self::assertSame($command::FAILURE, $code); @@ -140,13 +124,7 @@ public function testExplainId(): void At every minute. MSG, - implode( - PHP_EOL, - array_map( - static fn (string $s): string => rtrim($s), - explode(PHP_EOL, $tester->getDisplay()), - ), - ), + CommandOutputHelper::getCommandOutput($tester), ); self::assertSame($command::SUCCESS, $code); @@ -159,13 +137,7 @@ public function testExplainId(): void At every 30th minute past every hour from 7 through 15 on every day-of-week from Monday through Friday in America/New_York time zone. MSG, - implode( - PHP_EOL, - array_map( - static fn (string $s): string => rtrim($s), - explode(PHP_EOL, $tester->getDisplay()), - ), - ), + CommandOutputHelper::getCommandOutput($tester), ); self::assertSame($command::SUCCESS, $code); @@ -178,13 +150,7 @@ public function testExplainId(): void At every 10 seconds in April. MSG, - implode( - PHP_EOL, - array_map( - static fn (string $s): string => rtrim($s), - explode(PHP_EOL, $tester->getDisplay()), - ), - ), + CommandOutputHelper::getCommandOutput($tester), ); self::assertSame($command::SUCCESS, $code); } diff --git a/tests/Unit/Command/ListCommandTest.php b/tests/Unit/Command/ListCommandTest.php index 184d47a..05ce49b 100644 --- a/tests/Unit/Command/ListCommandTest.php +++ b/tests/Unit/Command/ListCommandTest.php @@ -15,12 +15,8 @@ use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Tester\CommandTester; use Tests\Orisai\Scheduler\Doubles\CallbackList; -use function array_map; -use function explode; -use function implode; +use Tests\Orisai\Scheduler\Helpers\CommandOutputHelper; use function putenv; -use function rtrim; -use const PHP_EOL; /** * @runTestsInSeparateProcesses @@ -42,7 +38,7 @@ public function testNoJobs(): void No scheduled jobs have been defined. MSG, - $tester->getDisplay(), + CommandOutputHelper::getCommandOutput($tester), ); self::assertSame($command::SUCCESS, $code); } @@ -84,13 +80,7 @@ public function testList(): void 30 * 12 10 * [3] tests/Doubles/CallbackList.php:32.. Next Due: 9 months MSG, - implode( - PHP_EOL, - array_map( - static fn (string $s): string => rtrim($s), - explode(PHP_EOL, $tester->getDisplay()), - ), - ), + CommandOutputHelper::getCommandOutput($tester), ); self::assertSame($command::SUCCESS, $code); @@ -105,13 +95,7 @@ public function testList(): void 30 * 12 10 * [3] tests/Doubles/CallbackList.php:32................................ Next Due: 9 months MSG, - implode( - PHP_EOL, - array_map( - static fn (string $s): string => rtrim($s), - explode(PHP_EOL, $tester->getDisplay()), - ), - ), + CommandOutputHelper::getCommandOutput($tester), ); self::assertSame($command::SUCCESS, $code); @@ -128,13 +112,7 @@ public function testList(): void 30 * 12 10 * [3] tests/Doubles/CallbackList.php:32......................... Next Due: 1970-10-12 00:30:00 +01:00 MSG, - implode( - PHP_EOL, - array_map( - static fn (string $s): string => rtrim($s), - explode(PHP_EOL, $tester->getDisplay()), - ), - ), + CommandOutputHelper::getCommandOutput($tester), ); self::assertSame($command::SUCCESS, $code); } @@ -189,13 +167,7 @@ public function testListWithSeconds(): void 30 * 12 10 * / 30 [4] tests/Doubles/CallbackList.php:32.................... Next Due: 1970-10-12 00:30:00 +01:00 MSG, - implode( - PHP_EOL, - array_map( - static fn (string $s): string => rtrim($s), - explode(PHP_EOL, $tester->getDisplay()), - ), - ), + CommandOutputHelper::getCommandOutput($tester), ); self::assertSame($command::SUCCESS, $code); } @@ -234,13 +206,7 @@ public function testNext(): void * * * 2 * [0] Tests\Orisai\Scheduler\Doubles\CallbackList::job1()......... Next Due: 1 month MSG, - implode( - PHP_EOL, - array_map( - static fn (string $s): string => rtrim($s), - explode(PHP_EOL, $tester->getDisplay()), - ), - ), + CommandOutputHelper::getCommandOutput($tester), ); self::assertSame($command::SUCCESS, $code); @@ -256,13 +222,7 @@ public function testNext(): void 2 * * * * [3] Tests\Orisai\Scheduler\Doubles\CallbackList::job1()........ Next Due: 1 minute MSG, - implode( - PHP_EOL, - array_map( - static fn (string $s): string => rtrim($s), - explode(PHP_EOL, $tester->getDisplay()), - ), - ), + CommandOutputHelper::getCommandOutput($tester), ); self::assertSame($command::SUCCESS, $code); } @@ -289,13 +249,7 @@ public function testNextOverlap(): void * * * * * / 1 [0] Tests\Orisai\Scheduler\Doubles\CallbackList::job1() Next Due: 1970-01-01 01:01:00 +01:00 MSG, - implode( - PHP_EOL, - array_map( - static fn (string $s): string => rtrim($s), - explode(PHP_EOL, $tester->getDisplay()), - ), - ), + CommandOutputHelper::getCommandOutput($tester), ); self::assertSame($command::SUCCESS, $code); } @@ -367,13 +321,7 @@ public function testTimeZone(): void 0 1 * * * (Australia/Sydney) [2] Tests\Orisai\Scheduler\Doubles\CallbackList::__invoke() Next Due: 1970-01-01 01:00:00 +00:00 MSG, - implode( - PHP_EOL, - array_map( - static fn (string $s): string => rtrim($s), - explode(PHP_EOL, $tester->getDisplay()), - ), - ), + CommandOutputHelper::getCommandOutput($tester), ); self::assertSame($command::SUCCESS, $code); @@ -390,13 +338,7 @@ public function testTimeZone(): void 0 1 * * * (Australia/Sydney) [2] Tests\Orisai\Scheduler\Doubles\CallbackList::__invoke() Next Due: 1970-01-02 01:00:00 +01:00 MSG, - implode( - PHP_EOL, - array_map( - static fn (string $s): string => rtrim($s), - explode(PHP_EOL, $tester->getDisplay()), - ), - ), + CommandOutputHelper::getCommandOutput($tester), ); self::assertSame($command::SUCCESS, $code); } @@ -446,13 +388,7 @@ public function testExplain(): void At every 30th minute past every hour from 7 through 15 on every day-of-week from Monday through Friday in America/New_York time zone. MSG, - implode( - PHP_EOL, - array_map( - static fn (string $s): string => rtrim($s), - explode(PHP_EOL, $tester->getDisplay()), - ), - ), + CommandOutputHelper::getCommandOutput($tester), ); self::assertSame($command::SUCCESS, $code); } diff --git a/tests/Unit/Command/RunCommandTest.php b/tests/Unit/Command/RunCommandTest.php index a6935db..7bcd0b8 100644 --- a/tests/Unit/Command/RunCommandTest.php +++ b/tests/Unit/Command/RunCommandTest.php @@ -15,13 +15,10 @@ use Tests\Orisai\Scheduler\Doubles\CallbackList; use Tests\Orisai\Scheduler\Doubles\CustomNameJob; use Tests\Orisai\Scheduler\Doubles\TestLockFactory; +use Tests\Orisai\Scheduler\Helpers\CommandOutputHelper; use Tests\Orisai\Scheduler\Unit\SchedulerProcessSetup; -use function array_map; use function explode; -use function implode; -use function preg_replace; use function putenv; -use function rtrim; use function sort; use const PHP_EOL; @@ -44,7 +41,7 @@ public function testNoJobs(): void <<<'MSG' MSG, - $tester->getDisplay(), + CommandOutputHelper::getCommandOutput($tester), ); self::assertSame($command::SUCCESS, $code); } @@ -76,7 +73,7 @@ public function testSuccess(): void 1970-01-01 01:00:01 Running [1] Tests\Orisai\Scheduler\Doubles\CallbackList::job2() 0ms DONE MSG, - $this->getNormalizedLines($tester), + CommandOutputHelper::getCommandOutput($tester), ); self::assertSame($command::SUCCESS, $code); @@ -89,7 +86,7 @@ public function testSuccess(): void 1970-01-01 01:00:01 Running [1] Tests\Orisai\Scheduler\Doubles\CallbackList::job2()........ 0ms DONE MSG, - $this->getNormalizedLines($tester), + CommandOutputHelper::getCommandOutput($tester), ); self::assertSame($command::SUCCESS, $code); @@ -130,7 +127,7 @@ public function testSuccess(): void ] MSG, - $this->getNormalizedLines($tester), + CommandOutputHelper::getCommandOutput($tester), ); self::assertSame($command::SUCCESS, $code); } @@ -165,7 +162,7 @@ public function testFailure(): void 1970-01-01 01:00:01 Running [1] Tests\Orisai\Scheduler\Doubles\CallbackList::exceptionJob() 0ms FAIL MSG, - $this->getNormalizedLines($tester), + CommandOutputHelper::getCommandOutput($tester), ); self::assertSame($command::FAILURE, $code); @@ -206,7 +203,7 @@ public function testFailure(): void ] MSG, - $this->getNormalizedLines($tester), + CommandOutputHelper::getCommandOutput($tester), ); self::assertSame($command::FAILURE, $code); } @@ -240,7 +237,7 @@ public function testLock(): void 1970-01-01 01:00:01 Running [0] job1................................... 0ms LOCK MSG, - $this->getNormalizedLines($tester), + CommandOutputHelper::getCommandOutput($tester), ); self::assertSame($command::SUCCESS, $code); } @@ -271,15 +268,4 @@ public function testProcessExecutor(): void self::assertSame($command::FAILURE, $code); } - public function getNormalizedLines(CommandTester $tester): string - { - return implode( - PHP_EOL, - array_map( - static fn (string $s): string => rtrim($s), - explode(PHP_EOL, preg_replace('~\R~u', PHP_EOL, $tester->getDisplay())), - ), - ); - } - } diff --git a/tests/Unit/Command/RunJobCommandTest.php b/tests/Unit/Command/RunJobCommandTest.php index 8b2fe49..3cb5758 100644 --- a/tests/Unit/Command/RunJobCommandTest.php +++ b/tests/Unit/Command/RunJobCommandTest.php @@ -17,15 +17,10 @@ use Tests\Orisai\Scheduler\Doubles\CallbackList; use Tests\Orisai\Scheduler\Doubles\CustomNameJob; use Tests\Orisai\Scheduler\Doubles\TestLockFactory; -use function array_map; -use function explode; -use function implode; +use Tests\Orisai\Scheduler\Helpers\CommandOutputHelper; use function json_encode; -use function preg_replace; use function putenv; -use function rtrim; use const JSON_THROW_ON_ERROR; -use const PHP_EOL; /** * @runTestsInSeparateProcesses @@ -79,13 +74,7 @@ public function testSuccess(): void 1970-01-01 01:00:01 Running [0] Tests\Orisai\Scheduler\Doubles\CallbackList::job1() 0ms DONE MSG, - implode( - PHP_EOL, - array_map( - static fn (string $s): string => rtrim($s), - explode(PHP_EOL, $tester->getDisplay()), - ), - ), + CommandOutputHelper::getCommandOutput($tester), ); self::assertSame($command::SUCCESS, $code); @@ -99,13 +88,7 @@ public function testSuccess(): void 1970-01-01 01:00:01 Running [0] Tests\Orisai\Scheduler\Doubles\CallbackList::job1()........ 0ms DONE MSG, - implode( - PHP_EOL, - array_map( - static fn (string $s): string => rtrim($s), - explode(PHP_EOL, $tester->getDisplay()), - ), - ), + CommandOutputHelper::getCommandOutput($tester), ); self::assertSame($command::SUCCESS, $code); } @@ -137,13 +120,7 @@ public function testFailure(): void 1970-01-01 01:00:01 Running [0] Tests\Orisai\Scheduler\Doubles\CallbackList::exceptionJob() 0ms FAIL MSG, - implode( - PHP_EOL, - array_map( - static fn (string $s): string => rtrim($s), - explode(PHP_EOL, $tester->getDisplay()), - ), - ), + CommandOutputHelper::getCommandOutput($tester), ); self::assertSame($command::FAILURE, $code); } @@ -179,13 +156,7 @@ public function testLock(): void 1970-01-01 01:00:01 Running [0] job1................................... 0ms LOCK MSG, - implode( - PHP_EOL, - array_map( - static fn (string $s): string => rtrim($s), - explode(PHP_EOL, $tester->getDisplay()), - ), - ), + CommandOutputHelper::getCommandOutput($tester), ); self::assertSame($command::SUCCESS, $code); } @@ -215,13 +186,7 @@ public function testNoForce(): void 1970-01-01 01:00:01 Running [0] Tests\Orisai\Scheduler\Doubles\CallbackList::job1() 0ms DONE MSG, - implode( - PHP_EOL, - array_map( - static fn (string $s): string => rtrim($s), - explode(PHP_EOL, $tester->getDisplay()), - ), - ), + CommandOutputHelper::getCommandOutput($tester), ); self::assertSame($command::SUCCESS, $code); @@ -236,13 +201,7 @@ public function testNoForce(): void Command was not executed because it is not its due time MSG, - implode( - PHP_EOL, - array_map( - static fn (string $s): string => rtrim($s), - explode(PHP_EOL, $tester->getDisplay()), - ), - ), + CommandOutputHelper::getCommandOutput($tester), ); self::assertSame($command::SUCCESS, $code); } @@ -273,13 +232,7 @@ public function testJson(): void null MSG, - implode( - PHP_EOL, - array_map( - static fn (string $s): string => rtrim($s), - explode(PHP_EOL, preg_replace('~\R~u', PHP_EOL, $tester->getDisplay())), - ), - ), + CommandOutputHelper::getCommandOutput($tester), ); self::assertSame($command::SUCCESS, $code); @@ -309,13 +262,7 @@ public function testJson(): void } MSG, - implode( - PHP_EOL, - array_map( - static fn (string $s): string => rtrim($s), - explode(PHP_EOL, preg_replace('~\R~u', PHP_EOL, $tester->getDisplay())), - ), - ), + CommandOutputHelper::getCommandOutput($tester), ); self::assertSame($command::SUCCESS, $code); } @@ -346,13 +293,7 @@ public function testEchoingJob(): void 1970-01-01 01:00:01 Running [0] Tests\Orisai\Scheduler\Doubles\CallbackList::echoingJob() 0ms DONE MSG, - implode( - PHP_EOL, - array_map( - static fn (string $s): string => rtrim($s), - explode(PHP_EOL, preg_replace('~\R~u', PHP_EOL, $tester->getDisplay())), - ), - ), + CommandOutputHelper::getCommandOutput($tester), ); self::assertSame($command::SUCCESS, $code); @@ -380,13 +321,7 @@ public function testEchoingJob(): void } MSG, - implode( - PHP_EOL, - array_map( - static fn (string $s): string => rtrim($s), - explode(PHP_EOL, preg_replace('~\R~u', PHP_EOL, $tester->getDisplay())), - ), - ), + CommandOutputHelper::getCommandOutput($tester), ); self::assertSame($command::SUCCESS, $code); } diff --git a/tests/Unit/Command/WorkerCommandTest.php b/tests/Unit/Command/WorkerCommandTest.php index 5142ab8..45ed5e8 100644 --- a/tests/Unit/Command/WorkerCommandTest.php +++ b/tests/Unit/Command/WorkerCommandTest.php @@ -7,12 +7,9 @@ use Orisai\Scheduler\Command\WorkerCommand; use PHPUnit\Framework\TestCase; use Symfony\Component\Console\Tester\CommandTester; -use function array_map; +use Tests\Orisai\Scheduler\Helpers\CommandOutputHelper; use function explode; -use function implode; -use function preg_replace; use function putenv; -use function rtrim; use const PHP_EOL; final class WorkerCommandTest extends TestCase @@ -34,13 +31,7 @@ public function testNoRuns(): void Running scheduled tasks every minute. MSG, - implode( - PHP_EOL, - array_map( - static fn (string $s): string => rtrim($s), - explode(PHP_EOL, $tester->getDisplay()), - ), - ), + CommandOutputHelper::getCommandOutput($tester), ); self::assertSame($command::SUCCESS, $code); } @@ -114,13 +105,7 @@ public function testNoJobs(): void Running scheduled tasks every minute. MSG, - implode( - PHP_EOL, - array_map( - static fn (string $s): string => rtrim($s), - explode(PHP_EOL, $tester->getDisplay()), - ), - ), + CommandOutputHelper::getCommandOutput($tester), ); } @@ -142,13 +127,7 @@ public function testDefaultExecutable(): void Could not open input file: bin/console MSG, - implode( - PHP_EOL, - array_map( - static fn (string $s): string => rtrim($s), - explode(PHP_EOL, preg_replace('~\R~u', PHP_EOL, $tester->getDisplay())), - ), - ), + CommandOutputHelper::getCommandOutput($tester), ); self::assertSame($command::SUCCESS, $code); } diff --git a/tools/phpstan.baseline.neon b/tools/phpstan.baseline.neon index b31903e..7a593d2 100644 --- a/tools/phpstan.baseline.neon +++ b/tools/phpstan.baseline.neon @@ -83,7 +83,7 @@ parameters: - message: "#^Parameter \\#2 \\$str of function explode expects string, string\\|null given\\.$#" count: 1 - path: ../tests/Unit/Command/RunCommandTest.php + path: ../tests/Helpers/CommandOutputHelper.php - message: "#^Parameter \\#2 \\$str of function explode expects string, string\\|null given\\.$#"