mirror of
https://github.com/barryvdh/laravel-ide-helper.git
synced 2026-08-17 17:47:13 +00:00
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:
@@ -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 '';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user