Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test enhancement #1

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
"autoload-dev": {
"psr-4": {
"Brick\\Reflection\\Tests\\": "tests/"
}
},
"files": [
"tests/functions.php"
]
}
}
}
41 changes: 41 additions & 0 deletions tests/ImportResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}
43 changes: 43 additions & 0 deletions tests/ReflectionTarget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Brick\Reflection\Tests;

/**
* The Reflection Target class.
*/
class ReflectionTarget
{
/**
* @param string
*/
private $foo;

/**
* @var string $bar
Copy link
Member

@BenMorel BenMorel Mar 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be @var string, without variable name

*/
private $bar;

/**
* @var \Exception $barWithType
*/
private $barWithType;

public function __construct()
{
$this->foo = 'foo';
$this->bar = 'bar';
}

/**
* @param string $str
*/
private function privateFunc(string $str)
{
return $str;
}

/**
* @return void
*/
public static function publicStaticMethod() {}
}
74 changes: 74 additions & 0 deletions tests/ReflectionToolsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
20 changes: 20 additions & 0 deletions tests/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Brick\Reflection\Tests;

/**
* The Target Reflection function without parameter.
*/
function reflectedFunc()
{
return 'test';
}

/**
* The Target Reflection function with string parameter.
* @param string $arg
*/
function reflectedParameterFunc(string $arg)
{
return isset($arg) ? $arg : 'test';
}