浏览代码

Add Shell::param()

euromark 11 年之前
父节点
当前提交
fc9b288933
共有 3 个文件被更改,包括 60 次插入1 次删除
  1. 13 0
      src/Console/Shell.php
  2. 46 0
      tests/TestCase/Console/ShellTest.php
  3. 1 1
      tests/TestCase/Network/RequestTest.php

+ 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.
  * Prompts the user for input, and returns it.
  *
  *
  * @param string $prompt Prompt text.
  * @param string $prompt Prompt text.

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

@@ -788,6 +788,52 @@ TEXT;
 		$this->assertEquals($expected, $this->Shell->TestApple->name);
 		$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.
  * 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,
 			'truthy' => 1,
 			'zero' => '0',
 			'zero' => '0',
 		));
 		));
-		$this->assertEquals($expected, $request->param($toRead));
+		$this->assertSame($expected, $request->param($toRead));
 	}
 	}
 
 
 /**
 /**