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

Source for file TestSuite.php

Documentation is available at TestSuite.php

  1. <?php
  2. //
  3. // +------------------------------------------------------------------------+
  4. // | PEAR :: PHPUnit2                                                       |
  5. // +------------------------------------------------------------------------+
  6. // | Copyright (c) 2002-2004 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: TestSuite.php,v 1.6 2004/05/26 06:24:05 sebastian Exp $
  16. //
  17.  
  18. require_once 'PHPUnit2/Framework/Test.php';
  19. require_once 'PHPUnit2/Framework/TestCase.php';
  20. require_once 'PHPUnit2/Framework/Warning.php';
  21.  
  22. /**
  23.  * A TestSuite is a composite of Tests. It runs a collection of test cases.
  24.  *
  25.  * Here is an example using the dynamic test definition.
  26.  *
  27.  * <code>
  28.  * <?php
  29.  * $suite = new PHPUnit2_Framework_TestSuite;
  30.  * $suite->addTest(new MathTest('testPass'));
  31.  * ?>
  32.  * </code>
  33.  *
  34.  * Alternatively, a TestSuite can extract the tests to be run automatically.
  35.  * To do so you pass a ReflectionClass instance for your
  36.  * PHPUnit2_Framework_TestCase class to the PHPUnit2_Framework_TestSuite
  37.  * constructor.
  38.  *
  39.  * <code>
  40.  * <?php
  41.  * $suite = new PHPUnit2_Framework_TestSuite(
  42.  *   new ReflectionClass('MathTest')
  43.  * );
  44.  * ?>
  45.  * </code>
  46.  *
  47.  * This constructor creates a suite with all the methods starting with
  48.  * "test" that take no arguments.
  49.  *
  50.  * @author      Sebastian Bergmann <sb@sebastian-bergmann.de>
  51.  * @copyright   Copyright &copy; 2002-2004 Sebastian Bergmann <sb@sebastian-bergmann.de>
  52.  * @license     http://www.php.net/license/3_0.txt The PHP License, Version 3.0
  53.  * @category    PHP
  54.  * @package     PHPUnit2
  55.  * @subpackage  Framework
  56.  */
  57. class PHPUnit2_Framework_TestSuite implements PHPUnit2_Framework_Test {
  58.     // {{{ Members
  59.  
  60.     /**
  61.     * The name of the test suite.
  62.     *
  63.     * @var    string 
  64.     * @access private
  65.     */
  66.     private $name '';
  67.  
  68.     /**
  69.     * The tests in the test suite.
  70.     *
  71.     * @var    array 
  72.     * @access private
  73.     */
  74.     private $tests = array();
  75.  
  76.     // }}}
  77.     // {{{ public function __construct($theClass = '', $name = '')
  78.  
  79.     /**
  80.     * Constructs a new TestSuite:
  81.     *
  82.     *   - PHPUnit2_Framework_TestSuite() constructs an empty TestSuite.
  83.     *
  84.     *   - PHPUnit2_Framework_TestSuite(ReflectionClass) constructs a
  85.     *     TestSuite from the given class.
  86.     *
  87.     *   - PHPUnit2_Framework_TestSuite(ReflectionClass, String)
  88.     *     constructs a TestSuite from the given class with the given
  89.     *     name.
  90.     *
  91.     *   - PHPUnit2_Framework_TestSuite(String) either constructs a
  92.     *     TestSuite from the given class (if the passed string is the
  93.     *     name of an existing class) or constructs an empty TestSuite
  94.     *     with the given name.
  95.     *
  96.     * @param  mixed  $theClass 
  97.     * @param  string $name 
  98.     * @access public
  99.     */
  100.     public function __construct($theClass ''$name ''{
  101.         if (is_string($theClass)) {
  102.             if (class_exists($theClass)) {
  103.                 if ($name == ''{
  104.                     $name $theClass;
  105.                 }
  106.  
  107.                 $theClass = new ReflectionClass($theClass);
  108.             else {
  109.                 $this->setName($theClass);
  110.  
  111.                 return;
  112.             }
  113.         }
  114.  
  115.         $this->setName($name);
  116.  
  117.         $constructor $theClass->getConstructor();
  118.  
  119.         if ($constructor === NULL ||
  120.             !$constructor->isPublic()) {
  121.             $this->addTest(
  122.               self::warning(
  123.                 sprintf(
  124.                   'Class %s has no public constructor',
  125.  
  126.                   $theClass->getName()
  127.                 )
  128.               )
  129.             );
  130.  
  131.             return;
  132.         }
  133.  
  134.         $methods $theClass->getMethods();
  135.         $names   = array();
  136.  
  137.         foreach ($methods as $method{
  138.             $this->addTestMethod($method$names$theClass);
  139.         }
  140.  
  141.         if (empty($this->tests)) {
  142.             $this->addTest(
  143.               self::warning(
  144.                 sprintf(
  145.                   'No tests found in %s',
  146.  
  147.                   $theClass->getName()
  148.                 )
  149.               )
  150.             );
  151.         }
  152.     }
  153.  
  154.     // }}}
  155.     // {{{ public function toString()
  156.  
  157.     /**
  158.     * Returns a string representation of the test suite.
  159.     *
  160.     * @return string 
  161.     * @access public
  162.     */
  163.     public function toString({
  164.         return $this->getName();
  165.     }
  166.  
  167.     // }}}
  168.     // {{{ public function addTest(PHPUnit2_Framework_Test $test)
  169.  
  170.     /**
  171.     * Adds a test to the suite.
  172.     *
  173.     * @param  PHPUnit2_Framework_Test 
  174.     * @access public
  175.     */
  176.     public function addTest($test{
  177.         $this->tests[$test;
  178.     }
  179.  
  180.     // }}}
  181.     // {{{ public function addTestSuite($testClass)
  182.  
  183.     /**
  184.     * Adds the tests from the given class to the suite.
  185.     *
  186.     * @param  mixed $testClass 
  187.     * @access public
  188.     */
  189.     public function addTestSuite($testClass{
  190.         if (is_string($testClass&&
  191.             class_exists($testClass)) {
  192.             $testClass = new ReflectionClass($testClass);
  193.         }
  194.  
  195.         if (is_object($testClass&&
  196.             $testClass instanceof ReflectionClass{
  197.             $this->addTest(new PHPUnit2_Framework_TestSuite($testClass));
  198.         }
  199.     }
  200.  
  201.     // }}}
  202.     // {{{ public function countTestCases()
  203.  
  204.     /**
  205.     * Counts the number of test cases that will be run by this test.
  206.     *
  207.     * @return integer 
  208.     * @access public
  209.     */
  210.     public function countTestCases({
  211.         $count = 0;
  212.  
  213.         foreach ($this->tests as $test{
  214.             $count += $test->countTestCases();
  215.         }
  216.  
  217.         return $count;
  218.     }
  219.  
  220.     // }}}
  221.     // {{{ public static function createTest(ReflectionClass $theClass, $name)
  222.  
  223.     /**
  224.     * @param  ReflectionClass $theClass 
  225.     * @param  string          $name 
  226.     * @return PHPUnit2_Framework_Test 
  227.     * @access public
  228.     * @static
  229.     */
  230.     public static function createTest(ReflectionClass $theClass$name{
  231.         $test $theClass->newInstance();
  232.  
  233.         if ($test instanceof PHPUnit2_Framework_TestCase{
  234.             $test->setName($name);
  235.         else {
  236.             return self::warning(
  237.               sprintf(
  238.                 'Cannot instantiate test case: %s',
  239.  
  240.                 $name
  241.               )
  242.             );
  243.         }
  244.  
  245.         return $test;
  246.     }
  247.  
  248.     // }}}
  249.     // {{{ protected function createResult()
  250.  
  251.     /**
  252.     * Creates a default TestResult object.
  253.     *
  254.     * @return PHPUnit2_Framework_TestResult 
  255.     * @access protected
  256.     */
  257.     protected function createResult({
  258.         return new PHPUnit2_Framework_TestResult;
  259.     }
  260.  
  261.     // }}}
  262.     // {{{ public function getName()
  263.  
  264.     /**
  265.     * Returns the name of the suite.
  266.     *
  267.     * @return string 
  268.     * @access public
  269.     */
  270.     public function getName({
  271.         return $this->name;
  272.     }
  273.  
  274.     // }}}
  275.     // {{{ public function run($result = null)
  276.  
  277.     /**
  278.     * Runs the tests and collects their result in a TestResult.
  279.     *
  280.     * @param  PHPUnit2_Framework_TestResult $result 
  281.     * @return PHPUnit2_Framework_TestResult 
  282.     * @access public
  283.     */
  284.     public function run($result = null{
  285.         if ($result === null{
  286.             $result $this->createResult();
  287.         }
  288.  
  289.         // XXX: Workaround for missing optional class type hints.
  290.         else if (!($result instanceof PHPUnit2_Framework_TestResult)) {
  291.             throw new Exception(
  292.               'Argument 1 must be an instance of PHPUnit2_Framework_TestResult.'
  293.             );
  294.         }
  295.  
  296.         foreach ($this->tests as $test{
  297.             if ($result->shouldStop()) {
  298.                 break;
  299.             }
  300.  
  301.             $this->runTest($test$result);
  302.         }
  303.  
  304.         return $result;
  305.     }
  306.  
  307.     // }}}
  308.     // {{{ public function runTest(PHPUnit2_Framework_Test $test, PHPUnit2_Framework_TestResult $result)
  309.  
  310.     /**
  311.     * Runs a test.
  312.     *
  313.     * @param  PHPUnit2_Framework_Test        $test 
  314.     * @param  PHPUnit2_Framework_TestResult  $testResult 
  315.     * @access public
  316.     */
  317.     public function runTest(PHPUnit2_Framework_Test $testPHPUnit2_Framework_TestResult $result{
  318.         $test->run($result);
  319.     }
  320.  
  321.     // }}}
  322.     // {{{ public function setName($name)
  323.  
  324.     /**
  325.     * Sets the name of the suite.
  326.     *
  327.     * @param  string 
  328.     * @access public
  329.     */
  330.     public function setName($name{
  331.         $this->name $name;
  332.     }
  333.  
  334.     // }}}
  335.     // {{{ public function testAt($index)
  336.  
  337.     /**
  338.     * Returns the test at the given index.
  339.     *
  340.     * @param  integer 
  341.     * @return PHPUnit2_Framework_Test 
  342.     * @access public
  343.     */
  344.     public function testAt($index{
  345.         if (isset($this->tests[$index])) {
  346.             return $this->tests[$index];
  347.         else {
  348.             return false;
  349.         }
  350.     }
  351.  
  352.     // }}}
  353.     // {{{ public function testCount()
  354.  
  355.     /**
  356.     * Returns the number of tests in this suite.
  357.     *
  358.     * @return integer 
  359.     * @access public
  360.     */
  361.     public function testCount({
  362.         return sizeof($this->tests);
  363.     }
  364.  
  365.     // }}}
  366.     // {{{ public function tests()
  367.  
  368.     /**
  369.     * Returns the tests as an enumeration.
  370.     *
  371.     * @return array 
  372.     * @access public
  373.     */
  374.     public function tests({
  375.         return $this->tests;
  376.     }
  377.  
  378.     // }}}
  379.     // {{{ public function addTestMethod(ReflectionMethod $method, &$names, ReflectionClass $theClass)
  380.  
  381.     /**
  382.     * @param  ReflectionMethod $method 
  383.     * @param  array            $names 
  384.     * @param  ReflectionClass  $theClass 
  385.     * @access private
  386.     */
  387.     private function addTestMethod(ReflectionMethod $method&$namesReflectionClass $theClass{
  388.         $name $method->getName();
  389.  
  390.         if (in_array($name$names)) {
  391.             return;
  392.         }
  393.  
  394.         if ($this->isPublicTestMethod($method)) {
  395.             $names[$name;
  396.  
  397.             $this->addTest(
  398.               self::createTest(
  399.                 $theClass,
  400.                 $name
  401.               )
  402.             );
  403.         }
  404.  
  405.         else if ($this->isTestMethod($method)) {
  406.             $this->addTest(
  407.               self::warning(
  408.                 sprintf(
  409.                   'Test method is not public: %s',
  410.  
  411.                   $name
  412.                 )
  413.               )
  414.             );
  415.         }
  416.     }
  417.  
  418.     // }}}
  419.     // {{{ private function isPublicTestMethod(ReflectionMethod $method)
  420.  
  421.     /**
  422.     * @param  ReflectionMethod $method 
  423.     * @return boolean 
  424.     * @access private
  425.     */
  426.     private function isPublicTestMethod(ReflectionMethod $method{
  427.         return ($this->isTestMethod($method&&
  428.                 $method->isPublic());
  429.     }
  430.  
  431.     // }}}
  432.     // {{{ private function isTestMethod(ReflectionMethod $method)
  433.  
  434.     /**
  435.     * @param  ReflectionMethod $method 
  436.     * @return boolean 
  437.     * @access private
  438.     */
  439.     private function isTestMethod(ReflectionMethod $method{
  440.         return (substr($method->name04== 'test');
  441.     }
  442.  
  443.     // }}}
  444.     // {{{ private static function warning($message)
  445.  
  446.     /**
  447.     * @param  string  $message 
  448.     * @return PHPUnit2_Framework_Warning 
  449.     * @access private
  450.     */
  451.     private static function warning($message{
  452.         return new PHPUnit2_Framework_Warning($message);
  453.     }
  454.  
  455.     // }}}
  456. }
  457.  
  458. /*
  459.  * vim600:  et sw=2 ts=2 fdm=marker
  460.  * vim<600: et sw=2 ts=2
  461.  */
  462. ?>

Documentation generated on Mon, 11 Mar 2019 13:55:59 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.