Fix issues with parsing invokable macros (#1124)

* Fix issues with parsing invokable macros

* Add macro test

# Conflicts:
#	tests/MacroTest.php
This commit is contained in:
Stan Daniëls
2021-01-09 13:24:28 +01:00
committed by GitHub
parent 4f13ba85bd
commit fd30d1190d
4 changed files with 65 additions and 2 deletions
+58
View File
@@ -8,6 +8,7 @@ use Barryvdh\LaravelIdeHelper\Macro;
use Barryvdh\Reflection\DocBlock;
use Barryvdh\Reflection\DocBlock\Tag;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Routing\UrlGenerator;
use ReflectionClass;
use ReflectionFunction;
use ReflectionFunctionAbstract;
@@ -200,6 +201,47 @@ class MacroTest extends TestCase
return $tags;
}
/**
* Test that we can actually instantiate the class
*/
public function testCanInstantiate()
{
$reflectionMethod = new \ReflectionMethod(UrlGeneratorMacroClass::class, '__invoke');
$macro = new Macro($reflectionMethod, UrlGenerator::class, new \ReflectionClass(UrlGenerator::class), 'macroName');
$this->assertInstanceOf(Macro::class, $macro);
}
/**
* Test the output of a class
*/
public function testOutput()
{
$reflectionMethod = new \ReflectionMethod(UrlGeneratorMacroClass::class, '__invoke');
$macro = new Macro($reflectionMethod, 'URL', new \ReflectionClass(UrlGenerator::class), 'macroName');
$output = <<<'DOC'
/**
*
*
* @see \Barryvdh\LaravelIdeHelper\Tests\UrlGeneratorMacroClass::__invoke()
* @param string $foo
* @param int $bar
* @return string
* @static
*/
DOC;
$this->assertSame($output, $macro->getDocComment(''));
$this->assertSame('__invoke', $macro->getRealName());
$this->assertSame('\\' . UrlGenerator::class, $macro->getDeclaringClass());
$this->assertSame('$foo, $bar', $macro->getParams(true));
$this->assertSame(['$foo', '$bar'], $macro->getParams(false));
$this->assertSame('$foo, $bar = 0', $macro->getParamsWithDefault(true));
$this->assertSame(['$foo', '$bar = 0'], $macro->getParamsWithDefault(false));
$this->assertTrue($macro->shouldReturn());
$this->assertSame('$instance->__invoke($foo, $bar)', $macro->getRootMethodCall());
}
}
/**
@@ -218,3 +260,19 @@ class MacroMock extends Macro
return (new Macro($method, '', $class ?? $method->getClosureScopeClass()))->phpdoc;
}
}
/**
* Example of an invokable class to be used as a macro.
*/
class UrlGeneratorMacroClass
{
/**
* @param string $foo
* @param int $bar
* @return string
*/
public function __invoke(string $foo, int $bar = 0): string
{
return '';
}
}