{"id":1285,"date":"2022-08-30T15:15:15","date_gmt":"2022-08-30T15:15:15","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/10\/zend-framework-2-unit-testing-controllers-post-get-data-not-sending-undefined-index-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:15:15","modified_gmt":"2022-08-30T15:15:15","slug":"zend-framework-2-unit-testing-controllers-post-get-data-not-sending-undefined-index-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/zend-framework-2-unit-testing-controllers-post-get-data-not-sending-undefined-index-collection-of-common-programming-errors\/","title":{"rendered":"Zend Framework 2 Unit testing controllers, POST\/GET data not sending. Undefined Index-Collection of common programming errors"},"content":{"rendered":"<p>I have been trying to test my controllers as per this tutorial -&gt; Zend Framework 2.1 Unit testing<\/p>\n<p>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 &#8220;Undefined Index&#8221; when I try to access that data from the $_POST array in the controller.<\/p>\n<p>I am using PHPUnit 3.7.17, Everything else works perfectly except for POST and GET data, I have tried the following code:<\/p>\n<pre><code>public function testIndexActionCanBeAccessed() {\n\n     $this-&gt;getRequest()\n    -&gt;setMethod(\"POST\")\n    -&gt;setPost(new \\Zend\\Stdlib\\Parameters(array('argument' =&gt; 'value')));\n\n    $response = $this-&gt;dispatch('\/app\/api\/index');\n    $this-&gt;assertResponseStatusCode(200);\n}\n<\/code><\/pre>\n<p>AND<\/p>\n<pre><code>public function testIndexActionCanBeAccessed() {\n\n    $post_data = array(\"argument\" =&gt; \"value\");\n\n    $response = $this-&gt;dispatch(\"\/app\/api\/index\", \"POST\", $post_data);\n    $this-&gt;assertResponseStatusCode(200);\n}\n<\/code><\/pre>\n<p>I can&#8217;t find any help on the web how to fix this issue. Can anyone help out? Any ideas?<\/p>\n<ol>\n<li>\n<p>Get\/Post data not sending, this issue is related to server, can you check the function use for fetching data from server.<\/p>\n<\/li>\n<li>\n<pre><code>    $p = new Parameters();\n    $p-&gt;set('username','foo');\n    $p-&gt;set('password','bar');\n    $this-&gt;getRequest()-&gt;setMethod('POST');\n    $this-&gt;getRequest()-&gt;setPost($p);\n    $this-&gt;dispatch('\/login');\n<\/code><\/pre>\n<p>This works for me. The Parameters() constructor doesn&#8217;t seem to be what you&#8217;re expecting it to be.<\/p>\n<p>The docs say it takes an array, but I could only get it to work this way. The Stdlib\\Parameters() constructor doesn&#8217;t seem to do anything with the array passed in.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-10 00:14:22. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have been trying to test my controllers as per this tutorial -&gt; 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 &#8220;Undefined Index&#8221; when I try to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,8],"tags":[],"class_list":["post-1285","post","type-post","status-publish","format-standard","hentry","category-uncategorized","category-zend-framework"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1285","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1285"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1285\/revisions"}],"predecessor-version":[{"id":8882,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1285\/revisions\/8882"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}