1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-12-11 17:09:41 +01:00
PHP-Parser/test/PhpParser/Builder/InterfaceTest.php

107 lines
3.1 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\Scalar\DNumber;
2016-11-23 22:58:18 +01:00
use PhpParser\Node\Stmt;
2017-04-27 18:14:07 +02:00
use PHPUnit\Framework\TestCase;
2017-04-27 18:14:07 +02:00
class InterfaceTest extends TestCase
2013-05-23 10:31:59 +02:00
{
/** @var Interface_ */
2013-05-23 10:31:59 +02:00
protected $builder;
protected function setUp() {
$this->builder = 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() {
$contract = $this->builder->getNode();
$this->assertInstanceOf('PhpParser\Node\Stmt\Interface_', $contract);
$this->assertEquals(new Node\Identifier('Contract'), $contract->name);
2013-05-23 10:31:59 +02:00
}
public function testExtending() {
$contract = $this->builder->extend('Space\Root1', 'Root2')->getNode();
$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');
2013-05-23 10:31:59 +02:00
$contract = $this->builder->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
]);
2013-05-23 10:31:59 +02:00
$contract = $this->builder->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');
2013-05-23 10:31:59 +02:00
$contract = $this->builder
->addStmt($method)
->addStmt($const)
->getNode()
;
$this->assertInstanceOf('PhpParser\Node\Stmt\ClassConst', $contract->stmts[0]);
$this->assertInstanceOf('PhpParser\Node\Stmt\ClassMethod', $contract->stmts[1]);
2013-05-23 10:31:59 +02:00
}
public function testDocComment() {
$node = $this->builder
->setDocComment('/** Test */')
->getNode();
2017-08-13 14:35:03 +02:00
$this->assertEquals(new Stmt\Interface_('Contract', [], [
'comments' => [new Comment\Doc('/** Test */')]
]), $node);
}
2013-05-23 10:31:59 +02:00
/**
* @expectedException \LogicException
2013-05-23 10:31:59 +02:00
* @expectedExceptionMessage Unexpected node of type "Stmt_PropertyProperty"
*/
public function testInvalidStmtError() {
$this->builder->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');
2013-05-23 10:31:59 +02:00
$contract = $this->builder
->addStmt($method)
->addStmt($const)
->getNode()
;
eval($this->dump($contract));
$this->assertTrue(interface_exists('Contract', false));
}
}