2012-10-22 51 views
2

我正在尝试Zunfo专辑模块中的phpunit。我遇到了一个关于路由的错误。ZF2单元测试相册模块返回路由问题

以下是调试信息。它说'路由与名称'相册“未找到',但是当我检查相册模块文件夹中的module.config.php时,我发现这是正确设置,并在浏览器中重定向到该路线工作正常。

Album\Controller\AlbumControllerTest::testDeleteActionCanBeAccessed 
Zend\Mvc\Router\Exception\RuntimeException: Route with name "album" not found 
D:\www\zend2\vendor\zendframework\zendframework\library\Zend\Mvc\Router\SimpleRouteStack.php:292 
D:\www\zend2\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\Plugin\Url.php:88 
D:\www\zend2\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\Plugin\Redirect.php:54 
D:\www\zend2\module\Album\src\Album\Controller\AlbumController.php:80 
D:\www\zend2\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\AbstractActionController.php:87 
D:\www\zend2\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php:468 
D:\www\zend2\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php:208 
D:\www\zend2\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\AbstractController.php:108 
D:\www\zend2\tests\module\Album\src\Album\Controller\AlbumControllerTest.php:35 
C:\wamp\bin\php\php5.4.3\phpunit:46 

据我所知,在AlbumController.php线80的问题是

return $this->redirect()->toRoute('album'); 

但不知道为什么它不工作。任何人遇到并克服了这些问题?

+0

遇到是的,我还没有克服这个呢.. –

回答

1

我希望它可以节省约。 Zend Framework中2码搜索30分钟:

class AlbumControllerTest extends PHPUnit_Framework_TestCase 
{ 

//... 

    protected function setUp() 
    { 
     $bootstrap  = \Zend\Mvc\Application::init(include 'config/application.config.php'); 
     $this->controller = new AlbumController(); 
     $this->request = new Request(); 
     $this->routeMatch = new RouteMatch(array('controller' => 'index')); 
     $this->event  = $bootstrap->getMvcEvent(); 

     $router = new \Zend\Mvc\Router\SimpleRouteStack(); 
     $options = array(
        'route' => '/album[/:action][/:id]', 
        'constraints' => array(
         'action' => '[a-zA-Z][a-zA-Z0-9_-]*', 
         'id'  => '[0-9]+', 
        ), 
        'defaults' => array(
         'controller' => 'Album\Controller\Album', 
         'action'  => 'index', 
        ), 
      ); 
     $route = \Zend\Mvc\Router\Http\Segment::factory($options); 

     $router->addRoute('album', $route); 

     $this->event->setRouter($router); 
     $this->event->setRouteMatch($this->routeMatch); 
     $this->controller->setEvent($this->event); 
     $this->controller->setEventManager($bootstrap->getEventManager()); 
     $this->controller->setServiceLocator($bootstrap->getServiceManager()); 
    } 

} 
+0

添加冗余路由不是正确的解决方案。 – Starx

2

为了避免重复代码,你可以从模块配置加载路线:

$module = new \YourNameSpace\Module(); 
$config = $module->getConfig(); 

$route = \Zend\Mvc\Router\Http\Segment::factory($config['router']['routes']['Home']['options']); 

$router = new \Zend\Mvc\Router\SimpleRouteStack(); 
$router->addRoute('Home', $route); 
+0

我认为这段代码仍然会错过代码'$ this-> event-> setRouter($ router);''addRoute''完成后''。 – atilacamurca

0

其实最简单的方法是让从配置数据业务经理:

$config = $serviceManager->get('Config'); 

的功能setUp()的完整代码