So I have PHPUnit running in PHPStorm 7.1, but I can't find out how to get the ANSI color codes working from within tests. My PHPunit.xml has colors = "true"
in the property list, but every time I try something like:
echo "\033[31mError! Error!\033[0m\n";
Inside one of my test cases, it just gives me:
[31mError! Error![0m
in the PHPstorm phpunit output. Is there any way to make the colors appear properly when using ANSI color codes in tests in PHPStorm?
This question was asked 5 years back, but if anyone stops by, I have written a simple PHP class as part of my open source project. It utilizes the VT-100 ANSI escape sequencing and has been tested with PHPStorm 2019.3 while running PHPUnit 8.5 in the console.
You can copy the code below to include in your software, or you can also install via composer '1happyplace/phpunit-colors' There is a full description here.
The following example code will create the output below:
// 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;
}
PhpStorm has special integration script to run PHPUnit tests (all messages/progress indicators are transferred into GUI part where you can easily see what tests have passed and what did not etc).
PhpStorm's console does not support ANSI colors -- http://youtrack.jetbrains.com/issue/IDEA-69880 and related tickets.
But you can install Grep Console plugin and it will add support for ANSI colors (needs to be activated in Settings). I have tried it with PHPUnit and it worked -- not everything was colored (some of the codes were not recognized, but most worked). You can contact plugin author with not working parts if desired.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With