Permalink
Browse files

fixes #167, creating classes from console.

  • Loading branch information...
1 parent 145ae44 commit 3d8771da43eaba17525dd28fb270756502f9ddd7 @gwoo gwoo committed Nov 5, 2010
View
29 libraries/lithium/console/command/Create.php
@@ -73,11 +73,12 @@ protected function _init() {
* @return boolean
*/
public function run($command = null) {
- $this->template = $this->template ?: $command;
-
- if ($command && !$this->request->args(1)) {
+ if ($command && !$this->request->args()) {
return $this->_default($command);
}
+ $this->request->shift();
+ $this->template = $this->template ?: $command;
+
if (!$command) {
$command = $this->in('What would you like to create?', array(
'choices' => array('model', 'view', 'controller', 'test', 'mock')
@@ -111,19 +112,16 @@ public function interactive() {
* @return void
*/
protected function _execute($command) {
- $this->request->shift(2);
-
if (!$class = $this->_instance($command)) {
return false;
}
$data = array();
$params = $class->invokeMethod('_params');
foreach ($params as $i => $param) {
- if (!$data[$param] = $class->invokeMethod("_{$param}", array($this->request))) {
- $data[$param] = $this->request->args($i);
- }
+ $data[$param] = $class->invokeMethod("_{$param}", array($this->request));
}
+
if ($message = $class->invokeMethod('_save', array($data))) {
$this->out($message);
return true;
@@ -139,16 +137,17 @@ protected function _execute($command) {
*/
protected function _default($name) {
$commands = array(
- array('model', $name),
- array('controller', $name),
- array('test', 'model', $name),
+ array('model', Inflector::classify($name)),
+ array('controller', Inflector::pluralize($name)),
+ array('test', 'model', Inflector::classify($name)),
array('test', 'controller', Inflector::pluralize($name))
);
- foreach ($commands as $command) {
- $this->template = $command[0];
- $this->request->params['args'] = $command;
+ foreach ($commands as $args) {
+ $command = $this->template = $this->request->params['command'] = array_shift($args);
+ $this->request->params['action'] = array_shift($args);
+ $this->request->params['args'] = $args;
- if (!$this->_execute($command[0])) {
+ if (!$this->_execute($command)) {
return false;
}
}
View
6 libraries/lithium/console/command/create/Mock.php
@@ -27,7 +27,7 @@ class Mock extends \lithium\console\command\Create {
* @return string
*/
protected function _namespace($request, $options = array()) {
- $request->shift();
+ $request->params['command'] = $request->action;
return parent::_namespace($request, array('prepend' => 'tests.mocks.'));
}
@@ -50,8 +50,8 @@ protected function _parent($request) {
* @return string
*/
protected function _class($request) {
- $name = $request->action;
- $type = $request->command;
+ $type = $request->action;
+ $name = $request->args();
if ($command = $this->_instance($type)) {
$request->params['action'] = $name;
View
33 libraries/lithium/console/command/create/Test.php
@@ -29,7 +29,7 @@ class Test extends \lithium\console\command\Create {
* @return string
*/
protected function _namespace($request, $options = array()) {
- $request->shift();
+ $request->params['command'] = $request->action;
return parent::_namespace($request, array('prepend' => 'tests.cases.'));
}
@@ -41,8 +41,8 @@ protected function _namespace($request, $options = array()) {
*/
protected function _use($request) {
$namespace = parent::_namespace($request);
- $class = $request->action;
- return "\\{$namespace}\\{$class}";
+ $name = $this->_name($request);
+ return "\\{$namespace}\\{$name}";
}
/**
@@ -52,13 +52,7 @@ protected function _use($request) {
* @return string
*/
protected function _class($request) {
- $name = $request->action;
- $type = $request->command;
-
- if ($command = $this->_instance($type)) {
- $request->params['action'] = $name;
- $name = $command->invokeMethod('_class', array($request));
- }
+ $name = $this->_name($request);
return Inflector::classify("{$name}Test");
}
@@ -76,14 +70,31 @@ protected function _methods($request) {
return "";
}
$methods = (array) Inspector::methods($use, 'extents');
-
$testMethods = array();
foreach (array_keys($methods) as $method) {
$testMethods[] = "\tpublic function test" . ucwords($method) . "() {}";
}
return join("\n", $testMethods);
}
+
+ /**
+ * Get the class to be tested
+ *
+ * @param string $request
+ * @return string
+ */
+ protected function _name($request) {
+ $type = $request->action;
+ $name = $request->args();
+
+ if ($command = $this->_instance($type)) {
+ $request->params['action'] = $name;
+ $name = $command->invokeMethod('_class', array($request));
+ }
+ $request->params['action'] = $type;
+ return $name;
+ }
}
?>
View
20 libraries/lithium/tests/cases/console/command/CreateTest.php
@@ -74,7 +74,7 @@ public function testNonExistentCommand() {
public function testNamespace() {
$create = new MockCreate(array('request' => $this->request));
- $this->request->command = 'one';
+ $this->request->params['command'] = 'one';
$expected = 'create_test\\two';
$result = $create->invokeMethod('_namespace', array(
@@ -116,8 +116,10 @@ public function testRunWithoutCommand() {
}
public function testRunNotSaved() {
- $this->request->params['library'] = 'not_here';
- $this->request->params['args'] = array('model', 'Post');
+ $this->request->params = array(
+ 'library' => 'not_here', 'command' => 'create', 'action' => 'model',
+ 'args' => array('model', 'Post')
+ );
$create = new MockCreate(array('request' => $this->request));
$expected = false;
@@ -132,15 +134,15 @@ public function testRunNotSaved() {
public function testRunWithModelCommand() {
$this->request->params = array(
'library' => 'create_test', 'command' => 'create', 'action' => 'model',
- 'args' => array('model', 'Post')
+ 'args' => array('Post')
);
$create = new MockCreate(array('request' => $this->request));
$create->run('model');
$expected = 'model';
- $result = $create->request->params['command'];
+ $result = $create->request->command;
$this->assertEqual($expected, $result);
$result = $this->_testPath . '/create_test/models/Post.php';
@@ -150,7 +152,7 @@ public function testRunWithModelCommand() {
public function testRunWithTestModelCommand() {
$this->request->params = array(
'library' => 'create_test', 'command' => 'create', 'action' => 'test',
- 'args' => array('test', 'model', 'Post'),
+ 'args' => array('model', 'Post'),
);
$create = new MockCreate(array('request' => $this->request));
@@ -168,7 +170,7 @@ public function testRunWithTestModelCommand() {
public function testRunWithTestControllerCommand() {
$this->request->params = array(
'library' => 'create_test', 'command' => 'create', 'action' => 'test',
- 'args' => array('test', 'controller', 'Post'),
+ 'args' => array('controller', 'Post'),
);
$create = new MockCreate(array('request' => $this->request));
@@ -186,7 +188,7 @@ public function testRunWithTestControllerCommand() {
public function testRunWithTestOtherCommand() {
$this->request->params = array(
'library' => 'create_test', 'command' => 'create', 'action' => 'test',
- 'args' => array('test', 'something', 'Post'),
+ 'args' => array('something', 'Post'),
);
$create = new MockCreate(array('request' => $this->request));
@@ -203,7 +205,7 @@ public function testRunWithTestOtherCommand() {
public function testRunAll() {
$this->request->params = array(
'library' => 'create_test', 'command' => 'create', 'action' => 'Post',
- 'args' => array('Post'),
+ 'args' => array(),
);
$create = new MockCreate(array('request' => $this->request));

0 comments on commit 3d8771d

Please sign in to comment.