2008-05-30 11:40:08 +00:00
< ? php
/**
* The TestTask handles creating and updating test files .
*
2009-11-06 06:46:59 +00:00
* CakePHP ( tm ) : Rapid Development Framework ( http :// cakephp . org )
2013-02-08 11:59:49 +00:00
* Copyright ( c ) Cake Software Foundation , Inc . ( http :// cakefoundation . org )
2008-05-30 11:40:08 +00:00
*
* Licensed under The MIT License
2013-02-08 12:22:51 +00:00
* For full copyright and license information , please see the LICENSE . txt
2008-05-30 11:40:08 +00:00
* Redistributions of files must retain the above copyright notice .
*
2013-02-08 11:59:49 +00:00
* @ copyright Copyright ( c ) Cake Software Foundation , Inc . ( http :// cakefoundation . org )
2009-11-06 06:00:11 +00:00
* @ link http :// cakephp . org CakePHP ( tm ) Project
2009-07-17 03:55:41 +00:00
* @ since CakePHP ( tm ) v 1.3
2013-05-30 22:11:14 +00:00
* @ license http :// www . opensource . org / licenses / mit - license . php MIT License
2008-05-30 11:40:08 +00:00
*/
2009-07-24 19:18:37 +00:00
2011-11-25 21:05:00 +00:00
App :: uses ( 'AppShell' , 'Console/Command' );
2010-12-08 00:20:56 +00:00
App :: uses ( 'BakeTask' , 'Console/Command/Task' );
App :: uses ( 'ClassRegistry' , 'Utility' );
2010-03-05 02:57:48 +00:00
2008-05-30 11:40:08 +00:00
/**
* Task class for creating and updating test files .
*
2011-07-26 06:16:14 +00:00
* @ package Cake . Console . Command . Task
2008-05-30 11:40:08 +00:00
*/
2010-03-05 02:57:48 +00:00
class TestTask extends BakeTask {
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* path to TESTS directory
*
* @ var string
*/
2010-04-04 07:14:00 +00:00
public $path = TESTS ;
2009-05-25 00:12:17 +00:00
2009-05-25 03:50:21 +00:00
/**
* Tasks used .
*
* @ var array
2009-11-14 12:19:25 +00:00
*/
2010-04-04 07:14:00 +00:00
public $tasks = array ( 'Template' );
2009-07-24 19:18:37 +00:00
2009-05-25 00:12:17 +00:00
/**
* class types that methods can be generated for
*
* @ var array
2009-11-14 12:19:25 +00:00
*/
2012-03-03 23:55:07 +00:00
public $classTypes = array (
2011-04-11 01:19:16 +00:00
'Model' => 'Model' ,
'Controller' => 'Controller' ,
'Component' => 'Controller/Component' ,
'Behavior' => 'Model/Behavior' ,
'Helper' => 'View/Helper'
);
2009-07-24 19:18:37 +00:00
2012-08-16 00:50:11 +00:00
/**
* Mapping between packages , and their baseclass + package .
* This is used to generate App :: uses () call to autoload base
* classes if a developer has forgotten to do so .
*
* @ var array
*/
public $baseTypes = array (
'Model' => array ( 'Model' , 'Model' ),
'Behavior' => array ( 'ModelBehavior' , 'Model' ),
'Controller' => array ( 'Controller' , 'Controller' ),
'Component' => array ( 'Component' , 'Controller' ),
'Helper' => array ( 'Helper' , 'View' )
);
2009-05-25 03:50:21 +00:00
/**
* Internal list of fixtures that have been added so far .
*
2011-07-29 02:03:44 +00:00
* @ var array
2009-11-14 12:19:25 +00:00
*/
2010-04-04 06:36:12 +00:00
protected $_fixtures = array ();
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Execution method always used for tasks
*
2011-07-29 02:03:44 +00:00
* @ return void
2008-05-30 11:40:08 +00:00
*/
2010-04-05 03:19:38 +00:00
public function execute () {
2010-10-19 03:09:23 +00:00
parent :: execute ();
2012-09-14 18:26:00 +00:00
$count = count ( $this -> args );
if ( ! $count ) {
2010-04-24 01:57:59 +00:00
$this -> _interactive ();
2008-05-30 11:40:08 +00:00
}
2008-09-24 13:49:37 +00:00
2012-09-14 18:26:00 +00:00
if ( $count === 1 ) {
2010-04-24 01:57:59 +00:00
$this -> _interactive ( $this -> args [ 0 ]);
2008-05-30 11:40:08 +00:00
}
2012-09-14 18:26:00 +00:00
if ( $count > 1 ) {
2012-04-29 15:16:29 +00:00
$type = Inflector :: classify ( $this -> args [ 0 ]);
2009-05-25 03:50:21 +00:00
if ( $this -> bake ( $type , $this -> args [ 1 ])) {
2010-10-13 02:25:04 +00:00
$this -> out ( '<success>Done</success>' );
2008-05-30 11:40:08 +00:00
}
}
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Handles interactive baking
*
2011-07-29 02:03:44 +00:00
* @ param string $type
* @ return string | boolean
2008-05-30 11:40:08 +00:00
*/
2010-04-24 01:57:59 +00:00
protected function _interactive ( $type = null ) {
2009-05-25 03:50:21 +00:00
$this -> interactive = true ;
2008-05-30 11:40:08 +00:00
$this -> hr ();
2011-03-19 17:32:35 +00:00
$this -> out ( __d ( 'cake_console' , 'Bake Tests' ));
2012-02-05 17:00:30 +00:00
$this -> out ( __d ( 'cake_console' , 'Path: %s' , $this -> getPath ()));
2008-05-30 11:40:08 +00:00
$this -> hr ();
2008-09-24 13:49:37 +00:00
2009-05-25 00:12:17 +00:00
if ( $type ) {
$type = Inflector :: camelize ( $type );
2011-04-11 01:19:16 +00:00
if ( ! isset ( $this -> classTypes [ $type ])) {
$this -> error ( __d ( 'cake_console' , 'Incorrect type provided. Please choose one of %s' , implode ( ', ' , array_keys ( $this -> classTypes ))));
2008-05-30 11:40:08 +00:00
}
2010-05-06 11:31:52 +00:00
} else {
2009-05-25 03:50:21 +00:00
$type = $this -> getObjectType ();
2009-05-25 00:12:17 +00:00
}
2009-05-25 03:50:21 +00:00
$className = $this -> getClassName ( $type );
return $this -> bake ( $type , $className );
2008-05-30 11:40:08 +00:00
}
2009-07-24 19:18:37 +00:00
2009-05-25 00:12:17 +00:00
/**
2009-05-25 03:50:21 +00:00
* Completes final steps for generating data to create test case .
2009-05-25 00:12:17 +00:00
*
2009-05-25 03:50:21 +00:00
* @ param string $type Type of object to bake test case for ie . Model , Controller
2009-07-24 19:18:37 +00:00
* @ param string $className the 'cake name' for the class ie . Posts for the PostsController
2011-07-29 02:03:44 +00:00
* @ return string | boolean
2009-05-25 03:50:21 +00:00
*/
2010-04-05 03:19:38 +00:00
public function bake ( $type , $className ) {
2011-10-03 01:46:35 +00:00
$plugin = null ;
if ( $this -> plugin ) {
$plugin = $this -> plugin . '.' ;
}
$realType = $this -> mapType ( $type , $plugin );
$fullClassName = $this -> getRealClassName ( $type , $className );
if ( $this -> typeCanDetectFixtures ( $type ) && $this -> isLoadableClass ( $realType , $fullClassName )) {
2011-03-19 17:32:35 +00:00
$this -> out ( __d ( 'cake_console' , 'Bake is detecting possible fixtures...' ));
2010-11-13 04:05:44 +00:00
$testSubject = $this -> buildTestSubject ( $type , $className );
2009-05-25 03:50:21 +00:00
$this -> generateFixtureList ( $testSubject );
} elseif ( $this -> interactive ) {
$this -> getUserFixtures ();
}
2012-08-16 00:50:11 +00:00
list ( $baseClass , $baseType ) = $this -> getBaseType ( $type );
App :: uses ( $baseClass , $baseType );
2011-10-03 01:46:35 +00:00
App :: uses ( $fullClassName , $realType );
2009-05-25 00:12:17 +00:00
2009-05-25 03:50:21 +00:00
$methods = array ();
if ( class_exists ( $fullClassName )) {
$methods = $this -> getTestableMethods ( $fullClassName );
2009-05-25 00:12:17 +00:00
}
2009-05-26 03:48:41 +00:00
$mock = $this -> hasMockClass ( $type , $fullClassName );
2012-05-10 22:33:10 +00:00
list ( $preConstruct , $construction , $postConstruct ) = $this -> generateConstructor ( $type , $fullClassName , $plugin );
2012-01-27 03:44:01 +00:00
$uses = $this -> generateUses ( $type , $realType , $fullClassName );
2009-05-25 04:54:23 +00:00
2011-04-25 19:17:59 +00:00
$this -> out ( " \n " . __d ( 'cake_console' , 'Baking test case for %s %s ...' , $className , $type ), 1 , Shell :: QUIET );
2009-05-25 03:50:21 +00:00
$this -> Template -> set ( 'fixtures' , $this -> _fixtures );
2009-05-25 04:54:23 +00:00
$this -> Template -> set ( 'plugin' , $plugin );
2011-10-03 01:46:35 +00:00
$this -> Template -> set ( compact (
2011-10-28 05:01:17 +00:00
'className' , 'methods' , 'type' , 'fullClassName' , 'mock' ,
2012-01-27 03:44:01 +00:00
'realType' , 'preConstruct' , 'postConstruct' , 'construction' ,
'uses'
2011-10-03 01:46:35 +00:00
));
2009-07-01 04:50:38 +00:00
$out = $this -> Template -> generate ( 'classes' , 'test' );
2009-05-25 03:50:21 +00:00
2009-06-07 01:03:26 +00:00
$filename = $this -> testCaseFileName ( $type , $className );
$made = $this -> createFile ( $filename , $out );
2009-05-25 03:50:21 +00:00
if ( $made ) {
return $out ;
}
return false ;
2008-05-30 11:40:08 +00:00
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
2009-05-25 03:50:21 +00:00
* Interact with the user and get their chosen type . Can exit the script .
2008-05-30 11:40:08 +00:00
*
2009-05-25 03:50:21 +00:00
* @ return string Users chosen type .
2009-11-14 12:19:25 +00:00
*/
2010-04-05 03:19:38 +00:00
public function getObjectType () {
2009-05-25 03:50:21 +00:00
$this -> hr ();
2011-03-19 17:32:35 +00:00
$this -> out ( __d ( 'cake_console' , 'Select an object type:' ));
2009-05-25 03:50:21 +00:00
$this -> hr ();
$keys = array ();
2011-04-11 01:19:16 +00:00
$i = 0 ;
foreach ( $this -> classTypes as $option => $package ) {
$this -> out ( ++ $i . '. ' . $option );
$keys [] = $i ;
2008-05-30 11:40:08 +00:00
}
2009-05-25 03:50:21 +00:00
$keys [] = 'q' ;
2011-03-19 17:32:35 +00:00
$selection = $this -> in ( __d ( 'cake_console' , 'Enter the type of object to bake a test for or (q)uit' ), $keys , 'q' );
2013-02-12 02:38:08 +00:00
if ( $selection === 'q' ) {
2009-05-25 03:50:21 +00:00
return $this -> _stop ();
}
2011-04-11 01:19:16 +00:00
$types = array_keys ( $this -> classTypes );
return $types [ $selection - 1 ];
2009-05-25 03:50:21 +00:00
}
2009-07-24 19:18:37 +00:00
2009-05-25 03:50:21 +00:00
/**
* Get the user chosen Class name for the chosen type
*
* @ param string $objectType Type of object to list classes for i . e . Model , Controller .
* @ return string Class name the user chose .
2009-11-14 12:19:25 +00:00
*/
2010-04-05 03:19:38 +00:00
public function getClassName ( $objectType ) {
2011-10-03 16:24:40 +00:00
$type = ucfirst ( strtolower ( $objectType ));
$typeLength = strlen ( $type );
$type = $this -> classTypes [ $type ];
2010-12-14 03:00:37 +00:00
if ( $this -> plugin ) {
2011-04-10 19:32:19 +00:00
$plugin = $this -> plugin . '.' ;
$options = App :: objects ( $plugin . $type );
2010-12-14 03:00:37 +00:00
} else {
$options = App :: objects ( $type );
}
2011-03-19 17:32:35 +00:00
$this -> out ( __d ( 'cake_console' , 'Choose a %s class' , $objectType ));
2009-05-25 03:50:21 +00:00
$keys = array ();
foreach ( $options as $key => $option ) {
$this -> out ( ++ $key . '. ' . $option );
$keys [] = $key ;
}
2011-11-17 01:38:31 +00:00
while ( empty ( $selection )) {
$selection = $this -> in ( __d ( 'cake_console' , 'Choose an existing class, or enter the name of a class that does not exist' ));
if ( is_numeric ( $selection ) && isset ( $options [ $selection - 1 ])) {
$selection = $options [ $selection - 1 ];
}
if ( $type !== 'Model' ) {
$selection = substr ( $selection , 0 , $typeLength * - 1 );
}
2009-05-25 03:50:21 +00:00
}
return $selection ;
}
2009-07-24 19:18:37 +00:00
2009-05-25 03:50:21 +00:00
/**
* Checks whether the chosen type can find its own fixtures .
* Currently only model , and controller are supported
2011-04-17 16:17:25 +00:00
*
2009-12-30 03:50:43 +00:00
* @ param string $type The Type of object you are generating tests for eg . controller
2009-05-25 03:50:21 +00:00
* @ return boolean
2009-11-14 12:19:25 +00:00
*/
2010-04-05 03:19:38 +00:00
public function typeCanDetectFixtures ( $type ) {
2009-05-25 03:50:21 +00:00
$type = strtolower ( $type );
2011-10-03 01:46:35 +00:00
return in_array ( $type , array ( 'controller' , 'model' ));
2009-05-25 03:50:21 +00:00
}
2009-07-24 19:18:37 +00:00
2009-05-25 03:50:21 +00:00
/**
2011-10-03 01:46:35 +00:00
* Check if a class with the given package is loaded or can be loaded .
2009-05-25 03:50:21 +00:00
*
2011-10-03 01:46:35 +00:00
* @ param string $package The package of object you are generating tests for eg . controller
2011-07-29 02:03:44 +00:00
* @ param string $class the Classname of the class the test is being generated for .
2009-05-25 03:50:21 +00:00
* @ return boolean
2009-11-14 12:19:25 +00:00
*/
2011-10-03 01:46:35 +00:00
public function isLoadableClass ( $package , $class ) {
App :: uses ( $class , $package );
2012-05-10 21:29:47 +00:00
list ( $plugin , $ns ) = pluginSplit ( $package );
if ( $plugin ) {
App :: uses ( " { $plugin } AppController " , $package );
App :: uses ( " { $plugin } AppModel " , $package );
App :: uses ( " { $plugin } AppHelper " , $package );
}
2011-10-03 01:46:35 +00:00
return class_exists ( $class );
2009-05-25 03:50:21 +00:00
}
2009-07-24 19:18:37 +00:00
2009-05-25 03:50:21 +00:00
/**
* Construct an instance of the class to be tested .
2009-05-25 04:54:23 +00:00
* So that fixtures can be detected
2009-05-25 03:50:21 +00:00
*
2009-12-30 03:50:43 +00:00
* @ param string $type The Type of object you are generating tests for eg . controller
* @ param string $class the Classname of the class the test is being generated for .
* @ return object And instance of the class that is going to be tested .
2009-11-14 12:19:25 +00:00
*/
2013-04-16 01:23:48 +00:00
public function buildTestSubject ( $type , $class ) {
2009-05-26 04:36:25 +00:00
ClassRegistry :: flush ();
2013-09-25 17:40:14 +00:00
App :: uses ( $class , $type );
2009-05-25 03:50:21 +00:00
$class = $this -> getRealClassName ( $type , $class );
2013-02-12 02:38:08 +00:00
if ( strtolower ( $type ) === 'model' ) {
2010-11-13 04:05:44 +00:00
$instance = ClassRegistry :: init ( $class );
2009-05-25 03:50:21 +00:00
} else {
2010-11-13 04:05:44 +00:00
$instance = new $class ();
2009-05-25 03:50:21 +00:00
}
return $instance ;
}
2009-07-24 19:18:37 +00:00
2009-05-25 03:50:21 +00:00
/**
2011-10-03 01:46:35 +00:00
* Gets the real class name from the cake short form . If the class name is already
* suffixed with the type , the type will not be duplicated .
2009-05-25 03:50:21 +00:00
*
2009-12-30 03:50:43 +00:00
* @ param string $type The Type of object you are generating tests for eg . controller
* @ param string $class the Classname of the class the test is being generated for .
2013-10-23 02:59:50 +00:00
* @ return string Real class name
2009-11-14 12:19:25 +00:00
*/
2010-04-05 03:19:38 +00:00
public function getRealClassName ( $type , $class ) {
2013-02-12 02:38:08 +00:00
if ( strtolower ( $type ) === 'model' || empty ( $this -> classTypes [ $type ])) {
2009-05-25 03:50:21 +00:00
return $class ;
}
2012-03-03 23:55:07 +00:00
2012-02-28 14:58:28 +00:00
$position = strpos ( $class , $type );
2012-03-03 23:55:07 +00:00
2014-04-29 12:19:33 +00:00
if ( $position !== false && ( strlen ( $class ) - $position ) === strlen ( $type )) {
2011-10-03 01:46:35 +00:00
return $class ;
}
2009-05-25 03:50:21 +00:00
return $class . $type ;
2008-05-30 11:40:08 +00:00
}
2009-07-24 19:18:37 +00:00
2011-10-03 01:46:35 +00:00
/**
* Map the types that TestTask uses to concrete types that App :: uses can use .
*
* @ param string $type The type of thing having a test generated .
* @ param string $plugin The plugin name .
* @ return string
2012-03-03 23:55:07 +00:00
* @ throws CakeException When invalid object types are requested .
2011-10-03 01:46:35 +00:00
*/
public function mapType ( $type , $plugin ) {
$type = ucfirst ( $type );
if ( empty ( $this -> classTypes [ $type ])) {
throw new CakeException ( __d ( 'cake_dev' , 'Invalid object type.' ));
}
$real = $this -> classTypes [ $type ];
if ( $plugin ) {
$real = trim ( $plugin , '.' ) . '.' . $real ;
}
return $real ;
}
2012-08-16 00:50:11 +00:00
/**
* Get the base class and package name for a given type .
*
2012-09-01 11:14:15 +00:00
* @ param string $type The type the class having a test
2012-08-16 00:50:11 +00:00
* generated for is in .
2012-09-01 11:14:15 +00:00
* @ return array Array of ( class , type )
2012-08-20 20:21:59 +00:00
* @ throws CakeException on invalid types .
2012-08-16 00:50:11 +00:00
*/
public function getBaseType ( $type ) {
if ( empty ( $this -> baseTypes [ $type ])) {
2012-08-20 20:21:59 +00:00
throw new CakeException ( __d ( 'cake_dev' , 'Invalid type name' ));
2012-08-16 00:50:11 +00:00
}
return $this -> baseTypes [ $type ];
}
2009-05-24 03:48:25 +00:00
/**
* Get methods declared in the class given .
* No parent methods will be returned
*
* @ param string $className Name of class to look at .
* @ return array Array of method names .
2009-11-14 12:19:25 +00:00
*/
2010-04-05 03:19:38 +00:00
public function getTestableMethods ( $className ) {
2009-05-24 03:48:25 +00:00
$classMethods = get_class_methods ( $className );
$parentMethods = get_class_methods ( get_parent_class ( $className ));
$thisMethods = array_diff ( $classMethods , $parentMethods );
$out = array ();
foreach ( $thisMethods as $method ) {
2013-02-12 02:38:08 +00:00
if ( substr ( $method , 0 , 1 ) !== '_' && $method != strtolower ( $className )) {
2009-05-24 03:48:25 +00:00
$out [] = $method ;
}
}
return $out ;
}
2009-07-24 19:18:37 +00:00
2009-05-24 05:15:31 +00:00
/**
2009-05-24 05:30:04 +00:00
* Generate the list of fixtures that will be required to run this test based on
2009-05-24 05:15:31 +00:00
* loaded models .
*
2009-12-30 03:50:43 +00:00
* @ param object $subject The object you want to generate fixtures for .
2009-05-24 05:15:31 +00:00
* @ return array Array of fixtures to be included in the test .
2009-11-14 12:19:25 +00:00
*/
2011-03-26 04:04:25 +00:00
public function generateFixtureList ( $subject ) {
2009-05-24 05:15:31 +00:00
$this -> _fixtures = array ();
2013-09-24 20:57:51 +00:00
if ( $subject instanceof Model ) {
2009-05-24 05:15:31 +00:00
$this -> _processModel ( $subject );
2013-09-24 20:57:51 +00:00
} elseif ( $subject instanceof Controller ) {
2009-05-24 05:15:31 +00:00
$this -> _processController ( $subject );
}
return array_values ( $this -> _fixtures );
}
2009-07-24 19:18:37 +00:00
2009-05-24 05:15:31 +00:00
/**
2009-05-24 05:30:04 +00:00
* Process a model recursively and pull out all the
2009-05-24 05:15:31 +00:00
* model names converting them to fixture names .
*
2009-12-30 03:50:43 +00:00
* @ param Model $subject A Model class to scan for associations and pull fixtures off of .
2009-05-24 05:15:31 +00:00
* @ return void
2009-11-14 12:19:25 +00:00
*/
2011-03-26 04:04:25 +00:00
protected function _processModel ( $subject ) {
2009-05-24 05:15:31 +00:00
$this -> _addFixture ( $subject -> name );
$associated = $subject -> getAssociated ();
foreach ( $associated as $alias => $type ) {
$className = $subject -> { $alias } -> name ;
if ( ! isset ( $this -> _fixtures [ $className ])) {
$this -> _processModel ( $subject -> { $alias });
}
2013-02-12 02:38:08 +00:00
if ( $type === 'hasAndBelongsToMany' ) {
2011-11-24 12:31:50 +00:00
if ( ! empty ( $subject -> hasAndBelongsToMany [ $alias ][ 'with' ])) {
2013-01-23 12:45:50 +00:00
list (, $joinModel ) = pluginSplit ( $subject -> hasAndBelongsToMany [ $alias ][ 'with' ]);
2011-11-24 12:31:50 +00:00
} else {
$joinModel = Inflector :: classify ( $subject -> hasAndBelongsToMany [ $alias ][ 'joinTable' ]);
}
2009-05-24 05:15:31 +00:00
if ( ! isset ( $this -> _fixtures [ $joinModel ])) {
$this -> _processModel ( $subject -> { $joinModel });
}
}
}
}
2009-07-24 19:18:37 +00:00
2009-05-24 05:30:04 +00:00
/**
* Process all the models attached to a controller
* and generate a fixture list .
*
2009-12-30 03:50:43 +00:00
* @ param Controller $subject A controller to pull model names off of .
2009-05-24 05:30:04 +00:00
* @ return void
2009-11-14 12:19:25 +00:00
*/
2011-03-26 04:04:25 +00:00
protected function _processController ( $subject ) {
2009-05-24 05:30:04 +00:00
$subject -> constructClasses ();
$models = array ( Inflector :: classify ( $subject -> name ));
if ( ! empty ( $subject -> uses )) {
$models = $subject -> uses ;
}
foreach ( $models as $model ) {
2013-01-23 12:45:50 +00:00
list (, $model ) = pluginSplit ( $model );
2009-05-24 05:30:04 +00:00
$this -> _processModel ( $subject -> { $model });
}
}
2009-07-24 19:18:37 +00:00
2009-05-24 05:15:31 +00:00
/**
2013-10-23 02:59:50 +00:00
* Add class name to the fixture list .
2009-05-24 05:15:31 +00:00
* Sets the app . or plugin . plugin_name . prefix .
*
2009-12-30 03:50:43 +00:00
* @ param string $name Name of the Model class that a fixture might be required for .
2009-05-24 05:15:31 +00:00
* @ return void
2009-11-14 12:19:25 +00:00
*/
2010-04-05 03:21:28 +00:00
protected function _addFixture ( $name ) {
2012-06-01 18:38:38 +00:00
if ( $this -> plugin ) {
$prefix = 'plugin.' . Inflector :: underscore ( $this -> plugin ) . '.' ;
} else {
$prefix = 'app.' ;
2009-05-24 05:15:31 +00:00
}
$fixture = $prefix . Inflector :: underscore ( $name );
$this -> _fixtures [ $name ] = $fixture ;
}
2009-07-24 19:18:37 +00:00
2009-05-25 03:50:21 +00:00
/**
* Interact with the user to get additional fixtures they want to use .
*
2009-12-30 03:50:43 +00:00
* @ return array Array of fixtures the user wants to add .
2009-11-14 12:19:25 +00:00
*/
2010-04-05 03:19:38 +00:00
public function getUserFixtures () {
2011-12-01 07:21:31 +00:00
$proceed = $this -> in ( __d ( 'cake_console' , 'Bake could not detect fixtures, would you like to add some?' ), array ( 'y' , 'n' ), 'n' );
2009-05-25 03:50:21 +00:00
$fixtures = array ();
2013-02-12 02:38:08 +00:00
if ( strtolower ( $proceed ) === 'y' ) {
2011-03-19 17:32:35 +00:00
$fixtureList = $this -> in ( __d ( 'cake_console' , " Please provide a comma separated list of the fixtures names you'd like to use. \n Example: 'app.comment, app.post, plugin.forums.post' " ));
2009-05-25 03:50:21 +00:00
$fixtureListTrimmed = str_replace ( ' ' , '' , $fixtureList );
$fixtures = explode ( ',' , $fixtureListTrimmed );
}
$this -> _fixtures = array_merge ( $this -> _fixtures , $fixtures );
return $fixtures ;
}
2009-07-24 19:18:37 +00:00
2009-05-25 04:54:23 +00:00
/**
2009-05-26 03:48:41 +00:00
* Is a mock class required for this type of test ?
* Controllers require a mock class .
2009-05-25 04:54:23 +00:00
*
2009-12-30 03:50:43 +00:00
* @ param string $type The type of object tests are being generated for eg . controller .
2009-05-26 03:48:41 +00:00
* @ return boolean
2009-11-14 12:19:25 +00:00
*/
2010-04-05 03:19:38 +00:00
public function hasMockClass ( $type ) {
2009-05-26 03:48:41 +00:00
$type = strtolower ( $type );
2013-02-12 02:38:08 +00:00
return $type === 'controller' ;
2009-05-25 04:54:23 +00:00
}
2009-07-24 19:18:37 +00:00
2009-05-25 04:54:23 +00:00
/**
2013-10-23 02:59:50 +00:00
* Generate a constructor code snippet for the type and class name
2009-05-25 04:54:23 +00:00
*
2009-12-30 03:50:43 +00:00
* @ param string $type The Type of object you are generating tests for eg . controller
2011-07-29 02:03:44 +00:00
* @ param string $fullClassName The Classname of the class the test is being generated for .
2012-05-10 22:33:10 +00:00
* @ param string $plugin The plugin name .
2012-01-27 03:44:01 +00:00
* @ return array Constructor snippets for the thing you are building .
2009-11-14 12:19:25 +00:00
*/
2012-05-10 22:33:10 +00:00
public function generateConstructor ( $type , $fullClassName , $plugin ) {
2009-05-25 04:54:23 +00:00
$type = strtolower ( $type );
2012-06-06 20:24:15 +00:00
$pre = $construct = $post = '' ;
2013-02-12 02:38:08 +00:00
if ( $type === 'model' ) {
2012-05-10 22:33:10 +00:00
$construct = " ClassRegistry::init(' { $plugin } $fullClassName '); \n " ;
2009-05-25 04:54:23 +00:00
}
2013-02-12 02:38:08 +00:00
if ( $type === 'behavior' ) {
2012-01-28 02:10:57 +00:00
$construct = " new $fullClassName (); \n " ;
}
2013-02-12 02:38:08 +00:00
if ( $type === 'helper' ) {
2012-01-27 03:44:01 +00:00
$pre = " \$ View = new View(); \n " ;
$construct = " new { $fullClassName } ( \$ View); \n " ;
}
2013-02-12 02:38:08 +00:00
if ( $type === 'component' ) {
2012-01-27 03:44:01 +00:00
$pre = " \$ Collection = new ComponentCollection(); \n " ;
$construct = " new { $fullClassName } ( \$ Collection); \n " ;
}
return array ( $pre , $construct , $post );
}
/**
2013-10-23 02:59:50 +00:00
* Generate the uses () calls for a type & class name
2012-01-27 03:44:01 +00:00
*
* @ param string $type The Type of object you are generating tests for eg . controller
2012-02-17 12:51:20 +00:00
* @ param string $realType The package name for the class .
2012-01-27 03:44:01 +00:00
* @ param string $className The Classname of the class the test is being generated for .
2012-02-17 12:51:20 +00:00
* @ return array An array containing used classes
2012-01-27 03:44:01 +00:00
*/
public function generateUses ( $type , $realType , $className ) {
$uses = array ();
2012-07-24 01:25:55 +00:00
$type = strtolower ( $type );
2013-02-12 02:38:08 +00:00
if ( $type === 'component' ) {
2012-01-27 03:44:01 +00:00
$uses [] = array ( 'ComponentCollection' , 'Controller' );
$uses [] = array ( 'Component' , 'Controller' );
}
2013-02-12 02:38:08 +00:00
if ( $type === 'helper' ) {
2012-01-27 03:44:01 +00:00
$uses [] = array ( 'View' , 'View' );
$uses [] = array ( 'Helper' , 'View' );
}
$uses [] = array ( $className , $realType );
return $uses ;
2009-05-25 04:54:23 +00:00
}
2009-07-24 19:18:37 +00:00
2009-06-07 01:03:26 +00:00
/**
2009-12-30 03:50:43 +00:00
* Make the filename for the test case . resolve the suffixes for controllers
2009-06-07 01:03:26 +00:00
* and get the plugin path if needed .
*
2009-12-30 03:50:43 +00:00
* @ param string $type The Type of object you are generating tests for eg . controller
* @ param string $className the Classname of the class the test is being generated for .
* @ return string filename the test should be created on .
2009-11-14 12:19:25 +00:00
*/
2010-04-05 03:19:38 +00:00
public function testCaseFileName ( $type , $className ) {
2011-04-11 01:19:16 +00:00
$path = $this -> getPath () . 'Case' . DS ;
2011-10-23 09:19:01 +00:00
$type = Inflector :: camelize ( $type );
2011-04-11 01:19:16 +00:00
if ( isset ( $this -> classTypes [ $type ])) {
$path .= $this -> classTypes [ $type ] . DS ;
}
2011-10-03 01:46:35 +00:00
$className = $this -> getRealClassName ( $type , $className );
2011-06-21 15:54:59 +00:00
return str_replace ( '/' , DS , $path ) . Inflector :: camelize ( $className ) . 'Test.php' ;
2009-06-07 01:03:26 +00:00
}
2009-08-17 01:32:10 +00:00
/**
2013-12-05 23:38:32 +00:00
* Gets the option parser instance and configures it .
2009-08-17 01:32:10 +00:00
*
2013-12-05 23:38:32 +00:00
* @ return ConsoleOptionParser
2009-11-14 12:19:25 +00:00
*/
2010-10-13 02:25:04 +00:00
public function getOptionParser () {
$parser = parent :: getOptionParser ();
2013-12-05 23:38:32 +00:00
$parser -> description (
__d ( 'cake_console' , 'Bake test case skeletons for classes.' )
) -> addArgument ( 'type' , array (
'help' => __d ( 'cake_console' , 'Type of class to bake, can be any of the following: controller, model, helper, component or behavior.' ),
'choices' => array (
'Controller' , 'controller' ,
'Model' , 'model' ,
'Helper' , 'helper' ,
'Component' , 'component' ,
'Behavior' , 'behavior'
)
)) -> addArgument ( 'name' , array (
'help' => __d ( 'cake_console' , 'An existing class to bake tests for.' )
)) -> addOption ( 'theme' , array (
'short' => 't' ,
'help' => __d ( 'cake_console' , 'Theme to use when baking code.' )
)) -> addOption ( 'plugin' , array (
'short' => 'p' ,
'help' => __d ( 'cake_console' , 'CamelCased name of the plugin to bake tests for.' )
)) -> addOption ( 'force' , array (
'short' => 'f' ,
'help' => __d ( 'cake_console' , 'Force overwriting existing files without prompting.' )
)) -> epilog (
__d ( 'cake_console' , 'Omitting all arguments and options will enter into an interactive mode.' )
);
return $parser ;
2009-08-17 01:32:10 +00:00
}
2012-03-03 23:55:07 +00:00
2008-05-30 11:40:08 +00:00
}