我正在尝试Zf2相册模块中的phpunit。我遇到了一个关于路由的错误。
以下是调试信息。它说“路由与名称”专辑“未找到”,但当我检查相册模块文件夹中的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 :(得分: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);
答案 1 :(得分:1)
我希望它可以节省约。在zend框架中搜索30分钟2代码:
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());
}
}
答案 2 :(得分:0)
实际上,简单的方法是从服务管理器获取配置数据:
$config = $serviceManager->get('Config');
函数setUp()
的完整代码:
protected function setUp() {
$serviceManager = Bootstrap::getServiceManager();
$this -> controller = new AlbumController();
$this -> request = new Request();
$this -> routeMatch = new RouteMatch(
array(
'controller' => 'index',
)
);
$this -> event = new MvcEvent();
$config = $serviceManager->get('Config');
$routerConfig = isset($config['router']) ? $config['router'] : array();
$router = HttpRouter::factory($routerConfig);
$this -> event -> setRouter($router);
$this -> event -> setRouteMatch($this -> routeMatch);
$this -> controller -> setEvent($this -> event);
$this -> controller -> setServiceLocator($serviceManager);
}