diff --git a/tests/Console/Command/Info/SignatureTest.php b/tests/Console/Command/Info/SignatureTest.php index d65dc2e8a..7d544f4a9 100644 --- a/tests/Console/Command/Info/SignatureTest.php +++ b/tests/Console/Command/Info/SignatureTest.php @@ -17,6 +17,7 @@ use Fidry\Console\Command\Command; use Fidry\Console\ExitCode; use Fidry\Console\Test\OutputAssertions; +use KevinGH\Box\Console\DisplayNormalizer; use KevinGH\Box\Phar\InvalidPhar; use KevinGH\Box\Test\CommandTestCase; use Symfony\Component\Filesystem\Path; @@ -83,13 +84,13 @@ public function test_it_cannot_provide_info_about_an_non_phar_archive(): void OutputAssertions::assertSameOutput( <<commandTester, + DisplayNormalizer::removeBlockLineReturn(...), ); } diff --git a/tests/Console/DisplayNormalizer.php b/tests/Console/DisplayNormalizer.php index 8dd2c22cc..1a0e02c17 100644 --- a/tests/Console/DisplayNormalizer.php +++ b/tests/Console/DisplayNormalizer.php @@ -17,6 +17,8 @@ use function array_map; use function explode; use function implode; +use function preg_match_all; +use function str_replace; final class DisplayNormalizer { @@ -67,4 +69,29 @@ public static function createReplaceBoxVersionNormalizer(): callable $output, ); } + + public static function removeBlockLineReturn(string $value): string + { + // Note: this is far from being robust enough for all scenarios but since it is for + // a specific case this will do for now and be adjusted as needed. + $regex = '/\n( \[\p{L}+\] .*\n(?:\s{9}.+\n)+?)\n/u'; + + if (1 !== preg_match_all($regex, $value, $matches)) { + return $value; + } + + unset($matches[0]); + + foreach ($matches as [$match]) { + $lines = explode("\n ", $match); + + $value = str_replace( + $match, + implode('', $lines), + $value, + ); + } + + return $value; + } } diff --git a/tests/Console/DisplayNormalizerTest.php b/tests/Console/DisplayNormalizerTest.php new file mode 100644 index 000000000..7c15fe1ae --- /dev/null +++ b/tests/Console/DisplayNormalizerTest.php @@ -0,0 +1,86 @@ + + * Théo Fidry + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace KevinGH\Box\Console; + +use PHPUnit\Framework\TestCase; + +/** + * @covers \KevinGH\Box\Console\DisplayNormalizer + * @internal + */ +final class DisplayNormalizerTest extends TestCase +{ + /** + * @dataProvider blockProvider + */ + public static function test_it_can_remove_the_line_returns_from_blocks( + string $value, + ?string $expected, + ): void { + $expected ??= $value; + + $actual = DisplayNormalizer::removeBlockLineReturn($value); + + self::assertSame($expected, $actual); + } + + public static function blockProvider(): iterable + { + $unchanged = null; + + yield 'no line return' => [ + <<<'EOF' + + [ERROR] Error message. + + + EOF, + $unchanged, + ]; + + yield 'one line return' => [ + <<<'EOF' + + [ERROR] Error message... + ...and the other piece here. + + + EOF, + <<<'EOF' + + [ERROR] Error message......and the other piece here. + + + EOF, + ]; + + yield 'two line return' => [ + <<<'EOF' + + [ERROR] Error message... + ...and the other piece here. + And one more. + + + EOF, + <<<'EOF' + + [ERROR] Error message......and the other piece here. And one more. + + + EOF, + ]; + } +}