home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / PHPUnit2 / TextUI / ResultPrinter.php next >
Encoding:
PHP Script  |  2008-07-02  |  10.0 KB  |  357 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * PHP Version 5
  6.  *
  7.  * Copyright (c) 2002-2006, Sebastian Bergmann <sb@sebastian-bergmann.de>.
  8.  * All rights reserved.
  9.  *
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions
  12.  * are met:
  13.  *
  14.  *   * Redistributions of source code must retain the above copyright
  15.  *     notice, this list of conditions and the following disclaimer.
  16.  * 
  17.  *   * Redistributions in binary form must reproduce the above copyright
  18.  *     notice, this list of conditions and the following disclaimer in
  19.  *     the documentation and/or other materials provided with the
  20.  *     distribution.
  21.  *
  22.  *   * Neither the name of Sebastian Bergmann nor the names of his
  23.  *     contributors may be used to endorse or promote products derived
  24.  *     from this software without specific prior written permission.
  25.  *
  26.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  29.  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  30.  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  31.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  32.  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  33.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  34.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
  35.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  36.  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37.  * POSSIBILITY OF SUCH DAMAGE.
  38.  *
  39.  * @category   Testing
  40.  * @package    PHPUnit2
  41.  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
  42.  * @copyright  2002-2006 Sebastian Bergmann <sb@sebastian-bergmann.de>
  43.  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
  44.  * @version    CVS: $Id: ResultPrinter.php,v 1.20.2.5 2005/12/17 16:04:57 sebastian Exp $
  45.  * @link       http://pear.php.net/package/PHPUnit2
  46.  * @since      File available since Release 2.0.0
  47.  */
  48.  
  49. require_once 'PHPUnit2/Framework/TestListener.php';
  50. require_once 'PHPUnit2/Framework/TestFailure.php';
  51. require_once 'PHPUnit2/Util/Filter.php';
  52. require_once 'PHPUnit2/Util/Printer.php';
  53.  
  54. /**
  55.  * Prints the result of a TextUI TestRunner run.
  56.  *
  57.  * @category   Testing
  58.  * @package    PHPUnit2
  59.  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
  60.  * @copyright  2002-2006 Sebastian Bergmann <sb@sebastian-bergmann.de>
  61.  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
  62.  * @version    Release: 2.3.6
  63.  * @link       http://pear.php.net/package/PHPUnit2
  64.  * @since      Class available since Release 2.0.0
  65.  */
  66. class PHPUnit2_TextUI_ResultPrinter extends PHPUnit2_Util_Printer implements PHPUnit2_Framework_TestListener {
  67.     /**
  68.      * @var    integer
  69.      * @access private
  70.      */
  71.     private $column = 0;
  72.  
  73.     /**
  74.      * @var    boolean
  75.      * @access private
  76.      */
  77.     private $lastTestFailed = FALSE;
  78.  
  79.     /**
  80.      * @param  PHPUnit2_Framework_TestResult $result
  81.      * @param  float                         $runTime
  82.      * @access public
  83.      */
  84.     public function printResult(PHPUnit2_Framework_TestResult $result, $timeElapsed) {
  85.         $this->printHeader($timeElapsed);
  86.         $this->printErrors($result);
  87.         $this->printFailures($result);
  88.         $this->printIncompletes($result);
  89.         $this->printFooter($result);
  90.     }
  91.  
  92.     /**
  93.      * @param  array   $defects
  94.      * @param  integer $count
  95.      * @param  string  $type
  96.      * @access protected
  97.      */
  98.     protected function printDefects($defects, $count, $type) {
  99.         if ($count == 0) {
  100.             return;
  101.         }
  102.  
  103.         $this->write(
  104.           sprintf(
  105.             "There %s %d %s%s:\n",
  106.  
  107.             ($count == 1) ? 'was' : 'were',
  108.             $count,
  109.             $type,
  110.             ($count == 1) ? '' : 's'
  111.           )
  112.         );
  113.  
  114.         $i = 1;
  115.  
  116.         foreach ($defects as $defect) {
  117.             $this->printDefect($defect, $i++);
  118.         }
  119.     }
  120.  
  121.     /**
  122.      * @param  PHPUnit2_Framework_TestFailure $defect
  123.      * @param  integer                        $count
  124.      * @access protected
  125.      */
  126.     protected function printDefect(PHPUnit2_Framework_TestFailure $defect, $count) {
  127.         $this->printDefectHeader($defect, $count);
  128.         $this->printDefectTrace($defect);
  129.     }
  130.  
  131.     /**
  132.      * @param  PHPUnit2_Framework_TestFailure $defect
  133.      * @param  integer                        $count
  134.      * @access protected
  135.      */
  136.     protected function printDefectHeader(PHPUnit2_Framework_TestFailure $defect, $count) {
  137.         $this->write(
  138.           sprintf(
  139.             "%d) %s\n",
  140.  
  141.             $count,
  142.             $defect->failedTest()->toString()
  143.           )
  144.         );
  145.     }
  146.  
  147.     /**
  148.      * @param  PHPUnit2_Framework_TestFailure $defect
  149.      * @access protected
  150.      */
  151.     protected function printDefectTrace(PHPUnit2_Framework_TestFailure $defect) {
  152.         $e       = $defect->thrownException();
  153.         $message = method_exists($e, 'toString') ? $e->toString() : $e->getMessage();
  154.  
  155.         $this->write($message . "\n");
  156.  
  157.         $this->write(
  158.           PHPUnit2_Util_Filter::getFilteredStacktrace(
  159.             $defect->thrownException()
  160.           )
  161.         );
  162.     }
  163.  
  164.     /**
  165.      * @param  PHPUnit2_Framework_TestResult  $result
  166.      * @access protected
  167.      */
  168.     protected function printErrors(PHPUnit2_Framework_TestResult $result) {
  169.         $this->printDefects($result->errors(), $result->errorCount(), 'error');
  170.     }
  171.  
  172.     /**
  173.      * @param  PHPUnit2_Framework_TestResult  $result
  174.      * @access protected
  175.      */
  176.     protected function printFailures(PHPUnit2_Framework_TestResult $result) {
  177.         $this->printDefects($result->failures(), $result->failureCount(), 'failure');
  178.     }
  179.  
  180.     /**
  181.      * @param  PHPUnit2_Framework_TestResult  $result
  182.      * @access protected
  183.      */
  184.     protected function printIncompletes(PHPUnit2_Framework_TestResult $result) {
  185.         $this->printDefects($result->notImplemented(), $result->notImplementedCount(), 'incomplete test case');
  186.     }
  187.  
  188.     /**
  189.      * @param  float   $timeElapsed
  190.      * @access protected
  191.      */
  192.     protected function printHeader($timeElapsed) {
  193.         $this->write(
  194.           sprintf(
  195.             "\n\nTime: %s\n",
  196.  
  197.             $timeElapsed
  198.           )
  199.         );
  200.     }
  201.  
  202.     /**
  203.      * @param  PHPUnit2_Framework_TestResult  $result
  204.      * @access protected
  205.      */
  206.     protected function printFooter(PHPUnit2_Framework_TestResult $result) {
  207.         if ($result->allCompletlyImplemented() &&
  208.             $result->wasSuccessful()) {
  209.             $this->write(
  210.               sprintf(
  211.                 "\nOK (%d test%s)\n",
  212.  
  213.                 $result->runCount(),
  214.                 ($result->runCount() == 1) ? '' : 's'
  215.               )
  216.             );
  217.         }
  218.         
  219.         else if (!$result->allCompletlyImplemented() &&
  220.                  $result->wasSuccessful()) {
  221.             $this->write(
  222.               sprintf(
  223.                 "\nOK, but incomplete test cases!!!\nTests run: %d, Incomplete Tests: %d.\n",
  224.  
  225.                 $result->runCount(),
  226.                 $result->notImplementedCount()
  227.               )
  228.             );
  229.         }        
  230.         
  231.         else {
  232.             $this->write(
  233.               sprintf(
  234.                 "\nFAILURES!!!\nTests run: %d, Failures: %d, Errors: %d, Incomplete Tests: %d.\n",
  235.  
  236.                 $result->runCount(),
  237.                 $result->failureCount(),
  238.                 $result->errorCount(),
  239.                 $result->notImplementedCount()
  240.               )
  241.             );
  242.         }
  243.     }
  244.  
  245.     /**
  246.      * @access public
  247.      */
  248.     public function printWaitPrompt() {
  249.         $this->write("\n<RETURN> to continue\n");
  250.     }
  251.  
  252.     /**
  253.      * An error occurred.
  254.      *
  255.      * @param  PHPUnit2_Framework_Test $test
  256.      * @param  Exception               $e
  257.      * @access public
  258.      */
  259.     public function addError(PHPUnit2_Framework_Test $test, Exception $e) {
  260.         $this->write('E');
  261.         $this->nextColumn();
  262.  
  263.         $this->lastTestFailed = TRUE;
  264.     }
  265.  
  266.     /**
  267.      * A failure occurred.
  268.      *
  269.      * @param  PHPUnit2_Framework_Test                 $test
  270.      * @param  PHPUnit2_Framework_AssertionFailedError $e
  271.      * @access public
  272.      */
  273.     public function addFailure(PHPUnit2_Framework_Test $test, PHPUnit2_Framework_AssertionFailedError $e) {
  274.         $this->write('F');
  275.         $this->nextColumn();
  276.  
  277.         $this->lastTestFailed = TRUE;
  278.     }
  279.  
  280.     /**
  281.      * Incomplete test.
  282.      *
  283.      * @param  PHPUnit2_Framework_Test $test
  284.      * @param  Exception               $e
  285.      * @access public
  286.      */
  287.     public function addIncompleteTest(PHPUnit2_Framework_Test $test, Exception $e) {
  288.         $this->write('I');
  289.         $this->nextColumn();
  290.  
  291.         $this->lastTestFailed = TRUE;
  292.     }
  293.  
  294.     /**
  295.      * A testsuite started.
  296.      *
  297.      * @param  PHPUnit2_Framework_TestSuite $suite
  298.      * @access public
  299.      * @since  Method available since Release 2.2.0
  300.      */
  301.     public function startTestSuite(PHPUnit2_Framework_TestSuite $suite) {
  302.     }
  303.  
  304.     /**
  305.      * A testsuite ended.
  306.      *
  307.      * @param  PHPUnit2_Framework_TestSuite $suite
  308.      * @access public
  309.      * @since  Method available since Release 2.2.0
  310.      */
  311.     public function endTestSuite(PHPUnit2_Framework_TestSuite $suite) {
  312.     }
  313.  
  314.     /**
  315.      * A test started.
  316.      *
  317.      * @param  PHPUnit2_Framework_Test $test
  318.      * @access public
  319.      */
  320.     public function startTest(PHPUnit2_Framework_Test $test) {
  321.     }
  322.  
  323.     /**
  324.      * A test ended.
  325.      *
  326.      * @param  PHPUnit2_Framework_Test $test
  327.      * @access public
  328.      */
  329.     public function endTest(PHPUnit2_Framework_Test $test) {
  330.         if (!$this->lastTestFailed) {
  331.             $this->write('.');
  332.             $this->nextColumn();
  333.         }
  334.  
  335.         $this->lastTestFailed = FALSE;
  336.     }
  337.  
  338.     /**
  339.      * @access protected
  340.      */
  341.     protected function nextColumn() {
  342.         if ($this->column++ >= 40) {
  343.             $this->column = 0;
  344.             $this->write("\n");
  345.         }
  346.     }
  347. }
  348.  
  349. /*
  350.  * Local variables:
  351.  * tab-width: 4
  352.  * c-basic-offset: 4
  353.  * c-hanging-comment-ender-p: nil
  354.  * End:
  355.  */
  356. ?>
  357.