Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/4366' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed May 2, 2013
3 parents ba39dc6 + 3922c9c + c4e2470 commit 26a7c2f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/PHPUnit/Controller/AbstractControllerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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();

Expand Down
41 changes: 41 additions & 0 deletions test/PHPUnit/Controller/AbstractControllerTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 26a7c2f

Please sign in to comment.