3 namespace PhpParser\Builder;
7 use PhpParser\Node\Scalar\DNumber;
8 use PhpParser\Node\Stmt;
10 class InterfaceTest extends \PHPUnit_Framework_TestCase
12 /** @var Interface_ */
15 protected function setUp() {
16 $this->builder = new Interface_('Contract');
19 private function dump($node) {
20 $pp = new \PhpParser\PrettyPrinter\Standard;
21 return $pp->prettyPrint(array($node));
24 public function testEmpty() {
25 $contract = $this->builder->getNode();
26 $this->assertInstanceOf('PhpParser\Node\Stmt\Interface_', $contract);
27 $this->assertSame('Contract', $contract->name);
30 public function testExtending() {
31 $contract = $this->builder->extend('Space\Root1', 'Root2')->getNode();
33 new Stmt\Interface_('Contract', array(
35 new Node\Name('Space\Root1'),
36 new Node\Name('Root2')
42 public function testAddMethod() {
43 $method = new Stmt\ClassMethod('doSomething');
44 $contract = $this->builder->addStmt($method)->getNode();
45 $this->assertSame(array($method), $contract->stmts);
48 public function testAddConst() {
49 $const = new Stmt\ClassConst(array(
50 new Node\Const_('SPEED_OF_LIGHT', new DNumber(299792458.0))
52 $contract = $this->builder->addStmt($const)->getNode();
53 $this->assertSame(299792458.0, $contract->stmts[0]->consts[0]->value->value);
56 public function testOrder() {
57 $const = new Stmt\ClassConst(array(
58 new Node\Const_('SPEED_OF_LIGHT', new DNumber(299792458))
60 $method = new Stmt\ClassMethod('doSomething');
61 $contract = $this->builder
67 $this->assertInstanceOf('PhpParser\Node\Stmt\ClassConst', $contract->stmts[0]);
68 $this->assertInstanceOf('PhpParser\Node\Stmt\ClassMethod', $contract->stmts[1]);
71 public function testDocComment() {
72 $node = $this->builder
73 ->setDocComment('/** Test */')
76 $this->assertEquals(new Stmt\Interface_('Contract', array(), array(
77 'comments' => array(new Comment\Doc('/** Test */'))
82 * @expectedException \LogicException
83 * @expectedExceptionMessage Unexpected node of type "Stmt_PropertyProperty"
85 public function testInvalidStmtError() {
86 $this->builder->addStmt(new Stmt\PropertyProperty('invalid'));
89 public function testFullFunctional() {
90 $const = new Stmt\ClassConst(array(
91 new Node\Const_('SPEED_OF_LIGHT', new DNumber(299792458))
93 $method = new Stmt\ClassMethod('doSomething');
94 $contract = $this->builder
100 eval($this->dump($contract));
102 $this->assertTrue(interface_exists('Contract', false));