diff --git a/composer.json b/composer.json index 17d37e9..ed260d5 100644 --- a/composer.json +++ b/composer.json @@ -23,6 +23,9 @@ "autoload-dev": { "psr-4": { "Brick\\Reflection\\Tests\\": "tests/" - } + }, + "files": [ + "tests/functions.php" + ] } -} \ No newline at end of file +} diff --git a/tests/ImportResolverTest.php b/tests/ImportResolverTest.php index 07a598b..a596c17 100644 --- a/tests/ImportResolverTest.php +++ b/tests/ImportResolverTest.php @@ -51,4 +51,45 @@ 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->assertSame(ReflectionTarget::class, $resolver->resolve('ReflectionTarget')); + } + + public function testConstructorWithReflectionMethod() + { + $resolver = new ImportResolver(new \ReflectionMethod(ReflectionTarget::class, 'publicStaticMethod')); + + $this->assertSame(ReflectionTarget::class, $resolver->resolve('ReflectionTarget')); + } + + public function testConstructorWithReflectionParameter() + { + $resolver = new ImportResolver(new \ReflectionParameter([ + ReflectionTarget::class, 'privateFunc', + ], 'str')); + + $this->assertSame(ReflectionTarget::class, $resolver->resolve('ReflectionTarget')); + } + + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Cannot infer the declaring class from the given ReflectionFunction + */ + public function testConstructorWithReflectionFunctionThrowsException() + { + $resolver = new ImportResolver(new \ReflectionFunction('Brick\Reflection\Tests\reflectedFunc')); + } } diff --git a/tests/ReflectionTarget.php b/tests/ReflectionTarget.php new file mode 100644 index 0000000..fadbdd7 --- /dev/null +++ b/tests/ReflectionTarget.php @@ -0,0 +1,43 @@ +foo = 'foo'; + $this->bar = 'bar'; + } + + /** + * @param string $str + */ + private function privateFunc(string $str) + { + return $str; + } + + /** + * @return void + */ + public static function publicStaticMethod() {} +} \ No newline at end of file diff --git a/tests/ReflectionToolsTest.php b/tests/ReflectionToolsTest.php index 17645e7..6b5b804 100644 --- a/tests/ReflectionToolsTest.php +++ b/tests/ReflectionToolsTest.php @@ -19,6 +19,80 @@ public function testGetMethodsDoesNotReturnStaticMethods() $this->assertCount(0, $methods); } + public function testGetReflectionFunction() + { + $reflectionFunc = function() {}; + $function = (new ReflectionTools)->getReflectionFunction($reflectionFunc); + + $this->assertInstanceOf(\ReflectionFunction::class, $function); + $this->assertSame('Brick\Reflection\Tests\{closure}', $function->getName()); + } + + public function testGetFunctionParameterTypesShouldReturnEmptyArray() + { + $types = (new ReflectionTools)->getFunctionParameterTypes(new \ReflectionFunction('Brick\Reflection\Tests\reflectedFunc')); + + $this->assertSame([], $types); + } + + public function testGetFunctionParameterTypesShouldReturnTypesArray() + { + $types = (new ReflectionTools)->getFunctionParameterTypes(new \ReflectionFunction('Brick\Reflection\Tests\reflectedParameterFunc')); + + $this->assertSame(['arg' => ['string']], $types); + } + + public function testGetParameterTypesShouldReturnTypeArray() + { + $types = (new ReflectionTools)->getParameterTypes(new \ReflectionParameter([ + ReflectionTarget::class, 'privateFunc', + ], 'str')); + + $this->assertSame(['string'], $types); + } + + public function testGetPropertyTypesShouldReturnEmptyArray() + { + $types = (new ReflectionTools)->getPropertyTypes(new \ReflectionProperty(ReflectionTarget::class, 'foo')); + + $this->assertCount(0, $types); + } + + public function testGetPropertyTypesShouldReturnTypeArray() + { + $types = (new ReflectionTools)->getPropertyTypes(new \ReflectionProperty(ReflectionTarget::class, 'bar')); + + $this->assertSame(['string'], $types); + } + + 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/functions.php b/tests/functions.php new file mode 100644 index 0000000..0d097b9 --- /dev/null +++ b/tests/functions.php @@ -0,0 +1,20 @@ +