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.6.0
  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.      * suite return code
  64.      *
  65.      * @var resource $_returncode 
  66.      * @access private
  67.      */
  68.     private $_returncode = 0;
  69.  
  70.     // }}}
  71.     // __construct() {{{
  72.  
  73.     /**
  74.      * Constructor.
  75.      *
  76.      * @access public
  77.      */
  78.     public function __construct()
  79.     {
  80.         $reg Testing_DocTest_Registry::singleton();
  81.         if ($reg->logfile{
  82.             $this->_logfile fopen($reg->logfile'wb');
  83.         }
  84.         $time        explode(' 'microtime());
  85.         $this->_time $time[1$time[0];
  86.     }
  87.  
  88.     // }}}
  89.     // __destruct() {{{
  90.  
  91.     /**
  92.      * Destructor.
  93.      * Will eventually close the opened logfile.
  94.      *
  95.      * @access public
  96.      */
  97.     public function __destruct()
  98.     {
  99.         if (false !== $this->_logfile && is_resource($this->_logfile)) {
  100.             fclose($this->_logfile);
  101.         }
  102.     }
  103.  
  104.     // }}}
  105.     // onBegin() {{{
  106.  
  107.     /**
  108.      * Called when a doctest session begins.
  109.      *
  110.      * @param array $suites an array of Testing_DocTest_TestSuite instances
  111.      *
  112.      * @access public
  113.      * @return void 
  114.      */
  115.     public function onBegin(array $suites)
  116.     {
  117.         if (empty($suites)) {
  118.             $this->_output("Nothing to process.\n");
  119.         }
  120.     }
  121.  
  122.     // }}}
  123.     // onTestSuiteBegin() {{{
  124.  
  125.     /**
  126.      * Called before the runner starts running a suite.
  127.      *
  128.      * @param object $suite an instance of Testing_DocTest_TestSuite
  129.      *
  130.      * @access public
  131.      * @return void 
  132.      */
  133.     public function onTestSuiteBegin(Testing_DocTest_TestSuite $suite)
  134.     {
  135.         $this->_output("Processing {$suite->name}"'36');
  136.         $this->_output("\n");
  137.     }
  138.  
  139.     // }}}
  140.     // onTestCaseBegin() {{{
  141.  
  142.     /**
  143.      * Called before the runner run a test case.
  144.      *
  145.      * @param object $case a test case instance
  146.      *
  147.      * @access public
  148.      * @return void 
  149.      */
  150.     public function onTestCaseBegin(Testing_DocTest_TestCase $case)
  151.     {
  152.     }
  153.  
  154.     // }}}
  155.     // onTestCasePass() {{{
  156.  
  157.     /**
  158.      * Called when a test passed.
  159.      *
  160.      * @param object $case a test case instance
  161.      *
  162.      * @access public
  163.      * @return void 
  164.      */
  165.     public function onTestCasePass(Testing_DocTest_TestCase $case)
  166.     {
  167.         $this->_output('[PASS]  '32);
  168.         $this->_output("{$case->name}\n");
  169.     }
  170.  
  171.     // }}}
  172.     // onTestCaseSkip() {{{
  173.  
  174.     /**
  175.      * Called when a test was skipped by the runner.
  176.      *
  177.      * @param object $case a test case instance
  178.      *
  179.      * @access public
  180.      * @return void 
  181.      */
  182.     public function onTestCaseSkip(Testing_DocTest_TestCase $case)
  183.     {
  184.         $this->_output('[SKIP]  '33);
  185.         $this->_output("{$case->name}\n");
  186.     }
  187.  
  188.     // }}}
  189.     // onTestCaseFail() {{{
  190.  
  191.     /**
  192.      * Called when a test failed.
  193.      *
  194.      * @param object $case a test case instance
  195.      *
  196.      * @access public
  197.      * @return void 
  198.      */
  199.     public function onTestCaseFail(Testing_DocTest_TestCase $case)
  200.     {
  201.         $this->_returncode=1;
  202.         $this->_output("[FAIL]  (Line number ~{$case->lineNumber})\n"31true);
  203.         $this->_output("{$case->name}\n"true);
  204.         $this->_output(str_pad(" Expected "72"="STR_PAD_BOTH)42true);
  205.         $this->_output("\n" trim($case->expectedValue"\n"falsetrue);
  206.         $this->_output(str_pad(" Actual "72"="STR_PAD_BOTH)41true);
  207.         $this->_output("\n" trim($case->actualValue"\n"falsetrue);
  208.         $this->_output("\n"falsetrue);
  209.     }
  210.  
  211.     // }}}
  212.     // onTestCaseError() {{{
  213.  
  214.     /**
  215.      * Called when a test has errors.
  216.      *
  217.      * @param object $case a test case instance
  218.      *
  219.      * @access public
  220.      * @return void 
  221.      */
  222.     public function onTestCaseError(Testing_DocTest_TestCase $case)
  223.     {
  224.         $this->_returncode=1;
  225.         $this->_output("[ERROR] \n"31true);
  226.         $this->_output(
  227.             "[PARSING ERROR]: (Line number ~{$case->lineNumber})".
  228.             " {$case->parsingError}\n",
  229.             31true
  230.         );
  231.         $this->_output("{$case->name} in file \"{$case->suite->name}\"\n"true);
  232.         $bar str_repeat('-'31);
  233.         $this->_output(str_pad(" Error "72"="STR_PAD_BOTH)41true);
  234.         $this->_output("\n" trim($case->actualValue"\n"falsetrue);
  235.     }
  236.  
  237.     // }}}
  238.     // onTestCaseEnd() {{{
  239.  
  240.     /**
  241.      * Called when the runner finished a test case.
  242.      *
  243.      * @param object $case a test case instance
  244.      *
  245.      * @access public
  246.      * @return void 
  247.      */
  248.     public function onTestCaseEnd(Testing_DocTest_TestCase $case)
  249.     {
  250.     }
  251.  
  252.     // }}}
  253.     // onTestSuiteEnd() {{{
  254.  
  255.     /**
  256.      * Called when the runner finished running the suite.
  257.      *
  258.      * @param object $suite an instance of Testing_DocTest_TestSuite
  259.      *
  260.      * @access public
  261.      * @return void 
  262.      */
  263.     public function onTestSuiteEnd(Testing_DocTest_TestSuite $suite)
  264.     {
  265.         $this->_output("\n");
  266.     }
  267.  
  268.     // }}}
  269.     // onEnd() {{{
  270.  
  271.     /**
  272.      * Called at the end of the DocTest session.
  273.      *
  274.      * @param array $suites an array of Testing_DocTest_TestSuite instances
  275.      *
  276.      * @access public
  277.      * @return void 
  278.      */
  279.     public function onEnd(array $suites)
  280.     {
  281.         if (empty($suites)) {
  282.             return;
  283.         }
  284.         $time   explode(' 'microtime());
  285.         $t      ($time[1$time[0]$this->_time;
  286.         $passed $skipped $failed $error = 0;
  287.         foreach ($suites as $suite{
  288.             foreach ($suite as $tc{
  289.                 if ($tc->state == Testing_DocTest_TestCase::STATE_SKIPPED{
  290.                     $skipped++;
  291.                 else if ($tc->state == Testing_DocTest_TestCase::STATE_PASSED{
  292.                     $passed++;
  293.                 else {
  294.                     $failed++;
  295.                 }
  296.             }
  297.         }
  298.         $this->_output(sprintf("\nTotal time    : %.4f sec.\n"$t)36);
  299.         if ($passed > 0{
  300.             $this->_output("Passed tests  : $passed\n"32);
  301.         else {
  302.             $this->_output("Passed tests  : $passed\n");
  303.         }
  304.         if ($skipped > 0{
  305.             $this->_output("Skipped tests : $skipped\n"33);
  306.         else {
  307.             $this->_output("Skipped tests : $skipped\n");
  308.         }
  309.         if ($failed > 0{
  310.             $this->_output("Failed tests  : $failed\n\n"31);
  311.         else {
  312.             $this->_output("Failed tests  : $failed\n\n");
  313.         }
  314.  
  315.         return $this->_returncode;
  316.     }
  317.  
  318.     // }}}
  319.     // _output() {{{
  320.  
  321.     /**
  322.      * Writes the message $msg to STDOUT or to the logfile.
  323.      *
  324.      * @param string $msg   the message to output
  325.      * @param int    $color the color code (optional)
  326.      * @param bool   $force force output even if quiet mode
  327.      *
  328.      * @return void 
  329.      * @access private
  330.      */
  331.     private function _output($msg$color=false$force=false)
  332.     {
  333.         $reg Testing_DocTest_Registry::singleton();
  334.         if ($reg->quiet && !$force{
  335.             return;
  336.         }
  337.         if ($color && !$reg->no_colors && !$this->_logfile{
  338.             echo "\033[{$color}m" . $msg "\033[0;0m";
  339.         else {
  340.             if ($this->_logfile{
  341.                 fwrite($this->_logfile$msg);
  342.             else {
  343.                 echo $msg;
  344.             }
  345.         }
  346.     }
  347.  
  348.     // }}}
  349. }

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