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.         $reg->parser->setShellOptions($options);
  106.     }
  107.  
  108.     // }}}
  109.     // accept() {{{
  110.  
  111.     /**
  112.      * Method to allow DocTest to accept a custom finder, reporter, parser or
  113.      * runner instance.
  114.      *
  115.      * <code>
  116.      * class MyRunner implements Testing_DocTest_RunnerInterface {
  117.      *     function run(Testing_DocTest_TestCase $tb) {
  118.      *         // do something here...
  119.      *     }
  120.      * }
  121.      *
  122.      * try {
  123.      *     $goodRunner = new MyRunner();
  124.      *     $badRunner  = new stdclass();
  125.      *     $doctest    = new Testing_DocTest();
  126.      *     $doctest->accept($goodRunner);
  127.      *     echo "Ok !\n";
  128.      *     $doctest->accept($badRunner);
  129.      * } catch (Testing_DocTest_Exception $exc) {
  130.      *     echo "Error !\n";
  131.      * }
  132.      * // expects:
  133.      * // Ok !
  134.      * // Error !
  135.      *
  136.      * </code>
  137.      *
  138.      * @param mixed $instance an instance implementing the finder, reporter,
  139.      *                         parser or runner interface.
  140.      *
  141.      * @access public
  142.      * @return void 
  143.      * @throws Testing_DocTest_Exception if wrong argument passed
  144.      */
  145.     public function accept($instance
  146.     {
  147.         $reg Testing_DocTest_Registry::singleton();
  148.         if ($instance instanceof Testing_DocTest_FinderInterface{
  149.             $reg->finder = $instance;
  150.         else if ($instance instanceof Testing_DocTest_ReporterInterface{
  151.             $reg->reporter = $instance;
  152.         else if ($instance instanceof Testing_DocTest_ParserInterface{
  153.             $reg->parser = $instance;
  154.         else if ($instance instanceof Testing_DocTest_RunnerInterface{
  155.             $reg->runner = $instance;
  156.         else {
  157.             throw new Testing_DocTest_Exception(
  158.                 'argument 1 of '
  159.                 . 'DocTest::accept must implement the finder, reporter, '
  160.                 . 'parser or runner interface.'
  161.             );
  162.         }
  163.     }
  164.  
  165.     // }}}
  166.     // run() {{{
  167.  
  168.     /**
  169.      * Run the tests contained in the given pathes.
  170.      *
  171.      * @param array $pathes an array of files and/or directories
  172.      *
  173.      * @access public
  174.      * @return void 
  175.      */
  176.     public function run(array $pathes)
  177.     {
  178.         $reg    = Testing_DocTest_Registry::singleton();
  179.         $suites $reg->parser->parse($reg->finder->find($pathes));
  180.         $reg->reporter->onBegin($suites);
  181.         foreach ($suites as $suite{
  182.             $reg->reporter->onTestSuiteBegin($suite);
  183.             foreach ($suite as $case{
  184.                 $reg->reporter->onTestCaseBegin($case);
  185.                 if (!isset($reg->tests|| in_array($case->name$reg->tests)) {
  186.                     $reg->runner->run($case);
  187.                 else {
  188.                     $case->state = Testing_DocTest_TestCase::STATE_SKIPPED;
  189.                 }
  190.                 switch ($case->state{
  191.                 case Testing_DocTest_TestCase::STATE_PASSED:
  192.                     $reg->reporter->onTestCasePass($case);
  193.                     break;
  194.                 case Testing_DocTest_TestCase::STATE_SKIPPED:
  195.                     $reg->reporter->onTestCaseSkip($case);
  196.                     break;
  197.                 case Testing_DocTest_TestCase::STATE_FAILED:
  198.                     $reg->reporter->onTestCaseFail($case);
  199.                     break;
  200.                 case Testing_DocTest_TestCase::STATE_ERROR:
  201.                     $reg->reporter->onTestCaseError($case);
  202.                     break;
  203.                 }
  204.                 $reg->reporter->onTestCaseEnd($case);
  205.             }
  206.             $reg->reporter->onTestSuiteEnd($suite);
  207.         }
  208.         return $reg->reporter->onEnd($suites);
  209.     }
  210.  
  211.     // }}}
  212. }

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