Skip to content

Commit

Permalink
Get more functionality working.
Browse files Browse the repository at this point in the history
Helper test generation works \o/
  • Loading branch information
markstory committed Mar 31, 2014
1 parent 7bbd542 commit f109ca1
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 110 deletions.
120 changes: 49 additions & 71 deletions src/Console/Command/Task/TestTask.php
Expand Up @@ -20,6 +20,7 @@
use Cake\Core\Plugin;
use Cake\Error;
use Cake\ORM\TableRegistry;
use Cake\Utility\Folder;
use Cake\Utility\Inflector;

/**
Expand Down Expand Up @@ -48,39 +49,26 @@ class TestTask extends BakeTask {
* @var array
*/
public $classTypes = [
'Entity' => 'Model/Entity',
'Table' => 'Model/Table',
'Controller' => 'Controller',
'Component' => 'Controller/Component',
'Behavior' => 'Model/Behavior',
'Helper' => 'View/Helper'
];

/**
* Mapping between type names and their namespaces
*
* @var array
*/
public $classNamespaces = [
'Model' => 'Model',
'Entity' => 'Model\Entity',
'Table' => 'Model\Table',
'Controller' => 'Controller',
'Component' => 'Controller\Component',
'Behavior' => 'Model\Behavior',
'Helper' => 'View\Helper'
];

