Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry committed Nov 27, 2023
1 parent 093c0df commit 3b014b3
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 2 deletions.
5 changes: 3 additions & 2 deletions tests/Console/Command/Info/SignatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -83,13 +84,13 @@ public function test_it_cannot_provide_info_about_an_non_phar_archive(): void
OutputAssertions::assertSameOutput(
<<<EOF
[ERROR] The file "{$expectedFilePath}" is
not a PHAR.
[ERROR] The file "{$expectedFilePath}" is not a PHAR.
EOF,
ExitCode::FAILURE,
$this->commandTester,
DisplayNormalizer::removeBlockLineReturn(...),
);
}

Expand Down
27 changes: 27 additions & 0 deletions tests/Console/DisplayNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}
}
86 changes: 86 additions & 0 deletions tests/Console/DisplayNormalizerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

declare(strict_types=1);

/*
* This file is part of the box project.
*
* (c) Kevin Herrera <[email protected]>
* Théo Fidry <[email protected]>
*
* 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,
];
}
}

0 comments on commit 3b014b3

Please sign in to comment.