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

Source for file XML.php

Documentation is available at XML.php

  1. <?php
  2. //
  3. // +------------------------------------------------------------------------+
  4. // | PEAR :: PHPUnit2                                                       |
  5. // +------------------------------------------------------------------------+
  6. // | Copyright (c) 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>. |
  7. // +------------------------------------------------------------------------+
  8. // | This source file is subject to version 3.00 of the PHP License,        |
  9. // | that is available at http://www.php.net/license/3_0.txt.               |
  10. // | If you did not receive a copy of the PHP license and are unable to     |
  11. // | obtain it through the world-wide-web, please send a note to            |
  12. // | license@php.net so we can mail you a copy immediately.                 |
  13. // +------------------------------------------------------------------------+
  14. //
  15. // $Id: XML.php,v 1.8.2.4 2005/02/17 15:41:30 sebastian Exp $
  16. //
  17.  
  18. require_once 'Benchmark/Timer.php';
  19. require_once 'PHPUnit2/Framework/AssertionFailedError.php';
  20. require_once 'PHPUnit2/Framework/Test.php';
  21. require_once 'PHPUnit2/Framework/TestFailure.php';
  22. require_once 'PHPUnit2/Framework/TestListener.php';
  23. require_once 'PHPUnit2/Framework/TestResult.php';
  24. require_once 'PHPUnit2/Framework/TestSuite.php';
  25. require_once 'PHPUnit2/Util/Filter.php';
  26. require_once 'PHPUnit2/Util/Printer.php';
  27.  
  28. /**
  29.  * A TestListener that generates an XML-based logfile
  30.  * of the test execution.
  31.  *
  32.  * @author      Sebastian Bergmann <sb@sebastian-bergmann.de>
  33.  * @copyright   Copyright &copy; 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>
  34.  * @license     http://www.php.net/license/3_0.txt The PHP License, Version 3.0
  35.  * @category    Testing
  36.  * @package     PHPUnit2
  37.  * @subpackage  Extensions
  38.  * @since       2.1.0
  39.  */
  40.     // {{{ Instance Variables
  41.  
  42.     /**
  43.     * @var    DOMDocument 
  44.     * @access private
  45.     */
  46.     private $document;
  47.  
  48.     /**
  49.     * @var    DOMElement 
  50.     * @access private
  51.     */
  52.     private $root;
  53.  
  54.     /**
  55.     * @var    boolean 
  56.     * @access private
  57.     */
  58.     private $writeDocument = TRUE;
  59.  
  60.     /**
  61.     * @var    DOMElement[] 
  62.     * @access private
  63.     */
  64.     private $testSuites = array();
  65.  
  66.     /**
  67.     * @var    integer[] 
  68.     * @access private
  69.     */
  70.     private $testSuiteTests = array(0);
  71.  
  72.     /**
  73.     * @var    integer[] 
  74.     * @access private
  75.     */
  76.     private $testSuiteErrors = array(0);
  77.  
  78.     /**
  79.     * @var    integer[] 
  80.     * @access private
  81.     */
  82.     private $testSuiteFailures = array(0);
  83.  
  84.     /**
  85.     * @var    integer[] 
  86.     * @access private
  87.     */
  88.     private $testSuiteTimes = array(0);
  89.  
  90.     /**
  91.     * @var    integer 
  92.     * @access private
  93.     */
  94.     private $testSuiteLevel = 0;
  95.  
  96.     /**
  97.     * @var    DOMElement 
  98.     * @access private
  99.     */
  100.     private $currentTestCase = NULL;
  101.  
  102.     /**
  103.     * @var    Benchmark_Timer 
  104.     * @access private
  105.     */
  106.     private $timer;
  107.  
  108.     // }}}
  109.     // {{{ public function __construct($out = NULL)
  110.  
  111.     /**
  112.     * Constructor.
  113.     *
  114.     * @param  mixed $out 
  115.     * @access public
  116.     */
  117.     public function __construct($out = NULL{
  118.         $this->document = new DOMDocument('1.0''UTF-8');
  119.         $this->document->formatOutput = TRUE;
  120.  
  121.         $this->root $this->document->createElement('testsuites');
  122.         $this->document->appendChild($this->root);
  123.  
  124.         $this->timer = new Benchmark_Timer;
  125.  
  126.         parent::__construct($out);
  127.     }
  128.  
  129.     // }}}
  130.     // {{{ public function __destruct()
  131.  
  132.     /**
  133.     * Destructor.
  134.     *
  135.     * @access public
  136.     */
  137.     public function __destruct({
  138.         if ($this->writeDocument === TRUE{
  139.             $this->write($this->getXML());
  140.         }
  141.  
  142.         parent::__destruct();
  143.     }
  144.  
  145.     // }}}
  146.     // {{{ public function addError(PHPUnit2_Framework_Test $test, Exception $e)
  147.  
  148.     /**
  149.     * An error occurred.
  150.     *
  151.     * @param  PHPUnit2_Framework_Test $test 
  152.     * @param  Exception               $e 
  153.     * @access public
  154.     */
  155.     public function addError(PHPUnit2_Framework_Test $testException $e{
  156.         $error $this->document->createElement('error'PHPUnit2_Util_Filter::getFilteredStacktrace($e));
  157.         $error->setAttribute('message'$e->getMessage());
  158.         $error->setAttribute('type'get_class($e));
  159.  
  160.         $this->currentTestCase->appendChild($error);
  161.  
  162.         $this->testSuiteErrors[$this->testSuiteLevel]++;
  163.     }
  164.  
  165.     // }}}
  166.     // {{{ public function addFailure(PHPUnit2_Framework_Test $test, PHPUnit2_Framework_AssertionFailedError $e)
  167.  
  168.     /**
  169.     * A failure occurred.
  170.     *
  171.     * @param  PHPUnit2_Framework_Test                 $test 
  172.     * @param  PHPUnit2_Framework_AssertionFailedError $e 
  173.     * @access public
  174.     */
  175.     public function addFailure(PHPUnit2_Framework_Test $testPHPUnit2_Framework_AssertionFailedError $e{
  176.         $failure $this->document->createElement('failure'PHPUnit2_Util_Filter::getFilteredStacktrace($e));
  177.         $failure->setAttribute('message'$e->getMessage());
  178.         $failure->setAttribute('type'get_class($e));
  179.  
  180.         $this->currentTestCase->appendChild($failure);
  181.  
  182.         $this->testSuiteFailures[$this->testSuiteLevel]++;
  183.     }
  184.  
  185.     // }}}
  186.     // {{{ public function addIncompleteTest(PHPUnit2_Framework_Test $test, Exception $e)
  187.  
  188.     /**
  189.     * Incomplete test.
  190.     *
  191.     * @param  PHPUnit2_Framework_Test $test 
  192.     * @param  Exception               $e 
  193.     * @access public
  194.     */
  195.     public function addIncompleteTest(PHPUnit2_Framework_Test $testException $e{
  196.         $error $this->document->createElement('error'PHPUnit2_Util_Filter::getFilteredStacktrace($e));
  197.         $error->setAttribute('message''Incomplete Test');
  198.         $error->setAttribute('type'get_class($e));
  199.  
  200.         $this->currentTestCase->appendChild($error);
  201.  
  202.         $this->testSuiteErrors[$this->testSuiteLevel]++;
  203.     }
  204.  
  205.     // }}}
  206.     // {{{ public function startTestSuite(PHPUnit2_Framework_TestSuite $suite)
  207.  
  208.     /**
  209.     * A testsuite started.
  210.     *
  211.     * @param  PHPUnit2_Framework_TestSuite $suite 
  212.     * @access public
  213.     * @since  2.2.0
  214.     */
  215.     public function startTestSuite(PHPUnit2_Framework_TestSuite $suite{
  216.         $testSuite $this->document->createElement('testsuite');
  217.         $testSuite->setAttribute('name'$suite->getName());
  218.  
  219.         try {
  220.             $class      = new ReflectionClass($suite->getName());
  221.             $docComment $class->getDocComment();
  222.  
  223.             if (preg_match('/@category[\s]+([\.\w]+)/'$docComment$matches)) {
  224.                 $testSuite->setAttribute('category'$matches[1]);
  225.             }
  226.  
  227.             if (preg_match('/@package[\s]+([\.\w]+)/'$docComment$matches)) {
  228.                 $testSuite->setAttribute('package'$matches[1]);
  229.             }
  230.  
  231.             if (preg_match('/@subpackage[\s]+([\.\w]+)/'$docComment$matches)) {
  232.                 $testSuite->setAttribute('subpackage'$matches[1]);
  233.             }
  234.         }
  235.  
  236.         catch (ReflectionException $e{
  237.         }
  238.         
  239.         if ($this->testSuiteLevel > 0{
  240.             $this->testSuites[$this->testSuiteLevel]->appendChild($testSuite);
  241.         else {
  242.             $this->root->appendChild($testSuite);
  243.         }
  244.  
  245.         $this->testSuiteLevel++;
  246.         $this->testSuites[$this->testSuiteLevel]        $testSuite;
  247.         $this->testSuiteTests[$this->testSuiteLevel]    = 0;
  248.         $this->testSuiteErrors[$this->testSuiteLevel]   = 0;
  249.         $this->testSuiteFailures[$this->testSuiteLevel= 0;
  250.         $this->testSuiteTimes[$this->testSuiteLevel]    = 0;
  251.     }
  252.  
  253.     // }}}
  254.     // {{{ public function endTestSuite(PHPUnit2_Framework_TestSuite $suite)
  255.  
  256.     /**
  257.     * A testsuite ended.
  258.     *
  259.     * @param  PHPUnit2_Framework_TestSuite $suite 
  260.     * @access public
  261.     * @since  2.2.0
  262.     */
  263.     public function endTestSuite(PHPUnit2_Framework_TestSuite $suite{
  264.         $this->testSuites[$this->testSuiteLevel]->setAttribute('tests'$this->testSuiteTests[$this->testSuiteLevel]);
  265.         $this->testSuites[$this->testSuiteLevel]->setAttribute('failures'$this->testSuiteFailures[$this->testSuiteLevel]);
  266.         $this->testSuites[$this->testSuiteLevel]->setAttribute('errors'$this->testSuiteErrors[$this->testSuiteLevel]);
  267.         $this->testSuites[$this->testSuiteLevel]->setAttribute('time'$this->testSuiteTimes[$this->testSuiteLevel]);
  268.  
  269.         if ($this->testSuiteLevel > 1{
  270.             $this->testSuiteTests[$this->testSuiteLevel - 1]    += $this->testSuiteTests[$this->testSuiteLevel];
  271.             $this->testSuiteErrors[$this->testSuiteLevel - 1]   += $this->testSuiteErrors[$this->testSuiteLevel];
  272.             $this->testSuiteFailures[$this->testSuiteLevel - 1+= $this->testSuiteFailures[$this->testSuiteLevel];
  273.             $this->testSuiteTimes[$this->testSuiteLevel - 1]    += $this->testSuiteTimes[$this->testSuiteLevel];
  274.         }
  275.  
  276.         $this->testSuiteLevel--;
  277.     }
  278.  
  279.     // }}}
  280.     // {{{ public function startTest(PHPUnit2_Framework_Test $test)
  281.  
  282.     /**
  283.     * A test started.
  284.     *
  285.     * @param  PHPUnit2_Framework_Test $test 
  286.     * @access public
  287.     */
  288.     public function startTest(PHPUnit2_Framework_Test $test{
  289.         $testCase $this->document->createElement('testcase');
  290.         $testCase->setAttribute('name'$test->getName());
  291.         $testCase->setAttribute('class'get_class($test));
  292.  
  293.         $this->testSuites[$this->testSuiteLevel]->appendChild($testCase);
  294.         $this->currentTestCase $testCase;
  295.  
  296.         $this->testSuiteTests[$this->testSuiteLevel]++;
  297.  
  298.         $this->timer->start();
  299.     }
  300.  
  301.     // }}}
  302.     // {{{ public function endTest(PHPUnit2_Framework_Test $test)
  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.     // {{{ public function getXML()
  322.  
  323.     /**
  324.     * Returns the XML as a string.
  325.     *
  326.     * @return string 
  327.     * @access public
  328.     * @since  2.2.0
  329.     */
  330.     public function getXML({
  331.         return $this->document->saveXML();
  332.     }
  333.  
  334.     // }}}
  335.     // {{{ public function setWriteDocument($flag)
  336.  
  337.     /**
  338.     * Enables or disables the writing of the document
  339.     * in __destruct().
  340.     *
  341.     * This is a "hack" needed for the integration of
  342.     * PHPUnit with Phing.
  343.     *
  344.     * @return string 
  345.     * @access public
  346.     * @since  2.2.0
  347.     */
  348.     public function setWriteDocument($flag{
  349.         if (is_bool($flag)) {
  350.             $this->writeDocument $flag;
  351.         }
  352.     }
  353.  
  354.     // }}}
  355. }
  356.  
  357. /*
  358.  * vim600:  et sw=2 ts=2 fdm=marker
  359.  * vim<600: et sw=2 ts=2
  360.  */
  361. ?>

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