2012-09-24 4 views
1

컨트롤러를 단위 테스트하려고하는데 routeMatch 객체에 몇 가지 추가 매개 변수를 전달하는 방법을 알 수 없습니다. routeMatch 객체에 추가 매개 변수를 전달하려면 어떻게해야합니까?

나는 http://devblog.x2k.co.uk/unit-testing-a-zend-framework-2-controller/http://devblog.x2k.co.uk/getting-the-servicemanager-into-the-test-environment-and-dependency-injection/에 tomoram에서 게시물을 따라,하지만 난 요청을 파견 할 때에/앨범 편집은 다음과 같은 예외/1, 예를 들어, 그것은 던졌습니다/: 여기

Zend\Mvc\Exception\DomainException: Url plugin requires that controller event compose a router; none found 

내입니다 PHPUnit 부트 스트랩 :

class Bootstrap 
{ 
    static $serviceManager; 
    static $di; 

    static public function go() 
    { 
     include 'init_autoloader.php'; 

     $config = include 'config/application.config.php'; 
     // append some testing configuration 
     $config['module_listener_options']['config_static_paths'] = array(getcwd() . '/config/test.config.php'); 

     // append some module-specific testing configuration 
     if (file_exists(__DIR__ . '/config/test.config.php')) { 
      $moduleConfig = include __DIR__ . '/config/test.config.php'; 
      array_unshift($config['module_listener_options']['config_static_paths'], $moduleConfig); 
     } 

     $serviceManager = Application::init($config)->getServiceManager(); 

     self::$serviceManager = $serviceManager; 

     // Setup Di 
     $di = new Di(); 

     $di->instanceManager()->addTypePreference('Zend\ServiceManager\ServiceLocatorInterface', 'Zend\ServiceManager\ServiceManager'); 
     $di->instanceManager()->addTypePreference('Zend\EventManager\EventManagerInterface', 'Zend\EventManager\EventManager'); 
     $di->instanceManager()->addTypePreference('Zend\EventManager\SharedEventManagerInterface', 'Zend\EventManager\SharedEventManager'); 

     self::$di = $di; 
    } 

    static public function getServiceManager() 
    { 
     return self::$serviceManager; 
    } 

    static public function getDi() 
    { 
     return self::$di; 
    } 

} 

Bootstrap::go(); 

기본적으로 우리는 Zend\Mvc\Application 환경을 만들고 있습니다.

PHPUnit_Framework_TestCase이 이렇게되면 사용자 정의 클래스에 묶여 :

abstract class ControllerTestCase extends TestCase 
{ 
    /** 
    * The ActionController we are testing 
    * 
    * @var Zend\Mvc\Controller\AbstractActionController 
    */ 
    protected $controller; 

    /** 
    * A request object 
    * 
    * @var Zend\Http\Request 
    */ 
    protected $request; 

    /** 
    * A response object 
    * 
    * @var Zend\Http\Response 
    */ 
    protected $response; 

    /** 
    * The matched route for the controller 
    * 
    * @var Zend\Mvc\Router\RouteMatch 
    */ 
    protected $routeMatch; 

    /** 
    * An MVC event to be assigned to the controller 
    * 
    * @var Zend\Mvc\MvcEvent 
    */ 
    protected $event; 

    /** 
    * The Controller fully qualified domain name, so each ControllerTestCase can create an instance 
    * of the tested controller 
    * 
    * @var string 
    */ 
    protected $controllerFQDN; 

    /** 
    * The route to the controller, as defined in the configuration files 
    * 
    * @var string 
    */ 
    protected $controllerRoute; 

    public function setup() 
    { 
     parent::setup(); 

     $di = \Bootstrap::getDi(); 

     // Create a Controller and set some properties 
     $this->controller = $di->newInstance($this->controllerFQDN); 

     $this->request = new Request(); 
     $this->routeMatch = new RouteMatch(array('controller' => $this->controllerRoute)); 
     $this->event  = new MvcEvent(); 

     $this->event->setRouteMatch($this->routeMatch); 

     $this->controller->setEvent($this->event); 
     $this->controller->setServiceLocator(\Bootstrap::getServiceManager()); 
    } 

    public function tearDown() 
    { 
     parent::tearDown(); 
     unset($this->controller); 
     unset($this->request); 
     unset($this->routeMatch); 
     unset($this->event); 
    } 
} 

그리고 우리는 컨트롤러 인스턴스와 RouteMatch와 요청을 만들 수 있습니다.

테스트를위한 코드 :

public function testEditActionWithGetRequest() 
{ 
    // Dispatch the edit action 
    $this->routeMatch->setParam('action', 'edit'); 
    $this->routeMatch->setParam('id', $album->id); 
    $result = $this->controller->dispatch($this->request, $this->response); 

    // rest of the code isn't executed 
} 

나는 내가 여기에 누락 모르겠어요. 테스트 부트 스트랩에 대한 구성이 될 수 있습니까? 아니면 다른 방법으로 매개 변수를 전달해야합니까? 아니면 무언가를 인스턴스화하는 것을 잊고 있습니까?

답변

0

이 문제를 해결하기 위해 수행 한 작업은 Application::init() 및 구성을 Bootstrap에서 setUp()으로 변경하는 것입니다. 로드하는 데 시간이 걸리지 만 작동합니다.

Bootstrap에는 오토로더를 구성하는 데 필요한 코드 만 있고 setUp() 방법은 이전 Bootstrap::go() 코드와 비슷한 코드가 있습니다.

관련 문제