mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-18 18:46:17 +00:00
Starting to refactor console output into ConsoleOutput.
This commit is contained in:
parent
3d65b68f1d
commit
7dea9b0dbd
2 changed files with 168 additions and 0 deletions
76
cake/console/console_output.php
Normal file
76
cake/console/console_output.php
Normal file
|
@ -0,0 +1,76 @@
|
|||
<?php
|
||||
/**
|
||||
* ConsoleOutput an object to provide methods for generating console output.
|
||||
* Can be connected to any stream resource that can be used with fopen()
|
||||
*
|
||||
* PHP 5
|
||||
*
|
||||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||||
* @link http://cakephp.org CakePHP(tm) Project
|
||||
* @package cake
|
||||
* @subpackage cake.cake.console
|
||||
* @since CakePHP(tm) v 2.0
|
||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
*/
|
||||
class ConsoleOutput {
|
||||
/**
|
||||
* File handle for output.
|
||||
*
|
||||
* @var resource
|
||||
*/
|
||||
protected $_output;
|
||||
|
||||
/**
|
||||
* Constant for a newline.
|
||||
*/
|
||||
const LF = PHP_EOL;
|
||||
|
||||
/**
|
||||
* Construct the output object.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($stream = 'php://stdout') {
|
||||
$this->_output = fopen($stream, 'w');
|
||||
}
|
||||
|
||||
/**
|
||||
* Outputs a single or multiple messages to stdout. If no parameters
|
||||
* are passed outputs just a newline.
|
||||
*
|
||||
* @param mixed $message A string or a an array of strings to output
|
||||
* @param integer $newlines Number of newlines to append
|
||||
* @return integer Returns the number of bytes returned from writing to stdout.
|
||||
*/
|
||||
public function write($message, $newlines = 1) {
|
||||
if (is_array($message)) {
|
||||
$message = implode(self::LF, $message);
|
||||
}
|
||||
return $this->_write($message . str_repeat(self::LF, $newlines));
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a message to the output stream
|
||||
*
|
||||
* @param string $message Message to write.
|
||||
* @return boolean success
|
||||
*/
|
||||
protected function _write($message) {
|
||||
return fwrite($this->_output, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* clean up and close handles
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __destruct() {
|
||||
fclose($this->_output);
|
||||
}
|
||||
}
|
92
cake/tests/cases/console/console_output.test.php
Normal file
92
cake/tests/cases/console/console_output.test.php
Normal file
|
@ -0,0 +1,92 @@
|
|||
<?php
|
||||
/**
|
||||
* ShellDispatcherTest file
|
||||
*
|
||||
* PHP 5
|
||||
*
|
||||
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
||||
* Copyright 2005-2010, Cake Software Foundation, Inc.
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistributions of files must retain the above copyright notice
|
||||
*
|
||||
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc.
|
||||
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
|
||||
* @package cake
|
||||
* @subpackage cake.tests.cases.console
|
||||
* @since CakePHP(tm) v 1.2.0.5432
|
||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
*/
|
||||
|
||||
require_once CAKE . 'console' . DS . 'console_output.php';
|
||||
|
||||
class ConsoleOutputTest extends CakeTestCase {
|
||||
|
||||
/**
|
||||
* setup
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function setUp() {
|
||||
parent::setUp();
|
||||
$this->output = $this->getMock('ConsoleOutput', array('_write'));
|
||||
}
|
||||
|
||||
/**
|
||||
* tearDown
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function tearDown() {
|
||||
unset($this->output);
|
||||
}
|
||||
|
||||
/**
|
||||
* test writing with no new line
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function testWriteNoNewLine() {
|
||||
$this->output->expects($this->once())->method('_write')
|
||||
->with('Some output');
|
||||
|
||||
$this->output->write('Some output', false);
|
||||
}
|
||||
|
||||
/**
|
||||
* test writing with no new line
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function testWriteNewLine() {
|
||||
$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
|
||||
*/
|
||||
function testWriteMultipleNewLines() {
|
||||
$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
|
||||
*/
|
||||
function testWriteArray() {
|
||||
$this->output->expects($this->once())->method('_write')
|
||||
->with('Line' . PHP_EOL . 'Line' . PHP_EOL . 'Line' . PHP_EOL);
|
||||
|
||||
$this->output->write(array('Line', 'Line', 'Line'));
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue