// echo out the escaped strings to create different levels of warnings
echo Display::warning("Warning!");
echo Display::caution("Caution...");
echo Display::OK("OK to go!");
// place the escaped string in the $message field to light up your output
$this->assertSame("one","two",Display::caution("This assertion has intentionally failed"));
class Display
{
/**
* The escape sequence that clears any active styling on the terminal.
*/
const CLEAR = "\e[0m";
/**
* Warning escape sequence which sets the background color to red and the
* foreground to white.
*/
const WARNING = "\e[41;97m";
/**
* Caution escape sequence which sets the background color to yellow and the
* foreground to black.
*/
const CAUTION = "\e[43;30m";
/**
* OK escape sequence which sets the background color to green and the
* foreground to black.
*/
const OK = "\e[42;30m";
/**
* Display the text with a red background and white foreground
* and end it with the newline character (if desired)
*
* @param mixed $text - the text of the message
* @param boolean $newline - whether to append a new line
*
* @return string - the escaped sequence and the optional newline
*/
public static function warning($text, $newline = true) {
// echo the string surrounded with the escape coding to
// display a warning
$text = self::WARNING . $text . self::CLEAR;
// if a carriage return was desired, send it out
$text .= $newline ? "\n" : "";
// return the escaped text
return $text;
}
/**
* Display the text with a yellow background and black foreground
* and end it with the newline character (if desired)
*
* @param mixed $text - the text of the message
* @param boolean $newline - whether to append a new line
*
* @return string - the escaped sequence and the optional newline
*/
public static function caution($text, $newline = true) {
// echo the string surrounded with the escape coding to
// display a cautionary message
$text = self::CAUTION . $text . self::CLEAR;
// if a carriage return was desired, send it out
$text .= $newline ? "\n" : "";
// return the escaped text
return $text;
}
/**
* Display the text with a green background and black foreground
* and end it with the newline character (if desired)
*
* @param mixed $text - the text of the message
* @param boolean $newline - whether to append a new line
*
* @return string - the escaped sequence and the optional newline
*/
public static function OK($text, $newline = true) {
// echo the string surrounded with the escape coding to
// display a positive message
$text = self::OK . $text . self::CLEAR;
// if a carriage return was desired, send it out
$text .= $newline ? "\n" : "";
// return the escaped text
return $text;
}
4条答案
按热度按时间k10s72fa1#
5年前有人问过这个问题,但如果有人路过,我会告诉你,作为我的开源项目的一部分,我编写了一个简单的PHP类。它使用VT-100 ANSI转义序列,并在控制台中运行PHPUnit 8.5时用PHPSorm 2019.3进行了测试。
您可以复制下面的代码,包括在您的软件,或者你也可以通过 composer 安装'1happyplace/phpunit-colors'有一个完整的说明here.
以下示例代码将创建以下输出:
ykejflvf2#
PhpStorm有特殊的集成脚本来运行PHPUnit测试(所有的消息/进度指示器都被传输到GUI部分,在那里你可以很容易地看到哪些测试已经通过,哪些没有等)。
PhpStorm的控制台不支持ANSI颜色--http://youtrack.jetbrains.com/issue/IDEA-69880和相关票证。
但是你可以安装Grep控制台插件,它将增加对ANSI颜色的支持(需要在设置中激活)。我已经用PHPUnit试过了,它工作了--不是所有的东西都是彩色的(一些代码不能识别,但大多数都工作了)。如果需要,你可以联系插件作者,告诉他不工作的部分。
5vf7fwbs3#
在项目根目录中创建一个
phpunit.xml
文件,内容如下:PhpStorm
〉Preferences
。+
以添加PHPUnit Local
。Path to phpunit.phar
单选按钮。phpunit
二进制文件。Test Runner
下,单击Default configuration file
复选框。phpunit.xml
文件。Apply
。这已经在PHPSorm 2021.2和PHPUnit 9.5.8中进行了测试。
qni6mghb4#
编辑运行/调试配置并添加“测试运行程序选项”字段:--颜色=始终
适用于本地和Docker/wsl环境。