php-parser/test/PhpParser/Builder/InterfaceTest.php

123 lines
3.9 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
2013-05-23 10:31:59 +02:00
namespace PhpParser\Builder;
2016-11-23 22:58:18 +01:00
use PhpParser\Comment;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\DNumber;
use PhpParser\Node\Scalar\LNumber;
2016-11-23 22:58:18 +01:00
use PhpParser\Node\Stmt;
class InterfaceTest extends \PHPUnit\Framework\TestCase
2013-05-23 10:31:59 +02:00
{
2019-06-23 15:03:40 +02:00
protected function createInterfaceBuilder() {
return new Interface_('Contract');
2013-05-23 10:31:59 +02:00
}
private function dump($node) {
$pp = new \PhpParser\PrettyPrinter\Standard;
2017-08-13 14:35:03 +02:00
return $pp->prettyPrint([$node]);
2013-05-23 10:31:59 +02:00
}
public function testEmpty() {
2019-06-23 15:03:40 +02:00
$contract = $this->createInterfaceBuilder()->getNode();
$this->assertInstanceOf(Stmt\Interface_::class, $contract);
$this->assertEquals(new Node\Identifier('Contract'), $contract->name);
2013-05-23 10:31:59 +02:00
}
public function testExtending() {
2019-06-23 15:03:40 +02:00
$contract = $this->createInterfaceBuilder()
->extend('Space\Root1', 'Root2')->getNode();
2013-05-23 10:31:59 +02:00
$this->assertEquals(
2017-08-13 14:35:03 +02:00
new Stmt\Interface_('Contract', [
'extends' => [
new Node\Name('Space\Root1'),
new Node\Name('Root2')
2017-08-13 14:35:03 +02:00
],
]), $contract
2013-05-23 10:31:59 +02:00
);
}
public function testAddMethod() {
$method = new Stmt\ClassMethod('doSomething');
2019-06-23 15:03:40 +02:00
$contract = $this->createInterfaceBuilder()->addStmt($method)->getNode();
2017-08-13 14:35:03 +02:00
$this->assertSame([$method], $contract->stmts);
2013-05-23 10:31:59 +02:00
}
public function testAddConst() {
2017-08-13 14:35:03 +02:00
$const = new Stmt\ClassConst([
new Node\Const_('SPEED_OF_LIGHT', new DNumber(299792458.0))
2017-08-13 14:35:03 +02:00
]);
2019-06-23 15:03:40 +02:00
$contract = $this->createInterfaceBuilder()->addStmt($const)->getNode();
$this->assertSame(299792458.0, $contract->stmts[0]->consts[0]->value->value);
2013-05-23 10:31:59 +02:00
}
public function testOrder() {
2017-08-13 14:35:03 +02:00
$const = new Stmt\ClassConst([
new Node\Const_('SPEED_OF_LIGHT', new DNumber(299792458))
2017-08-13 14:35:03 +02:00
]);
$method = new Stmt\ClassMethod('doSomething');
2019-06-23 15:03:40 +02:00
$contract = $this->createInterfaceBuilder()
2013-05-23 10:31:59 +02:00
->addStmt($method)
->addStmt($const)
->getNode()
;
$this->assertInstanceOf(Stmt\ClassConst::class, $contract->stmts[0]);
$this->assertInstanceOf(Stmt\ClassMethod::class, $contract->stmts[1]);
2013-05-23 10:31:59 +02:00
}
public function testDocComment() {
2019-06-23 15:03:40 +02:00
$node = $this->createInterfaceBuilder()
->setDocComment('/** Test */')
->getNode();
2017-08-13 14:35:03 +02:00
$this->assertEquals(new Stmt\Interface_('Contract', [], [
'comments' => [new Comment\Doc('/** Test */')]
]), $node);
}
public function testAddAttribute() {
$attribute = new Attribute(
new Name('Attr'),
[new Arg(new LNumber(1), false, false, [], new Identifier('name'))]
);
$attributeGroup = new AttributeGroup([$attribute]);
$node = $this->createInterfaceBuilder()
->addAttribute($attributeGroup)
->getNode();
$this->assertEquals(new Stmt\Interface_('Contract', [
'attrGroups' => [$attributeGroup],
], []), $node);
}
2013-05-23 10:31:59 +02:00
public function testInvalidStmtError() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Unexpected node of type "Stmt_PropertyProperty"');
2019-06-23 15:03:40 +02:00
$this->createInterfaceBuilder()->addStmt(new Stmt\PropertyProperty('invalid'));
2013-05-23 10:31:59 +02:00
}
public function testFullFunctional() {
2017-08-13 14:35:03 +02:00
$const = new Stmt\ClassConst([
new Node\Const_('SPEED_OF_LIGHT', new DNumber(299792458))
2017-08-13 14:35:03 +02:00
]);
$method = new Stmt\ClassMethod('doSomething');
2019-06-23 15:03:40 +02:00
$contract = $this->createInterfaceBuilder()
2013-05-23 10:31:59 +02:00
->addStmt($method)
->addStmt($const)
->getNode()
;
eval($this->dump($contract));
$this->assertTrue(interface_exists('Contract', false));
}
}