2013-05-23 10:31:59 +02:00
|
|
|
<?php
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser\Builder;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Stmt;
|
|
|
|
use PhpParser\Node\Scalar\DNumber;
|
|
|
|
|
|
|
|
class InterfaceTest extends \PHPUnit_Framework_TestCase
|
2013-05-23 10:31:59 +02:00
|
|
|
{
|
2014-02-06 14:44:16 +01:00
|
|
|
/** @var Interface_ */
|
2013-05-23 10:31:59 +02:00
|
|
|
protected $builder;
|
|
|
|
|
|
|
|
protected function setUp() {
|
2014-02-06 14:44:16 +01:00
|
|
|
$this->builder = new Interface_('Contract');
|
2013-05-23 10:31:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function dump($node) {
|
2014-02-06 14:44:16 +01:00
|
|
|
$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();
|
2014-02-06 14:44:16 +01:00
|
|
|
$this->assertInstanceOf('PhpParser\Node\Stmt\Interface_', $contract);
|
2013-05-23 10:31:59 +02:00
|
|
|
$this->assertEquals('Contract', $contract->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testExtending() {
|
|
|
|
$contract = $this->builder->extend('Space\Root1', 'Root2')->getNode();
|
|
|
|
$this->assertEquals(
|
2014-02-06 14:44:16 +01:00
|
|
|
new Stmt\Interface_('Contract', array(
|
2013-05-23 10:31:59 +02:00
|
|
|
'extends' => array(
|
2014-02-06 14:44:16 +01:00
|
|
|
new Node\Name('Space\Root1'),
|
|
|
|
new Node\Name('Root2')
|
2013-05-23 10:31:59 +02:00
|
|
|
),
|
|
|
|
)), $contract
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddMethod() {
|
2014-02-06 14:44:16 +01:00
|
|
|
$method = new Stmt\ClassMethod('doSomething');
|
2013-05-23 10:31:59 +02:00
|
|
|
$contract = $this->builder->addStmt($method)->getNode();
|
|
|
|
$this->assertEquals(array($method), $contract->stmts);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddConst() {
|
2014-02-06 14:44:16 +01:00
|
|
|
$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();
|
|
|
|
$this->assertEquals(299792458, $contract->stmts[0]->consts[0]->value->value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testOrder() {
|
2014-02-06 14:44:16 +01:00
|
|
|
$const = new Stmt\ClassConst(array(
|
|
|
|
new Node\Const_('SPEED_OF_LIGHT', new DNumber(299792458))
|
2013-05-23 10:31:59 +02:00
|
|
|
));
|
2014-02-06 14:44:16 +01:00
|
|
|
$method = new Stmt\ClassMethod('doSomething');
|
2013-05-23 10:31:59 +02:00
|
|
|
$contract = $this->builder
|
|
|
|
->addStmt($method)
|
|
|
|
->addStmt($const)
|
|
|
|
->getNode()
|
|
|
|
;
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-06 14:44:16 +01:00
|
|
|
* @expectedException \LogicException
|
2013-05-23 10:31:59 +02:00
|
|
|
* @expectedExceptionMessage Unexpected node of type "Stmt_PropertyProperty"
|
|
|
|
*/
|
|
|
|
public function testInvalidStmtError() {
|
2014-02-06 14:44:16 +01:00
|
|
|
$this->builder->addStmt(new Stmt\PropertyProperty('invalid'));
|
2013-05-23 10:31:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testFullFunctional() {
|
2014-02-06 14:44:16 +01:00
|
|
|
$const = new Stmt\ClassConst(array(
|
|
|
|
new Node\Const_('SPEED_OF_LIGHT', new DNumber(299792458))
|
2013-05-23 10:31:59 +02:00
|
|
|
));
|
2014-02-06 14:44:16 +01: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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|