public function testIsUserDefined() { $php = '<?php function foo() {}'; $reflector = new FunctionReflector(new StringSourceLocator($php)); $function = $reflector->reflect('foo'); $this->assertTrue($function->isUserDefined()); }
public function testReflectProxiesToGenericReflectMethod() { $php = '<?php function foo() {}'; /** @var StringSourceLocator|\PHPUnit_Framework_MockObject_MockObject $sourceLocator */ $sourceLocator = $this->getMockBuilder(StringSourceLocator::class)->setConstructorArgs([$php])->setMethods(['locateIdentifier'])->getMock(); $sourceLocator->expects($this->once())->method('locateIdentifier')->will($this->returnValue('foobar')); $reflector = new FunctionReflector($sourceLocator); $this->assertSame('foobar', $reflector->reflect('foo')); }
public function testGetAst() { $php = '<?php function foo() {} '; $reflector = new FunctionReflector(new StringSourceLocator($php)); $function = $reflector->reflect('foo'); $ast = $function->getAst(); $this->assertInstanceOf(Function_::class, $ast); $this->assertSame('foo', $ast->name); }
public function testGetFileName() { $reflector = new FunctionReflector(new SingleFileSourceLocator(__DIR__ . '/../Fixture/Functions.php')); $functionInfo = $reflector->reflect('BetterReflectionTest\\Fixture\\myFunction'); $this->assertContains('Fixture/Functions.php', $functionInfo->getFileName()); }
public function testFunctionReflectionFailsWhenFunctionNotDefined() { $reflector = new FunctionReflector(new AutoloadSourceLocator()); $this->setExpectedException(FunctionUndefined::class); $reflector->reflect('this function does not exist, hopefully'); }
/** * @param string $defaultValue * @dataProvider defaultValueStringProvider */ public function testGetDefaultValueAsString($defaultValue, $expectedValue) { $content = "<?php function myMethod(\$var = {$defaultValue}) {}"; $reflector = new FunctionReflector(new StringSourceLocator($content)); $functionInfo = $reflector->reflect('myMethod'); $paramInfo = $functionInfo->getParameter('var'); $this->assertSame($expectedValue, $paramInfo->getDefaultValueAsString()); }
public function testGetDocBlockReturnTypes() { $php = '<?php /** * @return bool */ function foo() {}'; $reflector = new FunctionReflector(new StringSourceLocator($php)); $function = $reflector->reflect('foo'); $types = $function->getDocBlockReturnTypes(); $this->assertInternalType('array', $types); $this->assertCount(1, $types); $this->assertInstanceOf(Boolean::class, $types[0]); }
public function testGetReturnStatementAstDoesNotGiveInnerScopeReturnStatements() { $php = <<<'PHP' <?php function foo($a) { $x = new class { public function __invoke() { return 5; } }; return function () use ($x) { return $x(); }; } PHP; $reflector = new FunctionReflector(new StringSourceLocator($php)); $function = $reflector->reflect('foo'); $nodes = $function->getReturnStatementsAst(); $this->assertCount(1, $nodes); $this->assertContainsOnlyInstancesOf(Return_::class, $nodes); reset($nodes); /** @var Return_ $first */ $first = current($nodes); $this->assertInstanceOf(Closure::class, $first->expr); $this->assertSame(8, $first->getAttribute('startLine')); $this->assertSame(10, $first->getAttribute('endLine')); }
/** * @param string $php * @param bool $expectingReturnsReference * @dataProvider returnsReferenceProvider */ public function testReturnsReference($php, $expectingReturnsReference) { $reflector = new FunctionReflector(new StringSourceLocator($php)); $function = $reflector->reflect('foo'); $this->assertSame($expectingReturnsReference, $function->returnsReference()); }