From 28b77970939500fb04180166a1f716e75a871ef8 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Wed, 17 Aug 2022 16:34:04 +0200 Subject: [PATCH 1/2] minor #47299 [Console] fix expected command name order with mixed integer and string namespaces (xabbuh) This PR was merged into the 5.4 branch. Discussion ---------- [Console] fix expected command name order with mixed integer and string namespaces | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | Commits ------- 5cd0eae5af fix expected command name order with mixed integer and string namespaces --- Descriptor/ApplicationDescription.php | 2 +- Tests/Descriptor/ApplicationDescriptionTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Descriptor/ApplicationDescription.php b/Descriptor/ApplicationDescription.php index 3970b9000..91b184605 100644 --- a/Descriptor/ApplicationDescription.php +++ b/Descriptor/ApplicationDescription.php @@ -131,7 +131,7 @@ private function sortCommands(array $commands): array } if ($namespacedCommands) { - ksort($namespacedCommands); + ksort($namespacedCommands, \SORT_STRING); foreach ($namespacedCommands as $key => $commandsSet) { ksort($commandsSet); $sortedCommands[$key] = $commandsSet; diff --git a/Tests/Descriptor/ApplicationDescriptionTest.php b/Tests/Descriptor/ApplicationDescriptionTest.php index 33d5c3840..f1408d087 100644 --- a/Tests/Descriptor/ApplicationDescriptionTest.php +++ b/Tests/Descriptor/ApplicationDescriptionTest.php @@ -36,7 +36,7 @@ public function getNamespacesProvider() return [ [['_global'], ['foobar']], [['a', 'b'], ['b:foo', 'a:foo', 'b:bar']], - [['_global', 'b', 'z', 22, 33], ['z:foo', '1', '33:foo', 'b:foo', '22:foo:bar']], + [['_global', 22, 33, 'b', 'z'], ['z:foo', '1', '33:foo', 'b:foo', '22:foo:bar']], ]; } } From 4f40012db8d55c956406890b5720f686fee7f7b7 Mon Sep 17 00:00:00 2001 From: Jesper Skytte Date: Mon, 3 Oct 2022 22:36:35 +0200 Subject: [PATCH 2/2] [Console] Fix `Helper::removeDecoration` hyperlink bug --- Helper/Helper.php | 2 ++ Tests/Helper/HelperTest.php | 19 +++++++++++++++++++ Tests/Helper/TableTest.php | 25 +++++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/Helper/Helper.php b/Helper/Helper.php index 0521aaf7d..18d85b940 100644 --- a/Helper/Helper.php +++ b/Helper/Helper.php @@ -135,6 +135,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; diff --git a/Tests/Helper/HelperTest.php b/Tests/Helper/HelperTest.php index 184d86e09..b8c891087 100644 --- a/Tests/Helper/HelperTest.php +++ b/Tests/Helper/HelperTest.php @@ -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 @@ -42,6 +43,16 @@ public function formatTimeProvider() ]; } + public function decoratedTextProvider() + { + return [ + ['abc', 'abc'], + ['abc', 'abc'], + ["a\033[1;36mbc", 'abc'], + ["a\033]8;;http://url\033\\b\033]8;;\033\\c", 'abc'], + ]; + } + /** * @dataProvider formatTimeProvider * @@ -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)); + } } diff --git a/Tests/Helper/TableTest.php b/Tests/Helper/TableTest.php index c0f3f96b2..0e3f30cda 100644 --- a/Tests/Helper/TableTest.php +++ b/Tests/Helper/TableTest.php @@ -1388,6 +1388,31 @@ public function testWithColspanAndMaxWith() | | | nsectetur | +-----------------+-----------------+-----------------+ +TABLE; + + $this->assertSame($expected, $this->getOutputContent($output)); + } + + public function testWithHyperlinkAndMaxWidth() + { + $table = new Table($output = $this->getOutputStream(true)); + $table + ->setRows([ + ['Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor'], + ]) + ; + $table->setColumnMaxWidth(0, 20); + $table->render(); + + $expected = + <<assertSame($expected, $this->getOutputContent($output));