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

106 lines
3.1 KiB
PHP
Raw Normal View History

2013-05-23 10:31:59 +02:00
<?php
namespace PhpParser\Builder;
use PhpParser\Node;
use PhpParser\Node\Stmt;
use PhpParser\Node\Scalar\DNumber;
use PhpParser\Comment;
class InterfaceTest extends \PHPUnit_Framework_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);
2014-09-30 20:38:09 +02:00
$this->assertSame('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))
2013-05-23 10:31:59 +02:00
));
$contract = $this->builder->addStmt($const)->getNode();
2014-09-30 20:38:09 +02:00
$this->assertSame(299792458, $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));
}
}