comparison vendor/nikic/php-parser/test/PhpParser/NodeAbstractTest.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; 3 namespace PhpParser;
4 4
5 class DummyNode extends NodeAbstract { 5 use PHPUnit\Framework\TestCase;
6
7 class DummyNode extends NodeAbstract
8 {
6 public $subNode1; 9 public $subNode1;
7 public $subNode2; 10 public $subNode2;
8 11
9 public function __construct($subNode1, $subNode2, $attributes) { 12 public function __construct($subNode1, $subNode2, $attributes) {
10 parent::__construct($attributes); 13 parent::__construct($attributes);
11 $this->subNode1 = $subNode1; 14 $this->subNode1 = $subNode1;
12 $this->subNode2 = $subNode2; 15 $this->subNode2 = $subNode2;
13 } 16 }
14 17
15 public function getSubNodeNames() { 18 public function getSubNodeNames() : array {
16 return array('subNode1', 'subNode2'); 19 return ['subNode1', 'subNode2'];
17 } 20 }
18 21
19 // This method is only overwritten because the node is located in an unusual namespace 22 // This method is only overwritten because the node is located in an unusual namespace
20 public function getType() { 23 public function getType() : string {
21 return 'Dummy'; 24 return 'Dummy';
22 } 25 }
23 } 26 }
24 27
25 class NodeAbstractTest extends \PHPUnit_Framework_TestCase 28 class NodeAbstractTest extends TestCase
26 { 29 {
27 public function provideNodes() { 30 public function provideNodes() {
28 $attributes = array( 31 $attributes = [
29 'startLine' => 10, 32 'startLine' => 10,
30 'comments' => array( 33 'endLine' => 11,
34 'startTokenPos' => 12,
35 'endTokenPos' => 13,
36 'startFilePos' => 14,
37 'endFilePos' => 15,
38 'comments' => [
31 new Comment('// Comment' . "\n"), 39 new Comment('// Comment' . "\n"),
32 new Comment\Doc('/** doc comment */'), 40 new Comment\Doc('/** doc comment */'),
33 ), 41 ],
34 ); 42 ];
35 43
36 $node = new DummyNode('value1', 'value2', $attributes); 44 $node = new DummyNode('value1', 'value2', $attributes);
37 $node->notSubNode = 'value3'; 45 $node->notSubNode = 'value3';
38 46
39 return array( 47 return [
40 array($attributes, $node), 48 [$attributes, $node],
41 ); 49 ];
42 } 50 }
43 51
44 /** 52 /**
45 * @dataProvider provideNodes 53 * @dataProvider provideNodes
46 */ 54 */
47 public function testConstruct(array $attributes, Node $node) { 55 public function testConstruct(array $attributes, Node $node) {
48 $this->assertSame('Dummy', $node->getType()); 56 $this->assertSame('Dummy', $node->getType());
49 $this->assertSame(array('subNode1', 'subNode2'), $node->getSubNodeNames()); 57 $this->assertSame(['subNode1', 'subNode2'], $node->getSubNodeNames());
50 $this->assertSame(10, $node->getLine()); 58 $this->assertSame(10, $node->getLine());
59 $this->assertSame(10, $node->getStartLine());
60 $this->assertSame(11, $node->getEndLine());
61 $this->assertSame(12, $node->getStartTokenPos());
62 $this->assertSame(13, $node->getEndTokenPos());
63 $this->assertSame(14, $node->getStartFilePos());
64 $this->assertSame(15, $node->getEndFilePos());
51 $this->assertSame('/** doc comment */', $node->getDocComment()->getText()); 65 $this->assertSame('/** doc comment */', $node->getDocComment()->getText());
52 $this->assertSame('value1', $node->subNode1); 66 $this->assertSame('value1', $node->subNode1);
53 $this->assertSame('value2', $node->subNode2); 67 $this->assertSame('value2', $node->subNode2);
54 $this->assertTrue(isset($node->subNode1)); 68 $this->assertObjectHasAttribute('subNode1', $node);
55 $this->assertTrue(isset($node->subNode2)); 69 $this->assertObjectHasAttribute('subNode2', $node);
56 $this->assertFalse(isset($node->subNode3)); 70 $this->assertObjectNotHasAttribute('subNode3', $node);
57 $this->assertSame($attributes, $node->getAttributes()); 71 $this->assertSame($attributes, $node->getAttributes());
72 $this->assertSame($attributes['comments'], $node->getComments());
58 73
59 return $node; 74 return $node;
60 } 75 }
61 76
62 /** 77 /**
63 * @dataProvider provideNodes 78 * @dataProvider provideNodes
64 */ 79 */
65 public function testGetDocComment(array $attributes, Node $node) { 80 public function testGetDocComment(array $attributes, Node $node) {
66 $this->assertSame('/** doc comment */', $node->getDocComment()->getText()); 81 $this->assertSame('/** doc comment */', $node->getDocComment()->getText());
67 array_pop($node->getAttribute('comments')); // remove doc comment 82 $comments = $node->getComments();
83
84 array_pop($comments); // remove doc comment
85 $node->setAttribute('comments', $comments);
68 $this->assertNull($node->getDocComment()); 86 $this->assertNull($node->getDocComment());
69 array_pop($node->getAttribute('comments')); // remove comment 87
88 array_pop($comments); // remove comment
89 $node->setAttribute('comments', $comments);
70 $this->assertNull($node->getDocComment()); 90 $this->assertNull($node->getDocComment());
71 } 91 }
72 92
73 public function testSetDocComment() { 93 public function testSetDocComment() {
74 $node = new DummyNode(null, null, []); 94 $node = new DummyNode(null, null, []);
94 114
95 /** 115 /**
96 * @dataProvider provideNodes 116 * @dataProvider provideNodes
97 */ 117 */
98 public function testChange(array $attributes, Node $node) { 118 public function testChange(array $attributes, Node $node) {
99 // change of line
100 $node->setLine(15);
101 $this->assertSame(15, $node->getLine());
102
103 // direct modification 119 // direct modification
104 $node->subNode = 'newValue'; 120 $node->subNode = 'newValue';
105 $this->assertSame('newValue', $node->subNode); 121 $this->assertSame('newValue', $node->subNode);
106 122
107 // indirect modification 123 // indirect modification
109 $subNode = 'newNewValue'; 125 $subNode = 'newNewValue';
110 $this->assertSame('newNewValue', $node->subNode); 126 $this->assertSame('newNewValue', $node->subNode);
111 127
112 // removal 128 // removal
113 unset($node->subNode); 129 unset($node->subNode);
114 $this->assertFalse(isset($node->subNode)); 130 $this->assertObjectNotHasAttribute('subNode', $node);
115 } 131 }
116 132
117 /** 133 /**
118 * @dataProvider provideNodes 134 * @dataProvider provideNodes
119 */ 135 */
123 $i = 0; 139 $i = 0;
124 foreach ($node as $key => $value) { 140 foreach ($node as $key => $value) {
125 if ($i === 0) { 141 if ($i === 0) {
126 $this->assertSame('subNode1', $key); 142 $this->assertSame('subNode1', $key);
127 $this->assertSame('value1', $value); 143 $this->assertSame('value1', $value);
128 } else if ($i === 1) { 144 } elseif ($i === 1) {
129 $this->assertSame('subNode2', $key); 145 $this->assertSame('subNode2', $key);
130 $this->assertSame('value2', $value); 146 $this->assertSame('value2', $value);
131 } else if ($i === 2) { 147 } elseif ($i === 2) {
132 $this->assertSame('notSubNode', $key); 148 $this->assertSame('notSubNode', $key);
133 $this->assertSame('value3', $value); 149 $this->assertSame('value3', $value);
134 } else { 150 } else {
135 throw new \Exception; 151 throw new \Exception;
136 } 152 }
139 $this->assertSame(3, $i); 155 $this->assertSame(3, $i);
140 } 156 }
141 157
142 public function testAttributes() { 158 public function testAttributes() {
143 /** @var $node Node */ 159 /** @var $node Node */
144 $node = $this->getMockForAbstractClass('PhpParser\NodeAbstract'); 160 $node = $this->getMockForAbstractClass(NodeAbstract::class);
145 161
146 $this->assertEmpty($node->getAttributes()); 162 $this->assertEmpty($node->getAttributes());
147 163
148 $node->setAttribute('key', 'value'); 164 $node->setAttribute('key', 'value');
149 $this->assertTrue($node->hasAttribute('key')); 165 $this->assertTrue($node->hasAttribute('key'));
157 $this->assertTrue($node->hasAttribute('null')); 173 $this->assertTrue($node->hasAttribute('null'));
158 $this->assertNull($node->getAttribute('null')); 174 $this->assertNull($node->getAttribute('null'));
159 $this->assertNull($node->getAttribute('null', 'default')); 175 $this->assertNull($node->getAttribute('null', 'default'));
160 176
161 $this->assertSame( 177 $this->assertSame(
162 array( 178 [
163 'key' => 'value', 179 'key' => 'value',
164 'null' => null, 180 'null' => null,
165 ), 181 ],
182 $node->getAttributes()
183 );
184
185 $node->setAttributes(
186 [
187 'a' => 'b',
188 'c' => null,
189 ]
190 );
191 $this->assertSame(
192 [
193 'a' => 'b',
194 'c' => null,
195 ],
166 $node->getAttributes() 196 $node->getAttributes()
167 ); 197 );
168 } 198 }
169 199
170 public function testJsonSerialization() { 200 public function testJsonSerialization() {
179 $expected = <<<'JSON' 209 $expected = <<<'JSON'
180 [ 210 [
181 { 211 {
182 "nodeType": "Stmt_Function", 212 "nodeType": "Stmt_Function",
183 "byRef": false, 213 "byRef": false,
184 "name": "functionName", 214 "name": {
215 "nodeType": "Identifier",
216 "name": "functionName",
217 "attributes": {
218 "startLine": 4,
219 "endLine": 4
220 }
221 },
185 "params": [ 222 "params": [
186 { 223 {
187 "nodeType": "Param", 224 "nodeType": "Param",
188 "type": null, 225 "type": null,
189 "byRef": true, 226 "byRef": true,
190 "variadic": false, 227 "variadic": false,
191 "name": "a", 228 "var": {
229 "nodeType": "Expr_Variable",
230 "name": "a",
231 "attributes": {
232 "startLine": 4,
233 "endLine": 4
234 }
235 },
192 "default": { 236 "default": {
193 "nodeType": "Scalar_LNumber", 237 "nodeType": "Scalar_LNumber",
194 "value": 0, 238 "value": 0,
195 "attributes": { 239 "attributes": {
196 "startLine": 4, 240 "startLine": 4,
206 { 250 {
207 "nodeType": "Param", 251 "nodeType": "Param",
208 "type": null, 252 "type": null,
209 "byRef": false, 253 "byRef": false,
210 "variadic": false, 254 "variadic": false,
211 "name": "b", 255 "var": {
256 "nodeType": "Expr_Variable",
257 "name": "b",
258 "attributes": {
259 "startLine": 4,
260 "endLine": 4
261 }
262 },
212 "default": { 263 "default": {
213 "nodeType": "Scalar_DNumber", 264 "nodeType": "Scalar_DNumber",
214 "value": 1, 265 "value": 1,
215 "attributes": { 266 "attributes": {
216 "startLine": 4, 267 "startLine": 4,
249 "comments": [ 300 "comments": [
250 { 301 {
251 "nodeType": "Comment", 302 "nodeType": "Comment",
252 "text": "\/\/ comment\n", 303 "text": "\/\/ comment\n",
253 "line": 2, 304 "line": 2,
254 "filePos": 6 305 "filePos": 6,
306 "tokenPos": 1
255 }, 307 },
256 { 308 {
257 "nodeType": "Comment_Doc", 309 "nodeType": "Comment_Doc",
258 "text": "\/** doc comment *\/", 310 "text": "\/** doc comment *\/",
259 "line": 3, 311 "line": 3,
260 "filePos": 17 312 "filePos": 17,
313 "tokenPos": 2
261 } 314 }
262 ], 315 ],
263 "endLine": 6 316 "endLine": 6
264 } 317 }
265 } 318 }