public function testDispatcher()
 {
     $di = new \Phalcon\DI\FactoryDefault\CLI();
     $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######');
     $dispatcher->setActionName('hello');
     $dispatcher->setParams(array('hello' => 'World', 'goodbye' => 'Everybody'));
     $dispatcher->dispatch();
     $this->assertTrue($dispatcher->hasParam('hello'));
     $this->assertTrue($dispatcher->hasParam('goodbye'));
     $this->assertFalse($dispatcher->hasParam('salutations'));
     // 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 handler class cannot be loaded');
     }
 }
Beispiel #2
0
 public function testIssue787()
 {
     $di = new \Phalcon\DI\FactoryDefault\CLI();
     $di->setShared('dispatcher', function () use($di) {
         $dispatcher = new Phalcon\CLI\Dispatcher();
         $dispatcher->setDI($di);
         return $dispatcher;
     });
     $console = new \Phalcon\CLI\Console();
     $console->setDI($di);
     $console->handle(array('task' => 'issue787', 'action' => 'main'));
     $this->assertTrue(class_exists('Issue787Task'));
     $actual = Issue787Task::$output;
     $expected = "beforeExecuteRoute\ninitialize\n";
     $this->assertEquals($actual, $expected);
 }
}
error_reporting(E_ALL | E_NOTICE);
if (!extension_loaded('phalcon')) {
    throw new Exception("Phalcon extension is required");
}
$params = $argv;
$file = array_shift($argv);
$task = array_shift($argv);
define('APP_ROOT', __DIR__ . '/app');
$loader = new Phalcon\Loader();
$loader->registerNamespaces(['ApiDocs' => APP_ROOT]);
$loader->register();
$di = new Phalcon\DI\FactoryDefault\CLI();
$di->setShared('dispatcher', function () use($di) {
    $dispatcher = new Phalcon\CLI\Dispatcher();
    $dispatcher->setDI($di);
    $dispatcher->setDefaultNamespace('ApiDocs\\Tasks');
    return $dispatcher;
});
$di->setShared('modelsManager', function () {
    return new Phalcon\Mvc\Model\Manager();
});
$di->setShared('db', function () use($di) {
    $connection = new Phalcon\Db\Adapter\Pdo\Mysql((array) $di->get('config')->db);
    return $connection;
});
$di->setShared('config', function () {
    return new Phalcon\Config(require APP_ROOT . '/config/config.php');
});
$console = new Phalcon\CLI\Console();
$console->setDI($di);