annotate vendor/nikic/php-parser/test/PhpParser/Builder/InterfaceTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@13 1 <?php declare(strict_types=1);
Chris@0 2
Chris@0 3 namespace PhpParser\Builder;
Chris@0 4
Chris@0 5 use PhpParser\Comment;
Chris@0 6 use PhpParser\Node;
Chris@0 7 use PhpParser\Node\Scalar\DNumber;
Chris@0 8 use PhpParser\Node\Stmt;
Chris@0 9
Chris@17 10 class InterfaceTest extends \PHPUnit\Framework\TestCase
Chris@0 11 {
Chris@0 12 /** @var Interface_ */
Chris@0 13 protected $builder;
Chris@0 14
Chris@0 15 protected function setUp() {
Chris@0 16 $this->builder = new Interface_('Contract');
Chris@0 17 }
Chris@0 18
Chris@0 19 private function dump($node) {
Chris@0 20 $pp = new \PhpParser\PrettyPrinter\Standard;
Chris@13 21 return $pp->prettyPrint([$node]);
Chris@0 22 }
Chris@0 23
Chris@0 24 public function testEmpty() {
Chris@0 25 $contract = $this->builder->getNode();
Chris@13 26 $this->assertInstanceOf(Stmt\Interface_::class, $contract);
Chris@13 27 $this->assertEquals(new Node\Identifier('Contract'), $contract->name);
Chris@0 28 }
Chris@0 29
Chris@0 30 public function testExtending() {
Chris@0 31 $contract = $this->builder->extend('Space\Root1', 'Root2')->getNode();
Chris@0 32 $this->assertEquals(
Chris@13 33 new Stmt\Interface_('Contract', [
Chris@13 34 'extends' => [
Chris@0 35 new Node\Name('Space\Root1'),
Chris@0 36 new Node\Name('Root2')
Chris@13 37 ],
Chris@13 38 ]), $contract
Chris@0 39 );
Chris@0 40 }
Chris@0 41
Chris@0 42 public function testAddMethod() {
Chris@0 43 $method = new Stmt\ClassMethod('doSomething');
Chris@0 44 $contract = $this->builder->addStmt($method)->getNode();
Chris@13 45 $this->assertSame([$method], $contract->stmts);
Chris@0 46 }
Chris@0 47
Chris@0 48 public function testAddConst() {
Chris@13 49 $const = new Stmt\ClassConst([
Chris@0 50 new Node\Const_('SPEED_OF_LIGHT', new DNumber(299792458.0))
Chris@13 51 ]);
Chris@0 52 $contract = $this->builder->addStmt($const)->getNode();
Chris@0 53 $this->assertSame(299792458.0, $contract->stmts[0]->consts[0]->value->value);
Chris@0 54 }
Chris@0 55
Chris@0 56 public function testOrder() {
Chris@13 57 $const = new Stmt\ClassConst([
Chris@0 58 new Node\Const_('SPEED_OF_LIGHT', new DNumber(299792458))
Chris@13 59 ]);
Chris@0 60 $method = new Stmt\ClassMethod('doSomething');
Chris@0 61 $contract = $this->builder
Chris@0 62 ->addStmt($method)
Chris@0 63 ->addStmt($const)
Chris@0 64 ->getNode()
Chris@0 65 ;
Chris@0 66
Chris@13 67 $this->assertInstanceOf(Stmt\ClassConst::class, $contract->stmts[0]);
Chris@13 68 $this->assertInstanceOf(Stmt\ClassMethod::class, $contract->stmts[1]);
Chris@0 69 }
Chris@0 70
Chris@0 71 public function testDocComment() {
Chris@0 72 $node = $this->builder
Chris@0 73 ->setDocComment('/** Test */')
Chris@0 74 ->getNode();
Chris@0 75
Chris@13 76 $this->assertEquals(new Stmt\Interface_('Contract', [], [
Chris@13 77 'comments' => [new Comment\Doc('/** Test */')]
Chris@13 78 ]), $node);
Chris@0 79 }
Chris@0 80
Chris@0 81 public function testInvalidStmtError() {
Chris@17 82 $this->expectException(\LogicException::class);
Chris@17 83 $this->expectExceptionMessage('Unexpected node of type "Stmt_PropertyProperty"');
Chris@0 84 $this->builder->addStmt(new Stmt\PropertyProperty('invalid'));
Chris@0 85 }
Chris@0 86
Chris@0 87 public function testFullFunctional() {
Chris@13 88 $const = new Stmt\ClassConst([
Chris@0 89 new Node\Const_('SPEED_OF_LIGHT', new DNumber(299792458))
Chris@13 90 ]);
Chris@0 91 $method = new Stmt\ClassMethod('doSomething');
Chris@0 92 $contract = $this->builder
Chris@0 93 ->addStmt($method)
Chris@0 94 ->addStmt($const)
Chris@0 95 ->getNode()
Chris@0 96 ;
Chris@0 97
Chris@0 98 eval($this->dump($contract));
Chris@0 99
Chris@0 100 $this->assertTrue(interface_exists('Contract', false));
Chris@0 101 }
Chris@0 102 }