Zend_Controller_Router_Exception:未定义默认路由 [英] Zend_Controller_Router_Exception: Route default is not defined

查看:72
本文介绍了Zend_Controller_Router_Exception:未定义默认路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试控制器. Zend Tool生成了以下代码:

I'm trying to test a controller. Zend Tool has generated the following code:

class Default_CarrinhoControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{

public function setUp()
{
    $this->bootstrap = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
    parent::setUp();
}

public function testIndexAction()
{
    $params = array('action' => 'index', 'controller' => 'Carrinho', 'module' => 'default');
    $urlParams = $this->urlizeOptions($params);
    $url = $this->url($urlParams);
    $this->dispatch($url);

    // assertions
    $this->assertModule($urlParams['module']);
    $this->assertController($urlParams['controller']);
    $this->assertAction($urlParams['action']);
    $this->assertQueryContentContains(
        'div#view-content p',
        'View script for controller <b>' . $params['controller'] . '</b> and script/action name <b>' . $params['action'] . '</b>'
        );
   }
}

PHPUnit引导程序

PHPUnit Bootstrap

<?php

// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')         : 'testing'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
        realpath(APPLICATION_PATH . '/../library'),
        get_include_path(),
    )));

require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();

// Create application, bootstrap, and run
$application = new Zend_Application(
            APPLICATION_ENV,
            APPLICATION_PATH . '/configs/application.ini'
);

但是它失败了,并且路由是正确的

But it has failed and route is correct

 Default_CarrinhoControllerTest::testIndexAction()
 Zend_Controller_Router_Exception: Route default is not defined
 C:\xampp\ZendFramework-1.11.11\library\Zend\Controller\Router\Rewrite.php:318
 C:\xampp\ZendFramework-1.11.11\library\Zend\Controller\Router\Rewrite.php:469
 C:\xampp\ZendFramework-1.11.11\library\Zend\Test\PHPUnit\ControllerTestCase.php:1180
 C:\htdocs\farmaciaonline\FarmaciaOnlineWeb\tests\application\modules\default\controllers\CarrinhoControllerTest.php:16
 C:\xampp\php\PEAR\PHPUnit\Framework\TestCase.php:939
 C:\xampp\php\PEAR\PHPUnit\Framework\TestCase.php:801
 C:\xampp\php\PEAR\PHPUnit\Framework\TestResult.php:649
 C:\xampp\php\PEAR\PHPUnit\Framework\TestCase.php:748
 C:\xampp\php\PEAR\PHPUnit\Framework\TestSuite.php:772
 C:\xampp\php\PEAR\PHPUnit\Framework\TestSuite.php:745
 C:\xampp\php\PEAR\PHPUnit\Framework\TestSuite.php:705
 C:\xampp\php\PEAR\PHPUnit\TextUI\TestRunner.php:325
 C:\xampp\php\PEAR\PHPUnit\TextUI\Command.php:187
 C:\xampp\php\PEAR\PHPUnit\TextUI\Command.php:125
 C:\xampp\php\phpunit:44

我有zend工具生成的默认phpunit生成的引导程序,我已经设置了一些自定义路由,但是默认路由仍在应用程序上运行.有什么问题吗?

I have the default phpunit generated bootstrap by zend tool, I've setted up some custom routes but the default routes are still working on the application. What could be wrong?

推荐答案

好像有一个如果设置了自定义路由,则控制器测试用例出现问题未设置默认路由(这是与框架不同的行为).

Looks like there's an issue with the the Controller Test Case not setting the default route, if custom routes have been set (this is a different behavior than the framework).

问题的修补程序:

/**
 * URL Helper
 * 
 * @param array $urlOptions
 * @param string $name
 * @param bool $reset
 * @param bool $encode
 */
public function url($urlOptions = array(), $name = null, $reset = false, $encode = true, $default = false)
{
    $frontController = $this->getFrontController();
    $router = $frontController->getRouter();
    if (!$router instanceof Zend_Controller_Router_Rewrite) {
        throw new Exception('This url helper utility function only works when the router is of type Zend_Controller_Router_Rewrite');
    }
    if ($default) {
        $router->addDefaultRoutes();
    }
    return $router->assemble($urlOptions, $name, $reset, $encode);
}

使用url()函数时,您需要将true作为最后一个参数传递.

You'll need to pass true as the last argument when using the url() function.

除了修补测试用例外,您还可以在引导过程中的某个位置添加默认路由:

Instead of patching the Test Case, you can just add the default routes someplace in the bootstrap process:

$frontController->getRouter()->addDefaultRoutes();

这篇关于Zend_Controller_Router_Exception:未定义默认路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