Zend Framework 2 Unit testing controllers, POST/GET data not sending. Undefined Index-Collection of common programming errors

I have been trying to test my controllers as per this tutorial -> Zend Framework 2.1 Unit testing

I have tried every possible variation of the code to send POST or GET data along with the dispatch but the only thing I get back from running the test is “Undefined Index” when I try to access that data from the $_POST array in the controller.

I am using PHPUnit 3.7.17, Everything else works perfectly except for POST and GET data, I have tried the following code:

public function testIndexActionCanBeAccessed() {

     $this->getRequest()
    ->setMethod("POST")
    ->setPost(new \Zend\Stdlib\Parameters(array('argument' => 'value')));

    $response = $this->dispatch('/app/api/index');
    $this->assertResponseStatusCode(200);
}

AND

public function testIndexActionCanBeAccessed() {

    $post_data = array("argument" => "value");

    $response = $this->dispatch("/app/api/index", "POST", $post_data);
    $this->assertResponseStatusCode(200);
}

I can’t find any help on the web how to fix this issue. Can anyone help out? Any ideas?

  1. Get/Post data not sending, this issue is related to server, can you check the function use for fetching data from server.

  2.     $p = new Parameters();
        $p->set('username','foo');
        $p->set('password','bar');
        $this->getRequest()->setMethod('POST');
        $this->getRequest()->setPost($p);
        $this->dispatch('/login');
    

    This works for me. The Parameters() constructor doesn’t seem to be what you’re expecting it to be.

    The docs say it takes an array, but I could only get it to work this way. The Stdlib\Parameters() constructor doesn’t seem to do anything with the array passed in.

Originally posted 2013-11-10 00:14:22.