-
Notifications
You must be signed in to change notification settings - Fork 3
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
base: master
Are you sure you want to change the base?
Changes from 4 commits
1bde7a1
506bdf8
c206ca6
14625aa
978255d
6583e51
acbbd21
91cab56
5a11f9a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, the first line is unused. |
||
} | ||
|
||
public function testConstructorWithReflectionParameter() | ||
{ | ||
$resolver = new ImportResolver(new \ReflectionParameter([ | ||
ReflectionTarget::class, 'privateFunc', | ||
], 'str')); | ||
|
||
$this->assertResolve('Brick\Reflection\Tests\privateFunc', 'privateFunc'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
} | ||
|
||
/** | ||
* @expectedException \InvalidArgumentException | ||
* @expectedExceptionMessage Cannot infer the declaring class from the given ReflectionFunction | ||
*/ | ||
public function testConstructorWithReflectedFunctionShouldThrowInvalidArgumentException() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very long name, please rename to |
||
{ | ||
$resolver = new ImportResolver(new \ReflectionFunction('Brick\Reflection\Tests\reflectedFunc')); | ||
|
||
$this->assertNull($resolver); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This statement will never be reached, you can remove it (and remove the assignment above). |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Brick\Reflection\Tests; | ||
|
||
/** | ||
* The Reflection Target class. | ||
*/ | ||
class ReflectionTarget | ||
{ | ||
/** | ||
* @param string $foo | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, just noticed that you're actually testing that it returns no types. Anyway, I don't think it's a good idea to put an invalid docblock in here. Maybe the best thing to do is to remove the docblock altogether? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's not the good way to remove the $foo docblock bcause it will output the following error message.
It will be failed when excuting the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was a bug in ReflectionTools, I never realized that You can now safely remove the docblock in the test! |
||
*/ | ||
private $foo; | ||
|
||
/** | ||
* @var string $bar | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be |
||
*/ | ||
private $bar; | ||
|
||
/** | ||
* @var \\Exception $barWithType | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extra |
||
*/ | ||
private $barWithType; | ||
|
||
public function __construct() | ||
{ | ||
$this->foo = 'foo'; | ||
$this->bar = 'bar'; | ||
} | ||
|
||
/** | ||
* @param string $str | ||
* @return string $str | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be |
||
*/ | ||
private function privateFunc(string $str) | ||
{ | ||
return $str; | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public static function publicStaticMethod() | ||
{ | ||
return 'publicStaticMethod'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need to return anything here. You've declared it |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,85 @@ public function testGetMethodsDoesNotReturnStaticMethods() | |
$this->assertCount(0, $methods); | ||
} | ||
|
||
public function testGetReflectionFunction() | ||
{ | ||
$reflectionFunc = function() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't have to provide a body, just |
||
{ | ||
return 'reflectedFunction'; | ||
}; | ||
$functions = (new ReflectionTools)->getReflectionFunction($reflectionFunc); | ||
|
||
$this->assertCount(0, $functions->getParameters()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would actually be better to check that |
||
} | ||
|
||
public function testGetFunctionParameterTypesShouldReturnEmptyArray() | ||
{ | ||
$functions = (new ReflectionTools)->getFunctionParameterTypes(new \ReflectionFunction('Brick\Reflection\Tests\reflectedFunc')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would call it |
||
|
||
$this->assertCount(0, $functions); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant with the test below it, you can remove this line. |
||
$this->assertSame([], $functions); | ||
} | ||
|
||
public function testGetFunctionParameterTypesShouldReturnTypesArray() | ||
{ | ||
$functions = (new ReflectionTools)->getFunctionParameterTypes(new \ReflectionFunction('Brick\Reflection\Tests\reflectedParameterFunc')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be called |
||
|
||
$this->assertCount(1, $functions); | ||
$this->assertSame('string', $functions['arg'][0]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you test the actual array returned with |
||
} | ||
|
||
public function testGetParameterTypesShouldReturnTypeArray() | ||
{ | ||
$parameters = (new ReflectionTools)->getParameterTypes(new \ReflectionParameter([ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be called |
||
ReflectionTarget::class, 'privateFunc', | ||
], 'str')); | ||
|
||
$this->assertCount(1, $parameters); | ||
$this->assertSame('string', $parameters[0]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, please replace with |
||
} | ||
|
||
public function testGetPropertyTypesShouldReturnEmptyArray() | ||
{ | ||
$properties = (new ReflectionTools)->getPropertyTypes(new \ReflectionProperty(ReflectionTarget::class, 'foo')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be called |
||
|
||
$this->assertCount(0, $properties); | ||
} | ||
|
||
public function testGetPropertyTypesShouldReturnTypeArray() | ||
{ | ||
$properties = (new ReflectionTools)->getPropertyTypes(new \ReflectionProperty(ReflectionTarget::class, 'bar')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be called |
||
|
||
$this->assertCount(1, $properties); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should use |
||
} | ||
|
||
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 | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Brick\Reflection\Tests; | ||
|
||
/** | ||
* The Target Reflection function without parameter. | ||
*/ | ||
function reflectedFunc() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you merge these 2 functions into one file, |
||
{ | ||
return 'test'; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace Brick\Reflection\Tests; | ||
|
||
/** | ||
* The Target Reflection function with string parameter. | ||
* @param string $arg | ||
*/ | ||
function reflectedParameterFunc(string $arg) | ||
{ | ||
return isset($arg) ? $arg : 'test'; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're not testing against
$resolver
here, you need to useassertSame(..., $resolver->resolve(...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right. The
assertResolve
will not be tested the correct test.I use the
assertSame
instead of theassertResolve
.