home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / PHPUnit2 / Util / Skeleton.php < prev   
Encoding:
PHP Script  |  2008-07-02  |  9.3 KB  |  341 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * PHP Version 5
  6.  *
  7.  * Copyright (c) 2002-2006, Sebastian Bergmann <sb@sebastian-bergmann.de>.
  8.  * All rights reserved.
  9.  *
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions
  12.  * are met:
  13.  *
  14.  *   * Redistributions of source code must retain the above copyright
  15.  *     notice, this list of conditions and the following disclaimer.
  16.  * 
  17.  *   * Redistributions in binary form must reproduce the above copyright
  18.  *     notice, this list of conditions and the following disclaimer in
  19.  *     the documentation and/or other materials provided with the
  20.  *     distribution.
  21.  *
  22.  *   * Neither the name of Sebastian Bergmann nor the names of his
  23.  *     contributors may be used to endorse or promote products derived
  24.  *     from this software without specific prior written permission.
  25.  *
  26.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  29.  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  30.  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  31.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  32.  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  33.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  34.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
  35.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  36.  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37.  * POSSIBILITY OF SUCH DAMAGE.
  38.  *
  39.  * @category   Testing
  40.  * @package    PHPUnit2
  41.  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
  42.  * @copyright  2002-2006 Sebastian Bergmann <sb@sebastian-bergmann.de>
  43.  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
  44.  * @version    CVS: $Id: Skeleton.php,v 1.24.2.3 2005/12/17 16:04:58 sebastian Exp $
  45.  * @link       http://pear.php.net/package/PHPUnit2
  46.  * @since      File available since Release 2.1.0
  47.  */
  48.  
  49. /**
  50.  * Class for creating a PHPUnit2_Framework_TestCase skeleton file.
  51.  *
  52.  * This class will take a classname as a parameter on construction and will
  53.  * create a PHP file that contains the skeleton of a PHPUnit2_Framework_TestCase
  54.  * subclass.
  55.  *
  56.  * <code>
  57.  * <?php
  58.  * require_once 'PHPUnit2/Util/Skeleton.php';
  59.  *
  60.  * $skeleton = new PHPUnit2_Util_Skeleton(
  61.  *   'PHPUnit2_Util_Skeleton',
  62.  *   'PHPUnit2/Util/Skeleton.php'
  63.  * );
  64.  *
  65.  * $skeleton->write();
  66.  * ?>
  67.  * </code>
  68.  *
  69.  * @category   Testing
  70.  * @package    PHPUnit2
  71.  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
  72.  * @copyright  2002-2006 Sebastian Bergmann <sb@sebastian-bergmann.de>
  73.  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
  74.  * @version    Release: 2.3.6
  75.  * @link       http://pear.php.net/package/PHPUnit2
  76.  * @since      Class available since Release 2.1.0
  77.  */
  78. class PHPUnit2_Util_Skeleton {
  79.     protected $templateClassHeader =
  80. '<?php
  81. // Call {className}Test::main() if this source file is executed directly.
  82. if (!defined("PHPUnit2_MAIN_METHOD")) {
  83.     define("PHPUnit2_MAIN_METHOD", "{className}Test::main");
  84. }
  85.  
  86. require_once "PHPUnit2/Framework/TestCase.php";
  87. require_once "PHPUnit2/Framework/TestSuite.php";
  88.  
  89. // You may remove the following line when all tests have been implemented.
  90. require_once "PHPUnit2/Framework/IncompleteTestError.php";
  91.  
  92. require_once "{classFile}";
  93.  
  94. /**
  95.  * Test class for {className}.
  96.  * Generated by PHPUnit2_Util_Skeleton on {date} at {time}.
  97.  */
  98. class {className}Test extends PHPUnit2_Framework_TestCase {
  99.     /**
  100.      * Runs the test methods of this class.
  101.      *
  102.      * @access public
  103.      * @static
  104.      */
  105.     public static function main() {
  106.         require_once "PHPUnit2/TextUI/TestRunner.php";
  107.  
  108.         $suite  = new PHPUnit2_Framework_TestSuite("{className}Test");
  109.         $result = PHPUnit2_TextUI_TestRunner::run($suite);
  110.     }
  111.  
  112.     /**
  113.      * Sets up the fixture, for example, open a network connection.
  114.      * This method is called before a test is executed.
  115.      *
  116.      * @access protected
  117.      */
  118.     protected function setUp() {
  119.     }
  120.  
  121.     /**
  122.      * Tears down the fixture, for example, close a network connection.
  123.      * This method is called after a test is executed.
  124.      *
  125.      * @access protected
  126.      */
  127.     protected function tearDown() {
  128.     }
  129. ';
  130.  
  131.     protected $templateClassFooter =
  132. '}
  133.  
  134. // Call {className}Test::main() if this source file is executed directly.
  135. if (PHPUnit2_MAIN_METHOD == "{className}Test::main") {
  136.     {className}Test::main();
  137. }
  138. ?>
  139. ';
  140.  
  141.     protected $templateMethod =
  142. '
  143.     /**
  144.      * @todo Implement test{methodName}().
  145.      */
  146.     public function test{methodName}() {
  147.         // Remove the following line when you implement this test.
  148.         throw new PHPUnit2_Framework_IncompleteTestError;
  149.     }
  150. ';
  151.  
  152.     /**
  153.      * @var    string
  154.      * @access protected
  155.      */
  156.     protected $className;
  157.  
  158.     /**
  159.      * @var    string
  160.      * @access protected
  161.      */
  162.     protected $classSourceFile;
  163.  
  164.     /**
  165.      * Constructor.
  166.      *
  167.      * @param  string  $className
  168.      * @param  string  $classSourceFile
  169.      * @throws Exception
  170.      * @access public
  171.      */
  172.     public function __construct($className, $classSourceFile = '') {
  173.         if ($classSourceFile == '') {
  174.             $classSourceFile = $className . '.php';
  175.         }
  176.  
  177.         if (file_exists($classSourceFile)) {
  178.             $this->classSourceFile = $classSourceFile;
  179.         } else {
  180.             throw new Exception(
  181.               sprintf(
  182.                 'Could not open %s.',
  183.  
  184.                 $classSourceFile
  185.               )
  186.             );
  187.         }
  188.  
  189.         @include_once $this->classSourceFile;
  190.  
  191.         if (class_exists($className)) {
  192.             $this->className = $className;
  193.         } else {
  194.             throw new Exception(
  195.               sprintf(
  196.                 'Could not find class "%s" in %s.',
  197.  
  198.                 $className,
  199.                 $classSourceFile
  200.               )
  201.             );
  202.         }
  203.     }
  204.  
  205.     /**
  206.      * Generates the test class' source.
  207.      *
  208.      * @return string
  209.      * @access public
  210.      */
  211.     public function generate() {
  212.         $testClassSource = $this->testClassHeader($this->className, $this->classSourceFile);
  213.  
  214.         $class = new ReflectionClass($this->className);
  215.  
  216.         foreach ($class->getMethods() as $method) {
  217.             if (!$method->isConstructor() &&
  218.                 !$method->isAbstract() &&
  219.                  $method->isUserDefined() &&
  220.                  $method->isPublic() &&
  221.                  $method->getDeclaringClass()->getName() == $this->className) {
  222.                 $testClassSource .= $this->testMethod($method->getName());
  223.             }
  224.         }
  225.  
  226.         $testClassSource .= $this->testClassFooter($this->className);
  227.  
  228.         return $testClassSource;
  229.     }
  230.  
  231.     /**
  232.      * Generates the test class and writes it to a source file.
  233.      *
  234.      * @param  string  $file
  235.      * @access public
  236.      */
  237.     public function write($file = '') {
  238.         if ($file == '') {
  239.             $file = $this->className . 'Test.php';
  240.         }
  241.  
  242.         if ($fp = @fopen($file, 'w')) {
  243.             @fputs($fp, $this->generate());
  244.             @fclose($fp);
  245.         }
  246.     }
  247.  
  248.     /**
  249.      * Sets the templates for class header, class footer, and method.
  250.      *
  251.      * @param  string  $classHeader
  252.      * @param  string  $classFooter
  253.      * @param  string  $method
  254.      * @access public
  255.      * @since  Method available since Release 2.2.0
  256.      */
  257.     public function setTemplates($classHeader, $classFooter, $method) {
  258.         if (is_file($classHeader)) {
  259.             $this->templateClassHeader = file_get_contents($classHeader);
  260.         } else {
  261.             $this->templateClassHeader = $classHeader;
  262.         }
  263.  
  264.         if (is_file($classFooter)) {
  265.             $this->templateClassFooter = file_get_contents($classFooter);
  266.         } else {
  267.             $this->templateClassFooter = $classFooter;
  268.         }
  269.  
  270.         if (is_file($method)) {
  271.             $this->templateMethod = file_get_contents($method);
  272.         } else {
  273.             $this->templateMethod = $method;
  274.         }
  275.     }
  276.  
  277.     /**
  278.      * @param  string  $className
  279.      * @param  string  $classSourceFile
  280.      * @access protected
  281.      */
  282.     protected function testClassHeader($className, $classSourceFile) {
  283.         return str_replace(
  284.           array(
  285.             '{className}',
  286.             '{classFile}',
  287.             '{date}',
  288.             '{time}'
  289.           ),
  290.           array(
  291.             $className,
  292.             $classSourceFile,
  293.             date('Y-m-d'),
  294.             date('H:i:s')
  295.           ),
  296.           $this->templateClassHeader
  297.         );
  298.     }
  299.  
  300.     /**
  301.      * @param  string  $className
  302.      * @access protected
  303.      */
  304.     protected function testClassFooter($className) {
  305.         return str_replace(
  306.           array(
  307.             '{className}'
  308.           ),
  309.           array(
  310.             $className
  311.           ),
  312.           $this->templateClassFooter
  313.         );
  314.     }
  315.  
  316.     /**
  317.      * @param  string  $methodName
  318.      * @access protected
  319.      */
  320.     protected function testMethod($methodName) {
  321.         return str_replace(
  322.           array(
  323.             '{methodName}'
  324.           ),
  325.           array(
  326.             ucfirst($methodName)
  327.           ),
  328.           $this->templateMethod
  329.         );
  330.     }
  331. }
  332.  
  333. /*
  334.  * Local variables:
  335.  * tab-width: 4
  336.  * c-basic-offset: 4
  337.  * c-hanging-comment-ender-p: nil
  338.  * End:
  339.  */
  340. ?>
  341.