Skip to content

Commit

Permalink
Tests: extract command output normalization to CommandOutputHelper class
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Mar 11, 2024
1 parent 52e5eb8 commit c03eb6d
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 240 deletions.
27 changes: 27 additions & 0 deletions tests/Helpers/CommandOutputHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types = 1);

namespace Tests\Orisai\Scheduler\Helpers;

use Symfony\Component\Console\Tester\CommandTester;
use function array_map;
use function explode;
use function implode;
use function preg_replace;
use function rtrim;
use const PHP_EOL;

final class CommandOutputHelper
{

public static function getCommandOutput(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())),
),
);
}

}
46 changes: 6 additions & 40 deletions tests/Unit/Command/ExplainCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);
}
Expand Down
88 changes: 12 additions & 76 deletions tests/Unit/Command/ListCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);

Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);

Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
30 changes: 8 additions & 22 deletions tests/Unit/Command/RunCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -44,7 +41,7 @@ public function testNoJobs(): void
<<<'MSG'

MSG,
$tester->getDisplay(),
CommandOutputHelper::getCommandOutput($tester),
);
self::assertSame($command::SUCCESS, $code);
}
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand Down Expand Up @@ -130,7 +127,7 @@ public function testSuccess(): void
]

MSG,
$this->getNormalizedLines($tester),
CommandOutputHelper::getCommandOutput($tester),
);
self::assertSame($command::SUCCESS, $code);
}
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -206,7 +203,7 @@ public function testFailure(): void
]

MSG,
$this->getNormalizedLines($tester),
CommandOutputHelper::getCommandOutput($tester),
);
self::assertSame($command::FAILURE, $code);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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())),
),
);
}

}
Loading

0 comments on commit c03eb6d

Please sign in to comment.