2

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

しかし、なぜそれが機能していないのかわかりません。誰かがそのような問題に遭遇し、克服しましたか?

4

3 に答える 3

2

コードの重複を避けるために、ModuleConfigからルートをロードできます。

$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);
于 2012-11-22T14:37:40.193 に答える
1

私はそれが約を節約することを願っています。zendフレームワーク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());
    }

}
于 2012-10-27T17:24:13.517 に答える
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);
}
于 2013-03-31T06:15:31.110 に答える