gpt4 book ai didi

PHPStorm + PHPUnit 颜色输出

转载 作者:可可西里 更新时间:2023-11-01 14:02:56 25 4
gpt4 key购买 nike

所以我在 PHPStorm 7.1 中运行了 PHPUnit,但我无法找到如何从测试中获取 ANSI 颜色代码。我的 PHPunit.xml 在属性列表中有 colors = "true",但每次我尝试类似的东西时:

echo "\033[31mError! Error!\033[0m\n";

在我的一个测试用例中,它只给我:

[31mError! Error![0m

在 PHPstorm phpunit 输出中。在 PHPStorm 的测试中使用 ANSI 颜色代码时,有什么方法可以使颜色正确显示?

最佳答案

这个问题是 5 年前提出的,但如果有人过来,我已经编写了一个简单的 PHP 类作为我的开源项目的一部分。它利用 VT-100 ANSI 转义序列,并在控制台中运行 PHPUnit 8.5 时使用 PHPStorm 2019.3 进行了测试。

您可以复制下面的代码以包含在您的软件中,或者您也可以通过 composer '1happyplace/phpunit-colors' 安装 有一个完整的描述 here .

以下示例代码将创建以下输出:

// 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"));

enter image description here

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;
}

关于PHPStorm + PHPUnit 颜色输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22394619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com