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 / Util / Log / XML.php < prev   
Encoding:
PHP Script  |  2008-07-02  |  10.9 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: XML.php,v 1.2.2.3 2005/12/17 16:04:58 sebastian Exp $
  45.  * @link       http://pear.php.net/package/PHPUnit2
  46.  * @since      File available since Release 2.3.0
  47.  */
  48.  
  49. require_once 'PHPUnit2/Framework/TestListener.php';
  50. require_once 'PHPUnit2/Util/Filter.php';
  51. require_once 'PHPUnit2/Util/Printer.php';
  52.  
  53. require_once 'Benchmark/Timer.php';
  54.  
  55. /**
  56.  * A TestListener that generates an XML-based logfile
  57.  * of the test execution.
  58.  *
  59.  * @category   Testing
  60.  * @package    PHPUnit2
  61.  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
  62.  * @copyright  2002-2006 Sebastian Bergmann <sb@sebastian-bergmann.de>
  63.  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
  64.  * @version    Release: 2.3.6
  65.  * @link       http://pear.php.net/package/PHPUnit2
  66.  * @since      Class available since Release 2.1.0
  67.  */
  68. class PHPUnit2_Util_Log_XML extends PHPUnit2_Util_Printer implements PHPUnit2_Framework_TestListener {
  69.     /**
  70.      * @var    DOMDocument
  71.      * @access private
  72.      */
  73.     private $document;
  74.  
  75.     /**
  76.      * @var    DOMElement
  77.      * @access private
  78.      */
  79.     private $root;
  80.  
  81.     /**
  82.      * @var    boolean
  83.      * @access private
  84.      */
  85.     private $writeDocument = TRUE;
  86.  
  87.     /**
  88.      * @var    DOMElement[]
  89.      * @access private
  90.      */
  91.     private $testSuites = array();
  92.  
  93.     /**
  94.      * @var    integer[]
  95.      * @access private
  96.      */
  97.     private $testSuiteTests = array(0);
  98.  
  99.     /**
  100.      * @var    integer[]
  101.      * @access private
  102.      */
  103.     private $testSuiteErrors = array(0);
  104.  
  105.     /**
  106.      * @var    integer[]
  107.      * @access private
  108.      */
  109.     private $testSuiteFailures = array(0);
  110.  
  111.     /**
  112.      * @var    integer[]
  113.      * @access private
  114.      */
  115.     private $testSuiteTimes = array(0);
  116.  
  117.     /**
  118.      * @var    integer
  119.      * @access private
  120.      */
  121.     private $testSuiteLevel = 0;
  122.  
  123.     /**
  124.      * @var    DOMElement
  125.      * @access private
  126.      */
  127.     private $currentTestCase = NULL;
  128.  
  129.     /**
  130.      * @var    Benchmark_Timer
  131.      * @access private
  132.      */
  133.     private $timer;
  134.  
  135.     /**
  136.      * Constructor.
  137.      *
  138.      * @param  mixed $out
  139.      * @access public
  140.      */
  141.     public function __construct($out = NULL) {
  142.         $this->document = new DOMDocument('1.0', 'UTF-8');
  143.         $this->document->formatOutput = TRUE;
  144.  
  145.         $this->root = $this->document->createElement('testsuites');
  146.         $this->document->appendChild($this->root);
  147.  
  148.         $this->timer = new Benchmark_Timer;
  149.  
  150.         parent::__construct($out);
  151.     }
  152.  
  153.     /**
  154.      * Destructor.
  155.      *
  156.      * @access public
  157.      */
  158.     public function __destruct() {
  159.         if ($this->writeDocument === TRUE) {
  160.             $this->write($this->getXML());
  161.         }
  162.  
  163.         parent::__destruct();
  164.     }
  165.  
  166.     /**
  167.      * An error occurred.
  168.      *
  169.      * @param  PHPUnit2_Framework_Test $test
  170.      * @param  Exception               $e
  171.      * @access public
  172.      */
  173.     public function addError(PHPUnit2_Framework_Test $test, Exception $e) {
  174.         $error = $this->document->createElement('error', PHPUnit2_Util_Filter::getFilteredStacktrace($e));
  175.         $error->setAttribute('message', $e->getMessage());
  176.         $error->setAttribute('type', get_class($e));
  177.  
  178.         $this->currentTestCase->appendChild($error);
  179.  
  180.         $this->testSuiteErrors[$this->testSuiteLevel]++;
  181.     }
  182.  
  183.     /**
  184.      * A failure occurred.
  185.      *
  186.      * @param  PHPUnit2_Framework_Test                 $test
  187.      * @param  PHPUnit2_Framework_AssertionFailedError $e
  188.      * @access public
  189.      */
  190.     public function addFailure(PHPUnit2_Framework_Test $test, PHPUnit2_Framework_AssertionFailedError $e) {
  191.         $failure = $this->document->createElement('failure', PHPUnit2_Util_Filter::getFilteredStacktrace($e));
  192.         $failure->setAttribute('message', $e->getMessage());
  193.         $failure->setAttribute('type', get_class($e));
  194.  
  195.         $this->currentTestCase->appendChild($failure);
  196.  
  197.         $this->testSuiteFailures[$this->testSuiteLevel]++;
  198.     }
  199.  
  200.     /**
  201.      * Incomplete test.
  202.      *
  203.      * @param  PHPUnit2_Framework_Test $test
  204.      * @param  Exception               $e
  205.      * @access public
  206.      */
  207.     public function addIncompleteTest(PHPUnit2_Framework_Test $test, Exception $e) {
  208.         $error = $this->document->createElement('error', PHPUnit2_Util_Filter::getFilteredStacktrace($e));
  209.         $error->setAttribute('message', 'Incomplete Test');
  210.         $error->setAttribute('type', get_class($e));
  211.  
  212.         $this->currentTestCase->appendChild($error);
  213.  
  214.         $this->testSuiteErrors[$this->testSuiteLevel]++;
  215.     }
  216.  
  217.     /**
  218.      * A testsuite started.
  219.      *
  220.      * @param  PHPUnit2_Framework_TestSuite $suite
  221.      * @access public
  222.      * @since  Method available since Release 2.2.0
  223.      */
  224.     public function startTestSuite(PHPUnit2_Framework_TestSuite $suite) {
  225.         $testSuite = $this->document->createElement('testsuite');
  226.         $testSuite->setAttribute('name', $suite->getName());
  227.  
  228.         try {
  229.             $class      = new ReflectionClass($suite->getName());
  230.             $docComment = $class->getDocComment();
  231.  
  232.             if (preg_match('/@category[\s]+([\.\w]+)/', $docComment, $matches)) {
  233.                 $testSuite->setAttribute('category', $matches[1]);
  234.             }
  235.  
  236.             if (preg_match('/@package[\s]+([\.\w]+)/', $docComment, $matches)) {
  237.                 $testSuite->setAttribute('package', $matches[1]);
  238.             }
  239.  
  240.             if (preg_match('/@subpackage[\s]+([\.\w]+)/', $docComment, $matches)) {
  241.                 $testSuite->setAttribute('subpackage', $matches[1]);
  242.             }
  243.         }
  244.  
  245.         catch (ReflectionException $e) {
  246.         }
  247.         
  248.         if ($this->testSuiteLevel > 0) {
  249.             $this->testSuites[$this->testSuiteLevel]->appendChild($testSuite);
  250.         } else {
  251.             $this->root->appendChild($testSuite);
  252.         }
  253.  
  254.         $this->testSuiteLevel++;
  255.         $this->testSuites[$this->testSuiteLevel]        = $testSuite;
  256.         $this->testSuiteTests[$this->testSuiteLevel]    = 0;
  257.         $this->testSuiteErrors[$this->testSuiteLevel]   = 0;
  258.         $this->testSuiteFailures[$this->testSuiteLevel] = 0;
  259.         $this->testSuiteTimes[$this->testSuiteLevel]    = 0;
  260.     }
  261.  
  262.     /**
  263.      * A testsuite ended.
  264.      *
  265.      * @param  PHPUnit2_Framework_TestSuite $suite
  266.      * @access public
  267.      * @since  Method available since Release 2.2.0
  268.      */
  269.     public function endTestSuite(PHPUnit2_Framework_TestSuite $suite) {
  270.         $this->testSuites[$this->testSuiteLevel]->setAttribute('tests', $this->testSuiteTests[$this->testSuiteLevel]);
  271.         $this->testSuites[$this->testSuiteLevel]->setAttribute('failures', $this->testSuiteFailures[$this->testSuiteLevel]);
  272.         $this->testSuites[$this->testSuiteLevel]->setAttribute('errors', $this->testSuiteErrors[$this->testSuiteLevel]);
  273.         $this->testSuites[$this->testSuiteLevel]->setAttribute('time', $this->testSuiteTimes[$this->testSuiteLevel]);
  274.  
  275.         if ($this->testSuiteLevel > 1) {
  276.             $this->testSuiteTests[$this->testSuiteLevel - 1]    += $this->testSuiteTests[$this->testSuiteLevel];
  277.             $this->testSuiteErrors[$this->testSuiteLevel - 1]   += $this->testSuiteErrors[$this->testSuiteLevel];
  278.             $this->testSuiteFailures[$this->testSuiteLevel - 1] += $this->testSuiteFailures[$this->testSuiteLevel];
  279.             $this->testSuiteTimes[$this->testSuiteLevel - 1]    += $this->testSuiteTimes[$this->testSuiteLevel];
  280.         }
  281.  
  282.         $this->testSuiteLevel--;
  283.     }
  284.  
  285.     /**
  286.      * A test started.
  287.      *
  288.      * @param  PHPUnit2_Framework_Test $test
  289.      * @access public
  290.      */
  291.     public function startTest(PHPUnit2_Framework_Test $test) {
  292.         $testCase = $this->document->createElement('testcase');
  293.         $testCase->setAttribute('name', $test->getName());
  294.         $testCase->setAttribute('class', get_class($test));
  295.  
  296.         $this->testSuites[$this->testSuiteLevel]->appendChild($testCase);
  297.         $this->currentTestCase = $testCase;
  298.  
  299.         $this->testSuiteTests[$this->testSuiteLevel]++;
  300.  
  301.         $this->timer->start();
  302.     }
  303.  
  304.     /**
  305.      * A test ended.
  306.      *
  307.      * @param  PHPUnit2_Framework_Test $test
  308.      * @access public
  309.      */
  310.     public function endTest(PHPUnit2_Framework_Test $test) {
  311.         $this->timer->stop();
  312.         $time = $this->timer->timeElapsed();
  313.  
  314.         $this->currentTestCase->setAttribute('time', $time);
  315.         $this->testSuiteTimes[$this->testSuiteLevel] += $time;
  316.  
  317.         $this->currentTestCase = NULL;
  318.     }
  319.  
  320.     /**
  321.      * Returns the XML as a string.
  322.      *
  323.      * @return string
  324.      * @access public
  325.      * @since  Method available since Release 2.2.0
  326.      */
  327.     public function getXML() {
  328.         return $this->document->saveXML();
  329.     }
  330.  
  331.     /**
  332.      * Enables or disables the writing of the document
  333.      * in __destruct().
  334.      *
  335.      * This is a "hack" needed for the integration of
  336.      * PHPUnit with Phing.
  337.      *
  338.      * @return string
  339.      * @access public
  340.      * @since  Method available since Release 2.2.0
  341.      */
  342.     public function setWriteDocument($flag) {
  343.         if (is_bool($flag)) {
  344.             $this->writeDocument = $flag;
  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.