public function testDispatcher()
{
$this->specify("Dispatcher doesn't throw exception when handler can't be loaded", function () {
$di = new Di();
$di->set("response", new Response());
$dispatcher = new Dispatcher();
$dispatcher->setDI($di);
expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di");
$di->set("dispatcher", $dispatcher);
$dispatcher->setControllerName("index");
$dispatcher->setActionName("index");
$dispatcher->setParams([]);
$dispatcher->dispatch();
}, ["throws" => [\Phalcon\Mvc\Dispatcher\Exception::class, "IndexController handler class cannot be loaded"]]);
$this->specify("Dispatcher doesn't throw exception when handler can't be loaded (2)", function () {
$di = new Di();
$di->set("response", new Response());
$dispatcher = new Dispatcher();
$dispatcher->setDI($di);
expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di");
$di->set("dispatcher", $dispatcher);
$dispatcher->setControllerName("essai");
$dispatcher->setActionName("index");
$dispatcher->setParams([]);
$dispatcher->dispatch();
}, ["throws" => [\Phalcon\Mvc\Dispatcher\Exception::class, "EssaiController handler class cannot be loaded"]]);
$this->specify("Dispatcher doesn't throw exception when handler can't be loaded (3)", function () {
$di = new Di();
$di->set("response", new Response());
$dispatcher = new Dispatcher();
$dispatcher->setDI($di);
expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di");
$di->set("dispatcher", $dispatcher);
$dispatcher->setControllerName("test0");
$dispatcher->setActionName("index");
$dispatcher->setParams([]);
$dispatcher->dispatch();
}, ["throws" => [\Phalcon\Mvc\Dispatcher\Exception::class, "Test0Controller handler class cannot be loaded"]]);
$this->specify("Dispatcher doesn't throw exception when action can't be found", function () {
$di = new Di();
$di->set("response", new Response());
$dispatcher = new Dispatcher();
$dispatcher->setDI($di);
expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di");
$di->set("dispatcher", $dispatcher);
$dispatcher->setControllerName("test1");
$dispatcher->setActionName("index");
$dispatcher->setParams([]);
$dispatcher->dispatch();
}, ["throws" => [\Phalcon\Mvc\Dispatcher\Exception::class, "Action 'index' was not found on handler 'test1'"]]);
$this->specify("Dispatcher doesn't return controller after dispatching", function () {
$di = new Di();
$di->set("response", new Response());
$dispatcher = new Dispatcher();
$dispatcher->setDI($di);
expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di");
$di->set("dispatcher", $dispatcher);
$dispatcher->setControllerName("test2");
$dispatcher->setActionName("index");
$dispatcher->setParams([]);
$controller = $dispatcher->dispatch();
expect($controller)->isInstanceOf("Test2Controller");
});
$this->specify("Dispatcher doesn't throw exception when action can't be found (2)", function () {
$di = new Di();
$di->set("response", new Response());
$dispatcher = new Dispatcher();
$dispatcher->setDI($di);
expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di");
$di->set("dispatcher", $dispatcher);
$dispatcher->setControllerName("test2");
$dispatcher->setActionName("essai");
$dispatcher->setParams([]);
$dispatcher->dispatch();
}, ["throws" => [\Phalcon\Mvc\Dispatcher\Exception::class, "Action 'essai' was not found on handler 'test2'"]]);
$this->specify("Dispatcher doesn't work as expected", function () {
$di = new Di();
$di->set("response", new Response());
$dispatcher = new Dispatcher();
$dispatcher->setDI($di);
expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di");
$di->set("dispatcher", $dispatcher);
$dispatcher->setControllerName("test2");
$dispatcher->setActionName("other");
$dispatcher->setParams([]);
$controller = $dispatcher->dispatch();
expect($controller)->isInstanceOf("Test2Controller");
});
$this->specify("Dispatcher doesn't work as expected (2)", function () {
$di = new Di();
$di->set("response", new Response());
$dispatcher = new Dispatcher();
$dispatcher->setDI($di);
expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di");
$di->set("dispatcher", $dispatcher);
$dispatcher->setControllerName("test2");
$dispatcher->setActionName("another");
$dispatcher->setParams([]);
$dispatcher->dispatch();
$value = $dispatcher->getReturnedValue();
expect($value)->equals(100);
});
$this->specify("Dispatcher doesn't work as expected (3)", function () {
$di = new Di();
$di->set("response", new Response());
$dispatcher = new Dispatcher();
$dispatcher->setDI($di);
expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di");
$di->set("dispatcher", $dispatcher);
$dispatcher->setControllerName("test2");
$dispatcher->setActionName("anotherTwo");
$dispatcher->setParams([2, "3"]);
$dispatcher->dispatch();
$value = $dispatcher->getReturnedValue();
expect($value)->equals(5);
});
$this->specify("Dispatcher doesn't work as expected (4)", function () {
$di = new Di();
$di->set("response", new Response());
$dispatcher = new Dispatcher();
$dispatcher->setDI($di);
expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di");
$di->set("dispatcher", $dispatcher);
$dispatcher->setControllerName("test2");
$dispatcher->setActionName("anotherthree");
$dispatcher->setParams([]);
$dispatcher->dispatch();
$value = $dispatcher->getActionName();
expect($value)->equals("anotherfour");
$value = $dispatcher->getReturnedValue();
expect($value)->equals(120);
});
$this->specify("Dispatcher doesn't work as expected (5)", function () {
$di = new Di();
$di->set("response", new Response());
$dispatcher = new Dispatcher();
$dispatcher->setDI($di);
expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di");
$di->set("dispatcher", $dispatcher);
$dispatcher->setControllerName("test2");
$dispatcher->setActionName("anotherFive");
$dispatcher->setParams(["param1" => 2, "param2" => 3]);
$dispatcher->dispatch();
$value = $dispatcher->getReturnedValue();
expect($value)->equals(5);
});
$this->specify("Dispatcher doesn't work as expected (6)", function () {
$di = new Di();
$di->set("response", new Response());
$dispatcher = new Dispatcher();
$dispatcher->setDI($di);
expect($dispatcher->getDI())->isInstanceOf("Phalcon\\Di");
$di->set("dispatcher", $dispatcher);
$dispatcher->setControllerName("test7");
$dispatcher->setActionName("service");
$dispatcher->setParams([]);
$dispatcher->dispatch();
$value = $dispatcher->getReturnedValue();
expect($value)->equals("hello");
$value = $dispatcher->getControllerClass();
expect($value)->equals("Test7Controller");
});
}