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

Source for file Skeleton.php

Documentation is available at Skeleton.php

  1. <?php
  2. //
  3. // +------------------------------------------------------------------------+
  4. // | PEAR :: PHPUnit                                                        |
  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.6 2004/12/22 08:06:11 sebastian Exp $
  16. //
  17.  
  18. /**
  19.  * Class for creating a PHPUnit_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 PHPUnit_TestCase
  23.  * subclass. The test case will contain a test foreach method of the class.
  24.  * Methods of the parent class will, by default, be excluded from the test
  25.  * class. Passing and optional construction parameter will include them.
  26.  *
  27.  * Example
  28.  *
  29.  *   <?php
  30.  *   require_once 'PHPUnit/Skeleton.php';
  31.  *   $ps = new PHPUnit_Skeleton('PHPUnit_Skeleton', 'PHPUnit/Skeleton.php');
  32.  *
  33.  *   // Generate the test class.
  34.  *   // Default settings will not include any parent class methods, but
  35.  *   // will include private methods.
  36.  *   $ps->createTestClass();
  37.  *
  38.  *   // Write the new test class to file.
  39.  *   // By default, code to run the test will be included.
  40.  *   $ps->writeTestClass();
  41.  *   ?>
  42.  *
  43.  * Now open the skeleton class and fill in the details.
  44.  * If you run the test as is, all tests will fail and
  45.  * you will see plenty of undefined constant errors.
  46.  *
  47.  * @author      Scott Mattocks <scott@crisscott.com>
  48.  * @copyright   Copyright &copy; 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>
  49.  * @license     http://www.php.net/license/3_0.txt The PHP License, Version 3.0
  50.  * @category    Testing
  51.  * @package     PHPUnit
  52.  */
  53.     /**
  54.      * Path to the class file to create a skeleton for.
  55.      * @var string 
  56.      */
  57.     var $classPath;
  58.  
  59.     /**
  60.      * The name of the class
  61.      * @var string 
  62.      */
  63.     var $className;
  64.  
  65.     /**
  66.      * Path to the configuration file needed by class to test.
  67.      * @var string 
  68.      */
  69.     var $configFile;
  70.  
  71.     /**
  72.      * Whether or not to include the methods of the parent class when testing.
  73.      * @var boolean 
  74.      */
  75.     var $includeParents;
  76.  
  77.     /**
  78.      * Whether or not to test private methods.
  79.      * @var boolean 
  80.      */
  81.     var $includePrivate;
  82.  
  83.     /**
  84.      * The test class that will be created.
  85.      * @var string 
  86.      */
  87.     var $testClass;
  88.  
  89.     /**
  90.      * Constructor. Sets the class members and check that the class
  91.      * to test is accessible.
  92.      *
  93.      * @access public
  94.      * @param  string  $className 
  95.      * @param  string  $classPath 
  96.      * @param  boolean $includeParents Wheter to include the parent's methods in the test.
  97.      * @return void 
  98.      */
  99.     function PHPUnit_Skeleton($className$classPath$includeParents = FALSE$includePrivate = TRUE{
  100.         // Set up the members.
  101.         if (@is_readable($classPath)) {
  102.             $this->className = $className;
  103.             $this->classPath = $classPath;
  104.         else {
  105.             $this->_handleErrors($classPath ' is not readable. Cannot create test class.');
  106.         }
  107.  
  108.         // Do we want to include parent methods?
  109.         $this->includeParents = $includeParents;
  110.  
  111.         // Do we want to allow private methods?
  112.         $this->includePrivate = $includePrivate;
  113.     }
  114.  
  115.     /**
  116.      * The class to test may require a special config file before it can be
  117.      * instantiated. This method lets you set that file.
  118.      *
  119.      * @access public
  120.      * @param  string $configPath 
  121.      * @return void 
  122.      */
  123.     function setConfigFile($configFile{
  124.         // Check that the file is readable
  125.         if (@is_readable($configFile)) {
  126.             $this->configFile = $configFile;
  127.         else {
  128.             $this->_handleErrors($configFile ' is not readable. Cannot create test class.');
  129.         }
  130.     }
  131.  
  132.     /**
  133.      * Create the code that will be the skeleton of the test case.
  134.      *
  135.      * The test case must have a clss definition, one var, a constructor
  136.      * setUp, tearDown, and methods. Optionally and by default the code
  137.      * to run the test is added when the class is written to file.
  138.      *
  139.      * @access public
  140.      * @param  none 
  141.      * @return void 
  142.      */
  143.     function createTestClass({
  144.         // Instantiate the object.
  145.         if (isset($this->configFile)) {
  146.             require_once $this->configFile;
  147.         }
  148.  
  149.         require_once $this->classPath;
  150.  
  151.         // Get the methods.
  152.         $classMethods get_class_methods($this->className);
  153.  
  154.         // Remove the parent methods if needed.
  155.         if (!$this->includeParents{
  156.             $parentMethods get_class_methods(get_parent_class($this->className));
  157.  
  158.             if (count($parentMethods)) {
  159.                 $classMethods array_diff($classMethods$parentMethods);
  160.             }
  161.         }
  162.  
  163.         // Create the class definition, constructor, setUp and tearDown.
  164.         $this->_createDefinition();
  165.         $this->_createConstructor();
  166.         $this->_createSetUpTearDown();
  167.  
  168.         if (count($classMethods)) {
  169.             // Foreach method create a test case.
  170.             foreach ($classMethods as $method{
  171.                 // Unless it is the constructor.
  172.                 if (strcasecmp($this->className$method!== 0{
  173.                   // Check for private methods.
  174.                   if (!$this->includePrivate && strpos($method'_'=== 0{
  175.                       continue;
  176.                   else {
  177.                       $this->_createMethod($method);
  178.                   }
  179.                 }
  180.             }
  181.         }
  182.  
  183.         // Finis off the class.
  184.         $this->_finishClass();
  185.     }
  186.  
  187.     /**
  188.      * Create the class definition.
  189.      *
  190.      * The definition consist of a header comment, require statment
  191.      * for getting the PHPUnit file, the actual class definition,
  192.      * and the definition of the class member variable.
  193.      *
  194.      * All of the code needed for the new class is stored in the
  195.      * testClass member.
  196.      *
  197.      * @access private
  198.      * @param  none 
  199.      * @return void 
  200.      */
  201.     function _createDefinition({
  202.         // Create header comment.
  203.         $this->testClass =
  204.           "/**\n" .
  205.           " * PHPUnit test case for " $this->className . "\n" .
  206.           " * \n" .
  207.           " * The method skeletons below need to be filled in with \n" .
  208.           " * real data so that the tests will run correctly. Replace \n" .
  209.           " * all EXPECTED_VAL and PARAM strings with real data. \n" .
  210.           " * \n" .
  211.           " * Created with PHPUnit_Skeleton on " date('Y-m-d'"\n" .
  212.           " */\n";
  213.  
  214.         // Add the require statements.
  215.         $this->testClass .= "require_once 'PHPUnit.php';\n";
  216.  
  217.         // Add the class definition and variable definition.
  218.         $this->testClass .=
  219.           "class " $this->className . "Test extends PHPUnit_TestCase {\n\n" .
  220.           "    var \$" $this->className . ";\n\n";
  221.     }
  222.  
  223.     /**
  224.      * Create the class constructor. (PHP4 style)
  225.      *
  226.      * The constructor simply calls the PHPUnit_TestCase method.
  227.      * This code is taken from the PHPUnit documentation.
  228.      *
  229.      * All of the code needed for the new class is stored in the
  230.      * testClass member.
  231.      *
  232.      * @access private
  233.      * @param  none 
  234.      * @return void 
  235.      */
  236.     function _createConstructor({
  237.         // Create the test class constructor.
  238.         $this->testClass.=
  239.           "    function " $this->className . "Test(\$name)\n" .
  240.           "    {\n" .
  241.           "        \$this->PHPUnit_TestCase(\$name);\n" .
  242.           "    }\n\n";
  243.     }
  244.  
  245.     /**
  246.      * Create setUp and tearDown methods.
  247.      *
  248.      * The setUp method creates the instance of the object to test.
  249.      * The tearDown method releases the instance.
  250.      * This code is taken from the PHPUnit documentation.
  251.      *
  252.      * All of the code needed for the new class is stored in the
  253.      * testClass member.
  254.      *
  255.      * @access private
  256.      * @param  none 
  257.      * @return void 
  258.      */
  259.     function _createSetUpTearDown({
  260.         // Create the setUp method.
  261.         $this->testClass .=
  262.           "    function setUp()\n" .
  263.           "    {\n";
  264.  
  265.         if (isset($this->configFile)) {
  266.             $this->testClass .=
  267.             "        require_once '" $this->configFile . "';\n";
  268.         }
  269.  
  270.         $this->testClass .=
  271.           "        require_once '" $this->classPath . "';\n" .
  272.           "        \$this->" $this->className . " =& new " $this->className . "(PARAM);\n" .
  273.           "    }\n\n";
  274.  
  275.         // Create the tearDown method.
  276.         $this->testClass .=
  277.           "    function tearDown()\n" .
  278.           "    {\n" .
  279.           "        unset(\$this->" $this->className . ");\n" .
  280.           "    }\n\n";
  281.     }
  282.  
  283.     /**
  284.      * Create a basic skeleton for test methods.
  285.      *
  286.      * This code is taken from the PHPUnit documentation.
  287.      *
  288.      * All of the code needed for the new class is stored in the
  289.      * testClass member.
  290.      *
  291.      * @access private
  292.      * @param  none 
  293.      * @return void 
  294.      */
  295.     function _createMethod($methodName{
  296.         // Create a test method.
  297.         $this->testClass .=
  298.           "    function test" $methodName "()\n" .
  299.           "    {\n" .
  300.           "        \$result   = \$this->" $this->className . "->" $methodName "(PARAM);\n" .
  301.           "        \$expected = EXPECTED_VAL;\n" .
  302.           "        \$this->assertEquals(\$expected, \$result);\n" .
  303.           "    }\n\n";
  304.     }
  305.  
  306.     /**
  307.      * Add the closing brace needed for a proper class definition.
  308.      *
  309.      * All of the code needed for the new class is stored in the
  310.      * testClass member.
  311.      *
  312.      * @access private
  313.      * @param  none 
  314.      * @return void 
  315.      */
  316.     function _finishClass({
  317.         // Close off the class.
  318.         $this->testClass.= "}\n";
  319.     }
  320.  
  321.     /**
  322.      * Create the code that will actually run the test.
  323.      *
  324.      * This code is added by default so that the test can be run
  325.      * just by running the file. To have it not added pass false
  326.      * as the second parameter to the writeTestClass method.
  327.      * This code is taken from the PHPUnit documentation.
  328.      *
  329.      * All of the code needed for the new class is stored in the
  330.      * testClass member.
  331.      *
  332.      * @access private
  333.      * @param  none 
  334.      * @return void 
  335.      */
  336.     function _createTest({
  337.         // Create a call to the test.
  338.         $test =
  339.           "// Running the test.\n" .
  340.           "\$suite  = new PHPUnit_TestSuite('" $this->className . "Test');\n" .
  341.           "\$result = PHPUnit::run(\$suite);\n" .
  342.           "echo \$result->toString();\n";
  343.  
  344.         return $test;
  345.     }
  346.  
  347.     /**
  348.      * Write the test class to file.
  349.      *
  350.      * This will write the test class created using the createTestClass
  351.      * method to a file called <className>Test.php. By default the file
  352.      * is written to the current directory and will have code to run
  353.      * the test appended to the bottom of the file.
  354.      *
  355.      * @access public
  356.      * @param  string  $destination The directory to write the file to.
  357.      * @param  boolean $addTest     Wheter to add the test running code.
  358.      * @return void 
  359.      */
  360.     function writeTestClass($destination './'$addTest = TRUE{
  361.         // Check for something to write to file.
  362.         if (!isset($this->testClass)) {
  363.             $this->_handleErrors('Noting to write.'PHPUS_WARNING);
  364.             return;
  365.         }
  366.  
  367.         // Open the destination file.
  368.         $fp fopen($destination $this->className . 'Test.php''w');
  369.         fwrite($fp"<?php\n");
  370.  
  371.         // Write the test class.
  372.         fwrite($fp$this->testClass);
  373.  
  374.         // Add the call to test the class in the file if we were asked to.
  375.         if ($addTest{
  376.             fwrite($fp$this->_createTest());
  377.         }
  378.  
  379.         // Close the file.
  380.         fwrite($fp"?>\n");
  381.         fclose($fp);
  382.     }
  383.  
  384.     /**
  385.      * Error handler.
  386.      *
  387.      * This method should be rewritten to use the prefered error
  388.      * handling method. (PEAR_ErrorStack)
  389.      *
  390.      * @access private
  391.      * @param  string  $message The error message.
  392.      * @param  integer $type    An indication of the severity of the error.
  393.      * @return void             Code may cause PHP to exit.
  394.      */
  395.     function _handleErrors($message$type = E_USER_ERROR{
  396.         // For now just echo the message.
  397.         echo $message;
  398.  
  399.         // Check to see if we should quit.
  400.         if ($type == E_USER_ERROR{
  401.             exit;
  402.         }
  403.     }
  404. }
  405. ?>

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