2017-11-02 18:56:01 +01:00
|
|
|
<?php declare(strict_types=1);
|
2013-05-23 10:31:59 +02:00
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser\Builder;
|
|
|
|
|
2016-11-23 22:58:18 +01:00
|
|
|
use PhpParser\Comment;
|
2014-02-06 14:44:16 +01:00
|
|
|
use PhpParser\Node;
|
2021-06-16 17:47:22 +02:00
|
|
|
use PhpParser\Node\Arg;
|
|
|
|
use PhpParser\Node\Attribute;
|
|
|
|
use PhpParser\Node\AttributeGroup;
|
|
|
|
use PhpParser\Node\Identifier;
|
|
|
|
use PhpParser\Node\Name;
|
2014-02-06 14:44:16 +01:00
|
|
|
use PhpParser\Node\Scalar\DNumber;
|
2021-06-16 17:47:22 +02:00
|
|
|
use PhpParser\Node\Scalar\LNumber;
|
2016-11-23 22:58:18 +01:00
|
|
|
use PhpParser\Node\Stmt;
|
2014-02-06 14:44:16 +01:00
|
|
|
|
2019-01-19 11:18:00 +01:00
|
|
|
class InterfaceTest extends \PHPUnit\Framework\TestCase
|
2013-05-23 10:31:59 +02:00
|
|
|
{
|
2019-06-23 15:03:40 +02:00
|
|
|
protected function createInterfaceBuilder() {
|
|
|
|
return 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;
|
2017-08-13 14:35:03 +02:00
|
|
|
return $pp->prettyPrint([$node]);
|
2013-05-23 10:31:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testEmpty() {
|
2019-06-23 15:03:40 +02:00
|
|
|
$contract = $this->createInterfaceBuilder()->getNode();
|
2018-01-13 16:08:17 +01:00
|
|
|
$this->assertInstanceOf(Stmt\Interface_::class, $contract);
|
2017-04-28 19:09:39 +02:00
|
|
|
$this->assertEquals(new Node\Identifier('Contract'), $contract->name);
|
2013-05-23 10:31:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testExtending() {
|
2019-06-23 15:03:40 +02:00
|
|
|
$contract = $this->createInterfaceBuilder()
|
|
|
|
->extend('Space\Root1', 'Root2')->getNode();
|
2013-05-23 10:31:59 +02:00
|
|
|
$this->assertEquals(
|
2017-08-13 14:35:03 +02:00
|
|
|
new Stmt\Interface_('Contract', [
|
|
|
|
'extends' => [
|
2014-02-06 14:44:16 +01:00
|
|
|
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() {
|
2014-02-06 14:44:16 +01:00
|
|
|
$method = new Stmt\ClassMethod('doSomething');
|
2019-06-23 15:03:40 +02:00
|
|
|
$contract = $this->createInterfaceBuilder()->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([
|
2015-03-23 11:43:22 +01:00
|
|
|
new Node\Const_('SPEED_OF_LIGHT', new DNumber(299792458.0))
|
2017-08-13 14:35:03 +02:00
|
|
|
]);
|
2019-06-23 15:03:40 +02:00
|
|
|
$contract = $this->createInterfaceBuilder()->addStmt($const)->getNode();
|
2015-03-23 11:43:22 +01:00
|
|
|
$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([
|
2014-02-06 14:44:16 +01:00
|
|
|
new Node\Const_('SPEED_OF_LIGHT', new DNumber(299792458))
|
2017-08-13 14:35:03 +02:00
|
|
|
]);
|
2014-02-06 14:44:16 +01:00
|
|
|
$method = new Stmt\ClassMethod('doSomething');
|
2019-06-23 15:03:40 +02:00
|
|
|
$contract = $this->createInterfaceBuilder()
|
2013-05-23 10:31:59 +02:00
|
|
|
->addStmt($method)
|
|
|
|
->addStmt($const)
|
|
|
|
->getNode()
|
|
|
|
;
|
|
|
|
|
2018-01-13 16:08:17 +01:00
|
|
|
$this->assertInstanceOf(Stmt\ClassConst::class, $contract->stmts[0]);
|
|
|
|
$this->assertInstanceOf(Stmt\ClassMethod::class, $contract->stmts[1]);
|
2013-05-23 10:31:59 +02:00
|
|
|
}
|
|
|
|
|
2014-12-13 13:44:40 +01:00
|
|
|
public function testDocComment() {
|
2019-06-23 15:03:40 +02:00
|
|
|
$node = $this->createInterfaceBuilder()
|
2014-12-13 13:44:40 +01:00
|
|
|
->setDocComment('/** Test */')
|
|
|
|
->getNode();
|
|
|
|
|
2017-08-13 14:35:03 +02:00
|
|
|
$this->assertEquals(new Stmt\Interface_('Contract', [], [
|
|
|
|
'comments' => [new Comment\Doc('/** Test */')]
|
|
|
|
]), $node);
|
2014-12-13 13:44:40 +01:00
|
|
|
}
|
|
|
|
|
2021-06-16 17:47:22 +02:00
|
|
|
public function testAddAttribute() {
|
|
|
|
$attribute = new Attribute(
|
|
|
|
new Name('Attr'),
|
|
|
|
[new Arg(new LNumber(1), false, false, [], new Identifier('name'))]
|
|
|
|
);
|
|
|
|
$attributeGroup = new AttributeGroup([$attribute]);
|
|
|
|
|
|
|
|
$node = $this->createInterfaceBuilder()
|
|
|
|
->addAttribute($attributeGroup)
|
|
|
|
->getNode();
|
|
|
|
|
|
|
|
$this->assertEquals(new Stmt\Interface_('Contract', [
|
|
|
|
'attrGroups' => [$attributeGroup],
|
|
|
|
], []), $node);
|
|
|
|
}
|
|
|
|
|
2013-05-23 10:31:59 +02:00
|
|
|
public function testInvalidStmtError() {
|
2018-09-22 10:43:54 +02:00
|
|
|
$this->expectException(\LogicException::class);
|
|
|
|
$this->expectExceptionMessage('Unexpected node of type "Stmt_PropertyProperty"');
|
2019-06-23 15:03:40 +02:00
|
|
|
$this->createInterfaceBuilder()->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([
|
2014-02-06 14:44:16 +01:00
|
|
|
new Node\Const_('SPEED_OF_LIGHT', new DNumber(299792458))
|
2017-08-13 14:35:03 +02:00
|
|
|
]);
|
2014-02-06 14:44:16 +01:00
|
|
|
$method = new Stmt\ClassMethod('doSomething');
|
2019-06-23 15:03:40 +02:00
|
|
|
$contract = $this->createInterfaceBuilder()
|
2013-05-23 10:31:59 +02:00
|
|
|
->addStmt($method)
|
|
|
|
->addStmt($const)
|
|
|
|
->getNode()
|
|
|
|
;
|
|
|
|
|
|
|
|
eval($this->dump($contract));
|
|
|
|
|
|
|
|
$this->assertTrue(interface_exists('Contract', false));
|
|
|
|
}
|
|
|
|
}
|