Source for file Skeleton.php
Documentation is available at Skeleton.php
// +------------------------------------------------------------------------+
// +------------------------------------------------------------------------+
// | Copyright (c) 2002-2004 Sebastian Bergmann <sb@sebastian-bergmann.de>. |
// +------------------------------------------------------------------------+
// | This source file is subject to version 3.00 of the PHP License, |
// | that is available at http://www.php.net/license/3_0.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +------------------------------------------------------------------------+
// $Id: Skeleton.php,v 1.6 2004/12/22 08:06:11 sebastian Exp $
* Class for creating a PHPUnit_TestCase skeleton file.
* This class will take a classname as a parameter on construction and will
* create a PHP file that contains the skeleton of a PHPUnit_TestCase
* subclass. The test case will contain a test foreach method of the class.
* Methods of the parent class will, by default, be excluded from the test
* class. Passing and optional construction parameter will include them.
* require_once 'PHPUnit/Skeleton.php';
* $ps = new PHPUnit_Skeleton('PHPUnit_Skeleton', 'PHPUnit/Skeleton.php');
* // Generate the test class.
* // Default settings will not include any parent class methods, but
* // will include private methods.
* $ps->createTestClass();
* // Write the new test class to file.
* // By default, code to run the test will be included.
* Now open the skeleton class and fill in the details.
* If you run the test as is, all tests will fail and
* you will see plenty of undefined constant errors.
* @author Scott Mattocks <scott@crisscott.com>
* @copyright Copyright © 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>
* @license http://www.php.net/license/3_0.txt The PHP License, Version 3.0
* Path to the class file to create a skeleton for.
* Path to the configuration file needed by class to test.
* Whether or not to include the methods of the parent class when testing.
* Whether or not to test private methods.
* The test class that will be created.
* Constructor. Sets the class members and check that the class
* @param string $className
* @param string $classPath
* @param boolean $includeParents Wheter to include the parent's methods in the test.
function PHPUnit_Skeleton($className, $classPath, $includeParents = FALSE , $includePrivate = TRUE ) {
$this->_handleErrors ($classPath . ' is not readable. Cannot create test class.');
// Do we want to include parent methods?
// Do we want to allow private methods?
* The class to test may require a special config file before it can be
* instantiated. This method lets you set that file.
* @param string $configPath
// Check that the file is readable
$this->_handleErrors ($configFile . ' is not readable. Cannot create test class.');
* Create the code that will be the skeleton of the test case.
* The test case must have a clss definition, one var, a constructor
* setUp, tearDown, and methods. Optionally and by default the code
* to run the test is added when the class is written to file.
// Instantiate the object.
// Remove the parent methods if needed.
if (count($parentMethods)) {
$classMethods = array_diff($classMethods, $parentMethods);
// Create the class definition, constructor, setUp and tearDown.
$this->_createDefinition ();
$this->_createConstructor ();
$this->_createSetUpTearDown ();
if (count($classMethods)) {
// Foreach method create a test case.
foreach ($classMethods as $method) {
// Unless it is the constructor.
// Check for private methods.
$this->_createMethod ($method);
* Create the class definition.
* The definition consist of a header comment, require statment
* for getting the PHPUnit file, the actual class definition,
* and the definition of the class member variable.
* All of the code needed for the new class is stored in the
function _createDefinition () {
// Create header comment.
" * PHPUnit test case for " . $this->className . "\n" .
" * The method skeletons below need to be filled in with \n" .
" * real data so that the tests will run correctly. Replace \n" .
" * all EXPECTED_VAL and PARAM strings with real data. \n" .
" * Created with PHPUnit_Skeleton on " . date('Y-m-d') . "\n" .
// Add the require statements.
$this->testClass .= "require_once 'PHPUnit.php';\n";
// Add the class definition and variable definition.
"class " . $this->className . "Test extends PHPUnit_TestCase {\n\n" .
* Create the class constructor. (PHP4 style)
* The constructor simply calls the PHPUnit_TestCase method.
* This code is taken from the PHPUnit documentation.
* All of the code needed for the new class is stored in the
function _createConstructor () {
// Create the test class constructor.
" function " . $this->className . "Test(\$name)\n" .
" \$this->PHPUnit_TestCase(\$name);\n" .
* Create setUp and tearDown methods.
* The setUp method creates the instance of the object to test.
* The tearDown method releases the instance.
* This code is taken from the PHPUnit documentation.
* All of the code needed for the new class is stored in the
function _createSetUpTearDown () {
// Create the setUp method.
" require_once '" . $this->classPath . "';\n" .
// Create the tearDown method.
" function tearDown()\n" .
" unset(\$this->" . $this->className . ");\n" .
* Create a basic skeleton for test methods.
* This code is taken from the PHPUnit documentation.
* All of the code needed for the new class is stored in the
function _createMethod ($methodName) {
" function test" . $methodName . "()\n" .
" \$result = \$this->" . $this->className . "->" . $methodName . "(PARAM);\n" .
" \$expected = EXPECTED_VAL;\n" .
" \$this->assertEquals(\$expected, \$result);\n" .
* Add the closing brace needed for a proper class definition.
* All of the code needed for the new class is stored in the
function _finishClass () {
* Create the code that will actually run the test.
* This code is added by default so that the test can be run
* just by running the file. To have it not added pass false
* as the second parameter to the writeTestClass method.
* This code is taken from the PHPUnit documentation.
* All of the code needed for the new class is stored in the
// Create a call to the test.
"// Running the test.\n" .
"\$suite = new PHPUnit_TestSuite('" . $this->className . "Test');\n" .
"\$result = PHPUnit::run(\$suite);\n" .
"echo \$result->toString();\n";
* Write the test class to file.
* This will write the test class created using the createTestClass
* method to a file called <className>Test.php. By default the file
* is written to the current directory and will have code to run
* the test appended to the bottom of the file.
* @param string $destination The directory to write the file to.
* @param boolean $addTest Wheter to add the test running code.
// Check for something to write to file.
$this->_handleErrors ('Noting to write.', PHPUS_WARNING );
// Open the destination file.
// Add the call to test the class in the file if we were asked to.
fwrite($fp, $this->_createTest ());
* This method should be rewritten to use the prefered error
* handling method. (PEAR_ErrorStack)
* @param string $message The error message.
* @param integer $type An indication of the severity of the error.
* @return void Code may cause PHP to exit.
function _handleErrors ($message, $type = E_USER_ERROR ) {
// For now just echo the message.
// Check to see if we should quit.
if ($type == E_USER_ERROR ) {
Documentation generated on Mon, 11 Mar 2019 14:22:35 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|