NodeAbstractTest.php 9.8 KB

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