comparison vendor/nikic/php-parser/test/PhpParser/Builder/ClassTest.php @ 13:5fb285c0d0e3

Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've been lucky to get away with this so far, as we don't support self-registration which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5 was vulnerable to.
author Chris Cannam
date Mon, 23 Apr 2018 09:33:26 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
comparison
equal deleted inserted replaced
12:7a779792577d 13:5fb285c0d0e3
1 <?php 1 <?php declare(strict_types=1);
2 2
3 namespace PhpParser\Builder; 3 namespace PhpParser\Builder;
4 4
5 use PhpParser\Comment; 5 use PhpParser\Comment;
6 use PhpParser\Node; 6 use PhpParser\Node;
7 use PhpParser\Node\Name; 7 use PhpParser\Node\Name;
8 use PhpParser\Node\Stmt; 8 use PhpParser\Node\Stmt;
9 use PHPUnit\Framework\TestCase;
9 10
10 class ClassTest extends \PHPUnit_Framework_TestCase 11 class ClassTest extends TestCase
11 { 12 {
12 protected function createClassBuilder($class) { 13 protected function createClassBuilder($class) {
13 return new Class_($class); 14 return new Class_($class);
14 } 15 }
15 16
20 ->implement('\Fully\Qualified', 'namespace\NamespaceRelative') 21 ->implement('\Fully\Qualified', 'namespace\NamespaceRelative')
21 ->getNode() 22 ->getNode()
22 ; 23 ;
23 24
24 $this->assertEquals( 25 $this->assertEquals(
25 new Stmt\Class_('SomeLogger', array( 26 new Stmt\Class_('SomeLogger', [
26 'extends' => new Name('BaseLogger'), 27 'extends' => new Name('BaseLogger'),
27 'implements' => array( 28 'implements' => [
28 new Name('Namespaced\Logger'), 29 new Name('Namespaced\Logger'),
29 new Name('SomeInterface'), 30 new Name('SomeInterface'),
30 new Name\FullyQualified('Fully\Qualified'), 31 new Name\FullyQualified('Fully\Qualified'),
31 new Name\Relative('NamespaceRelative'), 32 new Name\Relative('NamespaceRelative'),
32 ), 33 ],
33 )), 34 ]),
34 $node 35 $node
35 ); 36 );
36 } 37 }
37 38
38 public function testAbstract() { 39 public function testAbstract() {
40 ->makeAbstract() 41 ->makeAbstract()
41 ->getNode() 42 ->getNode()
42 ; 43 ;
43 44
44 $this->assertEquals( 45 $this->assertEquals(
45 new Stmt\Class_('Test', array( 46 new Stmt\Class_('Test', [
46 'flags' => Stmt\Class_::MODIFIER_ABSTRACT 47 'flags' => Stmt\Class_::MODIFIER_ABSTRACT
47 )), 48 ]),
48 $node 49 $node
49 ); 50 );
50 } 51 }
51 52
52 public function testFinal() { 53 public function testFinal() {
54 ->makeFinal() 55 ->makeFinal()
55 ->getNode() 56 ->getNode()
56 ; 57 ;
57 58
58 $this->assertEquals( 59 $this->assertEquals(
59 new Stmt\Class_('Test', array( 60 new Stmt\Class_('Test', [
60 'flags' => Stmt\Class_::MODIFIER_FINAL 61 'flags' => Stmt\Class_::MODIFIER_FINAL
61 )), 62 ]),
62 $node 63 $node
63 ); 64 );
64 } 65 }
65 66
66 public function testStatementOrder() { 67 public function testStatementOrder() {
67 $method = new Stmt\ClassMethod('testMethod'); 68 $method = new Stmt\ClassMethod('testMethod');
68 $property = new Stmt\Property( 69 $property = new Stmt\Property(
69 Stmt\Class_::MODIFIER_PUBLIC, 70 Stmt\Class_::MODIFIER_PUBLIC,
70 array(new Stmt\PropertyProperty('testProperty')) 71 [new Stmt\PropertyProperty('testProperty')]
71 ); 72 );
72 $const = new Stmt\ClassConst(array( 73 $const = new Stmt\ClassConst([
73 new Node\Const_('TEST_CONST', new Node\Scalar\String_('ABC')) 74 new Node\Const_('TEST_CONST', new Node\Scalar\String_('ABC'))
74 )); 75 ]);
75 $use = new Stmt\TraitUse(array(new Name('SomeTrait'))); 76 $use = new Stmt\TraitUse([new Name('SomeTrait')]);
76 77
77 $node = $this->createClassBuilder('Test') 78 $node = $this->createClassBuilder('Test')
78 ->addStmt($method) 79 ->addStmt($method)
79 ->addStmt($property) 80 ->addStmt($property)
80 ->addStmts(array($const, $use)) 81 ->addStmts([$const, $use])
81 ->getNode() 82 ->getNode()
82 ; 83 ;
83 84
84 $this->assertEquals( 85 $this->assertEquals(
85 new Stmt\Class_('Test', array( 86 new Stmt\Class_('Test', [
86 'stmts' => array($use, $const, $property, $method) 87 'stmts' => [$use, $const, $property, $method]
87 )), 88 ]),
88 $node 89 $node
89 ); 90 );
90 } 91 }
91 92
92 public function testDocComment() { 93 public function testDocComment() {
98 $class = $this->createClassBuilder('Test') 99 $class = $this->createClassBuilder('Test')
99 ->setDocComment($docComment) 100 ->setDocComment($docComment)
100 ->getNode(); 101 ->getNode();
101 102
102 $this->assertEquals( 103 $this->assertEquals(
103 new Stmt\Class_('Test', array(), array( 104 new Stmt\Class_('Test', [], [
104 'comments' => array( 105 'comments' => [
105 new Comment\Doc($docComment) 106 new Comment\Doc($docComment)
106 ) 107 ]
107 )), 108 ]),
108 $class 109 $class
109 ); 110 );
110 111
111 $class = $this->createClassBuilder('Test') 112 $class = $this->createClassBuilder('Test')
112 ->setDocComment(new Comment\Doc($docComment)) 113 ->setDocComment(new Comment\Doc($docComment))
113 ->getNode(); 114 ->getNode();
114 115
115 $this->assertEquals( 116 $this->assertEquals(
116 new Stmt\Class_('Test', array(), array( 117 new Stmt\Class_('Test', [], [
117 'comments' => array( 118 'comments' => [
118 new Comment\Doc($docComment) 119 new Comment\Doc($docComment)
119 ) 120 ]
120 )), 121 ]),
121 $class 122 $class
122 ); 123 );
123 } 124 }
124 125
125 /** 126 /**
126 * @expectedException \LogicException 127 * @expectedException \LogicException
127 * @expectedExceptionMessage Unexpected node of type "Stmt_Echo" 128 * @expectedExceptionMessage Unexpected node of type "Stmt_Echo"
128 */ 129 */
129 public function testInvalidStmtError() { 130 public function testInvalidStmtError() {
130 $this->createClassBuilder('Test') 131 $this->createClassBuilder('Test')
131 ->addStmt(new Stmt\Echo_(array())) 132 ->addStmt(new Stmt\Echo_([]))
132 ; 133 ;
133 } 134 }
134 135
135 /** 136 /**
136 * @expectedException \LogicException 137 * @expectedException \LogicException
150 ->extend(''); 151 ->extend('');
151 } 152 }
152 153
153 /** 154 /**
154 * @expectedException \LogicException 155 * @expectedException \LogicException
155 * @expectedExceptionMessage Name must be a string or an instance of PhpParser\Node\Name 156 * @expectedExceptionMessage Name must be a string or an instance of Node\Name
156 */ 157 */
157 public function testInvalidName() { 158 public function testInvalidName() {
158 $this->createClassBuilder('Test') 159 $this->createClassBuilder('Test')
159 ->extend(array('Foo')); 160 ->extend(['Foo']);
160 } 161 }
161 } 162 }