Файл: gapps/vendor/nikic/php-parser/test/PhpParser/BuilderFactoryTest.php
Строк: 74
<?php
namespace PhpParser;
use PhpParserNodeExpr;
class BuilderFactoryTest extends PHPUnit_Framework_TestCase
{
/**
* @dataProvider provideTestFactory
*/
public function testFactory($methodName, $className) {
$factory = new BuilderFactory;
$this->assertInstanceOf($className, $factory->$methodName('test'));
}
public function provideTestFactory() {
return array(
array('namespace', 'PhpParserBuilderNamespace_'),
array('class', 'PhpParserBuilderClass_'),
array('interface', 'PhpParserBuilderInterface_'),
array('trait', 'PhpParserBuilderTrait_'),
array('method', 'PhpParserBuilderMethod'),
array('function', 'PhpParserBuilderFunction_'),
array('property', 'PhpParserBuilderProperty'),
array('param', 'PhpParserBuilderParam'),
array('use', 'PhpParserBuilderUse_'),
);
}
public function testNonExistingMethod() {
$this->setExpectedException('LogicException', 'Method "foo" does not exist');
$factory = new BuilderFactory();
$factory->foo();
}
public function testIntegration() {
$factory = new BuilderFactory;
$node = $factory->namespace('NameSpace')
->addStmt($factory->use('FooBarSomeOtherClass'))
->addStmt($factory->use('FooBar')->as('A'))
->addStmt($factory
->class('SomeClass')
->extend('SomeOtherClass')
->implement('AFew', 'Interfaces')
->makeAbstract()
->addStmt($factory->method('firstMethod'))
->addStmt($factory->method('someMethod')
->makePublic()
->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 ExprPrint_(new ExprVariable('someParam'))))
->addStmt($factory->property('someProperty')->makeProtected())
->addStmt($factory->property('anotherProperty')
->makePrivate()
->setDefault(array(1, 2, 3))))
->getNode()
;
$expected = <<<'EOC'
<?php
namespace NameSpace;
use FooBarSomeOtherClass;
use FooBar as A;
abstract class SomeClass extends SomeOtherClass implements AFew, Interfaces
{
protected $someProperty;
private $anotherProperty = array(1, 2, 3);
function firstMethod()
{
}
/**
* 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 PrettyPrinterStandard();
$generated = $prettyPrinter->prettyPrintFile($stmts);
$this->assertEquals(
str_replace("rn", "n", $expected),
str_replace("rn", "n", $generated)
);
}
}