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-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: TestSuite.php,v 1.15.2.3 2005/02/09 16:35:27 sebastian Exp $
  16. //
  17.  
  18. require_once 'PHPUnit2/Framework/Test.php';
  19. require_once 'PHPUnit2/Framework/TestCase.php';
  20. require_once 'PHPUnit2/Framework/TestResult.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-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>
  52.  * @license     http://www.php.net/license/3_0.txt The PHP License, Version 3.0
  53.  * @category    Testing
  54.  * @package     PHPUnit2
  55.  * @subpackage  Framework
  56.  */
  57. class PHPUnit2_Framework_TestSuite implements PHPUnit2_Framework_Test {
  58.     // {{{ Instance Variables
  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.         if ($name != ''{
  116.             $this->setName($name);
  117.         else {
  118.             $this->setName($theClass->getName());
  119.         }
  120.  
  121.         $constructor $theClass->getConstructor();
  122.  
  123.         if ($constructor === NULL ||
  124.             !$constructor->isPublic()) {
  125.             $this->addTest(
  126.               self::warning(
  127.                 sprintf(
  128.                   'Class %s has no public constructor',
  129.  
  130.                   $theClass->getName()
  131.                 )
  132.               )
  133.             );
  134.  
  135.             return;
  136.         }
  137.  
  138.         $methods $theClass->getMethods();
  139.         $names   = array();
  140.  
  141.         foreach ($methods as $method{
  142.             $this->addTestMethod($method$names$theClass);
  143.         }
  144.  
  145.         if (empty($this->tests)) {
  146.             $this->addTest(
  147.               self::warning(
  148.                 sprintf(
  149.                   'No tests found in %s',
  150.  
  151.                   $theClass->getName()
  152.                 )
  153.               )
  154.             );
  155.         }
  156.     }
  157.  
  158.     // }}}
  159.     // {{{ public function toString()
  160.  
  161.     /**
  162.     * Returns a string representation of the test suite.
  163.     *
  164.     * @return string 
  165.     * @access public
  166.     */
  167.     public function toString({
  168.         return $this->getName();
  169.     }
  170.  
  171.     // }}}
  172.     // {{{ public function addTest(PHPUnit2_Framework_Test $test)
  173.  
  174.     /**
  175.     * Adds a test to the suite.
  176.     *
  177.     * @param  PHPUnit2_Framework_Test $test 
  178.     * @access public
  179.     */
  180.     public function addTest(PHPUnit2_Framework_Test $test{
  181.         $this->tests[$test;
  182.     }
  183.  
  184.     // }}}
  185.     // {{{ public function addTestSuite($testClass)
  186.  
  187.     /**
  188.     * Adds the tests from the given class to the suite.
  189.     *
  190.     * @param  mixed $testClass 
  191.     * @access public
  192.     */
  193.     public function addTestSuite($testClass{
  194.         if (is_string($testClass&&
  195.             class_exists($testClass)) {
  196.             $testClass = new ReflectionClass($testClass);
  197.         }
  198.  
  199.         if (is_object($testClass&&
  200.             $testClass instanceof ReflectionClass{
  201.             $this->addTest(new PHPUnit2_Framework_TestSuite($testClass));
  202.         }
  203.     }
  204.  
  205.     // }}}
  206.     // {{{ public function countTestCases()
  207.  
  208.     /**
  209.     * Counts the number of test cases that will be run by this test.
  210.     *
  211.     * @return integer 
  212.     * @access public
  213.     */
  214.     public function countTestCases({
  215.         $count = 0;
  216.  
  217.         foreach ($this->tests as $test{
  218.             $count += $test->countTestCases();
  219.         }
  220.  
  221.         return $count;
  222.     }
  223.  
  224.     // }}}
  225.     // {{{ public static function createTest(ReflectionClass $theClass, $name)
  226.  
  227.     /**
  228.     * @param  ReflectionClass $theClass 
  229.     * @param  string          $name 
  230.     * @return PHPUnit2_Framework_Test 
  231.     * @access public
  232.     * @static
  233.     */
  234.     public static function createTest(ReflectionClass $theClass$name{
  235.         if (!$theClass->isInstantiable()) {
  236.             return self::warning(
  237.               sprintf(
  238.                 'Cannot instantiate test case %s.',
  239.                 $theClass->getName()
  240.               )
  241.             );
  242.         }
  243.  
  244.         $constructor $theClass->getConstructor();
  245.  
  246.         if ($constructor !== NULL{
  247.             $parameters $constructor->getParameters();
  248.  
  249.             if (sizeof($parameters== 0{
  250.                 $test $theClass->newInstance();
  251.  
  252.                 if ($test instanceof PHPUnit2_Framework_TestCase{
  253.                     $test->setName($name);
  254.                 }
  255.             }
  256.  
  257.             else if (sizeof($parameters== 1 &&
  258.                      $parameters[0]->getClass(=== NULL{
  259.                 $test $theClass->newInstance($name);
  260.             }
  261.  
  262.             else {
  263.                 return self::warning(
  264.                   sprintf(
  265.                     'Constructor of class %s is not TestCase($name) or TestCase().',
  266.                     $theClass->getName()
  267.                   )
  268.                 );
  269.             }
  270.         }
  271.  
  272.         return $test;
  273.     }
  274.  
  275.     // }}}
  276.     // {{{ protected function createResult()
  277.  
  278.     /**
  279.     * Creates a default TestResult object.
  280.     *
  281.     * @return PHPUnit2_Framework_TestResult 
  282.     * @access protected
  283.     */
  284.     protected function createResult({
  285.         return new PHPUnit2_Framework_TestResult;
  286.     }
  287.  
  288.     // }}}
  289.     // {{{ public function getName()
  290.  
  291.     /**
  292.     * Returns the name of the suite.
  293.     *
  294.     * @return string 
  295.     * @access public
  296.     */
  297.     public function getName({
  298.         return $this->name;
  299.     }
  300.  
  301.     // }}}
  302.     // {{{ public function run($result = NULL)
  303.  
  304.     /**
  305.     * Runs the tests and collects their result in a TestResult.
  306.     *
  307.     * @param  PHPUnit2_Framework_TestResult $result 
  308.     * @return PHPUnit2_Framework_TestResult 
  309.     * @access public
  310.     */
  311.     public function run($result = NULL{
  312.         if ($result === NULL{
  313.             $result $this->createResult();
  314.         }
  315.  
  316.         // XXX: Workaround for missing ability to declare type-hinted parameters as optional.
  317.         else if (!($result instanceof PHPUnit2_Framework_TestResult)) {
  318.             throw new Exception(
  319.               'Argument 1 must be an instance of PHPUnit2_Framework_TestResult.'
  320.             );
  321.         }
  322.  
  323.         $result->startTestSuite($this);
  324.  
  325.         foreach ($this->tests as $test{
  326.             if ($result->shouldStop()) {
  327.                 break;
  328.             }
  329.  
  330.             $this->runTest($test$result);
  331.         }
  332.  
  333.         $result->endTestSuite($this);
  334.  
  335.         return $result;
  336.     }
  337.  
  338.     // }}}
  339.     // {{{ public function runTest(PHPUnit2_Framework_Test $test, PHPUnit2_Framework_TestResult $result)
  340.  
  341.     /**
  342.     * Runs a test.
  343.     *
  344.     * @param  PHPUnit2_Framework_Test        $test 
  345.     * @param  PHPUnit2_Framework_TestResult  $testResult 
  346.     * @access public
  347.     */
  348.     public function runTest(PHPUnit2_Framework_Test $testPHPUnit2_Framework_TestResult $result{
  349.         $test->run($result);
  350.     }
  351.  
  352.     // }}}
  353.     // {{{ public function setName($name)
  354.  
  355.     /**
  356.     * Sets the name of the suite.
  357.     *
  358.     * @param  string 
  359.     * @access public
  360.     */
  361.     public function setName($name{
  362.         $this->name $name;
  363.     }
  364.  
  365.     // }}}
  366.     // {{{ public function testAt($index)
  367.  
  368.     /**
  369.     * Returns the test at the given index.
  370.     *
  371.     * @param  integer 
  372.     * @return PHPUnit2_Framework_Test 
  373.     * @access public
  374.     */
  375.     public function testAt($index{
  376.         if (isset($this->tests[$index])) {
  377.             return $this->tests[$index];
  378.         else {
  379.             return FALSE;
  380.         }
  381.     }
  382.  
  383.     // }}}
  384.     // {{{ public function testCount()
  385.  
  386.     /**
  387.     * Returns the number of tests in this suite.
  388.     *
  389.     * @return integer 
  390.     * @access public
  391.     */
  392.     public function testCount({
  393.         return sizeof($this->tests);
  394.     }
  395.  
  396.     // }}}
  397.     // {{{ public function tests()
  398.  
  399.     /**
  400.     * Returns the tests as an enumeration.
  401.     *
  402.     * @return array 
  403.     * @access public
  404.     */
  405.     public function tests({
  406.         return $this->tests;
  407.     }
  408.  
  409.     // }}}
  410.     // {{{ public function addTestMethod(ReflectionMethod $method, &$names, ReflectionClass $theClass)
  411.  
  412.     /**
  413.     * @param  ReflectionMethod $method 
  414.     * @param  array            $names 
  415.     * @param  ReflectionClass  $theClass 
  416.     * @access private
  417.     */
  418.     private function addTestMethod(ReflectionMethod $method&$namesReflectionClass $theClass{
  419.         $name $method->getName();
  420.  
  421.         if (in_array($name$names)) {
  422.             return;
  423.         }
  424.  
  425.         if ($this->isPublicTestMethod($method)) {
  426.             $names[$name;
  427.  
  428.             $this->addTest(
  429.               self::createTest(
  430.                 $theClass,
  431.                 $name
  432.               )
  433.             );
  434.         }
  435.  
  436.         else if ($this->isTestMethod($method)) {
  437.             $this->addTest(
  438.               self::warning(
  439.                 sprintf(
  440.                   'Test method is not public: %s',
  441.  
  442.                   $name
  443.                 )
  444.               )
  445.             );
  446.         }
  447.     }
  448.  
  449.     // }}}
  450.     // {{{ private function isPublicTestMethod(ReflectionMethod $method)
  451.  
  452.     /**
  453.     * @param  ReflectionMethod $method 
  454.     * @return boolean 
  455.     * @access private
  456.     */
  457.     private function isPublicTestMethod(ReflectionMethod $method{
  458.         return ($this->isTestMethod($method&&
  459.                 $method->isPublic());
  460.     }
  461.  
  462.     // }}}
  463.     // {{{ private function isTestMethod(ReflectionMethod $method)
  464.  
  465.     /**
  466.     * @param  ReflectionMethod $method 
  467.     * @return boolean 
  468.     * @access private
  469.     */
  470.     private function isTestMethod(ReflectionMethod $method{
  471.         return (substr($method->name04== 'test');
  472.     }
  473.  
  474.     // }}}
  475.     // {{{ private static function warning($message)
  476.  
  477.     /**
  478.     * @param  string  $message 
  479.     * @return PHPUnit2_Framework_Warning 
  480.     * @access private
  481.     */
  482.     private static function warning($message{
  483.         require_once 'PHPUnit2/Framework/Warning.php';
  484.         return new PHPUnit2_Framework_Warning($message);
  485.     }
  486.  
  487.     // }}}
  488. }
  489.  
  490. /*
  491.  * vim600:  et sw=2 ts=2 fdm=marker
  492.  * vim<600: et sw=2 ts=2
  493.  */
  494. ?>

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