From a290f871238b6027978eb94652993bed348cc929 Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Tue, 14 May 2013 17:31:22 +0200 Subject: [PATCH 1/6] [Console] fix test for NullOutput that does not print anything and add a failing test for verbosity --- .../Console/Tests/Output/NullOutputTest.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Console/Tests/Output/NullOutputTest.php b/src/Symfony/Component/Console/Tests/Output/NullOutputTest.php index 8dd5f7cd9b..b24b7a1f4d 100644 --- a/src/Symfony/Component/Console/Tests/Output/NullOutputTest.php +++ b/src/Symfony/Component/Console/Tests/Output/NullOutputTest.php @@ -12,13 +12,27 @@ namespace Symfony\Component\Console\Tests\Output; use Symfony\Component\Console\Output\NullOutput; +use Symfony\Component\Console\Output\OutputInterface; class NullOutputTest extends \PHPUnit_Framework_TestCase { public function testConstructor() { $output = new NullOutput(); + + ob_start(); $output->write('foo'); - $this->assertTrue(true, '->write() does nothing'); // FIXME + $buffer = ob_get_clean(); + + $this->assertSame('', $buffer, '->write() does nothing (at least nothing is printed)'); + } + + public function testVerbosity() + { + $output = new NullOutput(); + $this->assertSame(OutputInterface::VERBOSITY_QUIET, $output->getVerbosity(), '->getVerbosity() returns VERBOSITY_QUIET for NullOutput by default'); + + $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); + $this->assertSame(OutputInterface::VERBOSITY_QUIET, $output->getVerbosity(), '->getVerbosity() always returns VERBOSITY_QUIET for NullOutput'); } } From ee0cc40c87b698480550ebe359534bb833c8622e Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Tue, 14 May 2013 17:58:06 +0200 Subject: [PATCH 2/6] [Console] fix NullOutput It should not extend from abstract Output class because than it inherits the useless constructor arguments and applies formatting in write() needlessly. --- .../Component/Console/Output/NullOutput.php | 71 +++++++++++++++++-- .../Console/Tests/Output/NullOutputTest.php | 1 + 2 files changed, 66 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/Console/Output/NullOutput.php b/src/Symfony/Component/Console/Output/NullOutput.php index 783d42b656..301b7f5f6a 100644 --- a/src/Symfony/Component/Console/Output/NullOutput.php +++ b/src/Symfony/Component/Console/Output/NullOutput.php @@ -11,24 +11,83 @@ namespace Symfony\Component\Console\Output; +use Symfony\Component\Console\Formatter\OutputFormatter; +use Symfony\Component\Console\Formatter\OutputFormatterInterface; + /** * NullOutput suppresses all output. * * $output = new NullOutput(); * * @author Fabien Potencier + * @author Tobias Schultze * * @api */ -class NullOutput extends Output +class NullOutput implements OutputInterface { /** - * Writes a message to the output. - * - * @param string $message A message to write to the output - * @param Boolean $newline Whether to add a newline or not + * {@inheritdoc} */ - protected function doWrite($message, $newline) + public function setFormatter(OutputFormatterInterface $formatter) { + // do nothing + } + + /** + * {@inheritdoc} + */ + public function getFormatter() + { + // to comply with the interface we must return a OutputFormatterInterface + return new OutputFormatter(); + } + + /** + * {@inheritdoc} + */ + public function setDecorated($decorated) + { + // do nothing + } + + /** + * {@inheritdoc} + */ + public function isDecorated() + { + return false; + } + + /** + * {@inheritdoc} + */ + public function setVerbosity($level) + { + // do nothing + } + + /** + * {@inheritdoc} + */ + public function getVerbosity() + { + return self::VERBOSITY_QUIET; + } + + /** + * {@inheritdoc} + */ + public function writeln($messages, $type = 0) + { + // do nothing + } + + /** + * {@inheritdoc} + */ + public function write($messages, $newline = false, $type = 0) + { + // do nothing } } diff --git a/src/Symfony/Component/Console/Tests/Output/NullOutputTest.php b/src/Symfony/Component/Console/Tests/Output/NullOutputTest.php index b24b7a1f4d..b20ae4e8d0 100644 --- a/src/Symfony/Component/Console/Tests/Output/NullOutputTest.php +++ b/src/Symfony/Component/Console/Tests/Output/NullOutputTest.php @@ -25,6 +25,7 @@ class NullOutputTest extends \PHPUnit_Framework_TestCase $buffer = ob_get_clean(); $this->assertSame('', $buffer, '->write() does nothing (at least nothing is printed)'); + $this->assertFalse($output->isDecorated(), '->isDecorated() returns false'); } public function testVerbosity() From 2628d889a7d88681b7764283d1ae7ca40ed38d7d Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Tue, 14 May 2013 18:09:22 +0200 Subject: [PATCH 3/6] [Console] the default type value should use the constant in OutputInterface::write --- src/Symfony/Component/Console/Output/NullOutput.php | 4 ++-- src/Symfony/Component/Console/Output/Output.php | 4 ++-- src/Symfony/Component/Console/Output/OutputInterface.php | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/Console/Output/NullOutput.php b/src/Symfony/Component/Console/Output/NullOutput.php index 301b7f5f6a..c75cfcae98 100644 --- a/src/Symfony/Component/Console/Output/NullOutput.php +++ b/src/Symfony/Component/Console/Output/NullOutput.php @@ -78,7 +78,7 @@ class NullOutput implements OutputInterface /** * {@inheritdoc} */ - public function writeln($messages, $type = 0) + public function writeln($messages, $type = self::OUTPUT_NORMAL) { // do nothing } @@ -86,7 +86,7 @@ class NullOutput implements OutputInterface /** * {@inheritdoc} */ - public function write($messages, $newline = false, $type = 0) + public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL) { // do nothing } diff --git a/src/Symfony/Component/Console/Output/Output.php b/src/Symfony/Component/Console/Output/Output.php index 2493ae66e6..ec0290e378 100644 --- a/src/Symfony/Component/Console/Output/Output.php +++ b/src/Symfony/Component/Console/Output/Output.php @@ -128,7 +128,7 @@ abstract class Output implements OutputInterface * * @api */ - public function writeln($messages, $type = 0) + public function writeln($messages, $type = self::OUTPUT_NORMAL) { $this->write($messages, true, $type); } @@ -144,7 +144,7 @@ abstract class Output implements OutputInterface * * @api */ - public function write($messages, $newline = false, $type = 0) + public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL) { if (self::VERBOSITY_QUIET === $this->verbosity) { return; diff --git a/src/Symfony/Component/Console/Output/OutputInterface.php b/src/Symfony/Component/Console/Output/OutputInterface.php index 4d7606cb53..eab9d54e14 100644 --- a/src/Symfony/Component/Console/Output/OutputInterface.php +++ b/src/Symfony/Component/Console/Output/OutputInterface.php @@ -43,7 +43,7 @@ interface OutputInterface * * @api */ - public function write($messages, $newline = false, $type = 0); + public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL); /** * Writes a message to the output and adds a newline at the end. @@ -53,7 +53,7 @@ interface OutputInterface * * @api */ - public function writeln($messages, $type = 0); + public function writeln($messages, $type = self::OUTPUT_NORMAL); /** * Sets the verbosity of the output. From 9dcc9facf025eb8043503987431af4b3bfe46a0f Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Tue, 14 May 2013 18:18:10 +0200 Subject: [PATCH 4/6] [Console] fix abstract Output class that fasly claims to support guessing of decorated variable. Also we don't need to typecast to boolean as its already done by the formatter and its his responsibility --- src/Symfony/Component/Console/Output/Output.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Console/Output/Output.php b/src/Symfony/Component/Console/Output/Output.php index ec0290e378..dd7dbe0f5b 100644 --- a/src/Symfony/Component/Console/Output/Output.php +++ b/src/Symfony/Component/Console/Output/Output.php @@ -36,16 +36,16 @@ abstract class Output implements OutputInterface * Constructor. * * @param integer $verbosity The verbosity level (self::VERBOSITY_QUIET, self::VERBOSITY_NORMAL, self::VERBOSITY_VERBOSE) - * @param Boolean $decorated Whether to decorate messages or not (null for auto-guessing) + * @param Boolean $decorated Whether to decorate messages or not * @param OutputFormatterInterface $formatter Output formatter instance * * @api */ - public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = null, OutputFormatterInterface $formatter = null) + public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = false, OutputFormatterInterface $formatter = null) { $this->verbosity = null === $verbosity ? self::VERBOSITY_NORMAL : $verbosity; $this->formatter = null === $formatter ? new OutputFormatter() : $formatter; - $this->formatter->setDecorated((Boolean) $decorated); + $this->formatter->setDecorated($decorated); } /** @@ -81,7 +81,7 @@ abstract class Output implements OutputInterface */ public function setDecorated($decorated) { - $this->formatter->setDecorated((Boolean) $decorated); + $this->formatter->setDecorated($decorated); } /** From bd61b925a1e34c696218dc84ef3d8e360a8a92bc Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Tue, 14 May 2013 18:38:03 +0200 Subject: [PATCH 5/6] [Console] fix phpdoc of Output classes (esp. regarding the new verbosities) also add missing information --- .../Component/Console/Output/ConsoleOutput.php | 7 +++---- .../Console/Output/ConsoleOutputInterface.php | 7 +++++++ src/Symfony/Component/Console/Output/Output.php | 16 +++++++++------- .../Component/Console/Output/OutputInterface.php | 14 ++++++++------ .../Component/Console/Output/StreamOutput.php | 9 ++++----- 5 files changed, 31 insertions(+), 22 deletions(-) diff --git a/src/Symfony/Component/Console/Output/ConsoleOutput.php b/src/Symfony/Component/Console/Output/ConsoleOutput.php index 46926c35c4..adea35f77e 100644 --- a/src/Symfony/Component/Console/Output/ConsoleOutput.php +++ b/src/Symfony/Component/Console/Output/ConsoleOutput.php @@ -36,10 +36,9 @@ class ConsoleOutput extends StreamOutput implements ConsoleOutputInterface /** * Constructor. * - * @param integer $verbosity The verbosity level (self::VERBOSITY_QUIET, self::VERBOSITY_NORMAL, - * self::VERBOSITY_VERBOSE) - * @param Boolean $decorated Whether to decorate messages or not (null for auto-guessing) - * @param OutputFormatterInterface $formatter Output formatter instance + * @param integer $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface) + * @param Boolean|null $decorated Whether to decorate messages (null for auto-guessing) + * @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter) * * @api */ diff --git a/src/Symfony/Component/Console/Output/ConsoleOutputInterface.php b/src/Symfony/Component/Console/Output/ConsoleOutputInterface.php index 5006b80070..c63bd157be 100644 --- a/src/Symfony/Component/Console/Output/ConsoleOutputInterface.php +++ b/src/Symfony/Component/Console/Output/ConsoleOutputInterface.php @@ -22,9 +22,16 @@ use Symfony\Component\Console\Output\OutputInterface; interface ConsoleOutputInterface extends OutputInterface { /** + * Gets the OutputInterface for errors. + * * @return OutputInterface */ public function getErrorOutput(); + /** + * Sets the OutputInterface used for errors. + * + * @param OutputInterface $error + */ public function setErrorOutput(OutputInterface $error); } diff --git a/src/Symfony/Component/Console/Output/Output.php b/src/Symfony/Component/Console/Output/Output.php index dd7dbe0f5b..89b68a2792 100644 --- a/src/Symfony/Component/Console/Output/Output.php +++ b/src/Symfony/Component/Console/Output/Output.php @@ -17,10 +17,12 @@ use Symfony\Component\Console\Formatter\OutputFormatter; /** * Base class for output classes. * - * There are three levels of verbosity: + * There are five levels of verbosity: * - * * normal: no option passed (normal output - information) - * * verbose: -v (more output - debug) + * * normal: no option passed (normal output) + * * verbose: -v (more output) + * * very verbose: -vv (highly extended output) + * * debug: -vvv (all debug output) * * quiet: -q (no output) * * @author Fabien Potencier @@ -35,9 +37,9 @@ abstract class Output implements OutputInterface /** * Constructor. * - * @param integer $verbosity The verbosity level (self::VERBOSITY_QUIET, self::VERBOSITY_NORMAL, self::VERBOSITY_VERBOSE) - * @param Boolean $decorated Whether to decorate messages or not - * @param OutputFormatterInterface $formatter Output formatter instance + * @param integer $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface) + * @param Boolean $decorated Whether to decorate messages + * @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter) * * @api */ @@ -137,7 +139,7 @@ abstract class Output implements OutputInterface * Writes a message to the output. * * @param string|array $messages The message as an array of lines or a single string - * @param Boolean $newline Whether to add a newline or not + * @param Boolean $newline Whether to add a newline * @param integer $type The type of output * * @throws \InvalidArgumentException When unknown output type is given diff --git a/src/Symfony/Component/Console/Output/OutputInterface.php b/src/Symfony/Component/Console/Output/OutputInterface.php index eab9d54e14..83d5013fdd 100644 --- a/src/Symfony/Component/Console/Output/OutputInterface.php +++ b/src/Symfony/Component/Console/Output/OutputInterface.php @@ -36,8 +36,8 @@ interface OutputInterface * Writes a message to the output. * * @param string|array $messages The message as an array of lines or a single string - * @param Boolean $newline Whether to add a newline or not - * @param integer $type The type of output (0: normal, 1: raw, 2: plain) + * @param Boolean $newline Whether to add a newline + * @param integer $type The type of output (one of the OUTPUT constants) * * @throws \InvalidArgumentException When unknown output type is given * @@ -49,7 +49,9 @@ interface OutputInterface * Writes a message to the output and adds a newline at the end. * * @param string|array $messages The message as an array of lines of a single string - * @param integer $type The type of output (0: normal, 1: raw, 2: plain) + * @param integer $type The type of output (one of the OUTPUT constants) + * + * @throws \InvalidArgumentException When unknown output type is given * * @api */ @@ -58,7 +60,7 @@ interface OutputInterface /** * Sets the verbosity of the output. * - * @param integer $level The level of verbosity + * @param integer $level The level of verbosity (one of the VERBOSITY constants) * * @api */ @@ -67,7 +69,7 @@ interface OutputInterface /** * Gets the current verbosity of the output. * - * @return integer The current level of verbosity + * @return integer The current level of verbosity (one of the VERBOSITY constants) * * @api */ @@ -76,7 +78,7 @@ interface OutputInterface /** * Sets the decorated flag. * - * @param Boolean $decorated Whether to decorate the messages or not + * @param Boolean $decorated Whether to decorate the messages * * @api */ diff --git a/src/Symfony/Component/Console/Output/StreamOutput.php b/src/Symfony/Component/Console/Output/StreamOutput.php index 831b2ae881..674bdad571 100644 --- a/src/Symfony/Component/Console/Output/StreamOutput.php +++ b/src/Symfony/Component/Console/Output/StreamOutput.php @@ -35,11 +35,10 @@ class StreamOutput extends Output /** * Constructor. * - * @param mixed $stream A stream resource - * @param integer $verbosity The verbosity level (self::VERBOSITY_QUIET, self::VERBOSITY_NORMAL, - * self::VERBOSITY_VERBOSE) - * @param Boolean $decorated Whether to decorate messages or not (null for auto-guessing) - * @param OutputFormatterInterface $formatter Output formatter instance + * @param mixed $stream A stream resource + * @param integer $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface) + * @param Boolean|null $decorated Whether to decorate messages (null for auto-guessing) + * @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter) * * @throws \InvalidArgumentException When first argument is not a real stream * From 940b7888ac16b3d1a60904c2c478c7c54a624367 Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Tue, 14 May 2013 18:52:49 +0200 Subject: [PATCH 6/6] [Console] use inheritdoc for Output classes --- .../Console/Output/ConsoleOutput.php | 14 ++++- .../Component/Console/Output/Output.php | 53 +++---------------- .../Component/Console/Output/StreamOutput.php | 7 +-- 3 files changed, 22 insertions(+), 52 deletions(-) diff --git a/src/Symfony/Component/Console/Output/ConsoleOutput.php b/src/Symfony/Component/Console/Output/ConsoleOutput.php index adea35f77e..6dad744606 100644 --- a/src/Symfony/Component/Console/Output/ConsoleOutput.php +++ b/src/Symfony/Component/Console/Output/ConsoleOutput.php @@ -54,18 +54,27 @@ class ConsoleOutput extends StreamOutput implements ConsoleOutputInterface $this->stderr = new StreamOutput(fopen('php://stderr', 'w'), $verbosity, $decorated, $formatter); } + /** + * {@inheritdoc} + */ public function setDecorated($decorated) { parent::setDecorated($decorated); $this->stderr->setDecorated($decorated); } + /** + * {@inheritdoc} + */ public function setFormatter(OutputFormatterInterface $formatter) { parent::setFormatter($formatter); $this->stderr->setFormatter($formatter); } + /** + * {@inheritdoc} + */ public function setVerbosity($level) { parent::setVerbosity($level); @@ -73,13 +82,16 @@ class ConsoleOutput extends StreamOutput implements ConsoleOutputInterface } /** - * @return OutputInterface + * {@inheritdoc} */ public function getErrorOutput() { return $this->stderr; } + /** + * {@inheritdoc} + */ public function setErrorOutput(OutputInterface $error) { $this->stderr = $error; diff --git a/src/Symfony/Component/Console/Output/Output.php b/src/Symfony/Component/Console/Output/Output.php index 89b68a2792..0daedc3eaf 100644 --- a/src/Symfony/Component/Console/Output/Output.php +++ b/src/Symfony/Component/Console/Output/Output.php @@ -51,11 +51,7 @@ abstract class Output implements OutputInterface } /** - * Sets output formatter. - * - * @param OutputFormatterInterface $formatter - * - * @api + * {@inheritdoc} */ public function setFormatter(OutputFormatterInterface $formatter) { @@ -63,11 +59,7 @@ abstract class Output implements OutputInterface } /** - * Returns current output formatter instance. - * - * @return OutputFormatterInterface - * - * @api + * {@inheritdoc} */ public function getFormatter() { @@ -75,11 +67,7 @@ abstract class Output implements OutputInterface } /** - * Sets the decorated flag. - * - * @param Boolean $decorated Whether to decorate the messages or not - * - * @api + * {@inheritdoc} */ public function setDecorated($decorated) { @@ -87,11 +75,7 @@ abstract class Output implements OutputInterface } /** - * Gets the decorated flag. - * - * @return Boolean true if the output will decorate messages, false otherwise - * - * @api + * {@inheritdoc} */ public function isDecorated() { @@ -99,11 +83,7 @@ abstract class Output implements OutputInterface } /** - * Sets the verbosity of the output. - * - * @param integer $level The level of verbosity - * - * @api + * {@inheritdoc} */ public function setVerbosity($level) { @@ -111,11 +91,7 @@ abstract class Output implements OutputInterface } /** - * Gets the current verbosity of the output. - * - * @return integer The current level of verbosity - * - * @api + * {@inheritdoc} */ public function getVerbosity() { @@ -123,12 +99,7 @@ abstract class Output implements OutputInterface } /** - * Writes a message to the output and adds a newline at the end. - * - * @param string|array $messages The message as an array of lines or a single string - * @param integer $type The type of output - * - * @api + * {@inheritdoc} */ public function writeln($messages, $type = self::OUTPUT_NORMAL) { @@ -136,15 +107,7 @@ abstract class Output implements OutputInterface } /** - * Writes a message to the output. - * - * @param string|array $messages The message as an array of lines or a single string - * @param Boolean $newline Whether to add a newline - * @param integer $type The type of output - * - * @throws \InvalidArgumentException When unknown output type is given - * - * @api + * {@inheritdoc} */ public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL) { diff --git a/src/Symfony/Component/Console/Output/StreamOutput.php b/src/Symfony/Component/Console/Output/StreamOutput.php index 674bdad571..09a5ca38b6 100644 --- a/src/Symfony/Component/Console/Output/StreamOutput.php +++ b/src/Symfony/Component/Console/Output/StreamOutput.php @@ -70,12 +70,7 @@ class StreamOutput extends Output } /** - * Writes a message to the output. - * - * @param string $message A message to write to the output - * @param Boolean $newline Whether to add a newline or not - * - * @throws \RuntimeException When unable to write output (should never happen) + * {@inheritdoc} */ protected function doWrite($message, $newline) {