【问题标题】:Routing tests in ZF2 / ZF2 equivalent of Zend_Test_PHPUnit_Controller_TestCase?ZF2 / ZF2 中的路由测试相当于 Zend_Test_PHPUnit_Controller_TestCase?
【发布时间】:2012-10-08 14:19:06
【问题描述】:

到目前为止,我一直在测试我的 ZF2 控制器,如下所示:

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;
}

这允许我在视图呈现之前测试 ViewModel 是否分配了正确的数据(如果有)。这可以很好地达到这个目的,但它不能像 ZF1 Zend_Test_PHPUnit_Controller_TestCase 测试那样测试我的路由是否正常工作。

在这些情况下,我会通过运行$this->dispatch('/some/relative/url') 开始测试,并且只有在正确设置路线的情况下才能获得积极的测试结果。通过这些 ZF2 测试,我专门告诉它使用哪个路由,这并不一定意味着真正的请求会被正确路由。

如何测试我的路由在 ZF2 中是否正常工作?

【问题讨论】:

    标签: php unit-testing routes phpunit zend-framework2


    【解决方案1】:

    我迟到了,但它仍然对新人有用。现在的解决方案是从\Zend\Test\PHPUnit\Controller\AbstractControllerTestCase继承,所以用法和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');
        }
    }
    

    注意:这使用\Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase,其中包括assertQuery($path)以及其他与网络相关的方法。

    【讨论】:

    • 这实际上不允许测试控制器调度的返回值,因为 AbstractHttpControllerTestCase::dispatch() 不返回任何东西。
    • @EricMORAND,难道 $this->getResponse()->getContent() 不允许你测试任何需要测试的东西吗?
    【解决方案2】:

    编辑: 自从我自行回答后,ZF2 已更新。 PowerKiki's answer 更好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-03
      • 2016-01-20
      • 2016-05-05
      • 1970-01-01
      相关资源
      最近更新 更多