NodeAbstractTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <?php declare(strict_types=1);
  2. namespace PhpParser;
  3. class DummyNode extends NodeAbstract
  4. {
  5. public $subNode1;
  6. public $subNode2;
  7. public function __construct($subNode1, $subNode2, $attributes) {
  8. parent::__construct($attributes);
  9. $this->subNode1 = $subNode1;
  10. $this->subNode2 = $subNode2;
  11. }
  12. public function getSubNodeNames() : array {
  13. return ['subNode1', 'subNode2'];
  14. }
  15. // This method is only overwritten because the node is located in an unusual namespace
  16. public function getType() : string {
  17. return 'Dummy';
  18. }
  19. }
  20. class NodeAbstractTest extends \PHPUnit\Framework\TestCase
  21. {
  22. public function provideNodes() {
  23. $attributes = [
  24. 'startLine' => 10,
  25. 'endLine' => 11,
  26. 'startTokenPos' => 12,
  27. 'endTokenPos' => 13,
  28. 'startFilePos' => 14,
  29. 'endFilePos' => 15,
  30. 'comments' => [
  31. new Comment('// Comment' . "\n"),
  32. new Comment\Doc('/** doc comment */'),
  33. ],
  34. ];
  35. $node = new DummyNode('value1', 'value2', $attributes);
  36. $node->notSubNode = 'value3';
  37. return [
  38. [$attributes, $node],
  39. ];
  40. }
  41. /**
  42. * @dataProvider provideNodes
  43. */
  44. public function testConstruct(array $attributes, Node $node) {
  45. $this->assertSame('Dummy', $node->getType());
  46. $this->assertSame(['subNode1', 'subNode2'], $node->getSubNodeNames());
  47. $this->assertSame(10, $node->getLine());
  48. $this->assertSame(10, $node->getStartLine());
  49. $this->assertSame(11, $node->getEndLine());
  50. $this->assertSame(12, $node->getStartTokenPos());
  51. $this->assertSame(13, $node->getEndTokenPos());
  52. $this->assertSame(14, $node->getStartFilePos());
  53. $this->assertSame(15, $node->getEndFilePos());
  54. $this->assertSame('/** doc comment */', $node->getDocComment()->getText());
  55. $this->assertSame('value1', $node->subNode1);
  56. $this->assertSame('value2', $node->subNode2);
  57. $this->assertObjectHasAttribute('subNode1', $node);
  58. $this->assertObjectHasAttribute('subNode2', $node);
  59. $this->assertObjectNotHasAttribute('subNode3', $node);
  60. $this->assertSame($attributes, $node->getAttributes());
  61. $this->assertSame($attributes['comments'], $node->getComments());
  62. return $node;
  63. }
  64. /**
  65. * @dataProvider provideNodes
  66. */
  67. public function testGetDocComment(array $attributes, Node $node) {
  68. $this->assertSame('/** doc comment */', $node->getDocComment()->getText());
  69. $comments = $node->getComments();
  70. array_pop($comments); // remove doc comment
  71. $node->setAttribute('comments', $comments);
  72. $this->assertNull($node->getDocComment());
  73. array_pop($comments); // remove comment
  74. $node->setAttribute('comments', $comments);
  75. $this->assertNull($node->getDocComment());
  76. }
  77. public function testSetDocComment() {
  78. $node = new DummyNode(null, null, []);
  79. // Add doc comment to node without comments
  80. $docComment = new Comment\Doc('/** doc */');
  81. $node->setDocComment($docComment);
  82. $this->assertSame($docComment, $node->getDocComment());
  83. // Replace it
  84. $docComment = new Comment\Doc('/** doc 2 */');
  85. $node->setDocComment($docComment);
  86. $this->assertSame($docComment, $node->getDocComment());
  87. // Add docmment to node with other comments
  88. $c1 = new Comment('/* foo */');
  89. $c2 = new Comment('/* bar */');
  90. $docComment = new Comment\Doc('/** baz */');
  91. $node->setAttribute('comments', [$c1, $c2]);
  92. $node->setDocComment($docComment);
  93. $this->assertSame([$c1, $c2, $docComment], $node->getAttribute('comments'));
  94. }
  95. /**
  96. * @dataProvider provideNodes
  97. */
  98. public function testChange(array $attributes, Node $node) {
  99. // direct modification
  100. $node->subNode = 'newValue';
  101. $this->assertSame('newValue', $node->subNode);
  102. // indirect modification
  103. $subNode =& $node->subNode;
  104. $subNode = 'newNewValue';
  105. $this->assertSame('newNewValue', $node->subNode);
  106. // removal
  107. unset($node->subNode);
  108. $this->assertObjectNotHasAttribute('subNode', $node);
  109. }
  110. /**
  111. * @dataProvider provideNodes
  112. */
  113. public function testIteration(array $attributes, Node $node) {
  114. // Iteration is simple object iteration over properties,
  115. // not over subnodes
  116. $i = 0;
  117. foreach ($node as $key => $value) {
  118. if ($i === 0) {
  119. $this->assertSame('subNode1', $key);
  120. $this->assertSame('value1', $value);
  121. } elseif ($i === 1) {
  122. $this->assertSame('subNode2', $key);
  123. $this->assertSame('value2', $value);
  124. } elseif ($i === 2) {
  125. $this->assertSame('notSubNode', $key);
  126. $this->assertSame('value3', $value);
  127. } else {
  128. throw new \Exception;
  129. }
  130. $i++;
  131. }
  132. $this->assertSame(3, $i);
  133. }
  134. public function testAttributes() {
  135. /** @var $node Node */
  136. $node = $this->getMockForAbstractClass(NodeAbstract::class);
  137. $this->assertEmpty($node->getAttributes());
  138. $node->setAttribute('key', 'value');
  139. $this->assertTrue($node->hasAttribute('key'));
  140. $this->assertSame('value', $node->getAttribute('key'));
  141. $this->assertFalse($node->hasAttribute('doesNotExist'));
  142. $this->assertNull($node->getAttribute('doesNotExist'));
  143. $this->assertSame('default', $node->getAttribute('doesNotExist', 'default'));
  144. $node->setAttribute('null', null);
  145. $this->assertTrue($node->hasAttribute('null'));
  146. $this->assertNull($node->getAttribute('null'));
  147. $this->assertNull($node->getAttribute('null', 'default'));
  148. $this->assertSame(
  149. [
  150. 'key' => 'value',
  151. 'null' => null,
  152. ],
  153. $node->getAttributes()
  154. );
  155. $node->setAttributes(
  156. [
  157. 'a' => 'b',
  158. 'c' => null,
  159. ]
  160. );
  161. $this->assertSame(
  162. [
  163. 'a' => 'b',
  164. 'c' => null,
  165. ],
  166. $node->getAttributes()
  167. );
  168. }
  169. public function testJsonSerialization() {
  170. $code = <<<'PHP'
  171. <?php
  172. // comment
  173. /** doc comment */
  174. function functionName(&$a = 0, $b = 1.0) {
  175. echo 'Foo';
  176. }
  177. PHP;
  178. $expected = <<<'JSON'
  179. [
  180. {
  181. "nodeType": "Stmt_Function",
  182. "byRef": false,
  183. "name": {
  184. "nodeType": "Identifier",
  185. "name": "functionName",
  186. "attributes": {
  187. "startLine": 4,
  188. "endLine": 4
  189. }
  190. },
  191. "params": [
  192. {
  193. "nodeType": "Param",
  194. "type": null,
  195. "byRef": true,
  196. "variadic": false,
  197. "var": {
  198. "nodeType": "Expr_Variable",
  199. "name": "a",
  200. "attributes": {
  201. "startLine": 4,
  202. "endLine": 4
  203. }
  204. },
  205. "default": {
  206. "nodeType": "Scalar_LNumber",
  207. "value": 0,
  208. "attributes": {
  209. "startLine": 4,
  210. "endLine": 4,
  211. "kind": 10
  212. }
  213. },
  214. "attributes": {
  215. "startLine": 4,
  216. "endLine": 4
  217. }
  218. },
  219. {
  220. "nodeType": "Param",
  221. "type": null,
  222. "byRef": false,
  223. "variadic": false,
  224. "var": {
  225. "nodeType": "Expr_Variable",
  226. "name": "b",
  227. "attributes": {
  228. "startLine": 4,
  229. "endLine": 4
  230. }
  231. },
  232. "default": {
  233. "nodeType": "Scalar_DNumber",
  234. "value": 1,
  235. "attributes": {
  236. "startLine": 4,
  237. "endLine": 4
  238. }
  239. },
  240. "attributes": {
  241. "startLine": 4,
  242. "endLine": 4
  243. }
  244. }
  245. ],
  246. "returnType": null,
  247. "stmts": [
  248. {
  249. "nodeType": "Stmt_Echo",
  250. "exprs": [
  251. {
  252. "nodeType": "Scalar_String",
  253. "value": "Foo",
  254. "attributes": {
  255. "startLine": 5,
  256. "endLine": 5,
  257. "kind": 1
  258. }
  259. }
  260. ],
  261. "attributes": {
  262. "startLine": 5,
  263. "endLine": 5
  264. }
  265. }
  266. ],
  267. "attributes": {
  268. "startLine": 4,
  269. "comments": [
  270. {
  271. "nodeType": "Comment",
  272. "text": "\/\/ comment\n",
  273. "line": 2,
  274. "filePos": 6,
  275. "tokenPos": 1
  276. },
  277. {
  278. "nodeType": "Comment_Doc",
  279. "text": "\/** doc comment *\/",
  280. "line": 3,
  281. "filePos": 17,
  282. "tokenPos": 2
  283. }
  284. ],
  285. "endLine": 6
  286. }
  287. }
  288. ]
  289. JSON;
  290. $parser = new Parser\Php7(new Lexer());
  291. $stmts = $parser->parse(canonicalize($code));
  292. $json = json_encode($stmts, JSON_PRETTY_PRINT);
  293. $this->assertEquals(canonicalize($expected), canonicalize($json));
  294. }
  295. }