From 1430138b62ff13166590b2b13eea0cdd6cc10add Mon Sep 17 00:00:00 2001 From: SpacePossum Date: Sun, 31 Jul 2016 13:37:14 +0200 Subject: [PATCH] Allow multiple options to be set. --- .../Console/Formatter/OutputFormatter.php | 22 ++++-- .../Tests/Formatter/OutputFormatterTest.php | 67 +++++++++++++++++++ 2 files changed, 82 insertions(+), 7 deletions(-) diff --git a/src/Symfony/Component/Console/Formatter/OutputFormatter.php b/src/Symfony/Component/Console/Formatter/OutputFormatter.php index 56cd5e568f16..bf1c86d9b0e4 100644 --- a/src/Symfony/Component/Console/Formatter/OutputFormatter.php +++ b/src/Symfony/Component/Console/Formatter/OutputFormatter.php @@ -139,7 +139,7 @@ public function format($message) $message = (string) $message; $offset = 0; $output = ''; - $tagRegex = '[a-z][a-z0-9_=;-]*+'; + $tagRegex = '[a-z][a-z0-9,_=;-]*+'; preg_match_all("#<(($tagRegex) | /($tagRegex)?)>#ix", $message, $matches, PREG_OFFSET_CAPTURE); foreach ($matches[0] as $i => $match) { $pos = $match[1]; @@ -202,7 +202,7 @@ private function createStyleFromString($string) return $this->styles[$string]; } - if (!preg_match_all('/([^=]+)=([^;]+)(;|$)/', strtolower($string), $matches, PREG_SET_ORDER)) { + if (!preg_match_all('/([^=]+)=([^;]+)(;|$)/', $string, $matches, PREG_SET_ORDER)) { return false; } @@ -214,12 +214,20 @@ private function createStyleFromString($string) $style->setForeground($match[1]); } elseif ('bg' == $match[0]) { $style->setBackground($match[1]); - } else { - try { - $style->setOption($match[1]); - } catch (\InvalidArgumentException $e) { - return false; + } elseif ('options' === $match[0]) { + preg_match_all('([^,;]+)', $match[1], $options); + $options = array_shift($options); + foreach ($options as $option) { + try { + $style->setOption($option); + } catch (\InvalidArgumentException $e) { + trigger_error(sprintf('Unknown style options are deprecated since version 3.2 and will be removed in 4.0. Exception "%s".', $e->getMessage()), E_USER_DEPRECATED); + + return false; + } } + } else { + return false; } } diff --git a/src/Symfony/Component/Console/Tests/Formatter/OutputFormatterTest.php b/src/Symfony/Component/Console/Tests/Formatter/OutputFormatterTest.php index b8d5ca6d9beb..fa47c4de0d99 100644 --- a/src/Symfony/Component/Console/Tests/Formatter/OutputFormatterTest.php +++ b/src/Symfony/Component/Console/Tests/Formatter/OutputFormatterTest.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Console\Tests\Formatter; +use Symfony\Bridge\PhpUnit\ErrorAssert; use Symfony\Component\Console\Formatter\OutputFormatter; use Symfony\Component\Console\Formatter\OutputFormatterStyle; @@ -152,6 +153,72 @@ public function testInlineStyle() $this->assertEquals("\033[34;41msome text\033[39;49m", $formatter->format('some text')); } + /** + * @param string $tag + * @param string|null $expected + * @param string|null $input + * + * @dataProvider provideInlineStyleOptionsCases + */ + public function testInlineStyleOptions($tag, $expected = null, $input = null) + { + $styleString = substr($tag, 1, -1); + $formatter = new OutputFormatter(true); + $method = new \ReflectionMethod($formatter, 'createStyleFromString'); + $method->setAccessible(true); + $result = $method->invoke($formatter, $styleString); + if (null === $expected) { + $this->assertFalse($result); + $expected = $tag.$input.''; + $this->assertSame($expected, $formatter->format($expected)); + } else { + /* @var OutputFormatterStyle $result */ + $this->assertInstanceOf(OutputFormatterStyle::class, $result); + $this->assertSame($expected, $formatter->format($tag.$input.'')); + $this->assertSame($expected, $formatter->format($tag.$input.'')); + } + } + + public function provideInlineStyleOptionsCases() + { + return array( + array(''), + array(''), + array('', "\033[32m[test]\033[39m", '[test]'), + array('', "\033[32;44ma\033[39;49m", 'a'), + array('', "\033[32;1mb\033[39;22m", 'b'), + array('', "\033[32;7m\033[39;27m", ''), + array('', "\033[32;1;4mz\033[39;22;24m", 'z'), + array('', "\033[32;1;4;7md\033[39;22;24;27m", 'd'), + ); + } + + /** + * @group legacy + * @dataProvider provideInlineStyleTagsWithUnknownOptions + * @requires function Symfony\Bridge\PhpUnit\ErrorAssert::assertDeprecationsAreTriggered + */ + public function testInlineStyleOptionsUnknownAreDeprecated($tag, $option) + { + ErrorAssert::assertDeprecationsAreTriggered( + array(sprintf('Unknown style options are deprecated since version 3.2 and will be removed in 4.0. Exception "Invalid option specified: "%s". Expected one of (bold, underscore, blink, reverse, conceal)".', $option)), + function () use ($tag) { + $formatter = new OutputFormatter(true); + $formatter->format($tag); + } + ); + } + + public function provideInlineStyleTagsWithUnknownOptions() + { + return array( + array('', 'abc'), + array('', 'abc'), + array('', 'xyz'), + array('', 'efg'), + ); + } + public function testNonStyleTag() { $formatter = new OutputFormatter(true);