2014-02-06 14:44:16 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace PhpParser;
|
|
|
|
|
2014-12-19 18:35:19 +01:00
|
|
|
use PhpParser\Node\Expr;
|
2017-04-24 21:42:59 +02:00
|
|
|
use PhpParser\Node\Expr\BinaryOp\Concat;
|
2017-04-24 21:28:03 +02:00
|
|
|
use PhpParser\Node\Scalar\String_;
|
2017-04-27 18:14:07 +02:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2014-12-19 18:35:19 +01:00
|
|
|
|
2017-04-27 18:14:07 +02:00
|
|
|
class BuilderFactoryTest extends TestCase
|
2014-02-06 14:44:16 +01:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @dataProvider provideTestFactory
|
|
|
|
*/
|
|
|
|
public function testFactory($methodName, $className) {
|
|
|
|
$factory = new BuilderFactory;
|
|
|
|
$this->assertInstanceOf($className, $factory->$methodName('test'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideTestFactory() {
|
|
|
|
return array(
|
2014-12-11 17:24:10 +01:00
|
|
|
array('namespace', 'PhpParser\Builder\Namespace_'),
|
2014-02-06 14:44:16 +01:00
|
|
|
array('class', 'PhpParser\Builder\Class_'),
|
|
|
|
array('interface', 'PhpParser\Builder\Interface_'),
|
2014-12-19 17:58:47 +01:00
|
|
|
array('trait', 'PhpParser\Builder\Trait_'),
|
2014-02-06 14:44:16 +01:00
|
|
|
array('method', 'PhpParser\Builder\Method'),
|
|
|
|
array('function', 'PhpParser\Builder\Function_'),
|
|
|
|
array('property', 'PhpParser\Builder\Property'),
|
|
|
|
array('param', 'PhpParser\Builder\Param'),
|
2015-03-10 16:05:55 +01:00
|
|
|
array('use', 'PhpParser\Builder\Use_'),
|
2014-02-06 14:44:16 +01:00
|
|
|
);
|
|
|
|
}
|
2014-12-19 18:35:19 +01:00
|
|
|
|
2015-07-12 22:11:43 +02:00
|
|
|
public function testNonExistingMethod() {
|
2017-04-27 18:14:07 +02:00
|
|
|
$this->expectException('LogicException');
|
|
|
|
$this->expectExceptionMessage('Method "foo" does not exist');
|
2015-07-12 22:11:43 +02:00
|
|
|
$factory = new BuilderFactory();
|
|
|
|
$factory->foo();
|
|
|
|
}
|
|
|
|
|
2017-04-24 21:28:03 +02:00
|
|
|
public function testVal() {
|
|
|
|
// This method is a wrapper around BuilderHelpers::normalizeValue(),
|
|
|
|
// which is already tested elsewhere
|
|
|
|
$factory = new BuilderFactory();
|
|
|
|
$this->assertEquals(
|
|
|
|
new String_("foo"),
|
|
|
|
$factory->val("foo")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-04-24 21:42:59 +02:00
|
|
|
public function testConcat() {
|
|
|
|
$factory = new BuilderFactory();
|
|
|
|
$varA = new Expr\Variable('a');
|
|
|
|
$varB = new Expr\Variable('b');
|
|
|
|
$varC = new Expr\Variable('c');
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
new Concat($varA, $varB),
|
|
|
|
$factory->concat($varA, $varB)
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
new Concat(new Concat($varA, $varB), $varC),
|
|
|
|
$factory->concat($varA, $varB, $varC)
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
new Concat(new Concat(new String_("a"), $varB), new String_("c")),
|
|
|
|
$factory->concat("a", $varB, "c")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \LogicException
|
|
|
|
* @expectedExceptionMessage Expected at least two expressions
|
|
|
|
*/
|
|
|
|
public function testConcatOneError() {
|
|
|
|
(new BuilderFactory())->concat("a");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \LogicException
|
|
|
|
* @expectedExceptionMessage Expected string or Expr
|
|
|
|
*/
|
|
|
|
public function testConcatInvalidExpr() {
|
|
|
|
(new BuilderFactory())->concat("a", 42);
|
|
|
|
}
|
|
|
|
|
2014-12-19 18:35:19 +01:00
|
|
|
public function testIntegration() {
|
|
|
|
$factory = new BuilderFactory;
|
|
|
|
$node = $factory->namespace('Name\Space')
|
2015-03-10 16:05:55 +01:00
|
|
|
->addStmt($factory->use('Foo\Bar\SomeOtherClass'))
|
|
|
|
->addStmt($factory->use('Foo\Bar')->as('A'))
|
2014-12-19 18:35:19 +01:00
|
|
|
->addStmt($factory
|
|
|
|
->class('SomeClass')
|
|
|
|
->extend('SomeOtherClass')
|
2014-12-19 18:48:21 +01:00
|
|
|
->implement('A\Few', '\Interfaces')
|
2014-12-19 18:35:19 +01:00
|
|
|
->makeAbstract()
|
|
|
|
|
2015-03-02 11:33:41 +01:00
|
|
|
->addStmt($factory->method('firstMethod'))
|
|
|
|
|
2014-12-19 18:35:19 +01:00
|
|
|
->addStmt($factory->method('someMethod')
|
2015-03-02 11:33:41 +01:00
|
|
|
->makePublic()
|
2014-12-19 18:35:19 +01:00
|
|
|
->makeAbstract()
|
|
|
|
->addParam($factory->param('someParam')->setTypeHint('SomeClass'))
|
|
|
|
->setDocComment('/**
|
|
|
|
* This method does something.
|
|
|
|
*
|
|
|
|
* @param SomeClass And takes a parameter
|
|
|
|
*/'))
|
|
|
|
|
|
|
|
->addStmt($factory->method('anotherMethod')
|
|
|
|
->makeProtected()
|
|
|
|
->addParam($factory->param('someParam')->setDefault('test'))
|
|
|
|
->addStmt(new Expr\Print_(new Expr\Variable('someParam'))))
|
|
|
|
|
|
|
|
->addStmt($factory->property('someProperty')->makeProtected())
|
|
|
|
->addStmt($factory->property('anotherProperty')
|
|
|
|
->makePrivate()
|
|
|
|
->setDefault(array(1, 2, 3))))
|
|
|
|
->getNode()
|
|
|
|
;
|
|
|
|
|
|
|
|
$expected = <<<'EOC'
|
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Name\Space;
|
|
|
|
|
2015-03-10 16:05:55 +01:00
|
|
|
use Foo\Bar\SomeOtherClass;
|
|
|
|
use Foo\Bar as A;
|
2014-12-19 18:48:21 +01:00
|
|
|
abstract class SomeClass extends SomeOtherClass implements A\Few, \Interfaces
|
2014-12-19 18:35:19 +01:00
|
|
|
{
|
|
|
|
protected $someProperty;
|
|
|
|
private $anotherProperty = array(1, 2, 3);
|
2015-03-02 11:33:41 +01:00
|
|
|
function firstMethod()
|
|
|
|
{
|
|
|
|
}
|
2014-12-19 18:35:19 +01:00
|
|
|
/**
|
|
|
|
* This method does something.
|
|
|
|
*
|
|
|
|
* @param SomeClass And takes a parameter
|
|
|
|
*/
|
|
|
|
public abstract function someMethod(SomeClass $someParam);
|
|
|
|
protected function anotherMethod($someParam = 'test')
|
|
|
|
{
|
|
|
|
print $someParam;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EOC;
|
|
|
|
|
|
|
|
$stmts = array($node);
|
|
|
|
$prettyPrinter = new PrettyPrinter\Standard();
|
|
|
|
$generated = $prettyPrinter->prettyPrintFile($stmts);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
str_replace("\r\n", "\n", $expected),
|
|
|
|
str_replace("\r\n", "\n", $generated)
|
|
|
|
);
|
|
|
|
}
|
2014-12-19 17:58:47 +01:00
|
|
|
}
|