Testing_DocTest
[ class tree: Testing_DocTest ] [ index: Testing_DocTest ] [ all elements ]

Source for file Default.php

Documentation is available at Default.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6.  * This file is part of the PEAR Testing_DocTest package.
  7.  *
  8.  * PHP version 5
  9.  *
  10.  * LICENSE: This source file is subject to the MIT license that is available
  11.  * through the world-wide-web at the following URI:
  12.  * http://opensource.org/licenses/mit-license.php
  13.  *
  14.  * @category  Testing
  15.  * @package   Testing_DocTest
  16.  * @author    David JEAN LOUIS <izimobil@gmail.com>
  17.  * @copyright 2008 David JEAN LOUIS
  18.  * @license   http://opensource.org/licenses/mit-license.php MIT License
  19.  * @version   CVS: $Id$
  20.  * @link      http://pear.php.net/package/Testing_DocTest
  21.  * @since     File available since release 0.1.0
  22.  * @filesource
  23.  */
  24.  
  25. /**
  26.  * Required file
  27.  */
  28. require_once 'Testing/DocTest/ReporterInterface.php';
  29.  
  30. /**
  31.  * DocTest Reporter default class.
  32.  *
  33.  * @category  Testing
  34.  * @package   Testing_DocTest
  35.  * @author    David JEAN LOUIS <izimobil@gmail.com>
  36.  * @copyright 2008 David JEAN LOUIS
  37.  * @license   http://opensource.org/licenses/mit-license.php MIT License
  38.  * @version   Release: 0.3.1
  39.  * @link      http://pear.php.net/package/Testing_DocTest
  40.  * @since     Class available since release 0.1.0
  41.  */
  42. class Testing_DocTest_Reporter_Default implements Testing_DocTest_ReporterInterface
  43. {
  44.     // properties {{{
  45.  
  46.     /**
  47.      * Timer beginning time.
  48.      *
  49.      * @var float time
  50.      * @access private
  51.      */
  52.     private $_time = 0;
  53.  
  54.     /**
  55.      * Opened log file.
  56.      *
  57.      * @var resource $_logfile 
  58.      * @access private
  59.      */
  60.     private $_logfile = false;
  61.  
  62.     // }}}
  63.     // __construct() {{{
  64.  
  65.     /**
  66.      * Constructor.
  67.      *
  68.      * @access public
  69.      */
  70.     public function __construct()
  71.     {
  72.         $reg Testing_DocTest_Registry::singleton();
  73.         if ($reg->logfile{
  74.             $this->_logfile fopen($reg->logfile'wb');
  75.         }
  76.         $time        explode(' 'microtime());
  77.         $this->_time $time[1$time[0];
  78.     }
  79.  
  80.     // }}}
  81.     // __destruct() {{{
  82.  
  83.     /**
  84.      * Destructor.
  85.      * Will eventually close the opened logfile.
  86.      *
  87.      * @access public
  88.      */
  89.     public function __destruct()
  90.     {
  91.         if (false !== $this->_logfile && is_resource($this->_logfile)) {
  92.             fclose($this->_logfile);
  93.         }
  94.     }
  95.  
  96.     // }}}
  97.     // onBegin() {{{
  98.  
  99.     /**
  100.      * Called when a doctest session begins.
  101.      *
  102.      * @param array $suites an array of Testing_DocTest_TestSuite instances
  103.      *
  104.      * @access public
  105.      * @return void 
  106.      */
  107.     public function onBegin(array $suites)
  108.     {
  109.         if (empty($suites)) {
  110.             $this->_output("Nothing to process.\n");
  111.         }
  112.     }
  113.  
  114.     // }}}
  115.     // onTestSuiteBegin() {{{
  116.  
  117.     /**
  118.      * Called before the runner starts running a suite.
  119.      *
  120.      * @param object $suite an instance of Testing_DocTest_TestSuite
  121.      *
  122.      * @access public
  123.      * @return void 
  124.      */
  125.     public function onTestSuiteBegin(Testing_DocTest_TestSuite $suite)
  126.     {
  127.         $this->_output("Processing {$suite->name}"'36');
  128.         $this->_output("\n");
  129.     }
  130.  
  131.     // }}}
  132.     // onTestCaseBegin() {{{
  133.  
  134.     /**
  135.      * Called before the runner run a test case.
  136.      *
  137.      * @param object $case a test case instance
  138.      *
  139.      * @access public
  140.      * @return void 
  141.      */
  142.     public function onTestCaseBegin(Testing_DocTest_TestCase $case)
  143.     {
  144.     }
  145.  
  146.     // }}}
  147.     // onTestCasePass() {{{
  148.  
  149.     /**
  150.      * Called when a test passed.
  151.      *
  152.      * @param object $case a test case instance
  153.      *
  154.      * @access public
  155.      * @return void 
  156.      */
  157.     public function onTestCasePass(Testing_DocTest_TestCase $case)
  158.     {
  159.         $this->_output('[PASS]  '32);
  160.         $this->_output("{$case->name}\n");
  161.     }
  162.  
  163.     // }}}
  164.     // onTestCaseSkip() {{{
  165.  
  166.     /**
  167.      * Called when a test was skipped by the runner.
  168.      *
  169.      * @param object $case a test case instance
  170.      *
  171.      * @access public
  172.      * @return void 
  173.      */
  174.     public function onTestCaseSkip(Testing_DocTest_TestCase $case)
  175.     {
  176.         $this->_output('[SKIP]  '33);
  177.         $this->_output("{$case->name}\n");
  178.     }
  179.  
  180.     // }}}
  181.     // onTestCaseFail() {{{
  182.  
  183.     /**
  184.      * Called when a test failed.
  185.      *
  186.      * @param object $case a test case instance
  187.      *
  188.      * @access public
  189.      * @return void 
  190.      */
  191.     public function onTestCaseFail(Testing_DocTest_TestCase $case)
  192.     {
  193.         $this->_output('[FAIL]  '31true);
  194.         $this->_output("{$case->name}\n"true);
  195.         $this->_output(str_pad(" Expected "72"="STR_PAD_BOTH)42true);
  196.         $this->_output("\n" trim($case->expectedValue"\n"falsetrue);
  197.         $this->_output(str_pad(" Actual "72"="STR_PAD_BOTH)41true);
  198.         $this->_output("\n" trim($case->actualValue"\n"falsetrue);
  199.         $this->_output("\n"falsetrue);
  200.     }
  201.  
  202.     // }}}
  203.     // onTestCaseError() {{{
  204.  
  205.     /**
  206.      * Called when a test has errors.
  207.      *
  208.      * @param object $case a test case instance
  209.      *
  210.      * @access public
  211.      * @return void 
  212.      */
  213.     public function onTestCaseError(Testing_DocTest_TestCase $case)
  214.     {
  215.         $this->_output('[ERROR] '31true);
  216.         $this->_output("{$case->name} in file \"{$case->suite->name}\"\n"true);
  217.         $bar str_repeat('-'31);
  218.         $this->_output(str_pad(" Error "72"="STR_PAD_BOTH)41true);
  219.         $this->_output("\n" trim($case->actualValue"\n"falsetrue);
  220.     }
  221.  
  222.     // }}}
  223.     // onTestCaseEnd() {{{
  224.  
  225.     /**
  226.      * Called when the runner finished a test case.
  227.      *
  228.      * @param object $case a test case instance
  229.      *
  230.      * @access public
  231.      * @return void 
  232.      */
  233.     public function onTestCaseEnd(Testing_DocTest_TestCase $case)
  234.     {
  235.     }
  236.  
  237.     // }}}
  238.     // onTestSuiteEnd() {{{
  239.  
  240.     /**
  241.      * Called when the runner finished running the suite.
  242.      *
  243.      * @param object $suite an instance of Testing_DocTest_TestSuite
  244.      *
  245.      * @access public
  246.      * @return void 
  247.      */
  248.     public function onTestSuiteEnd(Testing_DocTest_TestSuite $suite)
  249.     {
  250.         $this->_output("\n");
  251.     }
  252.  
  253.     // }}}
  254.     // onEnd() {{{
  255.  
  256.     /**
  257.      * Called at the end of the DocTest session.
  258.      *
  259.      * @param array $suites an array of Testing_DocTest_TestSuite instances
  260.      *
  261.      * @access public
  262.      * @return void 
  263.      */
  264.     public function onEnd(array $suites)
  265.     {
  266.         if (empty($suites)) {
  267.             return;
  268.         }
  269.         $time   explode(' 'microtime());
  270.         $t      ($time[1$time[0]$this->_time;
  271.         $passed $skipped $failed $error = 0;
  272.         foreach ($suites as $suite{
  273.             foreach ($suite as $tc{
  274.                 if ($tc->state == Testing_DocTest_TestCase::STATE_SKIPPED{
  275.                     $skipped++;
  276.                 else if (
  277.                     $tc->state == Testing_DocTest_TestCase::STATE_PASSED{
  278.                     $passed++;
  279.                 else {
  280.                     $failed++;
  281.                 }
  282.             }
  283.         }
  284.         $this->_output(sprintf("\nTotal time    : %.4f sec.\n"$t));
  285.         $this->_output("Passed tests  : $passed\n");
  286.         $this->_output("Skipped tests : $skipped\n");
  287.         $this->_output("Failed tests  : $failed\n\n");
  288.     }
  289.  
  290.     // }}}
  291.     // _output() {{{
  292.  
  293.     /**
  294.      * Writes the message $msg to STDOUT or to the logfile.
  295.      *
  296.      * @param string $msg   the message to output
  297.      * @param int    $color the color code (optional)
  298.      * @param bool   $force force output even if quiet mode
  299.      *
  300.      * @return void 
  301.      * @access private
  302.      */
  303.     private function _output($msg$color=false$force=false)
  304.     {
  305.         $reg Testing_DocTest_Registry::singleton();
  306.         if ($reg->quiet && !$force{
  307.             return;
  308.         }
  309.         if ($color && !$reg->no_colors && !$this->_logfile{
  310.             echo "\033[{$color}m" . $msg "\033[0;0m";
  311.         else {
  312.             if ($this->_logfile{
  313.                 fwrite($this->_logfile$msg);
  314.             else {
  315.                 echo $msg;
  316.             }
  317.         }
  318.     }
  319.  
  320.     // }}}
  321. }

Documentation generated on Mon, 11 Mar 2019 15:19:40 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.