我試圖單元測試一個控制器,但無法弄清楚如何將一些額外的參數傳遞給routeMatch對象。如何將額外參數傳遞給routeMatch對象?
我跟着從tomoram帖子在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/,但是當我嘗試派遣一個請求/專輯/編輯/ 1,例如,它會引發以下異常:
Zend\Mvc\Exception\DomainException: Url plugin requires that controller event compose a router; none found
這裏是我的PHPUnit Bootstrap:
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
}
我不知道我在這裏失蹤。它可以是測試引導的任何配置嗎?還是應該以其他方式傳遞參數?或者我忘了實例化一些東西?