【问题标题】:PHPStorm + PHPUnit Color outputPHPStorm + PHPUnit 颜色输出
【发布时间】:2014-04-19 03:09:25
【问题描述】:

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

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

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

[31mError! Error![0m

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

【问题讨论】:

    标签: php phpunit phpstorm


    【解决方案1】:

    这个问题是 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"));
    

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

    【讨论】:

      【解决方案2】:

      PhpStorm 有专门的集成脚本来运行 PHPUnit 测试(所有消息/进度指示器都转移到 GUI 部分,您可以轻松查看哪些测试已通过,哪些未通过等)。

      PhpStorm 的控制台不支持 ANSI 颜色 -- http://youtrack.jetbrains.com/issue/IDEA-69880 和相关票证。

      但是您可以安装 Grep Console 插件,它将添加对 ANSI 颜色的支持(需要在设置中激活)。我已经用 PHPUnit 尝试过它并且它有效 - 并非所有内容都是彩色的(有些代码无法识别,但大多数有效)。如果需要,您可以联系插件作者并提供不工作的部分。

      【讨论】:

      • 你用的是什么版本的phpstorm?我找不到将在 7.1 中安装的 grep 控制台...
      • 我已安装该插件并在 7.1.3(插件版本 3.1)和 8.0 EAP(插件版本 3.2)中运行。你是怎么安装的?
      • 3.1 版正在崩溃最新的 PHPStorm 7.1
      • @Niloct 无法确认:在 Windows 7 x64 SP1 上工作正常。因此: 1) 检查您的idea.log 以查看是否有任何提示(例外); 2) 与插件作者联系并提供崩溃详细信息——很遗憾,我无法在这里为您提供帮助(尤其是当它在我的环境中工作时)。
      • @Nicolt 这也可能取决于您如何运行 PHPUnit 测试。如果通过 PHPUnit Run/Debug Configuration .. 那么我认为没有理由 at all 在那里有颜色,因为它是在考虑 IDE 集成的情况下运行的(通过帮助脚本)——帮助脚本确实可能不喜欢 ANSI颜色,我不知道它会如何表现。如果您在内置控制台/终端中运行——那么颜色在这里可以工作(不是全部(对我来说),但可以工作)。
      【解决方案3】:

      在您的项目根目录中创建一个phpunit.xml 文件,其中包含以下内容:

      <phpunit
          colors="true">
      </phpunit>
      
      • 在 PHPStorm 中选择 PhpStorm > Preferences
      • 在PHP下>测试框架>点击+添加PHPUnit Local
      • 点击Path to phpunit.phar单选按钮。
      • 从 phpunit 供应商目录中选择 phpunit 二进制文件。
      • Test Runner 下单击Default configuration file 复选框。
      • 选择您之前创建的phpunit.xml 文件。
      • 点击Apply

      已通过 PHPStorm 2021.2 和 PHPUnit 9.5.8 测试。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-01
        • 2018-03-24
        • 2011-07-25
        • 2016-10-21
        • 2016-01-04
        • 2016-09-12
        • 1970-01-01
        • 2017-08-28
        相关资源
        最近更新 更多