From 1bde7a103e678f4d52106210bbd3f4817542b310 Mon Sep 17 00:00:00 2001 From: peter279k Date: Mon, 5 Mar 2018 14:07:53 +0800 Subject: [PATCH 1/8] enhance ReflectionTool test --- tests/ReflectionTarget.php | 47 +++++++++++++++++++ tests/ReflectionToolsTest.php | 79 ++++++++++++++++++++++++++++++++ tests/reflectedFunc.php | 11 +++++ tests/reflectedParameterFunc.php | 12 +++++ 4 files changed, 149 insertions(+) create mode 100644 tests/ReflectionTarget.php create mode 100644 tests/reflectedFunc.php create mode 100644 tests/reflectedParameterFunc.php diff --git a/tests/ReflectionTarget.php b/tests/ReflectionTarget.php new file mode 100644 index 0000000..1836ee2 --- /dev/null +++ b/tests/ReflectionTarget.php @@ -0,0 +1,47 @@ +foo = 'foo'; + $this->bar = 'bar'; + } + + /** + * @param string $str + * @return string $str + */ + private function privateFunc(string $str) + { + return $str; + } + + /** + * @return void + */ + public static function publicStaticMethod() + { + return 'publicStaticMethod'; + } +} \ No newline at end of file diff --git a/tests/ReflectionToolsTest.php b/tests/ReflectionToolsTest.php index 17645e7..454c402 100644 --- a/tests/ReflectionToolsTest.php +++ b/tests/ReflectionToolsTest.php @@ -19,6 +19,85 @@ public function testGetMethodsDoesNotReturnStaticMethods() $this->assertCount(0, $methods); } + public function testGetReflectionFunction() + { + $reflectionFunc = function() + { + return 'reflectedFunction'; + }; + $functions = (new ReflectionTools)->getReflectionFunction($reflectionFunc); + + $this->assertCount(0, $functions->getParameters()); + } + + public function testGetFunctionParameterTypesShouldReturnEmptyArray() + { + $functions = (new ReflectionTools)->getFunctionParameterTypes(new \ReflectionFunction('Brick\Reflection\Tests\reflectedFunc')); + + $this->assertCount(0, $functions); + $this->assertSame([], $functions); + } + + public function testGetFunctionParameterTypesShouldReturnTypesArray() + { + $functions = (new ReflectionTools)->getFunctionParameterTypes(new \ReflectionFunction('Brick\Reflection\Tests\reflectedParameterFunc')); + + $this->assertCount(1, $functions); + $this->assertSame('string', $functions['arg'][0]); + } + + public function testGetParameterTypesShouldReturnTypeArray() + { + $parameters = (new ReflectionTools)->getParameterTypes(new \ReflectionParameter([ + ReflectionTarget::class, 'privateFunc', + ], 'str')); + + $this->assertCount(1, $parameters); + $this->assertSame('string', $parameters[0]); + } + + public function testGetPropertyTypesShouldReturnEmptyArray() + { + $properties = (new ReflectionTools)->getPropertyTypes(new \ReflectionProperty(ReflectionTarget::class, 'foo')); + + $this->assertCount(0, $properties); + } + + public function testGetPropertyTypesShouldReturnTypeArray() + { + $properties = (new ReflectionTools)->getPropertyTypes(new \ReflectionProperty(ReflectionTarget::class, 'bar')); + + $this->assertCount(1, $properties); + } + + public function testGetPropertyClassShouldReturnNull() + { + $propertyClass = (new ReflectionTools)->getPropertyClass(new \ReflectionProperty(ReflectionTarget::class, 'foo')); + + $this->assertNull($propertyClass); + } + + public function testGetPropertyClassShouldReturnTypeString() + { + $propertyClass = (new ReflectionTools)->getPropertyClass(new \ReflectionProperty(ReflectionTarget::class, 'barWithType')); + + $this->assertSame('\Exception', $propertyClass); + } + + public function testGetFunctionNameShouldReturnClassMethodName() + { + $functionName = (new ReflectionTools)->getFunctionName(new \ReflectionMethod(ReflectionTarget::class, 'publicStaticMethod')); + + $this->assertSame('Brick\Reflection\Tests\ReflectionTarget::publicStaticMethod', $functionName); + } + + public function testGetFunctionNameShouldReturnCurrentFunctionName() + { + $functionName = (new ReflectionTools)->getFunctionName(new \ReflectionFunction('Brick\Reflection\Tests\reflectedFunc')); + + $this->assertSame('Brick\Reflection\Tests\reflectedFunc', $functionName); + } + /** * @return void */ diff --git a/tests/reflectedFunc.php b/tests/reflectedFunc.php new file mode 100644 index 0000000..36acb2e --- /dev/null +++ b/tests/reflectedFunc.php @@ -0,0 +1,11 @@ + Date: Mon, 5 Mar 2018 14:36:09 +0800 Subject: [PATCH 2/8] complete ImportResolver test --- tests/ImportResolverTest.php | 43 ++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tests/ImportResolverTest.php b/tests/ImportResolverTest.php index 07a598b..0faeb8f 100644 --- a/tests/ImportResolverTest.php +++ b/tests/ImportResolverTest.php @@ -51,4 +51,47 @@ public function testAliasedImport() $this->assertResolve(Tools::class . '\A', 'Tools\A'); $this->assertResolve(Tools::class . '\A\B', 'Tools\A\B'); } + + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Cannot infer the file name from the given ReflectionObject + */ + public function testConstructorWithInvalidInferFileNameShouldThrowInvalidArgumentException() + { + $resolver = new ImportResolver(new \ReflectionObject(new \Exception)); + } + + public function testConstructorWithReflectionProperty() + { + $resolver = new ImportResolver(new \ReflectionProperty(ReflectionTarget::class, 'foo')); + + $this->assertResolve(ReflectionTarget::class, 'ReflectionTarget'); + } + + public function testConstructorWithReflectionMethod() + { + $resolver = new ImportResolver(new \ReflectionMethod(ReflectionTarget::class, 'publicStaticMethod')); + + $this->assertResolve('Brick\Reflection\Tests\publicStaticMethod', 'publicStaticMethod'); + } + + public function testConstructorWithReflectionParameter() + { + $resolver = new ImportResolver(new \ReflectionParameter([ + ReflectionTarget::class, 'privateFunc', + ], 'str')); + + $this->assertResolve('Brick\Reflection\Tests\privateFunc', 'privateFunc'); + } + + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Cannot infer the declaring class from the given ReflectionFunction + */ + public function testConstructorWithReflectedFunctionShouldThrowInvalidArgumentException() + { + $resolver = new ImportResolver(new \ReflectionFunction('Brick\Reflection\Tests\reflectedFunc')); + + $this->assertNull($resolver); + } } From c206ca632db3eb16c2f35cdbfddddf882fbefdd2 Mon Sep 17 00:00:00 2001 From: peter279k Date: Mon, 5 Mar 2018 15:03:45 +0800 Subject: [PATCH 3/8] set the correct autoload-dev files --- composer.json | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 17d37e9..cba6d0e 100644 --- a/composer.json +++ b/composer.json @@ -23,6 +23,10 @@ "autoload-dev": { "psr-4": { "Brick\\Reflection\\Tests\\": "tests/" - } + }, + "files": [ + "tests/reflectedFunc.php", + "tests/reflectedParameterFunc.php" + ] } } \ No newline at end of file From 14625aa0aca90014cfd5f6607c5581c3bb274563 Mon Sep 17 00:00:00 2001 From: peter279k Date: Mon, 5 Mar 2018 15:04:22 +0800 Subject: [PATCH 4/8] add EOF for composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index cba6d0e..7b5209a 100644 --- a/composer.json +++ b/composer.json @@ -29,4 +29,4 @@ "tests/reflectedParameterFunc.php" ] } -} \ No newline at end of file +} From 978255d9f1e7df8b61ff7da058a0bf0593ca6394 Mon Sep 17 00:00:00 2001 From: peter279k Date: Mon, 5 Mar 2018 23:18:10 +0800 Subject: [PATCH 5/8] approve request changes, merge the two functions --- composer.json | 3 +- tests/ImportResolverTest.php | 8 ++--- tests/ReflectionToolsTest.php | 33 ++++++++----------- ...flectedParameterFunc.php => functions.php} | 8 +++++ tests/reflectedFunc.php | 11 ------- 5 files changed, 26 insertions(+), 37 deletions(-) rename tests/{reflectedParameterFunc.php => functions.php} (66%) delete mode 100644 tests/reflectedFunc.php diff --git a/composer.json b/composer.json index 7b5209a..ed260d5 100644 --- a/composer.json +++ b/composer.json @@ -25,8 +25,7 @@ "Brick\\Reflection\\Tests\\": "tests/" }, "files": [ - "tests/reflectedFunc.php", - "tests/reflectedParameterFunc.php" + "tests/functions.php" ] } } diff --git a/tests/ImportResolverTest.php b/tests/ImportResolverTest.php index 0faeb8f..15e54c5 100644 --- a/tests/ImportResolverTest.php +++ b/tests/ImportResolverTest.php @@ -65,14 +65,14 @@ public function testConstructorWithReflectionProperty() { $resolver = new ImportResolver(new \ReflectionProperty(ReflectionTarget::class, 'foo')); - $this->assertResolve(ReflectionTarget::class, 'ReflectionTarget'); + $this->assertSame(ReflectionTarget::class, $resolver->resolve('ReflectionTarget')); } public function testConstructorWithReflectionMethod() { $resolver = new ImportResolver(new \ReflectionMethod(ReflectionTarget::class, 'publicStaticMethod')); - $this->assertResolve('Brick\Reflection\Tests\publicStaticMethod', 'publicStaticMethod'); + $this->assertSame('Brick\Reflection\Tests\publicStaticMethod', $resolver->resolve('publicStaticMethod')); } public function testConstructorWithReflectionParameter() @@ -81,7 +81,7 @@ public function testConstructorWithReflectionParameter() ReflectionTarget::class, 'privateFunc', ], 'str')); - $this->assertResolve('Brick\Reflection\Tests\privateFunc', 'privateFunc'); + $this->assertSame('Brick\Reflection\Tests\privateFunc', $resolver->resolve('privateFunc')); } /** @@ -91,7 +91,5 @@ public function testConstructorWithReflectionParameter() public function testConstructorWithReflectedFunctionShouldThrowInvalidArgumentException() { $resolver = new ImportResolver(new \ReflectionFunction('Brick\Reflection\Tests\reflectedFunc')); - - $this->assertNull($resolver); } } diff --git a/tests/ReflectionToolsTest.php b/tests/ReflectionToolsTest.php index 454c402..77b9e7b 100644 --- a/tests/ReflectionToolsTest.php +++ b/tests/ReflectionToolsTest.php @@ -21,53 +21,48 @@ public function testGetMethodsDoesNotReturnStaticMethods() public function testGetReflectionFunction() { - $reflectionFunc = function() - { - return 'reflectedFunction'; - }; - $functions = (new ReflectionTools)->getReflectionFunction($reflectionFunc); + $reflectionFunc = function() {}; + $function = (new ReflectionTools)->getReflectionFunction($reflectionFunc); - $this->assertCount(0, $functions->getParameters()); + $this->assertInstanceOf(\ReflectionFunction::class, $function); + $this->assertSame('Brick\Reflection\Tests\{closure}', $function->getName()); } public function testGetFunctionParameterTypesShouldReturnEmptyArray() { - $functions = (new ReflectionTools)->getFunctionParameterTypes(new \ReflectionFunction('Brick\Reflection\Tests\reflectedFunc')); + $types = (new ReflectionTools)->getFunctionParameterTypes(new \ReflectionFunction('Brick\Reflection\Tests\reflectedFunc')); - $this->assertCount(0, $functions); - $this->assertSame([], $functions); + $this->assertSame([], $types); } public function testGetFunctionParameterTypesShouldReturnTypesArray() { - $functions = (new ReflectionTools)->getFunctionParameterTypes(new \ReflectionFunction('Brick\Reflection\Tests\reflectedParameterFunc')); + $types = (new ReflectionTools)->getFunctionParameterTypes(new \ReflectionFunction('Brick\Reflection\Tests\reflectedParameterFunc')); - $this->assertCount(1, $functions); - $this->assertSame('string', $functions['arg'][0]); + $this->assertSame(['arg' => ['string']], $types); } public function testGetParameterTypesShouldReturnTypeArray() { - $parameters = (new ReflectionTools)->getParameterTypes(new \ReflectionParameter([ + $types = (new ReflectionTools)->getParameterTypes(new \ReflectionParameter([ ReflectionTarget::class, 'privateFunc', ], 'str')); - $this->assertCount(1, $parameters); - $this->assertSame('string', $parameters[0]); + $this->assertSame(['string'], $types); } public function testGetPropertyTypesShouldReturnEmptyArray() { - $properties = (new ReflectionTools)->getPropertyTypes(new \ReflectionProperty(ReflectionTarget::class, 'foo')); + $types = (new ReflectionTools)->getPropertyTypes(new \ReflectionProperty(ReflectionTarget::class, 'foo')); - $this->assertCount(0, $properties); + $this->assertCount(0, $types); } public function testGetPropertyTypesShouldReturnTypeArray() { - $properties = (new ReflectionTools)->getPropertyTypes(new \ReflectionProperty(ReflectionTarget::class, 'bar')); + $types = (new ReflectionTools)->getPropertyTypes(new \ReflectionProperty(ReflectionTarget::class, 'bar')); - $this->assertCount(1, $properties); + $this->assertSame(['string'], $types); } public function testGetPropertyClassShouldReturnNull() diff --git a/tests/reflectedParameterFunc.php b/tests/functions.php similarity index 66% rename from tests/reflectedParameterFunc.php rename to tests/functions.php index 75aa338..0d097b9 100644 --- a/tests/reflectedParameterFunc.php +++ b/tests/functions.php @@ -2,6 +2,14 @@ namespace Brick\Reflection\Tests; +/** + * The Target Reflection function without parameter. + */ +function reflectedFunc() +{ + return 'test'; +} + /** * The Target Reflection function with string parameter. * @param string $arg diff --git a/tests/reflectedFunc.php b/tests/reflectedFunc.php deleted file mode 100644 index 36acb2e..0000000 --- a/tests/reflectedFunc.php +++ /dev/null @@ -1,11 +0,0 @@ - Date: Tue, 6 Mar 2018 11:16:43 +0800 Subject: [PATCH 6/8] fix the assertions and test method name --- tests/ImportResolverTest.php | 6 +++--- tests/ReflectionTarget.php | 13 +++++-------- tests/ReflectionToolsTest.php | 4 ++-- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/tests/ImportResolverTest.php b/tests/ImportResolverTest.php index 15e54c5..a596c17 100644 --- a/tests/ImportResolverTest.php +++ b/tests/ImportResolverTest.php @@ -72,7 +72,7 @@ public function testConstructorWithReflectionMethod() { $resolver = new ImportResolver(new \ReflectionMethod(ReflectionTarget::class, 'publicStaticMethod')); - $this->assertSame('Brick\Reflection\Tests\publicStaticMethod', $resolver->resolve('publicStaticMethod')); + $this->assertSame(ReflectionTarget::class, $resolver->resolve('ReflectionTarget')); } public function testConstructorWithReflectionParameter() @@ -81,14 +81,14 @@ public function testConstructorWithReflectionParameter() ReflectionTarget::class, 'privateFunc', ], 'str')); - $this->assertSame('Brick\Reflection\Tests\privateFunc', $resolver->resolve('privateFunc')); + $this->assertSame(ReflectionTarget::class, $resolver->resolve('ReflectionTarget')); } /** * @expectedException \InvalidArgumentException * @expectedExceptionMessage Cannot infer the declaring class from the given ReflectionFunction */ - public function testConstructorWithReflectedFunctionShouldThrowInvalidArgumentException() + public function testConstructorWithReflectionFunctionThrowsException() { $resolver = new ImportResolver(new \ReflectionFunction('Brick\Reflection\Tests\reflectedFunc')); } diff --git a/tests/ReflectionTarget.php b/tests/ReflectionTarget.php index 1836ee2..63f7b57 100644 --- a/tests/ReflectionTarget.php +++ b/tests/ReflectionTarget.php @@ -8,7 +8,7 @@ class ReflectionTarget { /** - * @param string $foo + * @param string */ private $foo; @@ -18,7 +18,7 @@ class ReflectionTarget private $bar; /** - * @var \\Exception $barWithType + * @var \Exception $barWithType */ private $barWithType; @@ -29,8 +29,8 @@ public function __construct() } /** - * @param string $str - * @return string $str + * @param string + * @return string */ private function privateFunc(string $str) { @@ -40,8 +40,5 @@ private function privateFunc(string $str) /** * @return void */ - public static function publicStaticMethod() - { - return 'publicStaticMethod'; - } + public static function publicStaticMethod() {} } \ No newline at end of file diff --git a/tests/ReflectionToolsTest.php b/tests/ReflectionToolsTest.php index 77b9e7b..294192b 100644 --- a/tests/ReflectionToolsTest.php +++ b/tests/ReflectionToolsTest.php @@ -48,7 +48,7 @@ public function testGetParameterTypesShouldReturnTypeArray() ReflectionTarget::class, 'privateFunc', ], 'str')); - $this->assertSame(['string'], $types); + $this->assertSame([], $types); } public function testGetPropertyTypesShouldReturnEmptyArray() @@ -76,7 +76,7 @@ public function testGetPropertyClassShouldReturnTypeString() { $propertyClass = (new ReflectionTools)->getPropertyClass(new \ReflectionProperty(ReflectionTarget::class, 'barWithType')); - $this->assertSame('\Exception', $propertyClass); + $this->assertSame('Exception', $propertyClass); } public function testGetFunctionNameShouldReturnClassMethodName() From 91cab5638653b6995b2079a3147084e80431816b Mon Sep 17 00:00:00 2001 From: peter279k Date: Sun, 18 Mar 2018 00:46:08 +0800 Subject: [PATCH 7/8] fix the typo doc block --- tests/ReflectionTarget.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/ReflectionTarget.php b/tests/ReflectionTarget.php index 63f7b57..fadbdd7 100644 --- a/tests/ReflectionTarget.php +++ b/tests/ReflectionTarget.php @@ -29,8 +29,7 @@ public function __construct() } /** - * @param string - * @return string + * @param string $str */ private function privateFunc(string $str) { From 5a11f9ab0c3fc8315ce430caf7dc3169cdbae994 Mon Sep 17 00:00:00 2001 From: peter279k Date: Sun, 18 Mar 2018 00:59:14 +0800 Subject: [PATCH 8/8] fix the assertions value --- tests/ReflectionToolsTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ReflectionToolsTest.php b/tests/ReflectionToolsTest.php index 294192b..6b5b804 100644 --- a/tests/ReflectionToolsTest.php +++ b/tests/ReflectionToolsTest.php @@ -48,7 +48,7 @@ public function testGetParameterTypesShouldReturnTypeArray() ReflectionTarget::class, 'privateFunc', ], 'str')); - $this->assertSame([], $types); + $this->assertSame(['string'], $types); } public function testGetPropertyTypesShouldReturnEmptyArray()