Zend_Controller_Router_Exception:默认路由未定义

发布于 2024-12-25 12:03:11 字数 2964 浏览 1 评论 0原文

我正在尝试测试控制器。 Zend 工具已生成以下代码:

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 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'
);

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

 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'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 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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

度的依靠╰つ 2025-01-01 12:03:11

看起来控制器测试用例存在问题未设置默认路由,如果已设置自定义路由(这是与框架不同的行为)。

该问题的补丁:

/**
 * 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 作为最后一个参数。

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

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

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).

The patch from the issue:

/**
 * 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);
}

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();
忆伤 2025-01-01 12:03:11

这听起来像是一个典型的问题,您没有在引导程序中正确配置应用程序,然后调用导致未定义路由的请求。在您的情况下,路线被指定为默认路线

路线可能是正确的,但只是未定义。

出于调试目的,输出您调用的 $url 以便您了解发生了什么。

This sounds like a typical problem you have not configured your app in the bootstrap properly and then invoking a request which leads to an undefined route. In your case the route is specified as Route default.

The route might be correct but just undefined.

For debugging purposes, output the $url you invoke so you see what's going on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文