class Reader
{
- private $rawDocBlock;
- private $parameters;
- private $keyPattern = "[A-z0-9\_\-]+";
- private $endPattern = "[ ]*(?:@|\r\n|\n)";
- private $parsedAll = FALSE;
-
- public function __construct()
- {
- $arguments = func_get_args();
- $count = count($arguments);
-
- // get reflection from class or class/method
- // (depends on constructor arguments)
- if($count === 0) {
- throw new \Exception("No zero argument constructor allowed");
- } else if($count === 1) {
- $reflection = new \ReflectionClass($arguments[0]);
- } else {
- $type = $count === 3 ? $arguments[2] : "method";
-
- if($type === "method") {
- $reflection = new \ReflectionMethod($arguments[0], $arguments[1]);
- } else if($type === "property") {
- $reflection = new \ReflectionProperty($arguments[0], $arguments[1]);
- }
- }
-
- $this->rawDocBlock = $reflection->getDocComment();
- $this->parameters = array();
- }
-
- private function parseSingle($key)
- {
- if(isset($this->parameters[$key]))
- {
- return $this->parameters[$key];
- }
- else
- {
- if(preg_match("/@".preg_quote($key).$this->endPattern."/", $this->rawDocBlock, $match))
- {
- return TRUE;
- }
- else
- {
- preg_match_all("/@".preg_quote($key)." (.*)".$this->endPattern."/U", $this->rawDocBlock, $matches);
- $size = sizeof($matches[1]);
-
- // not found
- if($size === 0)
- {
- return NULL;
- }
- // found one, save as scalar
- elseif($size === 1)
- {
- return $this->parseValue($matches[1][0]);
- }
- // found many, save as array
- else
- {
- $this->parameters[$key] = array();
- foreach($matches[1] as $elem)
- {
- $this->parameters[$key][] = $this->parseValue($elem);
- }
-
- return $this->parameters[$key];
- }
- }
- }
- }
-
- private function parse()
- {
- $pattern = "/@(?=(.*)".$this->endPattern.")/U";
-
- preg_match_all($pattern, $this->rawDocBlock, $matches);
-
- foreach($matches[1] as $rawParameter)
- {
- if(preg_match("/^(".$this->keyPattern.") (.*)$/", $rawParameter, $match))
- {
- $parsedValue = $this->parseValue($match[2]);
- if(isset($this->parameters[$match[1]]))
- {
- $this->parameters[$match[1]] = array_merge((array)$this->parameters[$match[1]], (array)$parsedValue);
- }
- else
- {
- $this->parameters[$match[1]] = $parsedValue;
- }
- }
- else if(preg_match("/^".$this->keyPattern."$/", $rawParameter, $match))
- {
- $this->parameters[$rawParameter] = TRUE;
- }
- else
- {
- $this->parameters[$rawParameter] = NULL;
- }
- }
- }
-
- public function getVariableDeclarations($name)
- {
- $declarations = (array)$this->getParameter($name);
-
- foreach($declarations as &$declaration)
- {
- $declaration = $this->parseVariableDeclaration($declaration, $name);
- }
-
- return $declarations;
- }
-
- private function parseVariableDeclaration($declaration, $name)
- {
- $type = gettype($declaration);
-
- if($type !== 'string')
- {
- throw new \InvalidArgumentException(
- "Raw declaration must be string, $type given. Key='$name'.");
- }
-
- if(strlen($declaration) === 0)
- {
- throw new \InvalidArgumentException(
- "Raw declaration cannot have zero length. Key='$name'.");
- }
-
- $declaration = explode(" ", $declaration);
- if(sizeof($declaration) == 1)
- {
- // string is default type
- array_unshift($declaration, "string");
- }
-
- // take first two as type and name
- $declaration = array(
- 'type' => $declaration[0],
- 'name' => $declaration[1]
- );
-
- return $declaration;
- }
-
- private function parseValue($originalValue)
- {
- if($originalValue && $originalValue !== 'null')
- {
- $lower = strtolower($originalValue);
- if(($lower === "true" || $lower === "false" || $lower === "null") && $lower !== $originalValue)
- {
- $value = $originalValue;
- }
- else if( ($json = json_decode($originalValue,TRUE)) === NULL)
- {
- // try to json decode, if cannot then store as string
- $value = $originalValue;
- }
- else
- {
- $value = $json;
- }
- }
- else
- {
- $value = NULL;
- }
-
- return $value;
- }
-
- public function getParameters()
- {
- if(! $this->parsedAll)
- {
- $this->parse();
- $this->parsedAll = TRUE;
- }
-
- return $this->parameters;
- }
-
- public function getParameter($key)
- {
- return $this->parseSingle($key);
- }
+ private $rawDocBlock;
+ private $parameters;
+ private $keyPattern = "[A-z0-9\_\-]+";
+ private $endPattern = "[ ]*(?:@|\r\n|\n)";
+ private $parsedAll = false;
+
+ public function __construct()
+ {
+ $arguments = func_get_args();
+ $count = count($arguments);
+
+ // get reflection from class or class/method
+ // (depends on constructor arguments)
+ if ($count === 0) {
+ throw new \Exception("No zero argument constructor allowed");
+ } else if ($count === 1) {
+ $reflection = new \ReflectionClass($arguments[0]);
+ } else {
+ $type = $count === 3 ? $arguments[2] : "method";
+
+ if ($type === "method") {
+ $reflection = new \ReflectionMethod($arguments[0], $arguments[1]);
+ } else if ($type === "property") {
+ $reflection = new \ReflectionProperty($arguments[0], $arguments[1]);
+ }
+ }
+
+ $this->rawDocBlock = $reflection->getDocComment();
+ $this->parameters = array();
+ }
+
+ private function parseSingle($key)
+ {
+ if (isset($this->parameters[$key])) {
+ return $this->parameters[$key];
+ } else {
+ if (preg_match("/@" . preg_quote($key) . $this->endPattern . "/", $this->rawDocBlock, $match)) {
+ return true;
+ } else {
+ preg_match_all("/@" . preg_quote($key) . " (.*)" . $this->endPattern . "/U", $this->rawDocBlock, $matches);
+ $size = sizeof($matches[1]);
+
+ // not found
+ if ($size === 0) {
+ return null;
+ } // found one, save as scalar
+ elseif ($size === 1) {
+ return $this->parseValue($matches[1][0]);
+ } // found many, save as array
+ else {
+ $this->parameters[$key] = array();
+ foreach ($matches[1] as $elem) {
+ $this->parameters[$key][] = $this->parseValue($elem);
+ }
+
+ return $this->parameters[$key];
+ }
+ }
+ }
+ }
+
+ private function parse()
+ {
+ $pattern = "/@(?=(.*)" . $this->endPattern . ")/U";
+
+ preg_match_all($pattern, $this->rawDocBlock, $matches);
+
+ foreach ($matches[1] as $rawParameter) {
+ if (preg_match("/^(" . $this->keyPattern . ") (.*)$/", $rawParameter, $match)) {
+ $parsedValue = $this->parseValue($match[2]);
+ if (isset($this->parameters[$match[1]])) {
+ $this->parameters[$match[1]] = array_merge((array)$this->parameters[$match[1]], (array)$parsedValue);
+ } else {
+ $this->parameters[$match[1]] = $parsedValue;
+ }
+ } else if (preg_match("/^" . $this->keyPattern . "$/", $rawParameter, $match)) {
+ $this->parameters[$rawParameter] = true;
+ } else {
+ $this->parameters[$rawParameter] = null;
+ }
+ }
+ }
+
+ public function getVariableDeclarations($name)
+ {
+ $declarations = (array)$this->getParameter($name);
+
+ foreach ($declarations as &$declaration) {
+ $declaration = $this->parseVariableDeclaration($declaration, $name);
+ }
+
+ return $declarations;
+ }
+
+ private function parseVariableDeclaration($declaration, $name)
+ {
+ $type = gettype($declaration);
+
+ if ($type !== 'string') {
+ throw new \InvalidArgumentException(
+ "Raw declaration must be string, $type given. Key='$name'.");
+ }
+
+ if (strlen($declaration) === 0) {
+ throw new \InvalidArgumentException(
+ "Raw declaration cannot have zero length. Key='$name'.");
+ }
+
+ $declaration = explode(" ", $declaration);
+ if (sizeof($declaration) == 1) {
+ // string is default type
+ array_unshift($declaration, "string");
+ }
+
+ // take first two as type and name
+ $declaration = array(
+ 'type' => $declaration[0],
+ 'name' => $declaration[1],
+ );
+
+ return $declaration;
+ }
+
+ private function parseValue($originalValue)
+ {
+ if ($originalValue && $originalValue !== 'null') {
+ $trimmed = trim($originalValue);
+ $lower = strtolower($trimmed);
+
+ if (($lower === "true" || $lower === "false" || $lower === "null") && $lower !== $trimmed) {
+ $value = $trimmed;
+ } else if ($trimmed === "null") {
+ // php 7.1 doesn't seem to decode "null" as json properly
+ $value = null;
+ } else if (($json = json_decode($trimmed, true)) === null) {
+ // try to json decode, if cannot then store as string
+ $value = $trimmed;
+ } else {
+ $value = $json;
+ }
+ } else {
+ $value = null;
+ }
+
+ return $value;
+ }
+
+ public function getParameters()
+ {
+ if (!$this->parsedAll) {
+ $this->parse();
+ $this->parsedAll = true;
+ }
+
+ return $this->parameters;
+ }
+
+ public function getParameter($key)
+ {
+ return $this->parseSingle($key);
+ }
}
namespace frostbane\DocBlockReader\test;
use frostbane\DocBlockReader\Reader;
+use PHPUnit\Framework\TestCase;
/**
- * @number 1
+ * @number 1
* @string "123"
- * @string2 abc
+ * @string2 abc
* @array ["a", "b"]
* @object {"x": "y"}
* @nested {"x": {"y": "z"}}
* @nestedArray {"x": {"y": ["z", "p"]}}
*
* @trueVar
- * @null-var null
+ * @null-var null
*
- * @booleanTrue true
- * @string3 tRuE
+ * @booleanTrue true
+ * @string3 tRuE
* @booleanFalse false
- * @booleanNull null
+ * @booleanNull null
*
*/
-class ReaderTest extends \PHPUnit_Framework_TestCase
+class ReaderTest extends TestCase
{
- /**
- * @number 1
- * @string "123"
- * @string2 abc
- * @array ["a", "b"]
- * @object {"x": "y"}
- * @nested {"x": {"y": "z"}}
- * @nestedArray {"x": {"y": ["z", "p"]}}
- *
- * @trueVar
- * @null-var null
- *
- * @booleanTrue true
- * @string3 tRuE
- * @booleanFalse false
- * @booleanNull null
- *
- */
- public $myVar = "test";
-
- /**
- * @x 1
- * @y yes!
- */
- private $myVar2;
-
- /**
- * @Lalala ["somejsonarray", "2"]
- * @Lalala ["anotherjsonarray", "3"]
- */
- public $issue2;
-
- public function testPropertyParsing() {
- $reader = new Reader($this, 'myVar', 'property');
- $this->commonTest($reader);
- }
-
- public function testPropertyParsing2() {
- $reader = new Reader($this, 'myVar2', 'property');
- $x = $reader->getParameter("x");
- $y = $reader->getParameter("y");
- $this->assertSame(1, $x);
- $this->assertSame("yes!", $y);
- }
-
- /**
- * Issue: https://github.com/jan-swiecki/php-simple-annotations/issues/2
- * Thanks to @KrekkieD (https://github.com/KrekkieD) for reporting this issue!
- */
- public function testIssue2Problem() {
- $reader = new Reader($this, 'issue2', 'property');
- $Lalala = $reader->getParameters()["Lalala"];
-
- $this->assertSame(array("somejsonarray", "2", "anotherjsonarray", "3"), $Lalala);
- }
-
- public function testParserOne()
- {
- $reader = new Reader($this, 'parserFixture');
- $this->commonTest($reader);
- }
-
- public function commonTest($reader) {
- $parameters = $reader->getParameters();
-
- $this->assertNotEmpty($parameters);
-
- $this->assertArrayHasKey('number', $parameters);
- $this->assertArrayHasKey('string', $parameters);
- $this->assertArrayHasKey('array', $parameters);
- $this->assertArrayHasKey('object', $parameters);
- $this->assertArrayHasKey('nested', $parameters);
- $this->assertArrayHasKey('nestedArray', $parameters);
- $this->assertArrayHasKey('trueVar', $parameters);
- $this->assertArrayHasKey('null-var', $parameters);
- $this->assertArrayHasKey('booleanTrue', $parameters);
- $this->assertArrayHasKey('booleanFalse', $parameters);
- $this->assertArrayHasKey('booleanNull', $parameters);
- $this->assertArrayNotHasKey('non_existent_key', $parameters);
-
- $this->assertSame(1, $parameters['number']);
- $this->assertSame("123", $parameters['string']);
- $this->assertSame("abc", $parameters['string2']);
- $this->assertSame(array("a", "b"), $parameters['array']);
- $this->assertSame(array("x" => "y"), $parameters['object']);
- $this->assertSame(array("x" => array("y" => "z")), $parameters['nested']);
- $this->assertSame(array("x" => array("y" => array("z", "p"))), $parameters['nestedArray']);
- $this->assertSame(TRUE, $parameters['trueVar']);
- $this->assertSame(NULL, $parameters['null-var']);
-
- $this->assertSame(TRUE, $parameters['booleanTrue']);
- $this->assertSame("tRuE", $parameters['string3']);
- $this->assertSame(FALSE, $parameters['booleanFalse']);
- $this->assertSame(NULL, $parameters['booleanNull']);
- }
-
- public function testParserOneFromClass()
- {
- $reader = new Reader($this);
- $parameters = $reader->getParameters();
-
- $this->assertNotEmpty($parameters);
-
- $this->assertArrayHasKey('number', $parameters);
- $this->assertArrayHasKey('string', $parameters);
- $this->assertArrayHasKey('array', $parameters);
- $this->assertArrayHasKey('object', $parameters);
- $this->assertArrayHasKey('nested', $parameters);
- $this->assertArrayHasKey('nestedArray', $parameters);
- $this->assertArrayHasKey('trueVar', $parameters);
- $this->assertArrayHasKey('null-var', $parameters);
- $this->assertArrayHasKey('booleanTrue', $parameters);
- $this->assertArrayHasKey('booleanFalse', $parameters);
- $this->assertArrayHasKey('booleanNull', $parameters);
- $this->assertArrayNotHasKey('non_existent_key', $parameters);
-
- $this->assertSame(1, $parameters['number']);
- $this->assertSame("123", $parameters['string']);
- $this->assertSame("abc", $parameters['string2']);
- $this->assertSame(array("a", "b"), $parameters['array']);
- $this->assertSame(array("x" => "y"), $parameters['object']);
- $this->assertSame(array("x" => array("y" => "z")), $parameters['nested']);
- $this->assertSame(array("x" => array("y" => array("z", "p"))), $parameters['nestedArray']);
- $this->assertSame(TRUE, $parameters['trueVar']);
- $this->assertSame(NULL, $parameters['null-var']);
-
- $this->assertSame(TRUE, $parameters['booleanTrue']);
- $this->assertSame("tRuE", $parameters['string3']);
- $this->assertSame(FALSE, $parameters['booleanFalse']);
- $this->assertSame(NULL, $parameters['booleanNull']);
- }
-
- public function testParserTwo()
- {
- $reader = new Reader($this, 'parserFixture');
-
- $this->assertSame(1, $reader->getParameter('number'));
- $this->assertSame("123", $reader->getParameter('string'));
- $this->assertSame(array("x" => array("y" => array("z", "p"))),
- $reader->getParameter('nestedArray'));
-
- $this->assertSame(NULL, $reader->getParameter('nullVar'));
- $this->assertSame(NULL, $reader->getParameter('null-var'));
- $this->assertSame(NULL, $reader->getParameter('non-existent'));
- }
-
- /**
- * @number 1
- * @string "123"
- * @string2 abc
- * @array ["a", "b"]
- * @object {"x": "y"}
- * @nested {"x": {"y": "z"}}
- * @nestedArray {"x": {"y": ["z", "p"]}}
- *
- * @trueVar
- * @null-var null
- *
- * @booleanTrue true
- * @string3 tRuE
- * @booleanFalse false
- * @booleanNull null
- *
- */
- private function parserFixture()
- {
- }
-
- public function testParserEmpty()
- {
- $reader = new Reader($this, 'parserEmptyFixture');
- $parameters = $reader->getParameters();
- $this->assertSame(array(), $parameters);
- }
-
- private function parserEmptyFixture()
- {
- }
-
- public function testParserMulti()
- {
- $reader = new Reader($this, 'parserMultiFixture');
- $parameters = $reader->getParameters();
-
- $this->assertNotEmpty($parameters);
- $this->assertArrayHasKey('param', $parameters);
- $this->assertArrayHasKey('var', $parameters);
-
- $this->assertSame("x",$parameters["var"]);
- $this->assertSame(1024,$parameters["var2"]);
-
- $this->assertSame(
- array("string x", "integer y", "array z"),
- $parameters["param"]);
-
- }
-
- /**
- * @var x
- * @var2 1024
- * @param string x
- * @param integer y
- * @param array z
- */
- private function parserMultiFixture()
- {
- }
-
- public function testParserThree()
- {
- $reader = new Reader($this, 'fixtureThree');
- // $allowedRequest = $reader->getParameter("allowedRequest");
-
- $postParam = $reader->getParameter("postParam");
-
- $this->assertNotEmpty($postParam);
- }
-
- /**
- * @allowedRequest ["ajax", "post"]
- * @postParam integer orderId
- * @postParam array productIds
- * @postParam string newValue
- */
- private function fixtureThree()
- {
-
- }
-
- public function testParserFour()
- {
- $reader = new Reader($this, 'fixtureFour');
-
- $this->assertSame(TRUE, $reader->getParameter('get'));
- $this->assertSame(TRUE, $reader->getParameter('post'));
- $this->assertSame(TRUE, $reader->getParameter('ajax'));
- $this->assertSame(array("x","y","z"), $reader->getParameter('postParam'));
- }
-
- public function testParserFourBis()
- {
- $reader = new Reader($this, 'fixtureFour');
-
- $parameters = $reader->getParameters();
-
- $this->assertArrayHasKey('get', $parameters);
- $this->assertArrayHasKey('post', $parameters);
- $this->assertArrayHasKey('ajax', $parameters);
- $this->assertArrayHasKey('postParam', $parameters);
-
- $this->assertSame(TRUE, $parameters['get']);
- $this->assertSame(TRUE, $parameters['post']);
- $this->assertSame(TRUE, $parameters['ajax']);
- $this->assertSame(array("x","y","z"), $parameters['postParam']);
-
- }
-
- /**
- * @get @post
- * @ajax
- * @postParam x
- * @postParam y
- * @postParam z
- */
- private function fixtureFour()
- {
- }
-
- public function testFive()
- {
- $reader1 = new Reader($this, 'fixtureFive');
- $reader2 = new Reader($this, 'fixtureFive');
-
- $parameters1 = $reader1->getParameters();
-
- $trueVar1 = $parameters1['trueVar1'];
-
- $this->assertSame(TRUE,$trueVar1);
- $this->assertSame(TRUE,$reader2->getParameter("trueVar2"));
-
- }
-
- /**
- * @trueVar1
- * @trueVar2
- */
- private function fixtureFive()
- {
- }
-
- public function testVariableDeclarations()
- {
- $reader = new Reader($this, 'fixtureVariableDeclarations');
- $declarations = $reader->getVariableDeclarations("param");
- $this->assertNotEmpty($declarations);
-
- $this->assertSame(array(
- array("type"=>"string", "name" => "var1"),
- array("type"=>"integer", "name" => "var2")
- ), $declarations);
- }
-
- /**
- * @param string var1
- * @param integer var2
- */
- private function fixtureVariableDeclarations()
- {
- }
-
- /**
- * @dataProvider badVariableDataProvider
- * @expectedException InvalidArgumentException
- */
- public function testBadVariableDeclarations($methodName)
- {
- $reader = new Reader($this, $methodName);
- $declarations = $reader->getVariableDeclarations("param");
- }
-
- /**
- * @param false
- */
- private function fixtureBadVariableDeclarationsOne()
- {
- }
-
- /**
- * @param true
- */
- private function fixtureBadVariableDeclarationsTwo()
- {
- }
-
- public function badVariableDataProvider()
- {
- return array(
- array('fixtureBadVariableDeclarationsOne'),
- array('fixtureBadVariableDeclarationsTwo')
- );
- }
+ /**
+ * @number 1
+ * @string "123"
+ * @string2 abc
+ * @array ["a", "b"]
+ * @object {"x": "y"}
+ * @nested {"x": {"y": "z"}}
+ * @nestedArray {"x": {"y": ["z", "p"]}}
+ *
+ * @trueVar
+ * @null-var null
+ *
+ * @booleanTrue true
+ * @string3 tRuE
+ * @booleanFalse false
+ * @booleanNull null
+ *
+ */
+ public $myVar = "test";
+
+ /**
+ * @x 1
+ * @y yes!
+ */
+ private $myVar2;
+
+ /**
+ * @Lalala ["somejsonarray", "2"]
+ * @Lalala ["anotherjsonarray", "3"]
+ */
+ public $issue2;
+
+ public function testPropertyParsing()
+ {
+ $reader = new Reader($this, 'myVar', 'property');
+ $this->commonTest($reader);
+ }
+
+ public function testPropertyParsing2()
+ {
+ $reader = new Reader($this, 'myVar2', 'property');
+ $x = $reader->getParameter("x");
+ $y = $reader->getParameter("y");
+ $this->assertSame(1, $x);
+ $this->assertSame("yes!", $y);
+ }
+
+ /**
+ * Issue:
+ * https://github.com/jan-swiecki/php-simple-annotations/issues/2
+ * Thanks to @KrekkieD (https://github.com/KrekkieD) for
+ * reporting this issue!
+ */
+ public function testIssue2Problem()
+ {
+ $reader = new Reader($this, 'issue2', 'property');
+ $Lalala = $reader->getParameters()["Lalala"];
+
+ $this->assertSame(array("somejsonarray", "2", "anotherjsonarray", "3"), $Lalala);
+ }
+
+ public function testParserOne()
+ {
+ $reader = new Reader($this, 'parserFixture');
+ $this->commonTest($reader);
+ }
+
+ public function commonTest($reader)
+ {
+ $parameters = $reader->getParameters();
+
+ $this->assertNotEmpty($parameters);
+
+ $this->assertArrayHasKey('number', $parameters);
+ $this->assertArrayHasKey('string', $parameters);
+ $this->assertArrayHasKey('array', $parameters);
+ $this->assertArrayHasKey('object', $parameters);
+ $this->assertArrayHasKey('nested', $parameters);
+ $this->assertArrayHasKey('nestedArray', $parameters);
+ $this->assertArrayHasKey('trueVar', $parameters);
+ $this->assertArrayHasKey('null-var', $parameters);
+ $this->assertArrayHasKey('booleanTrue', $parameters);
+ $this->assertArrayHasKey('booleanFalse', $parameters);
+ $this->assertArrayHasKey('booleanNull', $parameters);
+ $this->assertArrayNotHasKey('non_existent_key', $parameters);
+
+ $this->assertSame(1, $parameters['number']);
+ $this->assertSame("123", $parameters['string']);
+ $this->assertSame("abc", $parameters['string2']);
+ $this->assertSame(array("a", "b"), $parameters['array']);
+ $this->assertSame(array("x" => "y"), $parameters['object']);
+ $this->assertSame(array("x" => array("y" => "z")), $parameters['nested']);
+ $this->assertSame(array("x" => array("y" => array("z", "p"))), $parameters['nestedArray']);
+ $this->assertSame(true, $parameters['trueVar']);
+ $this->assertSame(null, $parameters['null-var']);
+
+ $this->assertSame(true, $parameters['booleanTrue']);
+ $this->assertSame("tRuE", $parameters['string3']);
+ $this->assertSame(false, $parameters['booleanFalse']);
+ $this->assertSame(null, $parameters['booleanNull']);
+ }
+
+ public function testParserOneFromClass()
+ {
+ $reader = new Reader($this);
+ $parameters = $reader->getParameters();
+
+ $this->assertNotEmpty($parameters);
+
+ $this->assertArrayHasKey('number', $parameters);
+ $this->assertArrayHasKey('string', $parameters);
+ $this->assertArrayHasKey('array', $parameters);
+ $this->assertArrayHasKey('object', $parameters);
+ $this->assertArrayHasKey('nested', $parameters);
+ $this->assertArrayHasKey('nestedArray', $parameters);
+ $this->assertArrayHasKey('trueVar', $parameters);
+ $this->assertArrayHasKey('null-var', $parameters);
+ $this->assertArrayHasKey('booleanTrue', $parameters);
+ $this->assertArrayHasKey('booleanFalse', $parameters);
+ $this->assertArrayHasKey('booleanNull', $parameters);
+ $this->assertArrayNotHasKey('non_existent_key', $parameters);
+
+ $this->assertSame(1, $parameters['number']);
+ $this->assertSame("123", $parameters['string']);
+ $this->assertSame("abc", $parameters['string2']);
+ $this->assertSame(array("a", "b"), $parameters['array']);
+ $this->assertSame(array("x" => "y"), $parameters['object']);
+ $this->assertSame(array("x" => array("y" => "z")), $parameters['nested']);
+ $this->assertSame(array("x" => array("y" => array("z", "p"))), $parameters['nestedArray']);
+ $this->assertSame(true, $parameters['trueVar']);
+ $this->assertSame(null, $parameters['null-var']);
+
+ $this->assertSame(true, $parameters['booleanTrue']);
+ $this->assertSame("tRuE", $parameters['string3']);
+ $this->assertSame(false, $parameters['booleanFalse']);
+ $this->assertSame(null, $parameters['booleanNull']);
+ }
+
+ public function testParserTwo()
+ {
+ $reader = new Reader($this, 'parserFixture');
+
+ $this->assertSame(1, $reader->getParameter('number'));
+ $this->assertSame("123", $reader->getParameter('string'));
+ $this->assertSame(array("x" => array("y" => array("z", "p"))),
+ $reader->getParameter('nestedArray'));
+
+ $this->assertSame(null, $reader->getParameter('nullVar'));
+ $this->assertSame(null, $reader->getParameter('null-var'));
+ $this->assertSame(null, $reader->getParameter('non-existent'));
+ }
+
+ /**
+ * @number 1
+ * @string "123"
+ * @string2 abc
+ * @array ["a", "b"]
+ * @object {"x": "y"}
+ * @nested {"x": {"y": "z"}}
+ * @nestedArray {"x": {"y": ["z", "p"]}}
+ *
+ * @trueVar
+ * @null-var null
+ *
+ * @booleanTrue true
+ * @string3 tRuE
+ * @booleanFalse false
+ * @booleanNull null
+ *
+ */
+ private function parserFixture()
+ {
+ }
+
+ public function testParserEmpty()
+ {
+ $reader = new Reader($this, 'parserEmptyFixture');
+ $parameters = $reader->getParameters();
+ $this->assertSame(array(), $parameters);
+ }
+
+ private function parserEmptyFixture()
+ {
+ }
+
+ public function testParserMulti()
+ {
+ $reader = new Reader($this, 'parserMultiFixture');
+ $parameters = $reader->getParameters();
+
+ $this->assertNotEmpty($parameters);
+ $this->assertArrayHasKey('param', $parameters);
+ $this->assertArrayHasKey('var', $parameters);
+
+ $this->assertSame("x", $parameters["var"]);
+ $this->assertSame(1024, $parameters["var2"]);
+
+ $this->assertSame(
+ array("string x", "integer y", "array z"),
+ $parameters["param"]);
+
+ }
+
+ /**
+ * @var x
+ * @var2 1024
+ *
+ * @param string x
+ * @param integer y
+ * @param array z
+ */
+ private function parserMultiFixture()
+ {
+ }
+
+ public function testParserThree()
+ {
+ $reader = new Reader($this, 'fixtureThree');
+ // $allowedRequest = $reader->getParameter("allowedRequest");
+
+ $postParam = $reader->getParameter("postParam");
+
+ $this->assertNotEmpty($postParam);
+ }
+
+ /**
+ * @allowedRequest ["ajax", "post"]
+ * @postParam integer orderId
+ * @postParam array productIds
+ * @postParam string newValue
+ */
+ private function fixtureThree()
+ {
+
+ }
+
+ public function testParserFour()
+ {
+ $reader = new Reader($this, 'fixtureFour');
+
+ $this->assertSame(true, $reader->getParameter('get'));
+ $this->assertSame(true, $reader->getParameter('post'));
+ $this->assertSame(true, $reader->getParameter('ajax'));
+ $this->assertSame(array("x", "y", "z"), $reader->getParameter('postParam'));
+ }
+
+ public function testParserFourBis()
+ {
+ $reader = new Reader($this, 'fixtureFour');
+
+ $parameters = $reader->getParameters();
+
+ $this->assertArrayHasKey('get', $parameters);
+ $this->assertArrayHasKey('post', $parameters);
+ $this->assertArrayHasKey('ajax', $parameters);
+ $this->assertArrayHasKey('postParam', $parameters);
+
+ $this->assertSame(true, $parameters['get']);
+ $this->assertSame(true, $parameters['post']);
+ $this->assertSame(true, $parameters['ajax']);
+ $this->assertSame(array("x", "y", "z"), $parameters['postParam']);
+
+ }
+
+ /**
+ * @get @post
+ * @ajax
+ * @postParam x
+ * @postParam y
+ * @postParam z
+ */
+ private function fixtureFour()
+ {
+ }
+
+ public function testFive()
+ {
+ $reader1 = new Reader($this, 'fixtureFive');
+ $reader2 = new Reader($this, 'fixtureFive');
+
+ $parameters1 = $reader1->getParameters();
+
+ $trueVar1 = $parameters1['trueVar1'];
+
+ $this->assertSame(true, $trueVar1);
+ $this->assertSame(true, $reader2->getParameter("trueVar2"));
+
+ }
+
+ /**
+ * @trueVar1
+ * @trueVar2
+ */
+ private function fixtureFive()
+ {
+ }
+
+ public function testVariableDeclarations()
+ {
+ $reader = new Reader($this, 'fixtureVariableDeclarations');
+ $declarations = $reader->getVariableDeclarations("param");
+ $this->assertNotEmpty($declarations);
+
+ $this->assertSame(array(
+ array("type" => "string", "name" => "var1"),
+ array("type" => "integer", "name" => "var2"),
+ ), $declarations);
+ }
+
+ /**
+ * @param string var1
+ * @param integer var2
+ */
+ private function fixtureVariableDeclarations()
+ {
+ }
+
+ /**
+ * @dataProvider badVariableDataProvider
+ * @expectedException InvalidArgumentException
+ */
+ public function testBadVariableDeclarations($methodName)
+ {
+ $reader = new Reader($this, $methodName);
+ $declarations = $reader->getVariableDeclarations("param");
+ }
+
+ /**
+ * @param false
+ */
+ private function fixtureBadVariableDeclarationsOne()
+ {
+ }
+
+ /**
+ * @param true
+ */
+ private function fixtureBadVariableDeclarationsTwo()
+ {
+ }
+
+ public function badVariableDataProvider()
+ {
+ return array(
+ array('fixtureBadVariableDeclarationsOne'),
+ array('fixtureBadVariableDeclarationsTwo'),
+ );
+ }
}