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

Source for file DocTest.php

Documentation is available at DocTest.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6.  * This file is part of the PEAR Testing_DocTest package.
  7.  *
  8.  * PHP version 5
  9.  *
  10.  * LICENSE: This source file is subject to the MIT license that is available
  11.  * through the world-wide-web at the following URI:
  12.  * http://opensource.org/licenses/mit-license.php
  13.  *
  14.  * @category  Testing
  15.  * @package   Testing_DocTest
  16.  * @author    David JEAN LOUIS <izimobil@gmail.com>
  17.  * @copyright 2008 David JEAN LOUIS
  18.  * @license   http://opensource.org/licenses/mit-license.php MIT License
  19.  * @version   CVS: $Id$
  20.  * @link      http://pear.php.net/package/Testing_DocTest
  21.  * @since     File available since release 0.1.0
  22.  * @filesource
  23.  */
  24.  
  25. /**
  26.  * Required unconditionally.
  27.  */
  28. require_once 'Testing/DocTest/Registry.php';
  29. require_once 'Testing/DocTest/Exception.php';
  30. require_once 'Testing/DocTest/Finder/Default.php';
  31. require_once 'Testing/DocTest/Reporter/Default.php';
  32. require_once 'Testing/DocTest/Parser/Default.php';
  33. require_once 'Testing/DocTest/Runner/Default.php';
  34.  
  35. /**
  36.  * DocTest.
  37.  *
  38.  * @category  Testing
  39.  * @package   Testing_DocTest
  40.  * @author    David JEAN LOUIS <izimobil@gmail.com>
  41.  * @copyright 2008 David JEAN LOUIS
  42.  * @license   http://opensource.org/licenses/mit-license.php MIT License
  43.  * @version   Release: @package_version@
  44.  * @link      http://pear.php.net/package/Testing_DocTest
  45.  * @since     Class available since release 0.1.0
  46.  */
  47. {
  48.     // Flags constants {{{
  49.  
  50.     /**
  51.      * Tell the runner to ignore *all* whitespace differences when comparing
  52.      * expected and actual results.
  53.      */
  54.     const FLAG_NORMALIZE_WHITESPACE = 0x01;
  55.  
  56.     /**
  57.      * Tell the runner to compare expected and actual results in case
  58.      * insensitive mode.
  59.      */
  60.     const FLAG_CASE_INSENSITIVE = 0x02;
  61.  
  62.     /**
  63.      * Tell the parser to skip the test.
  64.      */
  65.     const FLAG_SKIP = 0x04;
  66.  
  67.     /**
  68.      * Allow to pass a wildcard pattern: [...] that will match any string in
  69.      * the actual result.
  70.      */
  71.     const FLAG_ELLIPSIS = 0x08;
  72.  
  73.     // }}}
  74.     // __construct() {{{
  75.  
  76.     /**
  77.      * Constructor.
  78.      *
  79.      * The $options array can have the following elements
  80.      * - quiet: tells the reporter to turn on quiet mode, only errors will
  81.      *   be printed out (the default value is false);
  82.      * - no_colors: tells the reporter to not use colors when outputting
  83.      *   results (the default value is false);
  84.      * - logfile: tells the reporter to write the results in a logfile
  85.      *   instead of STDOUT;
  86.      *
  87.      * @param array $options an optional array of options
  88.      *
  89.      * @access public
  90.      * @return void 
  91.      */
  92.     public function __construct(array $options=array()) 
  93.     {
  94.         $reg Testing_DocTest_Registry::singleton();
  95.         // set user options
  96.         foreach ($options as $name => $value{
  97.             $reg->$name $value;
  98.         }
  99.         // set default workers
  100.         $reg->finder   = new Testing_DocTest_Finder_Default();
  101.         $reg->parser   = new Testing_DocTest_Parser_Default();
  102.         $reg->runner   = new Testing_DocTest_Runner_Default();
  103.         $reg->reporter = new Testing_DocTest_Reporter_Default();
  104.     }
  105.  
  106.     // }}}
  107.     // accept() {{{
  108.  
  109.     /**
  110.      * Method to allow DocTest to accept a custom finder, reporter, parser or
  111.      * runner instance.
  112.      *
  113.      * <code>
  114.      * class MyRunner implements Testing_DocTest_RunnerInterface {
  115.      *     function run(Testing_DocTest_TestCase $tb) {
  116.      *         // do something here...
  117.      *     }
  118.      * }
  119.      *
  120.      * try {
  121.      *     $goodRunner = new MyRunner();
  122.      *     $badRunner  = new stdclass();
  123.      *     $doctest    = new Testing_DocTest();
  124.      *     $doctest->accept($goodRunner);
  125.      *     echo "Ok !\n";
  126.      *     $doctest->accept($badRunner);
  127.      * } catch (Testing_DocTest_Exception $exc) {
  128.      *     echo "Error !\n";
  129.      * }
  130.      * // expects:
  131.      * // Ok !
  132.      * // Error !
  133.      *
  134.      * </code>
  135.      *
  136.      * @param mixed $instance an instance implementing the finder, reporter,
  137.      *                         parser or runner interface.
  138.      *
  139.      * @access public
  140.      * @return void 
  141.      * @throws Testing_DocTest_Exception if wrong argument passed
  142.      */
  143.     public function accept($instance
  144.     {
  145.         $reg Testing_DocTest_Registry::singleton();
  146.         if ($instance instanceof Testing_DocTest_FinderInterface{
  147.             $reg->finder = $instance;
  148.         else if ($instance instanceof Testing_DocTest_ReporterInterface{
  149.             $reg->reporter = $instance;
  150.         else if ($instance instanceof Testing_DocTest_ParserInterface{
  151.             $reg->parser = $instance;
  152.         else if ($instance instanceof Testing_DocTest_RunnerInterface{
  153.             $reg->runner = $instance;
  154.         else {
  155.             throw new Testing_DocTest_Exception('argument 1 of '
  156.                 . 'DocTest::accept must implement the finder, reporter, '
  157.                 . 'parser or runner interface.');
  158.         }
  159.     }
  160.  
  161.     // }}}
  162.     // run() {{{
  163.  
  164.     /**
  165.      * Run the tests contained in the given pathes.
  166.      *
  167.      * @param array $pathes an array of files and/or directories
  168.      *
  169.      * @access public
  170.      * @return void 
  171.      */
  172.     public function run(array $pathes)
  173.     {
  174.         $registry = Testing_DocTest_Registry::singleton();
  175.         $suites   $registry->parser->parse($registry->finder->find($pathes));
  176.         $registry->reporter->onBegin($suites);
  177.         foreach ($suites as $suite{
  178.             $registry->reporter->onTestSuiteBegin($suite);
  179.             foreach ($suite as $case{
  180.                 $registry->reporter->onTestCaseBegin($case);
  181.                 $registry->runner->run($case);
  182.                 switch ($case->state{
  183.                 case Testing_DocTest_TestCase::STATE_PASSED:
  184.                     $registry->reporter->onTestCasePass($case);
  185.                     break;
  186.                 case Testing_DocTest_TestCase::STATE_SKIPPED:
  187.                     $registry->reporter->onTestCaseSkip($case);
  188.                     break;
  189.                 case Testing_DocTest_TestCase::STATE_FAILED:
  190.                     $registry->reporter->onTestCaseFail($case);
  191.                     break;
  192.                 case Testing_DocTest_TestCase::STATE_ERROR:
  193.                     $registry->reporter->onTestCaseError($case);
  194.                     break;
  195.                 }
  196.                 $registry->reporter->onTestCaseEnd($case);
  197.             }
  198.             $registry->reporter->onTestSuiteEnd($suite);
  199.         }
  200.         $registry->reporter->onEnd($suites);
  201.     }
  202.  
  203.     // }}}
  204. }

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