Skip to content

Commit

Permalink
minor #34869 [Console] Improve speed NullOutput (tienvx)
Browse files Browse the repository at this point in the history
This PR was merged into the 5.1-dev branch.

Discussion
----------

[Console] Improve speed NullOutput

| Q             | A
| ------------- | ---
| Branch?       | master <!-- see below -->
| Bug fix?      | no
| New feature?  | yes <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | N/A <!-- prefix each issue number with "Fix #", if any -->
| License       | MIT
| Doc PR        | N/A <!-- required for new features -->

This PR improve NullOutput so that test cases that used it can be run faster.

Commits
-------

7538743 [Console] Improve speed NullOutput
  • Loading branch information
chalasr committed Dec 26, 2019
2 parents b138fbc + 7538743 commit 9f74fb0
Show file tree
Hide file tree
Showing 6 changed files with 275 additions and 2 deletions.
72 changes: 72 additions & 0 deletions src/Symfony/Component/Console/Formatter/NullOutputFormatter.php
@@ -0,0 +1,72 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <tien.xuan.vo@gmail.com>
*/
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
}
}
@@ -0,0 +1,65 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <tien.xuan.vo@gmail.com>
*/
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
}
}
9 changes: 7 additions & 2 deletions src/Symfony/Component/Console/Output/NullOutput.php
Expand Up @@ -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;

/**
Expand All @@ -24,6 +24,8 @@
*/
class NullOutput implements OutputInterface
{
private $formatter;

/**
* {@inheritdoc}
*/
Expand All @@ -37,8 +39,11 @@ public function setFormatter(OutputFormatterInterface $formatter)
*/
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();
}

/**
Expand Down
@@ -0,0 +1,56 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <tien.xuan.vo@gmail.com>
*/
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'));
}
}
@@ -0,0 +1,67 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <tien.xuan.vo@gmail.com>
*/
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());
}
}
8 changes: 8 additions & 0 deletions src/Symfony/Component/Console/Tests/Output/NullOutputTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -40,6 +41,13 @@ public function testVerbosity()
$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();
Expand Down

0 comments on commit 9f74fb0

Please sign in to comment.