TYPO3  7.6
OutputTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
13 
16 
17 class OutputTest extends \PHPUnit_Framework_TestCase
18 {
19  public function testConstructor()
20  {
21  $output = new TestOutput(Output::VERBOSITY_QUIET, true);
22  $this->assertEquals(Output::VERBOSITY_QUIET, $output->getVerbosity(), '__construct() takes the verbosity as its first argument');
23  $this->assertTrue($output->isDecorated(), '__construct() takes the decorated flag as its second argument');
24  }
25 
26  public function testSetIsDecorated()
27  {
28  $output = new TestOutput();
29  $output->setDecorated(true);
30  $this->assertTrue($output->isDecorated(), 'setDecorated() sets the decorated flag');
31  }
32 
33  public function testSetGetVerbosity()
34  {
35  $output = new TestOutput();
36  $output->setVerbosity(Output::VERBOSITY_QUIET);
37  $this->assertEquals(Output::VERBOSITY_QUIET, $output->getVerbosity(), '->setVerbosity() sets the verbosity');
38 
39  $this->assertTrue($output->isQuiet());
40  $this->assertFalse($output->isVerbose());
41  $this->assertFalse($output->isVeryVerbose());
42  $this->assertFalse($output->isDebug());
43 
44  $output->setVerbosity(Output::VERBOSITY_NORMAL);
45  $this->assertFalse($output->isQuiet());
46  $this->assertFalse($output->isVerbose());
47  $this->assertFalse($output->isVeryVerbose());
48  $this->assertFalse($output->isDebug());
49 
50  $output->setVerbosity(Output::VERBOSITY_VERBOSE);
51  $this->assertFalse($output->isQuiet());
52  $this->assertTrue($output->isVerbose());
53  $this->assertFalse($output->isVeryVerbose());
54  $this->assertFalse($output->isDebug());
55 
56  $output->setVerbosity(Output::VERBOSITY_VERY_VERBOSE);
57  $this->assertFalse($output->isQuiet());
58  $this->assertTrue($output->isVerbose());
59  $this->assertTrue($output->isVeryVerbose());
60  $this->assertFalse($output->isDebug());
61 
62  $output->setVerbosity(Output::VERBOSITY_DEBUG);
63  $this->assertFalse($output->isQuiet());
64  $this->assertTrue($output->isVerbose());
65  $this->assertTrue($output->isVeryVerbose());
66  $this->assertTrue($output->isDebug());
67  }
68 
69  public function testWriteWithVerbosityQuiet()
70  {
71  $output = new TestOutput(Output::VERBOSITY_QUIET);
72  $output->writeln('foo');
73  $this->assertEquals('', $output->output, '->writeln() outputs nothing if verbosity is set to VERBOSITY_QUIET');
74  }
75 
76  public function testWriteAnArrayOfMessages()
77  {
78  $output = new TestOutput();
79  $output->writeln(array('foo', 'bar'));
80  $this->assertEquals("foo\nbar\n", $output->output, '->writeln() can take an array of messages to output');
81  }
82 
86  public function testWriteRawMessage($message, $type, $expectedOutput)
87  {
88  $output = new TestOutput();
89  $output->writeln($message, $type);
90  $this->assertEquals($expectedOutput, $output->output);
91  }
92 
93  public function provideWriteArguments()
94  {
95  return array(
96  array('<info>foo</info>', Output::OUTPUT_RAW, "<info>foo</info>\n"),
97  array('<info>foo</info>', Output::OUTPUT_PLAIN, "foo\n"),
98  );
99  }
100 
102  {
103  $output = new TestOutput();
104  $output->setDecorated(false);
105  $output->writeln('<info>foo</info>');
106  $this->assertEquals("foo\n", $output->output, '->writeln() strips decoration tags if decoration is set to false');
107  }
108 
109  public function testWriteDecoratedMessage()
110  {
111  $fooStyle = new OutputFormatterStyle('yellow', 'red', array('blink'));
112  $output = new TestOutput();
113  $output->getFormatter()->setStyle('FOO', $fooStyle);
114  $output->setDecorated(true);
115  $output->writeln('<foo>foo</foo>');
116  $this->assertEquals("\033[33;41;5mfoo\033[39;49;25m\n", $output->output, '->writeln() decorates the output');
117  }
118 
124  {
125  $output = new TestOutput();
126  $output->writeln('<foo>foo</foo>', 24);
127  }
128 
129  public function testWriteWithInvalidStyle()
130  {
131  $output = new TestOutput();
132 
133  $output->clear();
134  $output->write('<bar>foo</bar>');
135  $this->assertEquals('<bar>foo</bar>', $output->output, '->write() do nothing when a style does not exist');
136 
137  $output->clear();
138  $output->writeln('<bar>foo</bar>');
139  $this->assertEquals("<bar>foo</bar>\n", $output->output, '->writeln() do nothing when a style does not exist');
140  }
141 }
142 
143 class TestOutput extends Output
144 {
145  public $output = '';
146 
147  public function clear()
148  {
149  $this->output = '';
150  }
151 
152  protected function doWrite($message, $newline)
153  {
154  $this->output .= $message.($newline ? "\n" : '');
155  }
156 }