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

Source for file Skeleton.php

Documentation is available at Skeleton.php

  1. <?php
  2. //
  3. // +------------------------------------------------------------------------+
  4. // | PEAR :: PHPUnit2                                                       |
  5. // +------------------------------------------------------------------------+
  6. // | Copyright (c) 2002-2004 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: Skeleton.php,v 1.11.2.1 2004/10/01 06:11:54 sebastian Exp $
  16. //
  17.  
  18. /**
  19.  * Class for creating a PHPUnit2_Framework_TestCase skeleton file.
  20.  *
  21.  * This class will take a classname as a parameter on construction and will
  22.  * create a PHP file that contains the skeleton of a PHPUnit2_Framework_TestCase
  23.  * subclass.
  24.  *
  25.  * <code>
  26.  * <?php
  27.  * require_once 'PHPUnit2/Util/Skeleton.php';
  28.  *
  29.  * $skeleton = new PHPUnit2_Util_Skeleton(
  30.  *   'PHPUnit2_Util_Skeleton',
  31.  *   'PHPUnit2/Util/Skeleton.php'
  32.  * );
  33.  *
  34.  * $skeleton->write();
  35.  * ?>
  36.  * </code>
  37.  *
  38.  * @author      Sebastian Bergmann <sb@sebastian-bergmann.de>
  39.  * @copyright   Copyright &copy; 2002-2004 Sebastian Bergmann <sb@sebastian-bergmann.de>
  40.  * @license     http://www.php.net/license/3_0.txt The PHP License, Version 3.0
  41.  * @category    Testing
  42.  * @package     PHPUnit2
  43.  * @subpackage  Util
  44.  * @since       2.1.0
  45.  * @abstract
  46.  */
  47.     // {{{ Constants
  48.  
  49.     const templateClassHeader =
  50. '<?php
  51. if (!defined("PHPUnit2_MAIN_METHOD")) {
  52.     define("PHPUnit2_MAIN_METHOD", "%sTest::main");
  53. }
  54.  
  55. require_once "PHPUnit2/Framework/IncompleteTestError.php";
  56. require_once "PHPUnit2/Framework/TestCase.php";
  57.  
  58. require_once "%s.php";
  59.  
  60. /**
  61.  * Test class for %s.
  62.  * Generated by PHPUnit2_Util_Skeleton on %s at %s.
  63.  */
  64. class %sTest extends PHPUnit2_Framework_TestCase {
  65.     public static function main() {
  66.         require_once "PHPUnit2/Framework/TestSuite.php";
  67.         require_once "PHPUnit2/TextUI/TestRunner.php";
  68.  
  69.         $suite  = new PHPUnit2_Framework_TestSuite("%sTest");
  70.         $result = PHPUnit2_TextUI_TestRunner::run($suite);
  71.     }
  72. ';
  73.  
  74.     const templateClassFooter =
  75. '}
  76.  
  77. if (PHPUnit2_MAIN_METHOD == "%sTest::main") {
  78.     %sTest::main();
  79. }
  80. ?>
  81. ';
  82.  
  83.     const templateMethod =
  84. '
  85.     /**
  86.     * @todo Implement test%s().
  87.     */
  88.     public function test%s() {
  89.         throw new PHPUnit2_Framework_IncompleteTestError;
  90.     }
  91. ';
  92.  
  93.     // }}}
  94.     // {{{ Members
  95.  
  96.     /**
  97.     * @var    string 
  98.     * @access protected
  99.     */
  100.     protected $className;
  101.  
  102.     /**
  103.     * @var    string 
  104.     * @access protected
  105.     */
  106.     protected $classSourceFile;
  107.  
  108.     // }}}
  109.     // {{{ public function __construct($className, $classSourceFile = '')
  110.  
  111.     /**
  112.     * Constructor.
  113.     *
  114.     * @param  string  $className 
  115.     * @param  string  $classSourceFile 
  116.     * @access public
  117.     */
  118.     public function __construct($className$classSourceFile ''{
  119.         if ($classSourceFile == ''{
  120.             $classSourceFile $className '.php';
  121.         }
  122.  
  123.         if (file_exists($classSourceFile)) {
  124.             $this->classSourceFile = $classSourceFile;
  125.         else {
  126.             throw new Exception('Could not open ' $classSourceFile '.');
  127.         }
  128.  
  129.         @include_once $this->classSourceFile;
  130.  
  131.         if (class_exists($className)) {
  132.             $this->className = $className;
  133.         else {
  134.             throw new Exception('Could not find class "' $className '".');
  135.         }
  136.     }
  137.  
  138.     // }}}
  139.     // {{{ public function generate()
  140.  
  141.     /**
  142.     * Generates the test class' source.
  143.     *
  144.     * @return string 
  145.     * @access public
  146.     */
  147.     public function generate({
  148.         $testClassSource $this->testClassHeader($this->className);
  149.  
  150.         $class = new ReflectionClass($this->className);
  151.  
  152.         foreach ($class->getMethods(as $method{
  153.             if (!$method->isConstructor(&&
  154.                 !$method->isAbstract(&&
  155.                  $method->isUserDefined(&&
  156.                  $method->isPublic(&&
  157.                  $method->getDeclaringClass()->getName(== $this->className{
  158.                 $testClassSource .= $this->testMethod($method->getName());
  159.             }
  160.         }
  161.  
  162.         $testClassSource .= $this->testClassFooter($this->className);
  163.  
  164.         return $testClassSource;
  165.     }
  166.  
  167.     // }}}
  168.     // {{{ public function write()
  169.  
  170.     /**
  171.     * Generates the test class and writes it to a source file.
  172.     *
  173.     * @param  string  $file 
  174.     * @access public
  175.     */
  176.     public function write($file ''{
  177.         if ($file == ''{
  178.             $file $this->className . 'Test.php';
  179.         }
  180.  
  181.         if ($fp @fopen($file'w')) {
  182.             @fputs($fp$this->generate());
  183.             @fclose($fp);
  184.         }
  185.     }
  186.  
  187.     // }}}
  188.     // {{{ protected function testClassHeader($className)
  189.  
  190.     /**
  191.     * @param  string  $className 
  192.     * @access protected
  193.     */
  194.     protected function testClassHeader($className{
  195.         return sprintf(
  196.           self::templateClassHeader,
  197.           $className,
  198.           $className,
  199.           $className,
  200.           date('Y-m-d'),
  201.           date('H:i:s'),
  202.           $className,
  203.           $className,
  204.           $className
  205.         );
  206.     }
  207.  
  208.     // }}}
  209.     // {{{ protected function testClassFooter($className)
  210.  
  211.     /**
  212.     * @param  string  $className 
  213.     * @access protected
  214.     */
  215.     protected function testClassFooter($className{
  216.         return sprintf(
  217.           self::templateClassFooter,
  218.           $className,
  219.           $className
  220.         );
  221.     }
  222.  
  223.     // }}}
  224.     // {{{ protected function testMethod($methodName)
  225.  
  226.     /**
  227.     * @param  string  $methodName 
  228.     * @access protected
  229.     */
  230.     protected function testMethod($methodName{
  231.         $methodName ucfirst($methodName);
  232.  
  233.         return sprintf(
  234.           self::templateMethod,
  235.           $methodName,
  236.           $methodName
  237.         );
  238.     }
  239.  
  240.     // }}}
  241. }
  242.  
  243. /*
  244.  * vim600:  et sw=2 ts=2 fdm=marker
  245.  * vim<600: et sw=2 ts=2
  246.  */
  247. ?>

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