Customize ResultPrinter in PHPUnit

There are one class that prints the results of the tests when you execute PHPUnit with a testsuite.

If you want, you can customize the way that you print it, setting the colors, the characters, the width, …

You have to create a class that extends “PHPUnit_TextUI_ResultPrinter”, and set that class in your phpunit.xml.

<?xml version="1.0" encoding="UTF-8"?>
 
<phpunit
    ...
    printerClass = "MyResultPrinter"
    ...
>
 
    <testsuite name="Application">
        ...
    </testsuite>
</phpunit>

The next class is an example, and it will print a logo created with ascii.

<?php
 
/**
 * Class MyResultPrinter
 */
class MyResultPrinter extends PHPUnit_TextUI_ResultPrinter
{
    /**
     * @var int
     */
    protected $currentLine = 0;
 
    /**
     * @var string
     */
    protected $logo = <<<HEREDOC
   U  ___ u   ____    __  __
    \/"_ \/U | __")uU|' \/ '|u
    | | | | \|  _ \/\| |\/| |/
.-,_| |_| |  | |_) | | |  | |
 \_)-\___/   |____/  |_|  |_|
      \\    _|| \\_ <<,-,,-.
     (__)  (__) (__) (./  \.)
HEREDOC;
 
    /**
     * @var array
     */
    protected $logoArray = array();
 
    /**
     * @var int
     */
    protected $logoNumLines = 0;
 
    /**
     * {@inheritDoc}
     */
    public function __construct($out = null, $verbose = false, $colors = false, $debug = false)
    {
        parent::__construct($out, $verbose, true, $debug);
    }
 
    /**
     * {@inheritDoc}
     */
    public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
    {
        parent::startTestSuite($suite);
 
        $this->maxColumn = 20;
 
        $this->logoNumLines = substr_count( $this->logo, "\n" ) + 2;
        $this->logoArray = explode("\n", $this->logo);
    }
 
    /**
     * {@inheritDoc}
     */
    protected function writeNewLine()
    {
        $this->write(' ');
 
        if (($this->logoNumLines-1) != ($this->currentLine%$this->logoNumLines)) {
            $logoLine = $this->logoArray[$this->currentLine%$this->logoNumLines];
            $this->write($logoLine);
        }
 
        $this->currentLine++;
 
        parent::writeNewLine();
    }
 
}

The output that you will see:

PHPUnit 3.7.19 by Sebastian Bergmann.
 
III.................  20 / 582 (  3%)    U  ___ u   ____    __  __
.....IIIIIIII......S  40 / 582 (  6%)     \/"_ \/U | __")uU|' \/ '|u
S...................  60 / 582 ( 10%)     | | | | \|  _ \/\| |\/| |/
................I...  80 / 582 ( 13%) .-,_| |_| |  | |_) | | |  | |
.................... 100 / 582 ( 17%)  \_)-\___/   |____/  |_|  |_|
.................... 120 / 582 ( 20%)       \    _|| \_ <<,-,,-.
.........I.......... 140 / 582 ( 24%)      (__)  (__) (__) (./  \.)
.................... 160 / 582 ( 27%)
..............SSSSSS 180 / 582 ( 30%)    U  ___ u   ____    __  __
SSSS................ 200 / 582 ( 34%)     \/"_ \/U | __")uU|' \/ '|u
.................... 220 / 582 ( 37%)     | | | | \|  _ \/\| |\/| |/
.................... 240 / 582 ( 41%) .-,_| |_| |  | |_) | | |  | |
.................... 260 / 582 ( 44%)  \_)-\___/   |____/  |_|  |_|
.................... 280 / 582 ( 48%)       \    _|| \_ <<,-,,-.
.................... 300 / 582 ( 51%)      (__)  (__) (__) (./  \.)
.................... 320 / 582 ( 54%)
..................SS 340 / 582 ( 58%)    U  ___ u   ____    __  __
S................... 360 / 582 ( 61%)     \/"_ \/U | __")uU|' \/ '|u
.................... 380 / 582 ( 65%)     | | | | \|  _ \/\| |\/| |/
.................... 400 / 582 ( 68%) .-,_| |_| |  | |_) | | |  | |
.................... 420 / 582 ( 72%)  \_)-\___/   |____/  |_|  |_|
.................... 440 / 582 ( 75%)       \    _|| \_ <<,-,,-.
.................... 460 / 582 ( 79%)      (__)  (__) (__) (./  \.)
.................... 480 / 582 ( 82%)
.................... 500 / 582 ( 85%)    U  ___ u   ____    __  __
.................... 520 / 582 ( 89%)     \/"_ \/U | __")uU|' \/ '|u
.................... 540 / 582 ( 92%)     | | | | \|  _ \/\| |\/| |/
.................... 560 / 582 ( 96%) .-,_| |_| |  | |_) | | |  | |
...I................ 580 / 582 ( 99%)  \_)-\___/   |____/  |_|  |_|
..

You can create the logo using for example the next tool: http://patorjk.com/software/taag/#p=testall&f=Ogre&t=OBM

Related link: http://php-and-symfony.matthiasnoback.nl/2011/11/phpunit-create-a-resultprinter-for-output-in-the-browser/

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos necesarios están marcados *

Puedes usar las siguientes etiquetas y atributos HTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>