Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix for issue #3945, and fix for PUT with request content #4366

Closed
wants to merge 3 commits into from

2 participants

@superdweebie

Small fixes to make the AbstractControllerTest more useful.

@weierophinney

@superdweebie Please take a look at the tests.

  • I tried merging locally, and immediately ran across an error where the request class was not an HTTP request, which made a call to getMethod() invalid.
  • After modifying the code to test the request type, I get 4 new failures.
@superdweebie

I'll be fixing it in a few hours. My mistake.

@superdweebie

Green :)

@weierophinney weierophinney closed this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/4366'
Close #4366
Fixes #3945
2159035
@weierophinney weierophinney referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/4366' into develop
Forward port #4366
45951c4
@weierophinney weierophinney was assigned
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/4366'
Close #4366
Fixes #3945
6719408
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/4366' into develop
Forward port #4366
7785b61
@weierophinney weierophinney referenced this pull request from a commit in zendframework/zend-test
@weierophinney weierophinney Merge pull request zendframework/zf2#4366 from superdweebie/AbstractC…
…ontrollerTest-fix

Fix for issue zendframework/zf2#3945, and fix for PUT with request content
c4e2470
@weierophinney weierophinney referenced this pull request from a commit in zendframework/zend-test
@weierophinney weierophinney Merge branch 'hotfix/4366' 41f8677
@weierophinney weierophinney referenced this pull request from a commit in zendframework/zend-test
@weierophinney weierophinney Merge branch 'hotfix/4366' into develop 26a7c2f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
28 library/Zend/Test/PHPUnit/Controller/AbstractControllerTestCase.php
@@ -19,7 +19,6 @@
use Zend\Stdlib\Parameters;
use Zend\Stdlib\ResponseInterface;
use Zend\Uri\Http as HttpUri;
-use Zend\View\Helper\Placeholder;
abstract class AbstractControllerTestCase extends PHPUnit_Framework_TestCase
{
@@ -213,15 +212,19 @@ public function url($url, $method = HttpRequest::METHOD_GET, $params = array())
}
if ($method == HttpRequest::METHOD_POST) {
- $post = $params;
+ if (count($params) != 0){
+ $post = $params;
+ }
} elseif ($method == HttpRequest::METHOD_GET) {
$query = array_merge($query, $params);
} elseif ($method == HttpRequest::METHOD_PUT) {
- array_walk($params,
- function(&$item, $key) { $item = $key . '=' . $item; }
- );
- $content = implode('&', $params);
- $request->setContent($content);
+ if (count($params) != 0){
+ array_walk($params,
+ function(&$item, $key) { $item = $key . '=' . $item; }
+ );
+ $content = implode('&', $params);
+ $request->setContent($content);
+ }
} elseif ($params) {
trigger_error(
'Additional params is only supported by GET, POST and PUT HTTP method',
@@ -249,8 +252,17 @@ function(&$item, $key) { $item = $key . '=' . $item; }
* @param array|null $params
* @throws \Exception
*/
- public function dispatch($url, $method = HttpRequest::METHOD_GET, $params = array())
+ public function dispatch($url, $method = null, $params = array())
{
+ if ( !isset($method) &&
+ $this->getRequest() instanceof HttpRequest &&
+ $requestMethod = $this->getRequest()->getMethod()
+ ) {
+ $method = $requestMethod;
+ } elseif (!isset($method)) {
+ $method = HttpRequest::METHOD_GET;
+ }
+
$this->url($url, $method, $params);
$this->getApplication()->run();
View
41 tests/ZendTest/Test/PHPUnit/Controller/AbstractControllerTestCaseTest.php
@@ -264,4 +264,45 @@ public function testDispatchRequestUri()
$this->dispatch('/tests');
$this->assertEquals('/tests', $this->getApplication()->getRequest()->getRequestUri());
}
+
+ public function testDefaultDispatchMethod()
+ {
+ $this->dispatch('/tests');
+ $this->assertEquals('GET', $this->getRequest()->getMethod());
+ }
+
+ public function testDispatchMethodSetOnRequest()
+ {
+ $this->getRequest()->setMethod('POST');
+ $this->dispatch('/tests');
+ $this->assertEquals('POST', $this->getRequest()->getMethod());
+ }
+
+ public function testExplicitDispatchMethodOverrideRequestMethod()
+ {
+ $this->getRequest()->setMethod('POST');
+ $this->dispatch('/tests', 'GET');
+ $this->assertEquals('GET', $this->getRequest()->getMethod());
+ }
+
+ public function testPutRequestParams()
+ {
+ $this->dispatch('/tests', 'PUT', array('a' => 1));
+ $this->assertEquals('a=1', $this->getRequest()->getContent());
+ }
+
+ public function testPreserveContentOfPutRequest()
+ {
+ $this->getRequest()->setMethod('PUT');
+ $this->getRequest()->setContent('my content');
+ $this->dispatch('/tests');
+ $this->assertEquals('my content', $this->getRequest()->getContent());
+ }
+
+ public function testExplicityPutParamsOverrideRequestContent()
+ {
+ $this->getRequest()->setContent('my content');
+ $this->dispatch('/tests', 'PUT', array('a' => 1));
+ $this->assertEquals('a=1', $this->getRequest()->getContent());
+ }
}
Something went wrong with that request. Please try again.