We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to test routes?

Hello,

I want to test my routes and visited https://docs.phalcon.io/en/latest/reference/routing.html#testing-your-routes . Unfortunately this does not work, I receive the error: Phalcon\Mvc\Router\Exception: Unexpected value type: expected object implementing Phalcon\DiInterface, null given from line $router->handle($testRoute); where $testRoute is e.g. "/member"

I also tried to call ->handle(); without argument, putting the $testRoute into $_GET['_url'] and setting $router->setUriSource(Phalcon\Mvc\Router::URI_SOURCE_GET_URL); - didn't change anything.

Any idea how I can test my routes?



2.2k

I can't reproduce it. You maybe do something wrong.

I run the code in https://docs.phalcon.io/en/latest/reference/routing.html#testing-your-routes .

Results:

Testing /
The route wasn't matched by any route

Testing /index
Controller: index
Action:

Testing /index/index
Controller: index
Action: index

Testing /index/test
Controller: index
Action: test

Testing /products
Controller: products
Action:

Testing /products/index/
Controller: products
Action: index

Testing /products/show/101
Controller: products
Action: show


8.6k

I use phalcon 1.3.4 with PHP 5.4

Here's my full code:

<?php

//These routes simulate real URIs
$testRoutes = array(
    '/',
    '/fooBar'
);

/** @var \Phalcon\Mvc\Router $router */
$router = new Phalcon\Mvc\Router(FALSE);
$router->removeExtraSlashes(TRUE);

$router->setDefaults(array(
    'namespace' => 'Streepoly\Controller',
    'controller' => 'index',
    'action' => 'index'
));

$router->notFound(array(
    'controller' => 'index',
    'action' => 'show404'
));

$router->addGet('/', array(
    'controller' => 'index',
    'action' => 'index'
));

//Testing each route
foreach ($testRoutes as $testRoute) {
    $router->handle($testRoute);

    echo 'Testing ', $testRoute . PHP_EOL;

    //Check if some route was matched
    if ($router->wasMatched()) {
        echo 'Controller: ', $router->getControllerName() . PHP_EOL;
        echo 'Action: ', $router->getActionName() . PHP_EOL;
    } else {
        echo 'The route wasn\'t matched by any route' . PHP_EOL;
    }
    echo PHP_EOL;

}
?>

The full error message I get is the following:

( ! ) Fatal error: Uncaught exception 'Phalcon\Mvc\Router\Exception' with message 'Unexpected value type: expected object implementing Phalcon\DiInterface, null given' in /home/dompie/project/public/testroutes.php on line 32
( ! ) Phalcon\Mvc\Router\Exception: Unexpected value type: expected object implementing Phalcon\DiInterface, null given in /home/dompie/project/public/testroutes.php on line 32
Call Stack
#   Time    Memory  Function    Location
1   0.0005  246712  {main}( )   ../testroutes.php:0
2   0.0006  251320  handle ( string(1) )    ../testroutes.php:32

Any suggestions what's wrong?



90
Accepted
answer

I was having this same problem, and after looking at Phalcon's unit tests, it looks like the Router needs a dependency injector to be able to use things other than the plain add() method (e.g. addGet() and addPost()). Here's a snippet from RouterMvcTest.php:

$router = new Phalcon\Mvc\Router();
$router->setDI($di);

$router->add('/docs/index', array(
    'controller' => 'documentation2',
    'action' => 'index'
));

$router->addPost('/docs/index', array(
    'controller' => 'documentation3',
    'action' => 'index'
));

So, I added this line right after creating my Router, and it now works:

$router->setDI($di);


8.6k
edited Mar '15

Thanks for sharing. In the meantime I solved the problem. The following things were missing/wrong in my approach:

  1. I was calling testroutes.php from CLI and not from "Web" sigh
  2. As I do not use default routes, the tests will test only GET routes - I added an extra loop in the test that adds GET to every route.
  3. I also needed to connect $di <-> $router

So this one is solved for me an can be closed.