1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2025-01-22 13:51:12 +01:00

Add tests for var() and propertyFetch()

This commit is contained in:
Nikita Popov 2018-07-20 21:59:42 +02:00
parent 6751ac3c9d
commit 0cd7207ca6
4 changed files with 44 additions and 2 deletions

View File

@ -6,6 +6,8 @@ Version 4.0.4-dev
* The following methods have been added to `BuilderFactory`:
* `useFunction()`
* `useConst()`
* `var()`
* `propertyFetch()`
Version 4.0.3 (2018-07-15)
--------------------------

View File

@ -153,6 +153,10 @@ class BuilderFactory
* @return Expr\Variable
*/
public function var($name) : Expr\Variable {
if (!\is_string($name) && !$name instanceof Expr) {
throw new \LogicException('Variable name must be string or Expr');
}
return new Expr\Variable($name);
}
@ -261,7 +265,7 @@ class BuilderFactory
* @return Expr\PropertyFetch
*/
public function propertyFetch(Expr $var, $name) : Expr\PropertyFetch {
return new Expr\PropertyFetch($var, $name);
return new Expr\PropertyFetch($var, BuilderHelpers::normalizeIdentifierOrExpr($name));
}
/**

View File

@ -71,7 +71,7 @@ final class BuilderHelpers
return new Identifier($name);
}
throw new \LogicException('Expected string or instance of Node\Identifier or Node\Expr');
throw new \LogicException('Expected string or instance of Node\Identifier');
}
/**

View File

@ -190,6 +190,34 @@ class BuilderFactoryTest extends TestCase
);
}
public function testVar() {
$factory = new BuilderFactory();
$this->assertEquals(
new Expr\Variable("foo"),
$factory->var("foo")
);
$this->assertEquals(
new Expr\Variable(new Expr\Variable("foo")),
$factory->var($factory->var("foo"))
);
}
public function testPropertyFetch() {
$f = new BuilderFactory();
$this->assertEquals(
new Expr\PropertyFetch(new Expr\Variable('foo'), 'bar'),
$f->propertyFetch($f->var('foo'), 'bar')
);
$this->assertEquals(
new Expr\PropertyFetch(new Expr\Variable('foo'), 'bar'),
$f->propertyFetch($f->var('foo'), new Identifier('bar'))
);
$this->assertEquals(
new Expr\PropertyFetch(new Expr\Variable('foo'), new Expr\Variable('bar')),
$f->propertyFetch($f->var('foo'), $f->var('bar'))
);
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Expected string or instance of Node\Identifier
@ -214,6 +242,14 @@ class BuilderFactoryTest extends TestCase
(new BuilderFactory())->funcCall(new Node\Stmt\Return_());
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Variable name must be string or Expr
*/
public function testInvalidVar() {
(new BuilderFactory())->var(new Node\Stmt\Return_());
}
public function testIntegration() {
$factory = new BuilderFactory;
$node = $factory->namespace('Name\Space')