0

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.

Community
  • 1
  • 1

1 Answers1

0

AbstractControllerTestCase has a method dispatch that takes care of setting up various things.

Try exchanging $result = $this->controller->dispatch($this->request); with $this->dispatch('/url-to-use')

Hope it helps...

malte
  • 1,439
  • 1
  • 11
  • 12