2012-10-22 2 views
2

Zf2 앨범 모듈에서 phpunit을 시험하고 있습니다. 라우팅에 대해 언급 한 오류가 발생했습니다.ZF2 단위 테스트 앨범 모듈이 라우팅 문제를 반환합니다.

다음은 디버그 정보입니다. 그것은 'Route with name'앨범을 찾지 못했지만, 앨범 모듈 폴더에서 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

대략 저장하길 바랍니다. 젠드 프레임 워크 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

나는이 코드가'addRoute'을 완료 한 후에 여전히'$ this-> event-> setRouter ($ router);'코드를 놓친다 고 생각합니다. – atilacamurca

0

사실 쉬운 방법은에서 구성 데이터를 얻을 수 있습니다 서비스 관리자 :

: 함수 setUp()에 대한

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

전체 코드

관련 문제