args)) { $this->_interactive(); } if (count($this->args) == 1) { $this->_interactive($this->args[0]); } if (count($this->args) > 1) { $type = Inflector::underscore($this->args[0]); if ($this->bake($type, $this->args[1])) { $this->out('Done'); } } } /** * Handles interactive baking * * @access private */ protected function _interactive($type = null) { $this->interactive = true; $this->hr(); $this->out(__('Bake Tests')); $this->out(__('Path: %s', $this->path)); $this->hr(); if ($type) { $type = Inflector::camelize($type); if (!in_array($type, $this->classTypes)) { $this->error(__('Incorrect type provided. Please choose one of %s', implode(', ', $this->classTypes))); } } else { $type = $this->getObjectType(); } $className = $this->getClassName($type); return $this->bake($type, $className); } /** * Completes final steps for generating data to create test case. * * @param string $type Type of object to bake test case for ie. Model, Controller * @param string $className the 'cake name' for the class ie. Posts for the PostsController */ public function bake($type, $className) { if ($this->typeCanDetectFixtures($type) && $this->isLoadableClass($type, $className)) { $this->out(__('Bake is detecting possible fixtures...')); $testSubject = $this->buildTestSubject($type, $className); $this->generateFixtureList($testSubject); } elseif ($this->interactive) { $this->getUserFixtures(); } $fullClassName = $this->getRealClassName($type, $className); $methods = array(); if (class_exists($fullClassName)) { $methods = $this->getTestableMethods($fullClassName); } $mock = $this->hasMockClass($type, $fullClassName); $construction = $this->generateConstructor($type, $fullClassName); $plugin = null; if ($this->plugin) { $plugin = $this->plugin . '.'; } $this->out("\nBaking test case for $className $type...", 1, Shell::QUIET); $this->Template->set('fixtures', $this->_fixtures); $this->Template->set('plugin', $plugin); $this->Template->set(compact('className', 'methods', 'type', 'fullClassName', 'mock', 'construction')); $out = $this->Template->generate('classes', 'test'); $filename = $this->testCaseFileName($type, $className); $made = $this->createFile($filename, $out); if ($made) { return $out; } return false; } /** * Interact with the user and get their chosen type. Can exit the script. * * @return string Users chosen type. */ public function getObjectType() { $this->hr(); $this->out(__('Select an object type:')); $this->hr(); $keys = array(); foreach ($this->classTypes as $key => $option) { $this->out(++$key . '. ' . $option); $keys[] = $key; } $keys[] = 'q'; $selection = $this->in(__('Enter the type of object to bake a test for or (q)uit'), $keys, 'q'); if ($selection == 'q') { return $this->_stop(); } return $this->classTypes[$selection - 1]; } /** * 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. */ public function getClassName($objectType) { $options = App::objects(strtolower($objectType)); $this->out(__('Choose a %s class', $objectType)); $keys = array(); foreach ($options as $key => $option) { $this->out(++$key . '. ' . $option); $keys[] = $key; } $selection = $this->in(__('Choose an existing class, or enter the name of a class that does not exist')); if (isset($options[$selection - 1])) { return $options[$selection - 1]; } return $selection; } /** * Checks whether the chosen type can find its own fixtures. * Currently only model, and controller are supported * * @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 boolean */ public function typeCanDetectFixtures($type) { $type = strtolower($type); return ($type == 'controller' || $type == 'model'); } /** * Check if a class with the given type is loaded or can be loaded. * * @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 boolean */ public function isLoadableClass($type, $class) { return App::import($type, $class); } /** * Construct an instance of the class to be tested. * So that fixtures can be detected * * @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. */ public function &buildTestSubject($type, $class) { ClassRegistry::flush(); App::import($type, $class); $class = $this->getRealClassName($type, $class); if (strtolower($type) == 'model') { $instance = ClassRegistry::init($class); } else { $instance = new $class(); } return $instance; } /** * Gets the real class name from the cake short form. * * @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 string Real classname */ public function getRealClassName($type, $class) { if (strtolower($type) == 'model') { return $class; } return $class . $type; } /** * 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. */ public function getTestableMethods($className) { $classMethods = get_class_methods($className); $parentMethods = get_class_methods(get_parent_class($className)); $thisMethods = array_diff($classMethods, $parentMethods); $out = array(); foreach ($thisMethods as $method) { if (substr($method, 0, 1) != '_' && $method != strtolower($className)) { $out[] = $method; } } return $out; } /** * Generate the list of fixtures that will be required to run this test based on * loaded models. * * @param object $subject The object you want to generate fixtures for. * @return array Array of fixtures to be included in the test. */ public function generateFixtureList(&$subject) { $this->_fixtures = array(); if (is_a($subject, 'Model')) { $this->_processModel($subject); } elseif (is_a($subject, 'Controller')) { $this->_processController($subject); } return array_values($this->_fixtures); } /** * Process a model recursively and pull out all the * model names converting them to fixture names. * * @param Model $subject A Model class to scan for associations and pull fixtures off of. * @return void */ protected function _processModel(&$subject) { $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}); } if ($type == 'hasAndBelongsToMany') { $joinModel = Inflector::classify($subject->hasAndBelongsToMany[$alias]['joinTable']); if (!isset($this->_fixtures[$joinModel])) { $this->_processModel($subject->{$joinModel}); } } } } /** * Process all the models attached to a controller * and generate a fixture list. * * @param Controller $subject A controller to pull model names off of. * @return void */ protected function _processController(&$subject) { $subject->constructClasses(); $models = array(Inflector::classify($subject->name)); if (!empty($subject->uses)) { $models = $subject->uses; } foreach ($models as $model) { $this->_processModel($subject->{$model}); } } /** * Add classname to the fixture list. * Sets the app. or plugin.plugin_name. prefix. * * @param string $name Name of the Model class that a fixture might be required for. * @return void */ protected function _addFixture($name) { $parent = get_parent_class($name); $prefix = 'app.'; if (strtolower($parent) != 'appmodel' && strtolower(substr($parent, -8)) == 'appmodel') { $pluginName = substr($parent, 0, strlen($parent) -8); $prefix = 'plugin.' . Inflector::underscore($pluginName) . '.'; } $fixture = $prefix . Inflector::underscore($name); $this->_fixtures[$name] = $fixture; } /** * Interact with the user to get additional fixtures they want to use. * * @return array Array of fixtures the user wants to add. */ public function getUserFixtures() { $proceed = $this->in(__('Bake could not detect fixtures, would you like to add some?'), array('y','n'), 'n'); $fixtures = array(); if (strtolower($proceed) == 'y') { $fixtureList = $this->in(__("Please provide a comma separated list of the fixtures names you'd like to use.\nExample: 'app.comment, app.post, plugin.forums.post'")); $fixtureListTrimmed = str_replace(' ', '', $fixtureList); $fixtures = explode(',', $fixtureListTrimmed); } $this->_fixtures = array_merge($this->_fixtures, $fixtures); return $fixtures; } /** * Is a mock class required for this type of test? * Controllers require a mock class. * * @param string $type The type of object tests are being generated for eg. controller. * @return boolean */ public function hasMockClass($type) { $type = strtolower($type); return $type == 'controller'; } /** * Generate a constructor code snippet for the type and classname * * @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 Constructor snippet for the thing you are building. */ public function generateConstructor($type, $fullClassName) { $type = strtolower($type); if ($type == 'model') { return "ClassRegistry::init('$fullClassName');\n"; } if ($type == 'controller') { $className = substr($fullClassName, 0, strlen($fullClassName) - 10); return "new Test$fullClassName();\n\t\t\$this->{$className}->constructClasses();\n"; } return "new $fullClassName();\n"; } /** * Make the filename for the test case. resolve the suffixes for controllers * and get the plugin path if needed. * * @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. */ public function testCaseFileName($type, $className) { $path = $this->getPath();; $path .= 'cases' . DS . strtolower($type) . 's' . DS; if (strtolower($type) == 'controller') { $className = $this->getRealClassName($type, $className); } return $path . Inflector::underscore($className) . '.test.php'; } /** * get the option parser. * * @return void */ public function getOptionParser() { $parser = parent::getOptionParser(); return $parser->description(__('Bake test case skeletons for classes.')) ->addArgument('type', array( 'help' => __('Type of class to bake, can be any of the following: controller, model, helper, component or behavior.'), 'choices' => array('controller', 'model', 'helper', 'component', 'behavior') ))->addArgument('name', array( 'help' => __('An existing class to bake tests for.') ))->addOption('plugin', array( 'short' => 'p', 'help' => __('CamelCased name of the plugin to bake tests for.') ))->epilog(__('Omitting all arguments and options will enter into an interactive mode.')); } }