From aab26ec52242eefdb435788464699c3d0ab4f602 Mon Sep 17 00:00:00 2001 From: Tim Roediger Date: Thu, 2 May 2013 08:43:24 +1000 Subject: [PATCH 1/2] Repair broken test. Add new tests to prevent regression --- .../Controller/AbstractControllerTestCase.php | 11 +++-- .../AbstractControllerTestCaseTest.php | 41 +++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/PHPUnit/Controller/AbstractControllerTestCase.php b/src/PHPUnit/Controller/AbstractControllerTestCase.php index 7d5d3c7d51..8fe7e5d662 100644 --- a/src/PHPUnit/Controller/AbstractControllerTestCase.php +++ b/src/PHPUnit/Controller/AbstractControllerTestCase.php @@ -216,7 +216,7 @@ public function url($url, $method = HttpRequest::METHOD_GET, $params = array()) } elseif ($method == HttpRequest::METHOD_GET) { $query = array_merge($query, $params); } elseif ($method == HttpRequest::METHOD_PUT) { - if (!$content = $request->getContent()){ + if (count($params) != 0){ array_walk($params, function(&$item, $key) { $item = $key . '=' . $item; } ); @@ -250,10 +250,15 @@ 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 ($requestMethod = $this->getRequest()->getMethod()){ + 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); diff --git a/test/PHPUnit/Controller/AbstractControllerTestCaseTest.php b/test/PHPUnit/Controller/AbstractControllerTestCaseTest.php index 1dfd36f716..d9ae5647eb 100644 --- a/test/PHPUnit/Controller/AbstractControllerTestCaseTest.php +++ b/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()); + } } From 25790508503c9bb7757b9c2da787bbcf5c235752 Mon Sep 17 00:00:00 2001 From: Tim Roediger Date: Thu, 2 May 2013 09:06:43 +1000 Subject: [PATCH 2/2] Fix another bug in AbstractControllerTestCase - POST params ignored --- src/PHPUnit/Controller/AbstractControllerTestCase.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/PHPUnit/Controller/AbstractControllerTestCase.php b/src/PHPUnit/Controller/AbstractControllerTestCase.php index 8fe7e5d662..6317577f4d 100644 --- a/src/PHPUnit/Controller/AbstractControllerTestCase.php +++ b/src/PHPUnit/Controller/AbstractControllerTestCase.php @@ -212,7 +212,9 @@ 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) {