I am new to Zend Framework 2 and phpunit-testing. First I had similar problem as here: Simple ZF2 Unit Tests for a controller using ZfcUser but this resulted a new problem I am trying to solve:
1) ApplicationControllerTest::testMyActionCanBeAccessed
Zend\Http\Exception\InvalidArgumentException: A field name was provided without
a field value
Here is my test code:
class ApplicationControllerTest extends AbstractControllerTestCase
{
protected $controller;
protected $request;
protected $response;
protected $routeMatch;
protected $event;
protected $mockedAuthenticationService;
public function setUp()
{
$serviceManager = Bootstrap::getServiceManager();
$this->controller = new ApplicationController();
$this->request = new Request();
$this->routeMatch = new RouteMatch(array
('application' => Application\Controller\Application'));
$this->event = new MvcEvent();
$config = $serviceManager->get('Config');
$this->event->setRouteMatch($this->routeMatch);
$this->controller->setEvent($this->event);
$this->controller->setServiceLocator($serviceManager);
$mockAuth = $this->getMock('ZfcUser\Entity\UserInterface');
$ZfcUserMock = $this->getMock('ZfcUser\Entity\User');
$ZfcUserMock->expects($this->any())
->method('getId')
->will($this->returnValue('1'));
$authMock = $this->getMock('ZfcUser\Controller\Plugin\ZfcUserAuthentication');
$authMock->expects($this->any())
->method('hasIdentity')
-> will($this->returnValue(true));
$authMock->expects($this->any())
->method('getIdentity')
->will($this->returnValue($ZfcUserMock));
$this->controller->getPluginManager()
->setService('zfcUserAuthentication', $authMock);
$this->setApplicationConfig(
include '/path/to/my/testing.php');
parent::setUp();
}
public function testMyActionCanBeAccessed(){
$this->routeMatch->setParam('action', 'my');
$result = $this->controller->dispatch($this->request);
$this->assertResponseStatusCode(200);
$this->assertModuleName('MyModule');
$this->assertControllerName('mymodule/application');
$this->assertControllerClass('ApplicationController');
$this->assertActionName('My');
$this->assertMatchedRouteName('My');
Stack trace is pointing to line "$result = $this->controller->dispatch($this->request);" so there seems to be something wrong with dispatch, but what? I have this same line in another test which is working fine.