2010-10-03 18:54:41 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2010-10-04 03:00:48 +00:00
|
|
|
* ConsoleOutputTest file
|
2010-10-03 18:54:41 +00:00
|
|
|
*
|
2012-04-27 02:49:18 +00:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
2013-02-08 12:28:17 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-10-03 18:54:41 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2010-10-03 18:54:41 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice
|
|
|
|
*
|
2013-02-08 12:28:17 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2012-04-27 02:49:18 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Console
|
2010-10-03 18:54:41 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.5432
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2010-10-03 18:54:41 +00:00
|
|
|
*/
|
|
|
|
|
2011-03-08 19:14:03 +00:00
|
|
|
App::uses('ConsoleOutput', 'Console');
|
2010-10-03 18:54:41 +00:00
|
|
|
|
2013-05-30 22:11:14 +00:00
|
|
|
/**
|
|
|
|
* Class ConsoleOutputTest
|
|
|
|
*
|
|
|
|
* @package Cake.Test.Case.Console
|
|
|
|
*/
|
2010-10-03 18:54:41 +00:00
|
|
|
class ConsoleOutputTest extends CakeTestCase {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* setup
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function setUp() {
|
2010-10-03 18:54:41 +00:00
|
|
|
parent::setUp();
|
|
|
|
$this->output = $this->getMock('ConsoleOutput', array('_write'));
|
2010-11-07 23:41:40 +00:00
|
|
|
$this->output->outputAs(ConsoleOutput::COLOR);
|
2010-10-03 18:54:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* tearDown
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function tearDown() {
|
2012-04-16 02:20:34 +00:00
|
|
|
parent::tearDown();
|
2010-10-03 18:54:41 +00:00
|
|
|
unset($this->output);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test writing with no new line
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testWriteNoNewLine() {
|
2010-10-03 18:54:41 +00:00
|
|
|
$this->output->expects($this->once())->method('_write')
|
|
|
|
->with('Some output');
|
|
|
|
|
|
|
|
$this->output->write('Some output', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test writing with no new line
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testWriteNewLine() {
|
2010-10-03 18:54:41 +00:00
|
|
|
$this->output->expects($this->once())->method('_write')
|
|
|
|
->with('Some output' . PHP_EOL);
|
|
|
|
|
|
|
|
$this->output->write('Some output');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test write() with multiple new lines
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testWriteMultipleNewLines() {
|
2010-10-03 18:54:41 +00:00
|
|
|
$this->output->expects($this->once())->method('_write')
|
|
|
|
->with('Some output' . PHP_EOL . PHP_EOL . PHP_EOL . PHP_EOL);
|
|
|
|
|
|
|
|
$this->output->write('Some output', 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test writing an array of messages.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testWriteArray() {
|
2010-10-03 18:54:41 +00:00
|
|
|
$this->output->expects($this->once())->method('_write')
|
|
|
|
->with('Line' . PHP_EOL . 'Line' . PHP_EOL . 'Line' . PHP_EOL);
|
|
|
|
|
|
|
|
$this->output->write(array('Line', 'Line', 'Line'));
|
|
|
|
}
|
|
|
|
|
2015-12-01 12:01:39 +00:00
|
|
|
/**
|
|
|
|
* test writing an array of messages.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2015-12-01 14:26:22 +00:00
|
|
|
public function testOverwrite() {
|
|
|
|
$testString = "Text";
|
2015-12-01 12:01:39 +00:00
|
|
|
|
2015-12-01 14:26:22 +00:00
|
|
|
$this->output->expects($this->at(0))->method('_write')
|
|
|
|
->with($testString);
|
2015-12-01 12:01:39 +00:00
|
|
|
|
2015-12-01 14:26:22 +00:00
|
|
|
$this->output->expects($this->at(1))->method('_write')
|
|
|
|
->with("");
|
2015-12-01 12:01:39 +00:00
|
|
|
|
2015-12-01 14:26:22 +00:00
|
|
|
$this->output->expects($this->at(2))->method('_write')
|
|
|
|
->with("Overwriting text");
|
2015-12-01 12:01:39 +00:00
|
|
|
|
2015-12-01 14:26:22 +00:00
|
|
|
$this->output->write($testString, 0);
|
|
|
|
$this->output->overwrite("Overwriting text");
|
|
|
|
}
|
2015-12-01 12:01:39 +00:00
|
|
|
|
2010-10-03 19:41:31 +00:00
|
|
|
/**
|
|
|
|
* test getting a style.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testStylesGet() {
|
2010-10-03 19:41:31 +00:00
|
|
|
$result = $this->output->styles('error');
|
2010-10-09 15:01:22 +00:00
|
|
|
$expected = array('text' => 'red', 'underline' => true);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2010-10-03 22:54:34 +00:00
|
|
|
|
2010-10-03 19:41:31 +00:00
|
|
|
$this->assertNull($this->output->styles('made_up_goop'));
|
2010-10-03 22:54:34 +00:00
|
|
|
|
|
|
|
$result = $this->output->styles();
|
|
|
|
$this->assertNotEmpty($result, 'error', 'Error is missing');
|
|
|
|
$this->assertNotEmpty($result, 'warning', 'Warning is missing');
|
2010-10-03 19:41:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test adding a style.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testStylesAdding() {
|
2010-10-03 19:41:31 +00:00
|
|
|
$this->output->styles('test', array('text' => 'red', 'background' => 'black'));
|
|
|
|
$result = $this->output->styles('test');
|
|
|
|
$expected = array('text' => 'red', 'background' => 'black');
|
|
|
|
$this->assertEquals($expected, $result);
|
2011-05-16 22:49:00 +00:00
|
|
|
|
2010-10-03 19:41:31 +00:00
|
|
|
$this->assertTrue($this->output->styles('test', false), 'Removing a style should return true.');
|
|
|
|
$this->assertNull($this->output->styles('test'), 'Removed styles should be null.');
|
|
|
|
}
|
|
|
|
|
2010-10-03 22:54:34 +00:00
|
|
|
/**
|
|
|
|
* test formatting text with styles.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testFormattingSimple() {
|
2010-10-03 22:54:34 +00:00
|
|
|
$this->output->expects($this->once())->method('_write')
|
2010-10-09 15:01:22 +00:00
|
|
|
->with("\033[31;4mError:\033[0m Something bad");
|
2010-10-03 22:54:34 +00:00
|
|
|
|
|
|
|
$this->output->write('<error>Error:</error> Something bad', false);
|
|
|
|
}
|
|
|
|
|
2010-10-10 16:24:37 +00:00
|
|
|
/**
|
|
|
|
* test that formatting doesn't eat tags it doesn't know about.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testFormattingNotEatingTags() {
|
2010-10-10 16:24:37 +00:00
|
|
|
$this->output->expects($this->once())->method('_write')
|
|
|
|
->with("<red> Something bad");
|
|
|
|
|
|
|
|
$this->output->write('<red> Something bad', false);
|
|
|
|
}
|
|
|
|
|
2010-10-03 23:01:27 +00:00
|
|
|
/**
|
|
|
|
* test formatting with custom styles.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testFormattingCustom() {
|
2010-10-03 23:01:27 +00:00
|
|
|
$this->output->styles('annoying', array(
|
|
|
|
'text' => 'magenta',
|
|
|
|
'background' => 'cyan',
|
|
|
|
'blink' => true,
|
|
|
|
'underline' => true
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->output->expects($this->once())->method('_write')
|
|
|
|
->with("\033[35;46;5;4mAnnoy:\033[0m Something bad");
|
|
|
|
|
|
|
|
$this->output->write('<annoying>Annoy:</annoying> Something bad', false);
|
|
|
|
}
|
|
|
|
|
2010-10-03 22:54:34 +00:00
|
|
|
/**
|
|
|
|
* test formatting text with missing styles.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testFormattingMissingStyleName() {
|
2010-10-03 22:54:34 +00:00
|
|
|
$this->output->expects($this->once())->method('_write')
|
2010-10-21 03:40:05 +00:00
|
|
|
->with("<not_there>Error:</not_there> Something bad");
|
2010-10-03 22:54:34 +00:00
|
|
|
|
|
|
|
$this->output->write('<not_there>Error:</not_there> Something bad', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test formatting text with multiple styles.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testFormattingMultipleStylesName() {
|
2010-10-03 22:54:34 +00:00
|
|
|
$this->output->expects($this->once())->method('_write')
|
2010-10-09 15:01:22 +00:00
|
|
|
->with("\033[31;4mBad\033[0m \033[33mWarning\033[0m Regular");
|
2010-10-03 22:54:34 +00:00
|
|
|
|
|
|
|
$this->output->write('<error>Bad</error> <warning>Warning</warning> Regular', false);
|
|
|
|
}
|
|
|
|
|
2010-10-14 01:09:33 +00:00
|
|
|
/**
|
|
|
|
* test that multiple tags of the same name work in one string.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testFormattingMultipleSameTags() {
|
2010-10-15 02:16:42 +00:00
|
|
|
$this->output->expects($this->once())->method('_write')
|
|
|
|
->with("\033[31;4mBad\033[0m \033[31;4mWarning\033[0m Regular");
|
|
|
|
|
|
|
|
$this->output->write('<error>Bad</error> <error>Warning</error> Regular', false);
|
2010-10-14 01:09:33 +00:00
|
|
|
}
|
|
|
|
|
2010-10-23 03:30:08 +00:00
|
|
|
/**
|
|
|
|
* test raw output not getting tags replaced.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testOutputAsRaw() {
|
2010-10-23 03:30:08 +00:00
|
|
|
$this->output->outputAs(ConsoleOutput::RAW);
|
|
|
|
$this->output->expects($this->once())->method('_write')
|
|
|
|
->with('<error>Bad</error> Regular');
|
|
|
|
|
|
|
|
$this->output->write('<error>Bad</error> Regular', false);
|
|
|
|
}
|
|
|
|
|
2010-10-23 03:32:01 +00:00
|
|
|
/**
|
|
|
|
* test plain output.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testOutputAsPlain() {
|
2010-10-23 03:32:01 +00:00
|
|
|
$this->output->outputAs(ConsoleOutput::PLAIN);
|
|
|
|
$this->output->expects($this->once())->method('_write')
|
|
|
|
->with('Bad Regular');
|
|
|
|
|
|
|
|
$this->output->write('<error>Bad</error> Regular', false);
|
|
|
|
}
|
2011-11-03 01:50:16 +00:00
|
|
|
|
2015-01-26 03:24:24 +00:00
|
|
|
/**
|
|
|
|
* test plain output when php://output, as php://output is
|
|
|
|
* not compatible with posix_ functions.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testOutputAsPlainWhenOutputStream() {
|
|
|
|
$output = $this->getMock('ConsoleOutput', array('_write'), array('php://output'));
|
|
|
|
$this->assertEquals(ConsoleOutput::PLAIN, $output->outputAs());
|
|
|
|
}
|
|
|
|
|
2011-11-03 01:50:16 +00:00
|
|
|
/**
|
|
|
|
* test plain output only strips tags used for formatting.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testOutputAsPlainSelectiveTagRemoval() {
|
|
|
|
$this->output->outputAs(ConsoleOutput::PLAIN);
|
|
|
|
$this->output->expects($this->once())->method('_write')
|
|
|
|
->with('Bad Regular <b>Left</b> <i>behind</i> <name>');
|
|
|
|
|
|
|
|
$this->output->write('<error>Bad</error> Regular <b>Left</b> <i>behind</i> <name>', false);
|
|
|
|
}
|
2011-05-16 22:49:00 +00:00
|
|
|
}
|