/**
* Mapping between packages, and their baseclass
* This is used to create use statements.
* class types that methods can be generated for
*
* @var array
*/
public $baseTypes = [
'Model' => ['Model', 'Model'],
'Behavior' => ['ModelBehavior', 'Model'],
'Controller' => ['Controller', 'Controller'],
'Component' => ['Component', 'Controller'],
'Helper' => ['Helper', 'View']
public $classSuffixes = [
'Entity' => '',
'Table' => 'Table',
'Controller' => 'Controller',
'Component' => 'Component',
'Behavior' => 'Behavior',
'Helper' => 'Helper'
];

/**
Expand All @@ -106,11 +94,8 @@ public function execute() {
return $this->outputClassChoices($this->args[0]);
}

if ($count > 1) {
$type = Inflector::classify($this->args[0]);
if ($this->bake($type, $this->args[1])) {
$this->out('<success>Done</success>');
}
if ($this->bake($this->args[0], $this->args[1])) {
$this->out('<success>Done</success>');
}
}

Expand Down Expand Up @@ -161,11 +146,21 @@ public function outputClassChoices($type) {
/**
* Get the possible classes for a given type.
*
* @param string $type The type name to look for classes in.
* @param string $namespace The namespace fragment to look for classes in.
* @return array
*/
protected function _getClassOptions($type) {
protected function _getClassOptions($namespace) {
$classes = [];
$base = APP;
if ($this->plugin) {
$base = Plugin::path($this->plugin);
}
$path = $base . str_replace('\\', DS, $namespace);
$folder = new Folder($path);
list($dirs, $files) = $folder->read();
foreach ($files as $file) {
$classes[] = str_replace('.php', '', $file);
}
return $classes;
}

Expand Down Expand Up @@ -202,29 +197,24 @@ protected function _interactive($type = null) {
* @return string|boolean
*/
public function bake($type, $className) {
$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)) {
if (!empty($this->params['fixtures'])) {
$fixtures = array_map('trim', explode(',', $this->params['fixtures']));
$this->_fixtures = $fixtures;
} elseif ($this->typeCanDetectFixtures($type) && $this->isLoadableClass($realType, $fullClassName)) {
$this->out(__d('cake_console', 'Bake is detecting possible fixtures...'));
$testSubject = $this->buildTestSubject($type, $className);
$this->generateFixtureList($testSubject);
} elseif ($this->interactive) {
$this->getUserFixtures();
}

$methods = [];
if (class_exists($fullClassName)) {
$methods = $this->getTestableMethods($fullClassName);
}
$mock = $this->hasMockClass($type, $fullClassName);
list($preConstruct, $construction, $postConstruct) = $this->generateConstructor($type, $fullClassName, $plugin);
$uses = $this->generateUses($type, $realType, $fullClassName);
list($preConstruct, $construction, $postConstruct) = $this->generateConstructor($type, $fullClassName);
$uses = $this->generateUses($type, $fullClassName);

$subject = $className;
list($namespace, $className) = namespaceSplit($fullClassName);
Expand All @@ -233,7 +223,7 @@ public function bake($type, $className) {
$this->out("\n" . __d('cake_console', 'Baking test case for %s ...', $fullClassName), 1, Shell::QUIET);

$this->Template->set('fixtures', $this->_fixtures);
$this->Template->set('plugin', $plugin);
$this->Template->set('plugin', $this->plugin);
$this->Template->set(compact(
'subject', 'className', 'methods', 'type', 'fullClassName', 'mock',
'realType', 'preConstruct', 'postConstruct', 'construction',
Expand All @@ -242,8 +232,7 @@ public function bake($type, $className) {
$out = $this->Template->generate('classes', 'test');

$filename = $this->testCaseFileName($type, $className);
$made = $this->createFile($filename, $out);
if ($made) {
if ($this->createFile($filename, $out)) {
return $out;
}
return false;
Expand Down Expand Up @@ -332,32 +321,19 @@ public function buildTestSubject($type, $class) {
*
* @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.
* @param string $plugin The plugin name of the class being generated.
* @return string Real classname
*/
public function getRealClassName($type, $class, $plugin = null) {
if (strpos('\\', $class)) {
return $class;
}
public function getRealClassName($type, $class) {
$namespace = Configure::read('App.namespace');
$loadedPlugin = $plugin && Plugin::loaded($plugin);
if ($loadedPlugin) {
$namespace = Plugin::getNamespace($plugin);
}
if ($plugin && !$loadedPlugin) {
$namespace = Inflector::camelize($plugin);
if ($this->plugin) {
$namespace = Plugin::getNamespace($this->plugin);
}
$subNamespace = $this->classNamespaces[$type];

$position = strpos($class, $type);

if (
strtolower($type) !== 'model' &&
($position === false || strlen($class) - $position !== strlen($type))
) {
$class .= $type;
$suffix = $this->classSuffixes[$type];
$subSpace = $this->mapType($type);
if ($suffix && strpos($class, $suffix) === false) {
$class .= $suffix;
}
return $namespace . '\\' . $subNamespace . '\\' . $class;
return $namespace . '\\' . $subSpace . '\\' . $class;
}

/**
Expand Down Expand Up @@ -525,17 +501,16 @@ public function hasMockClass($type) {
*
* @param string $type The Type of object you are generating tests for eg. controller
* @param string $fullClassName The full classname of the class the test is being generated for.
* @param string $plugin The plugin name.
* @return array Constructor snippets for the thing you are building.
*/
public function generateConstructor($type, $fullClassName, $plugin) {
public function generateConstructor($type, $fullClassName) {
list($namespace, $className) = namespaceSplit($fullClassName);
$type = strtolower($type);
$pre = $construct = $post = '';
if ($type === 'model') {
$construct = "ClassRegistry::init('{$plugin}$className');\n";
if ($type === 'table') {
$construct = "TableRegistry::init('{$className}', ['className' => '{$fullClassName}']);\n";
}
if ($type === 'behavior') {
if ($type === 'behavior' || $type === 'entity') {
$construct = "new {$className}();\n";
}
if ($type === 'helper') {
Expand All @@ -557,12 +532,15 @@ public function generateConstructor($type, $fullClassName, $plugin) {
* @param string $fullClassName The Classname of the class the test is being generated for.
* @return array An array containing used classes
*/
public function generateUses($type, $realType, $fullClassName) {
public function generateUses($type, $fullClassName) {
$uses = [];
$type = strtolower($type);
if ($type == 'component') {
$uses[] = 'Cake\Controller\ComponentRegistry';
}
if ($type == 'table') {
$uses[] = 'Cake\ORM\TableRegistry';
}
if ($type == 'helper') {
$uses[] = 'Cake\View\View';
}
Expand Down

0 comments on commit f109ca1

Please sign in to comment.