Example #1
0
 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');
     }
 }
<?php

$di = new Phalcon\DI();
$dispatcher = new Phalcon\CLI\Dispatcher();
$dispatcher->setDI($di);
$dispatcher->setTaskName('posts');
$dispatcher->setActionName('index');
$dispatcher->setParams(array());
$handle = $dispatcher->dispatch();