Cradle  0.3.3
Simple library for creating Web-based applications
Autogen Class Reference

Digger simple automatic generator of PHPUnit tests. More...

Public Member Functions

 __construct ($config=NULL, $generateNow=false)
 
 stripFunctionParams ($stringParams)
 
 generateCode ($sourceCode, $sourceFileName="", $resultFileName="NonameClass", $currentContent=NULL)
 
 generateFile ($sourceFile, $outFile)
 
 generate ($config=NULL)
 
 getResult ()
 
 generateRunFile ($destinations, $destinationRoot)
 

Static Public Member Functions

static placeAfter ($sourceText, $newText, $findPattern=NULL, $afterKeys=array('<\?php'))
 

Data Fields

 $testClassName = "[NAME]Test"
 
 $testName = "test_[NAME]"
 
 $excludeNames = [ "^__" ]
 
 $testNameSpace = ""
 
 $templateHeader
 
 $templateComments
 
 $templateClass
 
 $templateTest
 
 $sourcePaths = [ "./" ]
 
 $sourcePatterns = [ '\.php$' ]
 
 $sourceExclude = [ '^_', 'Test\.php$' ]
 
 $sourceRecursive = true
 
 $destinationRoot = "./tests"
 
 $includeRelativePath = true
 
 $destinationTree = true
 
 $testFileName = "[NAME]Test.php"
 
 $runFileName = "run.sh"
 
 $phpUnitFile = "phpunit"
 
 $runFileHeaderTemplate
 
 $runFileLineTemplate
 
 $includeAutoloaderFileName = ""
 

Detailed Description

Digger simple automatic generator of PHPUnit tests.

Generates PHPUnit test skeletons for PHP files with classes and functions.

Version
2.0
Author
Digger mrdig.nosp@m.ger@.nosp@m.sad-s.nosp@m.yste.nosp@m.ms.ru

Example of usage:

require_once 'autoload.php';
use digger\cradle\common\Autogen;
try {
$config = array( ... hash of properties values ... );
$autogen = new Autogen($config, true);
print_r($autogen->getResult());
} catch (Exception $e) {
echo "ERROR: " . $e->getMessage() . "\n";
}
Exceptions
Exception

Constructor & Destructor Documentation

__construct (   $config = NULL,
  $generateNow = false 
)

Class constructor

Parameters
hash | string$configHash array with values of class properties, or config file name contains it.
boolean$generateNowGenerate tests on create.

Member Function Documentation

generate (   $config = NULL)

Generate all tastes by input config or by class default values

Parameters
hash$configConfig params (class properties values)
Exceptions
Exception
Returns
array An array of generated files. (Array structure see in getResult)
See also
getResult
generateCode (   $sourceCode,
  $sourceFileName = "",
  $resultFileName = "NonameClass",
  $currentContent = NULL 
)

Generate code with PHPUnit tests for classes and functions of source code

Parameters
string$sourceCodePHP-file source code.
string$sourceFileName(Option) needed to create "require" directive.
string$resultFileName(Option) needed to create class name for stanalong functions and to create "require" directive with relative path.
string$currentContent(Option) needed to insert result code inside an existing content.
Returns
string Result code of PHP-file contains tests.
generateFile (   $sourceFile,
  $outFile 
)

Generate file with PHP-unit tests

Parameters
string$sourceFileInput file name.
string$outFileOutput file name.
Exceptions
Exception
generateRunFile (   $destinations,
  $destinationRoot 
)

Generate common file body to execute all tests

Parameters
array$destinationsList of destination file names.
string$destinationRootRoot path for destinations.
Returns
string A source code (text) of run-file.
getResult ( )

Returns a result of generation

Returns
array Array of structure:
array (
'destinations' => array( 'filename' => 'test|run', ... )
'errors' => array( 'filename' => 'error message', ... )
)
static placeAfter (   $sourceText,
  $newText,
  $findPattern = NULL,
  $afterKeys = array('<\?php') 
)
static

To place a some new text into the source text after a first found "key pattern"

Parameters
string$sourceTextA source text.
string$newTextA new text to insert.
string$findPatternRegular expression to find duplicates of $newText and to cancel insertion. If $findPattern=NULL $newText will inserted without check and duplicate may occur.
array$afterKeysThe $newText will inserted after first found "key pattern" from $afterKeys patterns.
Returns
string Result text.
stripFunctionParams (   $stringParams)

Strip function parameters. To clear any symbols befor "$paramName" (such as '&' or 'typeOfParameter')

Parameters
string$stringParamsString of function parameters.
Returns
string Stripped string.

Field Documentation

$destinationRoot = "./tests"

string Root of destination path

$destinationTree = true

boolean Create output sub directories

$excludeNames = [ "^__" ]

string Array of regular expressions to exclude some methods (such as: __construct, __destruct, and so on ...)

$includeAutoloaderFileName = ""

string Include 'requrie_once' directive in out test file

$includeRelativePath = true

boolean Use relative path of source file in "include_once" directive

$phpUnitFile = "phpunit"

string PHPUnit exec file

$runFileHeaderTemplate
Initial value:
= "#!/bin/sh
echo
echo '*=============================================================================='
echo '* Title : Run tests. This file was created by [GENERATOR_NAME]'
echo '* Author : Digger (c) SAD-Systems <http://sad-systems.ru>'
echo '* Created on : [DATE]'
echo '*=============================================================================='
echo
cd [RUN_DIR]
"

string Template for output run file

$runFileLineTemplate
Initial value:
= '
echo ______________________________________________________________________________
echo
echo Run test: [[TEST_TITLE]] from [[TEST_FILE]]
echo
[PHPUNIT] --log-junit [TEST_LOG].xml [TEST_NAME] [TEST_FILE]'

string Template for output run file line

$runFileName = "run.sh"

string Run file name

$sourceExclude = [ '^_', 'Test\.php$' ]

array Array of regexp patterns to exclude some file names

$sourcePaths = [ "./" ]

array Source paths to find all source files

$sourcePatterns = [ '\.php$' ]

array Array of regexp patterns to find only necessary file names

$sourceRecursive = true

boolean Searching for source files recursively

$templateClass
Initial value:
= "\n
class [TEST_CLASS_NAME] extends PHPUnit_Framework_TestCase
{
}\n"

string Class body template for output PHP file

$templateComments
Initial value:
=" *
* PHPUnit tests for:
*
* [CLASS_LIST]
*
* contains in file:
*
* [SOURCE_FILE]
*
*------------------------------------------------------------------------------
"

string Template for some system comments

$templateHeader
Initial value:
= "<?php
/*==============================================================================
* Title : PHPUnit test. This file was created by [GENERATOR_NAME]
* Author : Digger (c) SAD-Systems <http://sad-systems.ru>
* Created on : [DATE]
* Updated on : [DATE]
*==============================================================================
*/\n"

string Header template for output PHP file

$templateTest
Initial value:
='
public function [TEST_NAME]()
{
[CREATE]
$this->assertEquals( [INSTANCE], true );
}
'

string Class method/function body template for output PHP file

$testClassName = "[NAME]Test"

string Class name template for test

$testFileName = "[NAME]Test.php"

string Template for file name of test

$testName = "test_[NAME]"

string Method name template for test

$testNameSpace = ""

string Namespace for test

Warning
Do not recomended to change this default value!

The documentation for this class was generated from the following file: