Source for file simpletest.php

Documentation is available at simpletest.php

  1. <?php
  2. /**
  3.  *  Global state for SimpleTest and kicker script in future versions.
  4.  *  @package    SimpleTest
  5.  *  @subpackage UnitTester
  6.  *  @version    $Id: simpletest.php 1928 2009-07-31 13:00:11Z dgheath $
  7.  */
  8.  
  9. /**#@+
  10.  * include SimpleTest files
  11.  */
  12. if (version_compare(phpversion()'5'>= 0{
  13.     require_once(dirname(__FILE__'/reflection_php5.php');
  14. else {
  15.     require_once(dirname(__FILE__'/reflection_php4.php');
  16. }
  17. require_once(dirname(__FILE__'/default_reporter.php');
  18. require_once(dirname(__FILE__'/compatibility.php');
  19. /**#@-*/
  20.  
  21. /**
  22.  *    Registry and test context. Includes a few
  23.  *    global options that I'm slowly getting rid of.
  24.  *    @package  SimpleTest
  25.  *    @subpackage   UnitTester
  26.  */
  27. class SimpleTest {
  28.  
  29.     /**
  30.      *    Reads the SimpleTest version from the release file.
  31.      *    @return string        Version string.
  32.      *    @access public
  33.      */
  34.     static function getVersion({
  35.         $content file(dirname(__FILE__'/VERSION');
  36.         return trim($content[0]);
  37.     }
  38.  
  39.     /**
  40.      *    Sets the name of a test case to ignore, usually
  41.      *    because the class is an abstract case that should
  42.      *    not be run. Once PHP4 is dropped this will disappear
  43.      *    as a public method and "abstract" will rule.
  44.      *    @param string $class        Add a class to ignore.
  45.      *    @access public
  46.      */
  47.     static function ignore($class{
  48.         $registry &SimpleTest::getRegistry();
  49.         $registry['IgnoreList'][strtolower($class)true;
  50.     }
  51.  
  52.     /**
  53.      *    Scans the now complete ignore list, and adds
  54.      *    all parent classes to the list. If a class
  55.      *    is not a runnable test case, then it's parents
  56.      *    wouldn't be either. This is syntactic sugar
  57.      *    to cut down on ommissions of ignore()'s or
  58.      *    missing abstract declarations. This cannot
  59.      *    be done whilst loading classes wiithout forcing
  60.      *    a particular order on the class declarations and
  61.      *    the ignore() calls. It's just nice to have the ignore()
  62.      *    calls at the top of the file before the actual declarations.
  63.      *    @param array $classes     Class names of interest.
  64.      *    @access public
  65.      */
  66.     static function ignoreParentsIfIgnored($classes{
  67.         $registry &SimpleTest::getRegistry();
  68.         foreach ($classes as $class{
  69.             if (SimpleTest::isIgnored($class)) {
  70.                 $reflection new SimpleReflection($class);
  71.                 if ($parent $reflection->getParent()) {
  72.                     SimpleTest::ignore($parent);
  73.                 }
  74.             }
  75.         }
  76.     }
  77.  
  78.     /**
  79.      *   Puts the object to the global pool of 'preferred' objects
  80.      *   which can be retrieved with SimpleTest :: preferred() method.
  81.      *   Instances of the same class are overwritten.
  82.      *   @param object $object      Preferred object
  83.      *   @access public
  84.      *   @see preferred()
  85.      */
  86.     static function prefer($object{
  87.         $registry &SimpleTest::getRegistry();
  88.         $registry['Preferred'][$object;
  89.     }
  90.  
  91.     /**
  92.      *   Retrieves 'preferred' objects from global pool. Class filter
  93.      *   can be applied in order to retrieve the object of the specific
  94.      *   class
  95.      *   @param array|string$classes       Allowed classes or interfaces.
  96.      *   @access public
  97.      *   @return array|object|null
  98.      *   @see prefer()
  99.      */
  100.     static function preferred($classes{
  101.         if (is_array($classes)) {
  102.             $classes array($classes);
  103.         }
  104.         $registry &SimpleTest::getRegistry();
  105.         for ($i count($registry['Preferred']1$i >= 0$i--{
  106.             foreach ($classes as $class{
  107.                 if (SimpleTestCompatibility::isA($registry['Preferred'][$i]$class)) {
  108.                     return $registry['Preferred'][$i];
  109.                 }
  110.             }
  111.         }
  112.         return null;
  113.     }
  114.  
  115.     /**
  116.      *    Test to see if a test case is in the ignore
  117.      *    list. Quite obviously the ignore list should
  118.      *    be a separate object and will be one day.
  119.      *    This method is internal to SimpleTest. Don't
  120.      *    use it.
  121.      *    @param string $class        Class name to test.
  122.      *    @return boolean             True if should not be run.
  123.      *    @access public
  124.      */
  125.     static function isIgnored($class{
  126.         $registry &SimpleTest::getRegistry();
  127.         return isset($registry['IgnoreList'][strtolower($class)]);
  128.     }
  129.  
  130.     /**
  131.      *    Sets proxy to use on all requests for when
  132.      *    testing from behind a firewall. Set host
  133.      *    to false to disable. This will take effect
  134.      *    if there are no other proxy settings.
  135.      *    @param string $proxy     Proxy host as URL.
  136.      *    @param string $username  Proxy username for authentication.
  137.      *    @param string $password  Proxy password for authentication.
  138.      *    @access public
  139.      */
  140.     static function useProxy($proxy$username false$password false{
  141.         $registry &SimpleTest::getRegistry();
  142.         $registry['DefaultProxy'$proxy;
  143.         $registry['DefaultProxyUsername'$username;
  144.         $registry['DefaultProxyPassword'$password;
  145.     }
  146.  
  147.     /**
  148.      *    Accessor for default proxy host.
  149.      *    @return string       Proxy URL.
  150.      *    @access public
  151.      */
  152.     static function getDefaultProxy({
  153.         $registry &SimpleTest::getRegistry();
  154.         return $registry['DefaultProxy'];
  155.     }
  156.  
  157.     /**
  158.      *    Accessor for default proxy username.
  159.      *    @return string    Proxy username for authentication.
  160.      *    @access public
  161.      */
  162.     static function getDefaultProxyUsername({
  163.         $registry &SimpleTest::getRegistry();
  164.         return $registry['DefaultProxyUsername'];
  165.     }
  166.  
  167.     /**
  168.      *    Accessor for default proxy password.
  169.      *    @return string    Proxy password for authentication.
  170.      *    @access public
  171.      */
  172.     static function getDefaultProxyPassword({
  173.         $registry &SimpleTest::getRegistry();
  174.         return $registry['DefaultProxyPassword'];
  175.     }
  176.  
  177.     /**
  178.      *    Accessor for default HTML parsers.
  179.      *    @return array     List of parsers to try in
  180.      *                       order until one responds true
  181.      *                       to can().
  182.      */
  183.     static function getParsers({
  184.         $registry &SimpleTest::getRegistry();
  185.         return $registry['Parsers'];
  186.     }
  187.  
  188.     /**
  189.      *    Set the list of HTML parsers to attempt to use by default.
  190.      *    @param array $parsers    List of parsers to try in
  191.      *                              order until one responds true
  192.      *                              to can().
  193.      */
  194.     static function setParsers($parsers{
  195.         $registry &SimpleTest::getRegistry();
  196.         $registry['Parsers'$parsers;
  197.     }
  198.  
  199.     /**
  200.      *    Accessor for global registry of options.
  201.      *    @return hash           All stored values.
  202.      *    @access private
  203.      */
  204.     protected static function &getRegistry({
  205.         static $registry false;
  206.         if ($registry{
  207.             $registry SimpleTest::getDefaults();
  208.         }
  209.         return $registry;
  210.     }
  211.  
  212.     /**
  213.      *    Accessor for the context of the current
  214.      *    test run.
  215.      *    @return SimpleTestContext    Current test run.
  216.      *    @access public
  217.      */
  218.     static function getContext({
  219.         static $context false;
  220.         if ($context{
  221.             $context new SimpleTestContext();
  222.         }
  223.         return $context;
  224.     }
  225.  
  226.     /**
  227.      *    Constant default values.
  228.      *    @return hash       All registry defaults.
  229.      *    @access private
  230.      */
  231.     protected static function getDefaults({
  232.         return array(
  233.                 'Parsers' => false,
  234.                 'MockBaseClass' => 'SimpleMock',
  235.                 'IgnoreList' => array(),
  236.                 'DefaultProxy' => false,
  237.                 'DefaultProxyUsername' => false,
  238.                 'DefaultProxyPassword' => false,
  239.                 'Preferred' => array(new HtmlReporter()new TextReporter()new XmlReporter()));
  240.     }
  241.     
  242.     /**
  243.      *    @deprecated
  244.      */
  245.     static function setMockBaseClass($mock_base{
  246.         $registry &SimpleTest::getRegistry();
  247.         $registry['MockBaseClass'$mock_base;
  248.     }
  249.  
  250.     /**
  251.      *    @deprecated
  252.      */
  253.     static function getMockBaseClass({
  254.         $registry &SimpleTest::getRegistry();
  255.         return $registry['MockBaseClass'];
  256.     }
  257. }
  258.  
  259. /**
  260.  *    Container for all components for a specific
  261.  *    test run. Makes things like error queues
  262.  *    available to PHP event handlers, and also
  263.  *    gets around some nasty reference issues in
  264.  *    the mocks.
  265.  *    @package  SimpleTest
  266.  */
  267.     private $test;
  268.     private $reporter;
  269.     private $resources;
  270.  
  271.     /**
  272.      *    Clears down the current context.
  273.      *    @access public
  274.      */
  275.     function clear({
  276.         $this->resources array();
  277.     }
  278.  
  279.     /**
  280.      *    Sets the current test case instance. This
  281.      *    global instance can be used by the mock objects
  282.      *    to send message to the test cases.
  283.      *    @param SimpleTestCase $test        Test case to register.
  284.      *    @access public
  285.      */
  286.     function setTest($test{
  287.         $this->clear();
  288.         $this->test $test;
  289.     }
  290.  
  291.     /**
  292.      *    Accessor for currently running test case.
  293.      *    @return SimpleTestCase    Current test.
  294.      *    @access public
  295.      */
  296.     function getTest({
  297.         return $this->test;
  298.     }
  299.  
  300.     /**
  301.      *    Sets the current reporter. This
  302.      *    global instance can be used by the mock objects
  303.      *    to send messages.
  304.      *    @param SimpleReporter $reporter     Reporter to register.
  305.      *    @access public
  306.      */
  307.     function setReporter($reporter{
  308.         $this->clear();
  309.         $this->reporter $reporter;
  310.     }
  311.  
  312.     /**
  313.      *    Accessor for current reporter.
  314.      *    @return SimpleReporter    Current reporter.
  315.      *    @access public
  316.      */
  317.     function getReporter({
  318.         return $this->reporter;
  319.     }
  320.  
  321.     /**
  322.      *    Accessor for the Singleton resource.
  323.      *    @return object       Global resource.
  324.      *    @access public
  325.      */
  326.     function get($resource{
  327.         if (isset($this->resources[$resource])) {
  328.             $this->resources[$resourcenew $resource();
  329.         }
  330.         return $this->resources[$resource];
  331.     }
  332. }
  333.  
  334. /**
  335.  *    Interrogates the stack trace to recover the
  336.  *    failure point.
  337.  *    @package SimpleTest
  338.  *    @subpackage UnitTester
  339.  */
  340.     private $prefixes;
  341.  
  342.     /**
  343.      *    Stashes the list of target prefixes.
  344.      *    @param array $prefixes      List of method prefixes
  345.      *                                 to search for.
  346.      */
  347.     function __construct($prefixes{
  348.         $this->prefixes $prefixes;
  349.     }
  350.  
  351.     /**
  352.      *    Extracts the last method name that was not within
  353.      *    Simpletest itself. Captures a stack trace if none given.
  354.      *    @param array $stack      List of stack frames.
  355.      *    @return string           Snippet of test report with line
  356.      *                              number and file.
  357.      *    @access public
  358.      */
  359.     function traceMethod($stack false{
  360.         $stack $stack $stack $this->captureTrace();
  361.         foreach ($stack as $frame{
  362.             if ($this->frameLiesWithinSimpleTestFolder($frame)) {
  363.                 continue;
  364.             }
  365.             if ($this->frameMatchesPrefix($frame)) {
  366.                 return ' at [' $frame['file'' line ' $frame['line'']';
  367.             }
  368.         }
  369.         return '';
  370.     }
  371.  
  372.     /**
  373.      *    Test to see if error is generated by SimpleTest itself.
  374.      *    @param array $frame     PHP stack frame.
  375.      *    @return boolean         True if a SimpleTest file.
  376.      *    @access private
  377.      */
  378.     protected function frameLiesWithinSimpleTestFolder($frame{
  379.         if (isset($frame['file'])) {
  380.             $path substr(SIMPLE_TEST0-1);
  381.             if (strpos($frame['file']$path=== 0{
  382.                 if (dirname($frame['file']== $path{
  383.                     return true;
  384.                 }
  385.             }
  386.         }
  387.         return false;
  388.     }
  389.  
  390.     /**
  391.      *    Tries to determine if the method call is an assert, etc.
  392.      *    @param array $frame     PHP stack frame.
  393.      *    @return boolean         True if matches a target.
  394.      *    @access private
  395.      */
  396.     protected function frameMatchesPrefix($frame{
  397.         foreach ($this->prefixes as $prefix{
  398.             if (strncmp($frame['function']$prefixstrlen($prefix)) == 0{
  399.                 return true;
  400.             }
  401.         }
  402.         return false;
  403.     }
  404.  
  405.     /**
  406.      *    Grabs a current stack trace.
  407.      *    @return array        Fulle trace.
  408.      *    @access private
  409.      */
  410.     protected function captureTrace({
  411.         if (function_exists('debug_backtrace')) {
  412.             return array_reverse(debug_backtrace());
  413.         }
  414.         return array();
  415.     }
  416. }
  417. ?>

Documentation generated on Thu, 01 Oct 2009 20:55:50 -0500 by phpDocumentor 1.4.2