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-2005 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.15.2.5 2005/06/03 05:48:09 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-2005 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.     // {{{ Instance Variables
  48.  
  49.     protected $templateClassHeader =
  50. '<?php
  51. if (!defined("PHPUnit2_MAIN_METHOD")) {
  52.     define("PHPUnit2_MAIN_METHOD", "{className}Test::main");
  53. }
  54.  
  55. require_once "PHPUnit2/Framework/IncompleteTestError.php";
  56. require_once "PHPUnit2/Framework/TestCase.php";
  57. require_once "PHPUnit2/Framework/TestSuite.php";
  58.  
  59. require_once "{classFile}";
  60.  
  61. /**
  62.  * Test class for {className}.
  63.  * Generated by PHPUnit2_Util_Skeleton on {date} at {time}.
  64.  */
  65. class {className}Test extends PHPUnit2_Framework_TestCase {
  66.     public static function main() {
  67.         require_once "PHPUnit2/TextUI/TestRunner.php";
  68.  
  69.         $suite  = new PHPUnit2_Framework_TestSuite("{className}Test");
  70.         $result = PHPUnit2_TextUI_TestRunner::run($suite);
  71.     }
  72. ';
  73.  
  74.     protected $templateClassFooter =
  75. '}
  76.  
  77. if (PHPUnit2_MAIN_METHOD == "{className}Test::main") {
  78.     {className}Test::main();
  79. }
  80. ?>
  81. ';
  82.  
  83.     protected $templateMethod =
  84. '
  85.     /**
  86.     * @todo Implement test{methodName}().
  87.     */
  88.     public function test{methodName}() {
  89.         throw new PHPUnit2_Framework_IncompleteTestError;
  90.     }
  91. ';
  92.  
  93.     /**
  94.     * @var    string 
  95.     * @access protected
  96.     */
  97.     protected $className;
  98.  
  99.     /**
  100.     * @var    string 
  101.     * @access protected
  102.     */
  103.     protected $classSourceFile;
  104.  
  105.     // }}}
  106.     // {{{ public function __construct($className, $classSourceFile = '')
  107.  
  108.     /**
  109.     * Constructor.
  110.     *
  111.     * @param  string  $className 
  112.     * @param  string  $classSourceFile 
  113.     * @access public
  114.     */
  115.     public function __construct($className$classSourceFile ''{
  116.         if ($classSourceFile == ''{
  117.             $classSourceFile $className '.php';
  118.         }
  119.  
  120.         if (file_exists($classSourceFile)) {
  121.             $this->classSourceFile = $classSourceFile;
  122.         else {
  123.             throw new Exception(
  124.               sprintf(
  125.                 'Could not open %s.',
  126.  
  127.                 $classSourceFile
  128.               )
  129.             );
  130.         }
  131.  
  132.         @include_once $this->classSourceFile;
  133.  
  134.         if (class_exists($className)) {
  135.             $this->className = $className;
  136.         else {
  137.             throw new Exception(
  138.               sprintf(
  139.                 'Could not find class "%s" in %s.',
  140.  
  141.                 $className,
  142.                 $classSourceFile
  143.               )
  144.             );
  145.         }
  146.     }
  147.  
  148.     // }}}
  149.     // {{{ public function generate()
  150.  
  151.     /**
  152.     * Generates the test class' source.
  153.     *
  154.     * @return string 
  155.     * @access public
  156.     */
  157.     public function generate({
  158.         $testClassSource $this->testClassHeader($this->className$this->classSourceFile);
  159.  
  160.         $class = new ReflectionClass($this->className);
  161.  
  162.         foreach ($class->getMethods(as $method{
  163.             if (!$method->isConstructor(&&
  164.                 !$method->isAbstract(&&
  165.                  $method->isUserDefined(&&
  166.                  $method->isPublic(&&
  167.                  $method->getDeclaringClass()->getName(== $this->className{
  168.                 $testClassSource .= $this->testMethod($method->getName());
  169.             }
  170.         }
  171.  
  172.         $testClassSource .= $this->testClassFooter($this->className);
  173.  
  174.         return $testClassSource;
  175.     }
  176.  
  177.     // }}}
  178.     // {{{ public function write()
  179.  
  180.     /**
  181.     * Generates the test class and writes it to a source file.
  182.     *
  183.     * @param  string  $file 
  184.     * @access public
  185.     */
  186.     public function write($file ''{
  187.         if ($file == ''{
  188.             $file $this->className . 'Test.php';
  189.         }
  190.  
  191.         if ($fp @fopen($file'w')) {
  192.             @fputs($fp$this->generate());
  193.             @fclose($fp);
  194.         }
  195.     }
  196.  
  197.     // }}}
  198.     // {{{ public function setTemplates($classHeader, $classFooter, $method)
  199.  
  200.     /**
  201.     * Sets the templates for class header, class footer, and method.
  202.     *
  203.     * @param  string  $classHeader 
  204.     * @param  string  $classFooter 
  205.     * @param  string  $method 
  206.     * @access public
  207.     * @since  2.2.0
  208.     */
  209.     public function setTemplates($classHeader$classFooter$method{
  210.         if (is_file($classHeader)) {
  211.             $this->templateClassHeader = file_get_contents($classHeader);
  212.         else {
  213.             $this->templateClassHeader = $classHeader;
  214.         }
  215.  
  216.         if (is_file($classFooter)) {
  217.             $this->templateClassFooter = file_get_contents($classFooter);
  218.         else {
  219.             $this->templateClassFooter = $classFooter;
  220.         }
  221.  
  222.         if (is_file($method)) {
  223.             $this->templateMethod = file_get_contents($method);
  224.         else {
  225.             $this->templateMethod = $method;
  226.         }
  227.     }
  228.  
  229.     // }}}
  230.     // {{{ protected function testClassHeader($className, $classSourceFile)
  231.  
  232.     /**
  233.     * @param  string  $className 
  234.     * @param  string  $classSourceFile 
  235.     * @access protected
  236.     */
  237.     protected function testClassHeader($className$classSourceFile{
  238.         return str_replace(
  239.           array(
  240.             '{className}',
  241.             '{classFile}',
  242.             '{date}',
  243.             '{time}'
  244.           ),
  245.           array(
  246.             $className,
  247.             $classSourceFile,
  248.             date('Y-m-d'),
  249.             date('H:i:s')
  250.           ),
  251.           $this->templateClassHeader
  252.         );
  253.     }
  254.  
  255.     // }}}
  256.     // {{{ protected function testClassFooter($className)
  257.  
  258.     /**
  259.     * @param  string  $className 
  260.     * @access protected
  261.     */
  262.     protected function testClassFooter($className{
  263.         return str_replace(
  264.           array(
  265.             '{className}'
  266.           ),
  267.           array(
  268.             $className
  269.           ),
  270.           $this->templateClassFooter
  271.         );
  272.     }
  273.  
  274.     // }}}
  275.     // {{{ protected function testMethod($methodName)
  276.  
  277.     /**
  278.     * @param  string  $methodName 
  279.     * @access protected
  280.     */
  281.     protected function testMethod($methodName{
  282.         return str_replace(
  283.           array(
  284.             '{methodName}'
  285.           ),
  286.           array(
  287.             ucfirst($methodName)
  288.           ),
  289.           $this->templateMethod
  290.         );
  291.     }
  292.  
  293.     // }}}
  294. }
  295.  
  296. /*
  297.  * vim600:  et sw=2 ts=2 fdm=marker
  298.  * vim<600: et sw=2 ts=2
  299.  */
  300. ?>

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