dqprf0976 2012-10-08 14:19 采纳率: 100%
浏览 44
已采纳

ZF2 / ZF2中的路由测试相当于Zend_Test_PHPUnit_Controller_TestCase?

Thus far I've been testing my ZF2 controllers as follows:

namespace Application\Controller;

use Application\Controller\IndexController;
use Zend\Http\Request;
use Zend\Http\Response;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\RouteMatch;
use PHPUnit_Framework_TestCase;

class IndexControllerTest extends PHPUnit_Framework_TestCase
{
    public function testIndexActionCanBeAccessed()
    {
        $this->routeMatch->setParam('action', 'index');

        $result   = $this->controller->dispatch($this->request);
        $response = $this->controller->getResponse();

        $this->assertEquals(200, $response->getStatusCode());
        $this->assertInstanceOf('Zend\View\Model\ViewModel', $result);
    }

    protected function setUp()
    {
        \Zend\Mvc\Application::init(include 'config/application.config.php');

        $this->controller = new IndexController();
        $this->request    = new Request();
        $this->routeMatch = new RouteMatch(array('controller' => 'index'));
        $this->event      = new MvcEvent();
        $this->event->setRouteMatch($this->routeMatch);
        $this->controller->setEvent($this->event);
    }

    protected $controller = null;
    protected $event = null;
    protected $request = null;
    protected $response = null;
    protected $routeMatch = null;
}

This allows me to test that the ViewModel is having the correct data (if any) assigned to it before the view is rendered. This serves that purpose just fine, but what it doesn't do is test that my routing is working correctly like the ZF1 Zend_Test_PHPUnit_Controller_TestCase tests would.

In those, I'd kick off the test by running $this->dispatch('/some/relative/url') and only get positive test results if the routes were set up correctly. With these ZF2 tests, I'm specifically telling it which route to use, which doesn't necessarily mean that a real request will be routed correctly.

How do I test that my routing is working correctly in ZF2?

  • 写回答

2条回答 默认 最新

  • dreamMyDream2014 2013-03-06 02:20
    关注

    I'm late to the party, but it still could be useful for newcomers. The solution nowadays would be to inherit from \Zend\Test\PHPUnit\Controller\AbstractControllerTestCase, so the usage would be very similar to ZF1:

    class IndexControllerTest extends \Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase
    {
        public function setUp()
        {
            $this->setApplicationConfig(
                    include __DIR__ . '/../../../../../config/application.config.php'
            );
            parent::setUp();
        }
    
        public function testIndexActionCanBeAccessed()
        {
            $this->dispatch('/');
    
            $this->assertResponseStatusCode(200);
            $this->assertModuleName('application');
            $this->assertControllerName('application\controller\index');
            $this->assertControllerClass('IndexController');
            $this->assertMatchedRouteName('home');
    
            $this->assertQuery('html > head');
        }
    }
    

    Note: This uses \Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase which includes assertQuery($path) as well as other web related methods.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程