1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-12-04 02:07:59 +01:00
PHP-Parser/test/PhpParser/Builder/InterfaceTest.php

107 lines
3.2 KiB
PHP
Raw Normal View History

2013-05-23 10:31:59 +02:00
<?php
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;
2013-05-23 10:31:59 +02:00
return $pp->prettyPrint(array($node));
}
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(
new Stmt\Interface_('Contract', array(
2013-05-23 10:31:59 +02:00
'extends' => array(
new Node\Name('Space\Root1'),
new Node\Name('Root2')
2013-05-23 10:31:59 +02:00
),
)), $contract
);
}
public function testAddMethod() {
$method = new Stmt\ClassMethod('doSomething');
2013-05-23 10:31:59 +02:00
$contract = $this->builder->addStmt($method)->getNode();
2014-09-30 20:38:09 +02:00
$this->assertSame(array($method), $contract->stmts);
2013-05-23 10:31:59 +02:00
}
public function testAddConst() {
$const = new Stmt\ClassConst(array(
new Node\Const_('SPEED_OF_LIGHT', new DNumber(299792458.0))
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() {
$const = new Stmt\ClassConst(array(
new Node\Const_('SPEED_OF_LIGHT', new DNumber(299792458))
2013-05-23 10:31:59 +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();
$this->assertEquals(new Stmt\Interface_('Contract', array(), array(
'comments' => array(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() {
$const = new Stmt\ClassConst(array(
new Node\Const_('SPEED_OF_LIGHT', new DNumber(299792458))
2013-05-23 10:31:59 +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));
}
}