[Pkg-owncloud-commits] [owncloud] 80/172: make it possible to omit parameters and use the default parameters from the controller method

David Prévot taffit at moszumanska.debian.org
Sun May 18 20:09:42 UTC 2014


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository owncloud.

commit a152e320f6aa98774950eb088588cd16387226f8
Author: Bernhard Posselt <dev at bernhard-posselt.com>
Date:   Tue May 13 10:40:49 2014 +0200

    make it possible to omit parameters and use the default parameters from the controller method
---
 lib/private/appframework/http/dispatcher.php       |  4 +--
 .../utility/controllermethodreflector.php          |  9 +++--
 tests/lib/appframework/http/DispatcherTest.php     | 40 ++++++++++++++++++----
 .../utility/ControllerMethodReflectorTest.php      |  6 ++--
 4 files changed, 46 insertions(+), 13 deletions(-)

diff --git a/lib/private/appframework/http/dispatcher.php b/lib/private/appframework/http/dispatcher.php
index 39ca339..442e33e 100644
--- a/lib/private/appframework/http/dispatcher.php
+++ b/lib/private/appframework/http/dispatcher.php
@@ -126,11 +126,11 @@ class Dispatcher {
 		// valid types that will be casted
 		$types = array('int', 'integer', 'bool', 'boolean', 'float');
 
-		foreach($this->reflector->getParameters() as $param) {
+		foreach($this->reflector->getParameters() as $param => $default) {
 
 			// try to get the parameter from the request object and cast
 			// it to the type annotated in the @param annotation
-			$value = $this->request->getParam($param);
+			$value = $this->request->getParam($param, $default);
 			$type = $this->reflector->getType($param);
 			
 			// if this is submitted using GET or a POST form, 'false' should be 
diff --git a/lib/private/appframework/utility/controllermethodreflector.php b/lib/private/appframework/utility/controllermethodreflector.php
index c9cdadc..a1519c7 100644
--- a/lib/private/appframework/utility/controllermethodreflector.php
+++ b/lib/private/appframework/utility/controllermethodreflector.php
@@ -59,7 +59,12 @@ class ControllerMethodReflector {
 
 		// get method parameters
 		foreach ($reflection->getParameters() as $param) {
-			$this->parameters[] = $param->name;
+			if($param->isOptional()) {
+				$default = $param->getDefaultValue();
+			} else {
+				$default = null;
+			}
+			$this->parameters[$param->name] = $default;
 		}
 	}
 
@@ -81,7 +86,7 @@ class ControllerMethodReflector {
 
 
 	/**
-	 * @return array the arguments of the method
+	 * @return array the arguments of the method with key => default value
 	 */
 	public function getParameters() {
 		return $this->parameters;
diff --git a/tests/lib/appframework/http/DispatcherTest.php b/tests/lib/appframework/http/DispatcherTest.php
index 2aef291..8117eec 100644
--- a/tests/lib/appframework/http/DispatcherTest.php
+++ b/tests/lib/appframework/http/DispatcherTest.php
@@ -40,11 +40,11 @@ class TestController extends Controller {
 	 * @param int $int
 	 * @param bool $bool
 	 */
-	public function exec($int, $bool) {
+	public function exec($int, $bool, $test=4, $test2=1) {
 		$this->registerResponder('text', function($in) {
 			return new JSONResponse(array('text' => $in));
 		});
-		return array($int, $bool);
+		return array($int, $bool, $test, $test2);
 	}
 }
 
@@ -262,6 +262,7 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase {
 			}));
 	}
 
+
 	public function testControllerParametersInjected() {
 		$this->request = new Request(array(
 			'post' => array(
@@ -280,10 +281,34 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase {
 		$this->dispatcherPassthrough();
 		$response = $this->dispatcher->dispatch($controller, 'exec');
 
-		$this->assertEquals('[3,true]', $response[2]);
+		$this->assertEquals('[3,true,4,1]', $response[2]);
+	}
+
+
+	public function testControllerParametersInjectedDefaultOverwritten() {
+		$this->request = new Request(array(
+			'post' => array(
+				'int' => '3',
+				'bool' => 'false',
+				'test2' => 7
+			),
+			'method' => 'POST'
+		));
+		$this->dispatcher = new Dispatcher(
+			$this->http, $this->middlewareDispatcher, $this->reflector,
+			$this->request
+		);
+		$controller = new TestController('app', $this->request);
+
+		// reflector is supposed to be called once
+		$this->dispatcherPassthrough();
+		$response = $this->dispatcher->dispatch($controller, 'exec');
+
+		$this->assertEquals('[3,true,4,7]', $response[2]);
 	}
 
 
+
 	public function testResponseTransformedByUrlFormat() {
 		$this->request = new Request(array(
 			'post' => array(
@@ -305,7 +330,7 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase {
 		$this->dispatcherPassthrough();
 		$response = $this->dispatcher->dispatch($controller, 'exec');
 
-		$this->assertEquals('{"text":[3,false]}', $response[2]);
+		$this->assertEquals('{"text":[3,false,4,1]}', $response[2]);
 	}
 
 
@@ -331,7 +356,7 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase {
 		$this->dispatcherPassthrough();
 		$response = $this->dispatcher->dispatch($controller, 'exec');
 
-		$this->assertEquals('{"text":[3,false]}', $response[2]);
+		$this->assertEquals('{"text":[3,false,4,1]}', $response[2]);
 	}
 
 
@@ -359,7 +384,10 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase {
 		$this->dispatcherPassthrough();
 		$response = $this->dispatcher->dispatch($controller, 'exec');
 
-		$this->assertEquals('{"text":[3,true]}', $response[2]);
+		$this->assertEquals('{"text":[3,true,4,1]}', $response[2]);
 	}
 
+
+
+
 }
diff --git a/tests/lib/appframework/utility/ControllerMethodReflectorTest.php b/tests/lib/appframework/utility/ControllerMethodReflectorTest.php
index cccc768..8939a20 100644
--- a/tests/lib/appframework/utility/ControllerMethodReflectorTest.php
+++ b/tests/lib/appframework/utility/ControllerMethodReflectorTest.php
@@ -88,7 +88,7 @@ class ControllerMethodReflectorTest extends \PHPUnit_Framework_TestCase {
 	}
 
 
-	public function arguments($arg, $arg2) {}
+	public function arguments($arg, $arg2='hi') {}
 	public function testReflectParameters() {
 		$reader = new ControllerMethodReflector();
 		$reader->reflect(
@@ -96,7 +96,7 @@ class ControllerMethodReflectorTest extends \PHPUnit_Framework_TestCase {
 			'arguments'
 		);
 
-		$this->assertEquals(array('arg', 'arg2'), $reader->getParameters());	
+		$this->assertEquals(array('arg' => null, 'arg2' => 'hi'), $reader->getParameters());	
 	}
 
 
@@ -108,7 +108,7 @@ class ControllerMethodReflectorTest extends \PHPUnit_Framework_TestCase {
 			'arguments2'
 		);
 
-		$this->assertEquals(array('arg',), $reader->getParameters());	
+		$this->assertEquals(array('arg' => null), $reader->getParameters());	
 	}
 
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud.git



More information about the Pkg-owncloud-commits mailing list