Browse Source

Add Shell::param()

euromark 11 years ago
parent
commit
fc9b288933

+ 13 - 0
src/Console/Shell.php

@@ -429,6 +429,19 @@ class Shell {
 	}
 
 /**
+ * Safely access the values in $this->params.
+ *
+ * @param string $name The name of the parameter to get.
+ * @return string|bool|null Value. Will return null if it doesn't exist.
+ */
+	public function param($name) {
+		if (!isset($this->params[$name])) {
+			return null;
+		}
+		return $this->params[$name];
+	}
+
+/**
  * Prompts the user for input, and returns it.
  *
  * @param string $prompt Prompt text.

+ 46 - 0
tests/TestCase/Console/ShellTest.php

@@ -788,6 +788,52 @@ TEXT;
 		$this->assertEquals($expected, $this->Shell->TestApple->name);
 	}
 
+
+/**
+ * Test reading params
+ *
+ * @dataProvider paramReadingDataProvider
+ */
+	public function testParamReading($toRead, $expected) {
+		$this->Shell->params = array(
+			'key' => 'value',
+			'help' => false,
+			'emptykey' => '',
+			'truthy' => true
+		);
+		$this->assertSame($expected, $this->Shell->param($toRead));
+	}
+
+/**
+ * Data provider for testing reading values with Shell::param()
+ *
+ * @return array
+ */
+	public function paramReadingDataProvider() {
+		return array(
+			array(
+				'key',
+				'index',
+			),
+			array(
+				'help',
+				false,
+			),
+			array(
+				'emptykey',
+				'',
+			),
+			array(
+				'truthy',
+				true,
+			),
+			array(
+				'does_not_exist',
+				null,
+			)
+		);
+	}
+
 /**
  * Test that option parsers are created with the correct name/command.
  *

+ 1 - 1
tests/TestCase/Network/RequestTest.php

@@ -1927,7 +1927,7 @@ class RequestTest extends TestCase {
 			'truthy' => 1,
 			'zero' => '0',
 		));
-		$this->assertEquals($expected, $request->param($toRead));
+		$this->assertSame($expected, $request->param($toRead));
 	}
 
 /**