diff --git a/src/Symfony/Component/Console/Formatter/NullOutputFormatter.php b/src/Symfony/Component/Console/Formatter/NullOutputFormatter.php new file mode 100644 index 0000000000..0aa0a5c252 --- /dev/null +++ b/src/Symfony/Component/Console/Formatter/NullOutputFormatter.php @@ -0,0 +1,72 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Console\Formatter; + +/** + * @author Tien Xuan Vo + */ +final class NullOutputFormatter implements OutputFormatterInterface +{ + private $style; + + /** + * {@inheritdoc} + */ + public function format(?string $message): void + { + // do nothing + } + + /** + * {@inheritdoc} + */ + public function getStyle(string $name): OutputFormatterStyleInterface + { + if ($this->style) { + return $this->style; + } + // to comply with the interface we must return a OutputFormatterStyleInterface + return $this->style = new NullOutputFormatterStyle(); + } + + /** + * {@inheritdoc} + */ + public function hasStyle(string $name): bool + { + return false; + } + + /** + * {@inheritdoc} + */ + public function isDecorated(): bool + { + return false; + } + + /** + * {@inheritdoc} + */ + public function setDecorated(bool $decorated): void + { + // do nothing + } + + /** + * {@inheritdoc} + */ + public function setStyle(string $name, OutputFormatterStyleInterface $style): void + { + // do nothing + } +} diff --git a/src/Symfony/Component/Console/Formatter/NullOutputFormatterStyle.php b/src/Symfony/Component/Console/Formatter/NullOutputFormatterStyle.php new file mode 100644 index 0000000000..bfd0afedd4 --- /dev/null +++ b/src/Symfony/Component/Console/Formatter/NullOutputFormatterStyle.php @@ -0,0 +1,65 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Console\Formatter; + +/** + * @author Tien Xuan Vo + */ +final class NullOutputFormatterStyle implements OutputFormatterStyleInterface +{ + /** + * {@inheritdoc} + */ + public function apply(string $text): string + { + return $text; + } + + /** + * {@inheritdoc} + */ + public function setBackground(string $color = null): void + { + // do nothing + } + + /** + * {@inheritdoc} + */ + public function setForeground(string $color = null): void + { + // do nothing + } + + /** + * {@inheritdoc} + */ + public function setOption(string $option): void + { + // do nothing + } + + /** + * {@inheritdoc} + */ + public function setOptions(array $options): void + { + // do nothing + } + + /** + * {@inheritdoc} + */ + public function unsetOption(string $option): void + { + // do nothing + } +} diff --git a/src/Symfony/Component/Console/Output/NullOutput.php b/src/Symfony/Component/Console/Output/NullOutput.php index 78a1cb4bbf..3bbe63ea0a 100644 --- a/src/Symfony/Component/Console/Output/NullOutput.php +++ b/src/Symfony/Component/Console/Output/NullOutput.php @@ -11,7 +11,7 @@ namespace Symfony\Component\Console\Output; -use Symfony\Component\Console\Formatter\OutputFormatter; +use Symfony\Component\Console\Formatter\NullOutputFormatter; use Symfony\Component\Console\Formatter\OutputFormatterInterface; /** @@ -24,6 +24,8 @@ use Symfony\Component\Console\Formatter\OutputFormatterInterface; */ class NullOutput implements OutputInterface { + private $formatter; + /** * {@inheritdoc} */ @@ -37,8 +39,11 @@ class NullOutput implements OutputInterface */ public function getFormatter() { + if ($this->formatter) { + return $this->formatter; + } // to comply with the interface we must return a OutputFormatterInterface - return new OutputFormatter(); + return $this->formatter = new NullOutputFormatter(); } /** diff --git a/src/Symfony/Component/Console/Tests/Formatter/NullOutputFormatterStyleTest.php b/src/Symfony/Component/Console/Tests/Formatter/NullOutputFormatterStyleTest.php new file mode 100644 index 0000000000..616e7f7141 --- /dev/null +++ b/src/Symfony/Component/Console/Tests/Formatter/NullOutputFormatterStyleTest.php @@ -0,0 +1,56 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Console\Tests\Output; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Console\Formatter\NullOutputFormatterStyle; + +/** + * @author Tien Xuan Vo + */ +class NullOutputFormatterStyleTest extends TestCase +{ + public function testApply() + { + $style = new NullOutputFormatterStyle(); + + $this->assertSame('foo', $style->apply('foo')); + } + + public function testSetForeground() + { + $style = new NullOutputFormatterStyle(); + $style->setForeground('black'); + $this->assertSame('foo', $style->apply('foo')); + } + + public function testSetBackground() + { + $style = new NullOutputFormatterStyle(); + $style->setBackground('blue'); + $this->assertSame('foo', $style->apply('foo')); + } + + public function testOptions() + { + $style = new NullOutputFormatterStyle(); + + $style->setOptions(['reverse', 'conceal']); + $this->assertSame('foo', $style->apply('foo')); + + $style->setOption('bold'); + $this->assertSame('foo', $style->apply('foo')); + + $style->unsetOption('reverse'); + $this->assertSame('foo', $style->apply('foo')); + } +} diff --git a/src/Symfony/Component/Console/Tests/Formatter/NullOutputFormatterTest.php b/src/Symfony/Component/Console/Tests/Formatter/NullOutputFormatterTest.php new file mode 100644 index 0000000000..a717cf3d51 --- /dev/null +++ b/src/Symfony/Component/Console/Tests/Formatter/NullOutputFormatterTest.php @@ -0,0 +1,67 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Console\Tests\Output; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Console\Formatter\NullOutputFormatter; +use Symfony\Component\Console\Formatter\NullOutputFormatterStyle; +use Symfony\Component\Console\Formatter\OutputFormatterStyle; + +/** + * @author Tien Xuan Vo + */ +class NullOutputFormatterTest extends TestCase +{ + public function testFormat() + { + $formatter = new NullOutputFormatter(); + + $message = 'this message will not be changed'; + $formatter->format($message); + + $this->assertSame('this message will not be changed', $message); + } + + public function testGetStyle() + { + $formatter = new NullOutputFormatter(); + $this->assertInstanceof(NullOutputFormatterStyle::class, $style = $formatter->getStyle('null')); + $this->assertSame($style, $formatter->getStyle('null')); + } + + public function testSetStyle() + { + $formatter = new NullOutputFormatter(); + $style = new OutputFormatterStyle(); + $formatter->setStyle('null', $style); + $this->assertNotSame($style, $formatter->getStyle('null')); + } + + public function testHasStyle() + { + $formatter = new NullOutputFormatter(); + $this->assertFalse($formatter->hasStyle('null')); + } + + public function testIsDecorated() + { + $formatter = new NullOutputFormatter(); + $this->assertFalse($formatter->isDecorated()); + } + + public function testSetDecorated() + { + $formatter = new NullOutputFormatter(); + $formatter->setDecorated(true); + $this->assertFalse($formatter->isDecorated()); + } +} diff --git a/src/Symfony/Component/Console/Tests/Output/NullOutputTest.php b/src/Symfony/Component/Console/Tests/Output/NullOutputTest.php index b7ff4be312..1e0967ea5e 100644 --- a/src/Symfony/Component/Console/Tests/Output/NullOutputTest.php +++ b/src/Symfony/Component/Console/Tests/Output/NullOutputTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Console\Tests\Output; use PHPUnit\Framework\TestCase; +use Symfony\Component\Console\Formatter\NullOutputFormatter; use Symfony\Component\Console\Formatter\OutputFormatter; use Symfony\Component\Console\Output\NullOutput; use Symfony\Component\Console\Output\Output; @@ -40,6 +41,13 @@ class NullOutputTest extends TestCase $this->assertSame(OutputInterface::VERBOSITY_QUIET, $output->getVerbosity(), '->getVerbosity() always returns VERBOSITY_QUIET for NullOutput'); } + public function testGetFormatter() + { + $output = new NullOutput(); + $this->assertInstanceof(NullOutputFormatter::class, $formatter = $output->getFormatter()); + $this->assertSame($formatter, $output->getFormatter()); + } + public function testSetFormatter() { $output = new NullOutput();