PHPUnit_TextUI_ResultPrinter::formatWithColor PHP Méthode

formatWithColor() protected méthode

Formats a buffer with a specified ANSI color sequence if colors are enabled.
protected formatWithColor ( string $color, string $buffer ) : string
$color string
$buffer string
Résultat string
    protected function formatWithColor($color, $buffer)
    {
        if (!$this->colors) {
            return $buffer;
        }
        $codes = array_map('trim', explode(',', $color));
        $lines = explode("\n", $buffer);
        $padding = max(array_map('strlen', $lines));
        $styles = [];
        foreach ($codes as $code) {
            $styles[] = self::$ansiCodes[$code];
        }
        $style = sprintf("[%sm", implode(';', $styles));
        $styledLines = [];
        foreach ($lines as $line) {
            $styledLines[] = $style . str_pad($line, $padding) . "";
        }
        return implode("\n", $styledLines);
    }

Usage Example

 /**
  * @param string $color
  * @param string $buffer Result of the Test Case => . F S I R
  */
 private function printTestCaseStatus($color, $buffer)
 {
     if ($this->column >= $this->maxNumberOfColumns) {
         $this->writeNewLine();
         $padding = $this->maxClassNameLength;
         $this->column = $padding;
         echo str_pad(' ', $padding) . "\t";
     }
     if ($this->isCIEnvironment()) {
         echo $buffer;
         $this->column++;
         return;
     }
     switch (strtoupper($buffer)) {
         case '.':
             $color = 'fg-green,bold';
             $buffer = \mb_convert_encoding("'", 'UTF-8', 'UTF-16BE');
             break;
         case 'F':
             $color = 'fg-red,bold';
             $buffer = \mb_convert_encoding("'", 'UTF-8', 'UTF-16BE');
             break;
     }
     echo parent::formatWithColor($color, $buffer);
     $this->column++;
 }