Skip to content

Commit

Permalink
Merge branch '6.0' into 6.1
Browse files Browse the repository at this point in the history
* 6.0:
  [Console] Fix `Helper::removeDecoration` hyperlink bug
  Guard scripts from being run in non-CLI contexts
  Guard scripts from being run in non-CLI contexts
  a readonly property must not be reported as being writable
  • Loading branch information
fabpot committed Oct 7, 2022
2 parents 17524a6 + 1f89cab commit 7fa3b9c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Helper/Helper.php
Expand Up @@ -145,6 +145,8 @@ public static function removeDecoration(OutputFormatterInterface $formatter, ?st
$string = $formatter->format($string ?? '');
// remove already formatted characters
$string = preg_replace("/\033\[[^m]*m/", '', $string ?? '');
// remove terminal hyperlinks
$string = preg_replace('/\\033]8;[^;]*;[^\\033]*\\033\\\\/', '', $string ?? '');
$formatter->setDecorated($isDecorated);

return $string;
Expand Down
19 changes: 19 additions & 0 deletions Tests/Helper/HelperTest.php
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Console\Tests\Helper;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Helper\Helper;

class HelperTest extends TestCase
Expand Down Expand Up @@ -42,6 +43,16 @@ public function formatTimeProvider()
];
}

public function decoratedTextProvider()
{
return [
['abc', 'abc'],
['abc<fg=default;bg=default>', 'abc'],
["a\033[1;36mbc", 'abc'],
["a\033]8;;http://url\033\\b\033]8;;\033\\c", 'abc'],
];
}

/**
* @dataProvider formatTimeProvider
*
Expand All @@ -52,4 +63,12 @@ public function testFormatTime($secs, $expectedFormat)
{
$this->assertEquals($expectedFormat, Helper::formatTime($secs));
}

/**
* @dataProvider decoratedTextProvider
*/
public function testRemoveDecoration(string $decoratedText, string $undecoratedText)
{
$this->assertEquals($undecoratedText, Helper::removeDecoration(new OutputFormatter(), $decoratedText));
}
}
25 changes: 25 additions & 0 deletions Tests/Helper/TableTest.php
Expand Up @@ -1969,4 +1969,29 @@ public function testVerticalRender(string $expectedOutput, array $headers, array

$this->assertEquals($expectedOutput, $this->getOutputContent($output));
}

public function testWithHyperlinkAndMaxWidth()
{
$table = new Table($output = $this->getOutputStream(true));
$table
->setRows([
['<href=Lorem>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor</>'],
])
;
$table->setColumnMaxWidth(0, 20);
$table->render();

$expected =
<<<TABLE
+----------------------+
| \033]8;;Lorem\033\\Lorem ipsum dolor si\033]8;;\033\\ |
| \033]8;;Lorem\033\\t amet, consectetur \033]8;;\033\\ |
| \033]8;;Lorem\033\\adipiscing elit, sed\033]8;;\033\\ |
| \033]8;;Lorem\033\\do eiusmod tempor\033]8;;\033\\ |
+----------------------+
TABLE;

$this->assertSame($expected, $this->getOutputContent($output));
}
}

0 comments on commit 7fa3b9c

Please sign in to comment.