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

Conversation

peter279k
Copy link

Changed log

  • enhance the tests.

{
$resolver = new ImportResolver(new \ReflectionFunction('Brick\Reflection\Tests\reflectedFunc'));

$this->assertNull($resolver);
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.

This statement will never be reached, you can remove it (and remove the assignment above).

{
$resolver = new ImportResolver(new \ReflectionProperty(ReflectionTarget::class, 'foo'));

$this->assertResolve(ReflectionTarget::class, 'ReflectionTarget');
Copy link
Member

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 use assertSame(..., $resolver->resolve(...

Copy link
Author

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 the assertResolve.

{
$resolver = new ImportResolver(new \ReflectionMethod(ReflectionTarget::class, 'publicStaticMethod'));

$this->assertResolve('Brick\Reflection\Tests\publicStaticMethod', 'publicStaticMethod');
Copy link
Member

Choose a reason for hiding this comment

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

Same here, the first line is unused.

ReflectionTarget::class, 'privateFunc',
], 'str'));

$this->assertResolve('Brick\Reflection\Tests\privateFunc', 'privateFunc');
Copy link
Member

Choose a reason for hiding this comment

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

Same here

@@ -19,6 +19,85 @@ public function testGetMethodsDoesNotReturnStaticMethods()
$this->assertCount(0, $methods);
}

public function testGetReflectionFunction()
{
$reflectionFunc = function()
Copy link
Member

Choose a reason for hiding this comment

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

You don't have to provide a body, just = function() {}; will be shorter.

};
$functions = (new ReflectionTools)->getReflectionFunction($reflectionFunc);

$this->assertCount(0, $functions->getParameters());
Copy link
Member

Choose a reason for hiding this comment

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

It would actually be better to check that $function (without s, please) is an instance of ReflectionFunction and that getName() returns the proper value!

{
$functions = (new ReflectionTools)->getFunctionParameterTypes(new \ReflectionFunction('Brick\Reflection\Tests\reflectedFunc'));

$this->assertCount(0, $functions);
Copy link
Member

Choose a reason for hiding this comment

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

Redundant with the test below it, you can remove this line.


public function testGetFunctionParameterTypesShouldReturnEmptyArray()
{
$functions = (new ReflectionTools)->getFunctionParameterTypes(new \ReflectionFunction('Brick\Reflection\Tests\reflectedFunc'));
Copy link
Member

Choose a reason for hiding this comment

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

I would call it $types here!

$functions = (new ReflectionTools)->getFunctionParameterTypes(new \ReflectionFunction('Brick\Reflection\Tests\reflectedParameterFunc'));

$this->assertCount(1, $functions);
$this->assertSame('string', $functions['arg'][0]);
Copy link
Member

Choose a reason for hiding this comment

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

Can you test the actual array returned with assertSame() instead of these 2 lines?


public function testGetFunctionParameterTypesShouldReturnTypesArray()
{
$functions = (new ReflectionTools)->getFunctionParameterTypes(new \ReflectionFunction('Brick\Reflection\Tests\reflectedParameterFunc'));
Copy link
Member

Choose a reason for hiding this comment

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

Should be called $types as well.


public function testGetParameterTypesShouldReturnTypeArray()
{
$parameters = (new ReflectionTools)->getParameterTypes(new \ReflectionParameter([
Copy link
Member

Choose a reason for hiding this comment

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

Should be called $types as well.

], 'str'));

$this->assertCount(1, $parameters);
$this->assertSame('string', $parameters[0]);
Copy link
Member

Choose a reason for hiding this comment

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

Same here, please replace with assertSame([...], ...


public function testGetPropertyTypesShouldReturnEmptyArray()
{
$properties = (new ReflectionTools)->getPropertyTypes(new \ReflectionProperty(ReflectionTarget::class, 'foo'));
Copy link
Member

Choose a reason for hiding this comment

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

Should be called $types

{
$properties = (new ReflectionTools)->getPropertyTypes(new \ReflectionProperty(ReflectionTarget::class, 'bar'));

$this->assertCount(1, $properties);
Copy link
Member

Choose a reason for hiding this comment

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

Should use assertSame()


public function testGetPropertyTypesShouldReturnTypeArray()
{
$properties = (new ReflectionTools)->getPropertyTypes(new \ReflectionProperty(ReflectionTarget::class, 'bar'));
Copy link
Member

Choose a reason for hiding this comment

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

Should be called $types

/**
* The Target Reflection function without parameter.
*/
function reflectedFunc()
Copy link
Member

Choose a reason for hiding this comment

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

Can you merge these 2 functions into one file, functions.php?

private $bar;

/**
* @var \\Exception $barWithType
Copy link
Member

Choose a reason for hiding this comment

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

Extra \ here

class ReflectionTarget
{
/**
* @param string $foo
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, and no variable name

Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Author

@peter279k peter279k Mar 6, 2018

Choose a reason for hiding this comment

The 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.

1) Brick\Reflection\Tests\ReflectionToolsTest::testGetPropertyTypesShouldReturnEmptyArray
TypeError: preg_match() expects parameter 2 to be string, boolean given

/home/lee/reflection/src/ReflectionTools.php:174
/home/lee/reflection/tests/ReflectionToolsTest.php:56

It will be failed when excuting the preg_match function.
I think we just keep the invalid docblock to test whether it returns the no types.

Copy link
Member

Choose a reason for hiding this comment

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

This was a bug in ReflectionTools, I never realized that getDocComment() could return false! I just fixed it, and reported the documentation mistake on php.net.

You can now safely remove the docblock in the test!

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


/**
* @param string $str
* @return string $str
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 @return string, no variable name

{
$resolver = new ImportResolver(new \ReflectionMethod(ReflectionTarget::class, 'publicStaticMethod'));

$this->assertSame('Brick\Reflection\Tests\publicStaticMethod', $resolver->resolve('publicStaticMethod'));
Copy link
Member

Choose a reason for hiding this comment

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

This test is actually weird, you're attempting to resolve a class called publicStaticMethod which is very confusing. Let's just call it 'Foo'?

Copy link
Author

Choose a reason for hiding this comment

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

I write the wrong assertions here and it should be ReflectionTarget class.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, FYI it can be any class name, even if it does not exist!

ReflectionTarget::class, 'privateFunc',
], 'str'));

$this->assertSame('Brick\Reflection\Tests\privateFunc', $resolver->resolve('privateFunc'));
Copy link
Member

Choose a reason for hiding this comment

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

Same remark as above, please choose another name like 'Foo' to remove any confusion.

Copy link
Author

Choose a reason for hiding this comment

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

It's same as the publicStaticMethod.

*/
public static function publicStaticMethod()
{
return 'publicStaticMethod';
Copy link
Member

Choose a reason for hiding this comment

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

You don't need to return anything here. You've declared it @return void, let's remove the body.

* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Cannot infer the declaring class from the given ReflectionFunction
*/
public function testConstructorWithReflectedFunctionShouldThrowInvalidArgumentException()
Copy link
Member

Choose a reason for hiding this comment

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

Very long name, please rename to testConstructorWithReflectionFunctionThrowsException

}

/**
* @param string
Copy link
Member

Choose a reason for hiding this comment

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

Now this should be @param string $str!

/**
 * @param string $str
 *
 * @return string
*/

@param mentions the parameter name
@return does not

@BenMorel BenMorel force-pushed the master branch 2 times, most recently from 65ad42b to 9d2f509 Compare June 25, 2022 23:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants