Created
September 6, 2012 17:46
-
-
Save racklin/3658914 to your computer and use it in GitHub Desktop.
Phalcon CLI descriptions (invo web)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
+------------------------------------------------------------------------+ | |
| Phalcon Framework | | |
+------------------------------------------------------------------------+ | |
| Copyright (c) 2011-2012 Phalcon Team (http://www.phalconphp.com) | | |
+------------------------------------------------------------------------+ | |
| This source file is subject to the New BSD License that is bundled | | |
| with this package in the file docs/LICENSE.txt. | | |
| | | |
| If you did not receive a copy of the license and are unable to | | |
| obtain it through the world-wide-web, please send an email | | |
| to [email protected] so we can send you a copy immediately. | | |
+------------------------------------------------------------------------+ | |
| Authors: Andres Gutierrez <[email protected]> | | |
| Eduar Carvajal <[email protected]> | | |
| Rack Lin <[email protected]> | | |
+------------------------------------------------------------------------+ | |
*/ | |
class CliTest extends PHPUnit_Framework_TestCase | |
{ | |
public function setup() { | |
require_once 'unit-tests/tasks/MainTask.php'; | |
require_once 'unit-tests/tasks/EchoTask.php'; | |
} | |
public function testTasks() | |
{ | |
$di = new Phalcon\DI(); | |
$di->set('data', function(){ | |
return "data"; | |
}); | |
$task = new MainTask(); | |
$task->setDI($di); | |
$this->assertEquals($task->requestDiAction(), 'data'); | |
$this->assertEquals($task->helloAction(), 'Hello !'); | |
$this->assertEquals($task->helloAction('World'), 'Hello World!'); | |
$task2 = new EchoTask(); | |
$task2->setDI($di); | |
$this->assertEquals($task2->mainAction(), 'echoMainAction'); | |
} | |
public function testRouters() | |
{ | |
$di = new Phalcon\DI(); | |
$di->set('data', function(){ | |
return "data"; | |
}); | |
$router = new \Phalcon\CLI\Router(); | |
$router->handle(array()); | |
$this->assertEquals($router->getModuleName(), null); | |
$this->assertEquals($router->getTaskName(), null); | |
$this->assertEquals($router->getActionName(), null); | |
$this->assertEquals($router->getParams(), array()); | |
$router->handle(array('shell_script_name', 'main')); | |
$this->assertEquals($router->getModuleName(), null); | |
$this->assertEquals($router->getTaskName(), 'main'); | |
$this->assertEquals($router->getActionName(), null); | |
$this->assertEquals($router->getParams(), array()); | |
$router->handle(array('shell_script_name', 'echo')); | |
$this->assertEquals($router->getModuleName(), null); | |
$this->assertEquals($router->getTaskName(), 'echo'); | |
$this->assertEquals($router->getActionName(), null); | |
$this->assertEquals($router->getParams(), array()); | |
$router->handle(array('shell_script_name', 'main', 'hello')); | |
$this->assertEquals($router->getModuleName(), null); | |
$this->assertEquals($router->getTaskName(), 'main'); | |
$this->assertEquals($router->getActionName(), 'hello'); | |
$this->assertEquals($router->getParams(), array()); | |
$router->handle(array('shell_script_name', 'main', 'hello', 'arg1', 'arg2')); | |
$this->assertEquals($router->getModuleName(), null); | |
$this->assertEquals($router->getTaskName(), 'main'); | |
$this->assertEquals($router->getActionName(), 'hello'); | |
$this->assertEquals($router->getParams(), array('arg1', 'arg2')); | |
$router->handle(array('shell_script_name', 'devtools:main', 'hello', 'arg1', 'arg2')); | |
$this->assertEquals($router->getModuleName(), 'devtools'); | |
$this->assertEquals($router->getTaskName(), 'main'); | |
$this->assertEquals($router->getActionName(), 'hello'); | |
$this->assertEquals($router->getParams(), array('arg1', 'arg2')); | |
$router->handle(array('shell_script_name', 'devtools:echo', 'hello', 'arg1', 'arg2')); | |
$this->assertEquals($router->getModuleName(), 'devtools'); | |
$this->assertEquals($router->getTaskName(), 'echo'); | |
$this->assertEquals($router->getActionName(), 'hello'); | |
$this->assertEquals($router->getParams(), array('arg1', 'arg2')); | |
} | |
public function testDispatchers() | |
{ | |
$di = new Phalcon\DI(); | |
$di->set('data', function(){ | |
return "data"; | |
}); | |
$dispatcher = new \Phalcon\CLI\Dispatcher(); | |
$dispatcher->setDI($di); | |
$dispatcher->dispatch(); | |
$this->assertEquals($dispatcher->getTaskName(), 'main'); | |
$this->assertEquals($dispatcher->getActionName(), 'main'); | |
$this->assertEquals($dispatcher->getParams(), array()); | |
$this->assertEquals($dispatcher->getReturnedValue(), 'mainAction'); | |
$dispatcher->setTaskName('echo'); | |
$dispatcher->dispatch(); | |
$this->assertEquals($dispatcher->getTaskName(), 'echo'); | |
$this->assertEquals($dispatcher->getActionName(), 'main'); | |
$this->assertEquals($dispatcher->getParams(), array()); | |
$this->assertEquals($dispatcher->getReturnedValue(), 'echoMainAction'); | |
$dispatcher->setTaskName('main'); | |
$dispatcher->setActionName('hello'); | |
$dispatcher->dispatch(); | |
$this->assertEquals($dispatcher->getTaskName(), 'main'); | |
$this->assertEquals($dispatcher->getActionName(), 'hello'); | |
$this->assertEquals($dispatcher->getParams(), array()); | |
$this->assertEquals($dispatcher->getReturnedValue(), 'Hello !'); | |
$dispatcher->setActionName('hello'); | |
$dispatcher->setParams(array('World', '######')); | |
$dispatcher->dispatch(); | |
$this->assertEquals($dispatcher->getTaskName(), 'main'); | |
$this->assertEquals($dispatcher->getActionName(), 'hello'); | |
$this->assertEquals($dispatcher->getParams(), array('World', '######')); | |
$this->assertEquals($dispatcher->getReturnedValue(), 'Hello World######'); | |
// testing namespace | |
try { | |
$dispatcher->setDefaultNamespace('Dummy\\'); | |
$dispatcher->setTaskName('main'); | |
$dispatcher->setActionName('hello'); | |
$dispatcher->setParams(array('World')); | |
$dispatcher->dispatch(); | |
$this->assertEquals($dispatcher->getTaskName(), 'main'); | |
$this->assertEquals($dispatcher->getActionName(), 'hello'); | |
$this->assertEquals($dispatcher->getParams(), array('World')); | |
$this->assertEquals($dispatcher->getReturnedValue(), 'Hello World!'); | |
}catch (Exception $e) { | |
$this->assertEquals($e->getMessage(), 'Dummy\MainTask task class cannot be loaded'); | |
} | |
} | |
public function testConsoles() | |
{ | |
$di = new Phalcon\DI(); | |
$di->set('data', function(){ | |
return "data"; | |
}); | |
$console = new \Phalcon\CLI\Console(); | |
$console->setDI($di); | |
$dispatcher = $console->getDI()->getShared('dispatcher'); | |
$console->handle(array('shell_script_name')); | |
$this->assertEquals($dispatcher->getTaskName(), 'main'); | |
$this->assertEquals($dispatcher->getActionName(), 'main'); | |
$this->assertEquals($dispatcher->getParams(), array()); | |
$this->assertEquals($dispatcher->getReturnedValue(), 'mainAction'); | |
$console->handle(array('shell_script_name', 'echo')); | |
$this->assertEquals($dispatcher->getTaskName(), 'echo'); | |
$this->assertEquals($dispatcher->getActionName(), 'main'); | |
$this->assertEquals($dispatcher->getParams(), array()); | |
$this->assertEquals($dispatcher->getReturnedValue(), 'echoMainAction'); | |
$console->handle(array('shell_script_name', 'main', 'hello')); | |
$this->assertEquals($dispatcher->getTaskName(), 'main'); | |
$this->assertEquals($dispatcher->getActionName(), 'hello'); | |
$this->assertEquals($dispatcher->getParams(), array()); | |
$this->assertEquals($dispatcher->getReturnedValue(), 'Hello !'); | |
$console->handle(array('shell_script_name', 'main', 'hello', 'World', '######')); | |
$this->assertEquals($dispatcher->getTaskName(), 'main'); | |
$this->assertEquals($dispatcher->getActionName(), 'hello'); | |
$this->assertEquals($dispatcher->getParams(), array('World', '######')); | |
$this->assertEquals($dispatcher->getReturnedValue(), 'Hello World######'); | |
// testing module | |
try { | |
$console->handle(array('shell_script_name', 'devtools:main', 'hello', 'World', '######')); | |
$this->assertEquals($dispatcher->getTaskName(), 'main'); | |
$this->assertEquals($dispatcher->getActionName(), 'hello'); | |
$this->assertEquals($dispatcher->getParams(), array('World', '######')); | |
$this->assertEquals($dispatcher->getReturnedValue(), 'Hello World######'); | |
} catch (Exception $e) { | |
$this->assertEquals($e->getMessage(), "Module 'devtools' isn't registered in the console container"); | |
} | |
// testing namespace | |
try { | |
$dispatcher->setDefaultNamespace('Dummy\\'); | |
$console->handle(array('shell_script_name', 'main', 'hello', 'World', '!')); | |
$this->assertEquals($dispatcher->getTaskName(), 'main'); | |
$this->assertEquals($dispatcher->getActionName(), 'hello'); | |
$this->assertEquals($dispatcher->getParams(), array('World')); | |
$this->assertEquals($dispatcher->getReturnedValue(), 'Hello World!'); | |
}catch (Exception $e) { | |
$this->assertEquals($e->getMessage(), 'Dummy\MainTask task class cannot be loaded'); | |
} | |
} | |
public function testModules() { | |
$di = new Phalcon\DI(); | |
$di->set('data', function(){ | |
return "data"; | |
}); | |
$console = new \Phalcon\CLI\Console(); | |
$console->setDI($di); | |
$expected = array('devtools'=> array('className' =>'dummy', 'path' => 'dummy_file')); | |
$console->registerModules($expected); | |
$this->assertEquals($console->getModules(), $expected); | |
$userModules = array('front'=>array('className'=>'front', 'path'=>'front_file'), 'worker'=>array('className'=>'worker', 'path'=>'worker_file')); | |
$expected = array('devtools'=> array('className' =>'dummy', 'path' => 'dummy_file'), 'front'=>array('className'=>'front', 'path'=>'front_file'), 'worker'=>array('className'=>'worker', 'path'=>'worker_file')); | |
; | |
$console->registerModules($userModules); | |
$this->assertEquals($console->getModules(), $expected); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[database] | |
host = localhost | |
username = root | |
password = secret | |
name = invo | |
[phalcon] | |
controllersDir = /../app/controllers/ | |
modelsDir = /../app/models/ | |
viewsDir = /../app/views/ | |
pluginsDir = /../app/plugins/ | |
libraryDir = /../app/library/ | |
tasksDir = /../app/tasks/ | |
baseUri = / | |
[metadata] | |
adapter = "Apc" | |
;suffix = my-suffix | |
;ifetime = 3600 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
error_reporting(E_ALL); | |
try { | |
//Read the configuration | |
$config = new Phalcon\Config\Adapter\Ini(__DIR__.'/../app/config/config.ini'); | |
$loader = new \Phalcon\Loader(); | |
/** | |
* We're a registering a set of directories taken from the configuration file | |
*/ | |
$loader->registerDirs( | |
array( | |
__DIR__.$config->phalcon->controllersDir, | |
__DIR__.$config->phalcon->pluginsDir, | |
__DIR__.$config->phalcon->libraryDir, | |
__DIR__.$config->phalcon->modelsDir, | |
__DIR__.$config->phalcon->tasksDir, | |
) | |
)->register(); | |
/** | |
* The FactoryDefault Dependency Injector automatically register the right services providing a full stack framework | |
*/ | |
$di = new \Phalcon\DI\FactoryDefault\CLI(); | |
/** | |
* Database connection is created based in the parameters defined in the configuration file | |
*/ | |
$di->set('db', function() use ($config) { | |
return new \Phalcon\Db\Adapter\Pdo\Mysql(array( | |
"host" => $config->database->host, | |
"username" => $config->database->username, | |
"password" => $config->database->password, | |
"dbname" => $config->database->name | |
)); | |
}); | |
/** | |
* If the configuration specify the use of metadata adapter use it or use memory otherwise | |
*/ | |
$di->set('modelsMetadata', function() use ($config) { | |
if(isset($config->models->metadata)){ | |
$metaDataConfig = $config->models->metadata; | |
$metadataAdapter = 'Phalcon\Mvc\Model\Metadata\\'.$metaDataConfig->adapter; | |
return new $metadataAdapter(); | |
} else { | |
return new Phalcon\Mvc\Model\Metadata\Memory(); | |
} | |
}); | |
$console = new \Phalcon\CLI\Console(); | |
$console->setDI($di); | |
// pass command-line arguments to handle | |
$console->handle($_SERVER['argv']); | |
} catch (Phalcon\Exception $e) { | |
echo $e->getMessage(); | |
} catch (PDOException $e){ | |
echo $e->getMessage(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MainTask extends \Phalcon\CLI\Task | |
{ | |
public function mainAction() | |
{ | |
echo "SADFASDF"; | |
} | |
public function helloAction($world = "!") { | |
echo "\n Hello " . $world . "\n\n"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# Running default task / action | |
# | |
# MainTask's mainAction will execute. | |
## | |
php console.php | |
## | |
# Running MainTest / default action | |
# | |
# MainTask's mainAction will execute. | |
## | |
php console.php main | |
## | |
# Running MainTest / hello action | |
# | |
# MainTask's helloAction will execute. | |
## | |
php console.php main hello | |
## | |
# Running MainTest / hello action | |
# | |
# MainTask's helloAction will execute with parameters | |
## | |
php console.php main hello world | |
## | |
# Running MainTest / hello action | |
# | |
# MainTask's helloAction will execute with any number parameters | |
## | |
php console.php main hello world very long paramter | |
## | |
# Running devtools Module 's MainTest / hello action | |
# | |
# MainTask's helloAction will execute with any number parameters | |
## | |
php console.php devtools:main hello world very long paramter | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment