PHPStorm + PHPUnit 颜色输出

标签 php phpunit phpstorm

所以我在 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/

相关文章:

php - 将导航到 protected 文件夹的个人指向登录页面

php - MySQL 查询 w/2 个表以获得所需的结果

PHP LDAP 身份验证不工作

php - 使用 Docker 在 PhpStorm 中使用 PHPUnit

fopen/fwrite 链的 PHPUnit 测试

symfony - 如何在 Symfony 中使用 Monolog 断言记录了一行

css - phpstorm 中 compass 的文件观察器

php - 单元测试 Zend Controller 和模拟一些已执行的操作

google-maps - 为 PhpStorm 中的代码补全添加 Google Maps 文档

laravel - 使用 Laravel Homestead 进行 PhpStorm 调试无法正常工作