Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
minor #34869 [Console] Improve speed NullOutput (tienvx)
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
Showing
6 changed files
with
275 additions
and
2 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
src/Symfony/Component/Console/Formatter/NullOutputFormatter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
src/Symfony/Component/Console/Formatter/NullOutputFormatterStyle.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/Symfony/Component/Console/Tests/Formatter/NullOutputFormatterStyleTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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')); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
src/Symfony/Component/Console/Tests/Formatter/NullOutputFormatterTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters