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 1723 2008-04-08 00:34:10Z lastcraft $
  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.  *    Registry and test context. Includes a few
  22.  *    global options that I'm slowly getting rid of.
  23.  *    @package  SimpleTest
  24.  *    @subpackage   UnitTester
  25.  */
  26. class SimpleTest {
  27.  
  28.     /**
  29.      *    Reads the SimpleTest version from the release file.
  30.      *    @return string        Version string.
  31.      *    @static
  32.      *    @access public
  33.      */
  34.     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.      *    @static
  46.      *    @access public
  47.      */
  48.     function ignore($class{
  49.         $registry &SimpleTest::_getRegistry();
  50.         $registry['IgnoreList'][strtolower($class)true;
  51.     }
  52.  
  53.     /**
  54.      *    Scans the now complete ignore list, and adds
  55.      *    all parent classes to the list. If a class
  56.      *    is not a runnable test case, then it's parents
  57.      *    wouldn't be either. This is syntactic sugar
  58.      *    to cut down on ommissions of ignore()'s or
  59.      *    missing abstract declarations. This cannot
  60.      *    be done whilst loading classes wiithout forcing
  61.      *    a particular order on the class declarations and
  62.      *    the ignore() calls. It's just nice to have the ignore()
  63.      *    calls at the top of the file before the actual declarations.
  64.      *    @param array $classes     Class names of interest.
  65.      *    @static
  66.      *    @access public
  67.      */
  68.     function ignoreParentsIfIgnored($classes{
  69.         $registry &SimpleTest::_getRegistry();
  70.         foreach ($classes as $class{
  71.             if (SimpleTest::isIgnored($class)) {
  72.                 $reflection new SimpleReflection($class);
  73.                 if ($parent $reflection->getParent()) {
  74.                     SimpleTest::ignore($parent);
  75.                 }
  76.             }
  77.         }
  78.     }
  79.  
  80.     /**
  81.      *   Puts the object to the global pool of 'preferred' objects
  82.      *   which can be retrieved with SimpleTest :: preferred() method.
  83.      *   Instances of the same class are overwritten.
  84.      *   @param object $object      Preferred object
  85.      *   @static
  86.      *   @access public
  87.      *   @see preferred()
  88.      */
  89.     function prefer(&$object{
  90.         $registry &SimpleTest::_getRegistry();
  91.         $registry['Preferred'][&$object;
  92.     }
  93.  
  94.     /**
  95.      *   Retrieves 'preferred' objects from global pool. Class filter
  96.      *   can be applied in order to retrieve the object of the specific
  97.      *   class
  98.      *   @param array|string$classes       Allowed classes or interfaces.
  99.      *   @static
  100.      *   @access public
  101.      *   @return array|object|null
  102.      *   @see prefer()
  103.      */
  104.     function &preferred($classes{
  105.         if (is_array($classes)) {
  106.             $classes array($classes);
  107.         }
  108.         $registry &SimpleTest::_getRegistry();
  109.         for ($i count($registry['Preferred']1$i >= 0$i--{
  110.             foreach ($classes as $class{
  111.                 if (SimpleTestCompatibility::isA($registry['Preferred'][$i]$class)) {
  112.                     return $registry['Preferred'][$i];
  113.                 }
  114.             }
  115.         }
  116.         return null;
  117.     }
  118.  
  119.     /**
  120.      *    Test to see if a test case is in the ignore
  121.      *    list. Quite obviously the ignore list should
  122.      *    be a separate object and will be one day.
  123.      *    This method is internal to SimpleTest. Don't
  124.      *    use it.
  125.      *    @param string $class        Class name to test.
  126.      *    @return boolean             True if should not be run.
  127.      *    @access public
  128.      *    @static
  129.      */
  130.     function isIgnored($class{
  131.         $registry &SimpleTest::_getRegistry();
  132.         return isset($registry['IgnoreList'][strtolower($class)]);
  133.     }
  134.  
  135.     /**
  136.      *    @deprecated
  137.      */
  138.     function setMockBaseClass($mock_base{
  139.         $registry &SimpleTest::_getRegistry();
  140.         $registry['MockBaseClass'$mock_base;
  141.     }
  142.  
  143.     /**
  144.      *    @deprecated
  145.      */
  146.     function getMockBaseClass({
  147.         $registry &SimpleTest::_getRegistry();
  148.         return $registry['MockBaseClass'];
  149.     }
  150.  
  151.     /**
  152.      *    Sets proxy to use on all requests for when
  153.      *    testing from behind a firewall. Set host
  154.      *    to false to disable. This will take effect
  155.      *    if there are no other proxy settings.
  156.      *    @param string $proxy     Proxy host as URL.
  157.      *    @param string $username  Proxy username for authentication.
  158.      *    @param string $password  Proxy password for authentication.
  159.      *    @access public
  160.      */
  161.     function useProxy($proxy$username false$password false{
  162.         $registry &SimpleTest::_getRegistry();
  163.         $registry['DefaultProxy'$proxy;
  164.         $registry['DefaultProxyUsername'$username;
  165.         $registry['DefaultProxyPassword'$password;
  166.     }
  167.  
  168.     /**
  169.      *    Accessor for default proxy host.
  170.      *    @return string       Proxy URL.
  171.      *    @access public
  172.      */
  173.     function getDefaultProxy({
  174.         $registry &SimpleTest::_getRegistry();
  175.         return $registry['DefaultProxy'];
  176.     }
  177.  
  178.     /**
  179.      *    Accessor for default proxy username.
  180.      *    @return string    Proxy username for authentication.
  181.      *    @access public
  182.      */
  183.     function getDefaultProxyUsername({
  184.         $registry &SimpleTest::_getRegistry();
  185.         return $registry['DefaultProxyUsername'];
  186.     }
  187.  
  188.     /**
  189.      *    Accessor for default proxy password.
  190.      *    @return string    Proxy password for authentication.
  191.      *    @access public
  192.      */
  193.     function getDefaultProxyPassword({
  194.         $registry &SimpleTest::_getRegistry();
  195.         return $registry['DefaultProxyPassword'];
  196.     }
  197.  
  198.     /**
  199.      *    Accessor for global registry of options.
  200.      *    @return hash           All stored values.
  201.      *    @access private
  202.      *    @static
  203.      */
  204.     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.      *    @static
  218.      */
  219.     function &getContext({
  220.         static $context false;
  221.         if ($context{
  222.             $context new SimpleTestContext();
  223.         }
  224.         return $context;
  225.     }
  226.  
  227.     /**
  228.      *    Constant default values.
  229.      *    @return hash       All registry defaults.
  230.      *    @access private
  231.      *    @static
  232.      */
  233.     function _getDefaults({
  234.         return array(
  235.                 'StubBaseClass' => 'SimpleStub',
  236.                 'MockBaseClass' => 'SimpleMock',
  237.                 'IgnoreList' => array(),
  238.                 'DefaultProxy' => false,
  239.                 'DefaultProxyUsername' => false,
  240.                 'DefaultProxyPassword' => false,
  241.                 'Preferred' => array(new HtmlReporter()new TextReporter()new XmlReporter()));
  242.     }
  243. }
  244.  
  245. /**
  246.  *    Container for all components for a specific
  247.  *    test run. Makes things like error queues
  248.  *    available to PHP event handlers, and also
  249.  *    gets around some nasty reference issues in
  250.  *    the mocks.
  251.  *    @package  SimpleTest
  252.  */
  253.     var $_test;
  254.     var $_reporter;
  255.     var $_resources;
  256.  
  257.     /**
  258.      *    Clears down the current context.
  259.      *    @access public
  260.      */
  261.     function clear({
  262.         $this->_resources = array();
  263.     }
  264.  
  265.     /**
  266.      *    Sets the current test case instance. This
  267.      *    global instance can be used by the mock objects
  268.      *    to send message to the test cases.
  269.      *    @param SimpleTestCase $test        Test case to register.
  270.      *    @access public
  271.      */
  272.     function setTest(&$test{
  273.         $this->clear();
  274.         $this->_test = &$test;
  275.     }
  276.  
  277.     /**
  278.      *    Accessor for currently running test case.
  279.      *    @return SimpleTestCase    Current test.
  280.      *    @access public
  281.      */
  282.     function &getTest({
  283.         return $this->_test;
  284.     }
  285.  
  286.     /**
  287.      *    Sets the current reporter. This
  288.      *    global instance can be used by the mock objects
  289.      *    to send messages.
  290.      *    @param SimpleReporter $reporter     Reporter to register.
  291.      *    @access public
  292.      */
  293.     function setReporter(&$reporter{
  294.         $this->clear();
  295.         $this->_reporter = &$reporter;
  296.     }
  297.  
  298.     /**
  299.      *    Accessor for current reporter.
  300.      *    @return SimpleReporter    Current reporter.
  301.      *    @access public
  302.      */
  303.     function &getReporter({
  304.         return $this->_reporter;
  305.     }
  306.  
  307.     /**
  308.      *    Accessor for the Singleton resource.
  309.      *    @return object       Global resource.
  310.      *    @access public
  311.      *    @static
  312.      */
  313.     function &get($resource{
  314.         if (isset($this->_resources[$resource])) {
  315.             $this->_resources[$resource&new $resource();
  316.         }
  317.         return $this->_resources[$resource];
  318.     }
  319. }
  320.  
  321. /**
  322.  *    Interrogates the stack trace to recover the
  323.  *    failure point.
  324.  *    @package SimpleTest
  325.  *    @subpackage UnitTester
  326.  */
  327.     var $_prefixes;
  328.  
  329.     /**
  330.      *    Stashes the list of target prefixes.
  331.      *    @param array $prefixes      List of method prefixes
  332.      *                                 to search for.
  333.      */
  334.     function SimpleStackTrace($prefixes{
  335.         $this->_prefixes = $prefixes;
  336.     }
  337.  
  338.     /**
  339.      *    Extracts the last method name that was not within
  340.      *    Simpletest itself. Captures a stack trace if none given.
  341.      *    @param array $stack      List of stack frames.
  342.      *    @return string           Snippet of test report with line
  343.      *                              number and file.
  344.      *    @access public
  345.      */
  346.     function traceMethod($stack false{
  347.         $stack $stack $stack $this->_captureTrace();
  348.         foreach ($stack as $frame{
  349.             if ($this->_frameLiesWithinSimpleTestFolder($frame)) {
  350.                 continue;
  351.             }
  352.             if ($this->_frameMatchesPrefix($frame)) {
  353.                 return ' at [' $frame['file'' line ' $frame['line'']';
  354.             }
  355.         }
  356.         return '';
  357.     }
  358.  
  359.     /**
  360.      *    Test to see if error is generated by SimpleTest itself.
  361.      *    @param array $frame     PHP stack frame.
  362.      *    @return boolean         True if a SimpleTest file.
  363.      *    @access private
  364.      */
  365.     function _frameLiesWithinSimpleTestFolder($frame{
  366.         if (isset($frame['file'])) {
  367.             $path substr(SIMPLE_TEST0-1);
  368.             if (strpos($frame['file']$path=== 0{
  369.                 if (dirname($frame['file']== $path{
  370.                     return true;
  371.                 }
  372.             }
  373.         }
  374.         return false;
  375.     }
  376.  
  377.     /**
  378.      *    Tries to determine if the method call is an assert, etc.
  379.      *    @param array $frame     PHP stack frame.
  380.      *    @return boolean         True if matches a target.
  381.      *    @access private
  382.      */
  383.     function _frameMatchesPrefix($frame{
  384.         foreach ($this->_prefixes as $prefix{
  385.             if (strncmp($frame['function']$prefixstrlen($prefix)) == 0{
  386.                 return true;
  387.             }
  388.         }
  389.         return false;
  390.     }
  391.  
  392.     /**
  393.      *    Grabs a current stack trace.
  394.      *    @return array        Fulle trace.
  395.      *    @access private
  396.      */
  397.     function _captureTrace({
  398.         if (function_exists('debug_backtrace')) {
  399.             return array_reverse(debug_backtrace());
  400.         }
  401.         return array();
  402.     }
  403. }
  404.  
  405. /**
  406.  *    @package SimpleTest
  407.  *    @subpackage UnitTester
  408.  *    @deprecated
  409.  */
  410. class SimpleTestOptions extends SimpleTest {
  411.  
  412.     /**
  413.      *    @deprecated
  414.      */
  415.     function getVersion({
  416.         return Simpletest::getVersion();
  417.     }
  418.  
  419.     /**
  420.      *    @deprecated
  421.      */
  422.     function ignore($class{
  423.         return Simpletest::ignore($class);
  424.     }
  425.  
  426.     /**
  427.      *    @deprecated
  428.      */
  429.     function isIgnored($class{
  430.         return Simpletest::isIgnored($class);
  431.     }
  432.  
  433.     /**
  434.      *    @deprecated
  435.      */
  436.     function setMockBaseClass($mock_base{
  437.         return Simpletest::setMockBaseClass($mock_base);
  438.     }
  439.  
  440.     /**
  441.      *    @deprecated
  442.      */
  443.     function getMockBaseClass({
  444.         return Simpletest::getMockBaseClass();
  445.     }
  446.  
  447.     /**
  448.      *    @deprecated
  449.      */
  450.     function useProxy($proxy$username false$password false{
  451.         return Simpletest::useProxy($proxy$username$password);
  452.     }
  453.  
  454.     /**
  455.      *    @deprecated
  456.      */
  457.     function getDefaultProxy({
  458.         return Simpletest::getDefaultProxy();
  459.     }
  460.  
  461.     /**
  462.      *    @deprecated
  463.      */
  464.     function getDefaultProxyUsername({
  465.         return Simpletest::getDefaultProxyUsername();
  466.     }
  467.  
  468.     /**
  469.      *    @deprecated
  470.      */
  471.     function getDefaultProxyPassword({
  472.         return Simpletest::getDefaultProxyPassword();
  473.     }
  474. }
  475. ?>

Documentation generated on Sun, 04 May 2008 09:22:09 -0500 by phpDocumentor 1.3.0