BuilderHelpersTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php declare(strict_types=1);
  2. namespace PhpParser;
  3. use PhpParser\Builder\Class_;
  4. use PhpParser\Node\Scalar;
  5. use PhpParser\Node\Stmt;
  6. use PhpParser\Node\Expr;
  7. class BuilderHelpersTest extends \PHPUnit\Framework\TestCase
  8. {
  9. public function testNormalizeNode() {
  10. $builder = new Class_('SomeClass');
  11. $this->assertEquals($builder->getNode(), BuilderHelpers::normalizeNode($builder));
  12. $attribute = new Node\Attribute(new Node\Name('Test'));
  13. $this->assertSame($attribute, BuilderHelpers::normalizeNode($attribute));
  14. $this->expectException(\LogicException::class);
  15. $this->expectExceptionMessage('Expected node or builder object');
  16. BuilderHelpers::normalizeNode('test');
  17. }
  18. public function testNormalizeStmt() {
  19. $stmt = new Node\Stmt\Class_('Class');
  20. $this->assertSame($stmt, BuilderHelpers::normalizeStmt($stmt));
  21. $expr = new Expr\Variable('fn');
  22. $normalizedExpr = BuilderHelpers::normalizeStmt($expr);
  23. $this->assertEquals(new Stmt\Expression($expr), $normalizedExpr);
  24. $this->assertSame($expr, $normalizedExpr->expr);
  25. $this->expectException(\LogicException::class);
  26. $this->expectExceptionMessage('Expected statement or expression node');
  27. BuilderHelpers::normalizeStmt(new Node\Attribute(new Node\Name('Test')));
  28. }
  29. public function testNormalizeStmtInvalidType() {
  30. $this->expectException(\LogicException::class);
  31. $this->expectExceptionMessage('Expected node or builder object');
  32. BuilderHelpers::normalizeStmt('test');
  33. }
  34. public function testNormalizeIdentifier() {
  35. $identifier = new Node\Identifier('fn');
  36. $this->assertSame($identifier, BuilderHelpers::normalizeIdentifier($identifier));
  37. $this->assertEquals($identifier, BuilderHelpers::normalizeIdentifier('fn'));
  38. $this->expectException(\LogicException::class);
  39. $this->expectExceptionMessage('Expected string or instance of Node\Identifier');
  40. BuilderHelpers::normalizeIdentifier(1);
  41. }
  42. public function testNormalizeIdentifierOrExpr() {
  43. $identifier = new Node\Identifier('fn');
  44. $this->assertSame($identifier, BuilderHelpers::normalizeIdentifierOrExpr($identifier));
  45. $expr = new Expr\Variable('fn');
  46. $this->assertSame($expr, BuilderHelpers::normalizeIdentifierOrExpr($expr));
  47. $this->assertEquals($identifier, BuilderHelpers::normalizeIdentifierOrExpr('fn'));
  48. $this->expectException(\LogicException::class);
  49. $this->expectExceptionMessage('Expected string or instance of Node\Identifier');
  50. BuilderHelpers::normalizeIdentifierOrExpr(1);
  51. }
  52. public function testNormalizeName() {
  53. $name = new Node\Name('test');
  54. $this->assertSame($name, BuilderHelpers::normalizeName($name));
  55. $this->assertEquals(
  56. new Node\Name\FullyQualified(['Namespace', 'Test']),
  57. BuilderHelpers::normalizeName('\\Namespace\\Test')
  58. );
  59. $this->assertEquals(
  60. new Node\Name\Relative(['Test']),
  61. BuilderHelpers::normalizeName('namespace\\Test')
  62. );
  63. $this->assertEquals($name, BuilderHelpers::normalizeName('test'));
  64. $this->expectException(\LogicException::class);
  65. $this->expectExceptionMessage('Name cannot be empty');
  66. BuilderHelpers::normalizeName('');
  67. }
  68. public function testNormalizeNameInvalidType() {
  69. $this->expectException(\LogicException::class);
  70. $this->expectExceptionMessage('Name must be a string or an instance of Node\Name');
  71. BuilderHelpers::normalizeName(1);
  72. }
  73. public function testNormalizeNameOrExpr() {
  74. $expr = new Expr\Variable('fn');
  75. $this->assertSame($expr, BuilderHelpers::normalizeNameOrExpr($expr));
  76. $name = new Node\Name('test');
  77. $this->assertSame($name, BuilderHelpers::normalizeNameOrExpr($name));
  78. $this->assertEquals(
  79. new Node\Name\FullyQualified(['Namespace', 'Test']),
  80. BuilderHelpers::normalizeNameOrExpr('\\Namespace\\Test')
  81. );
  82. $this->assertEquals(
  83. new Node\Name\Relative(['Test']),
  84. BuilderHelpers::normalizeNameOrExpr('namespace\\Test')
  85. );
  86. $this->assertEquals($name, BuilderHelpers::normalizeNameOrExpr('test'));
  87. $this->expectException(\LogicException::class);
  88. $this->expectExceptionMessage('Name cannot be empty');
  89. BuilderHelpers::normalizeNameOrExpr('');
  90. }
  91. public function testNormalizeNameOrExpInvalidType() {
  92. $this->expectException(\LogicException::class);
  93. $this->expectExceptionMessage('Name must be a string or an instance of Node\Name or Node\Expr');
  94. BuilderHelpers::normalizeNameOrExpr(1);
  95. }
  96. public function testNormalizeType() {
  97. $this->assertEquals(new Node\Identifier('array'), BuilderHelpers::normalizeType('array'));
  98. $this->assertEquals(new Node\Identifier('callable'), BuilderHelpers::normalizeType('callable'));
  99. $this->assertEquals(new Node\Identifier('string'), BuilderHelpers::normalizeType('string'));
  100. $this->assertEquals(new Node\Identifier('int'), BuilderHelpers::normalizeType('int'));
  101. $this->assertEquals(new Node\Identifier('float'), BuilderHelpers::normalizeType('float'));
  102. $this->assertEquals(new Node\Identifier('bool'), BuilderHelpers::normalizeType('bool'));
  103. $this->assertEquals(new Node\Identifier('iterable'), BuilderHelpers::normalizeType('iterable'));
  104. $this->assertEquals(new Node\Identifier('void'), BuilderHelpers::normalizeType('void'));
  105. $this->assertEquals(new Node\Identifier('object'), BuilderHelpers::normalizeType('object'));
  106. $this->assertEquals(new Node\Identifier('null'), BuilderHelpers::normalizeType('null'));
  107. $this->assertEquals(new Node\Identifier('false'), BuilderHelpers::normalizeType('false'));
  108. $this->assertEquals(new Node\Identifier('mixed'), BuilderHelpers::normalizeType('mixed'));
  109. $this->assertEquals(new Node\Identifier('never'), BuilderHelpers::normalizeType('never'));
  110. $this->assertEquals(new Node\Identifier('true'), BuilderHelpers::normalizeType('true'));
  111. $intIdentifier = new Node\Identifier('int');
  112. $this->assertSame($intIdentifier, BuilderHelpers::normalizeType($intIdentifier));
  113. $intName = new Node\Name('int');
  114. $this->assertSame($intName, BuilderHelpers::normalizeType($intName));
  115. $intNullable = new Node\NullableType('int');
  116. $this->assertSame($intNullable, BuilderHelpers::normalizeType($intNullable));
  117. $unionType = new Node\UnionType([new Node\Identifier('int'), new Node\Identifier('string')]);
  118. $this->assertSame($unionType, BuilderHelpers::normalizeType($unionType));
  119. $intersectionType = new Node\IntersectionType([new Node\Name('A'), new Node\Name('B')]);
  120. $this->assertSame($intersectionType, BuilderHelpers::normalizeType($intersectionType));
  121. $expectedNullable = new Node\NullableType($intIdentifier);
  122. $nullable = BuilderHelpers::normalizeType('?int');
  123. $this->assertEquals($expectedNullable, $nullable);
  124. $this->assertEquals($intIdentifier, $nullable->type);
  125. $this->expectException(\LogicException::class);
  126. $this->expectExceptionMessage('Type must be a string, or an instance of Name, Identifier or ComplexType');
  127. BuilderHelpers::normalizeType(1);
  128. }
  129. public function testNormalizeTypeNullableVoid() {
  130. $this->expectException(\LogicException::class);
  131. $this->expectExceptionMessage('void type cannot be nullable');
  132. BuilderHelpers::normalizeType('?void');
  133. }
  134. public function testNormalizeTypeNullableMixed() {
  135. $this->expectException(\LogicException::class);
  136. $this->expectExceptionMessage('mixed type cannot be nullable');
  137. BuilderHelpers::normalizeType('?mixed');
  138. }
  139. public function testNormalizeTypeNullableNever() {
  140. $this->expectException(\LogicException::class);
  141. $this->expectExceptionMessage('never type cannot be nullable');
  142. BuilderHelpers::normalizeType('?never');
  143. }
  144. public function testNormalizeValue() {
  145. $expression = new Scalar\LNumber(1);
  146. $this->assertSame($expression, BuilderHelpers::normalizeValue($expression));
  147. $this->assertEquals(new Expr\ConstFetch(new Node\Name('null')), BuilderHelpers::normalizeValue(null));
  148. $this->assertEquals(new Expr\ConstFetch(new Node\Name('true')), BuilderHelpers::normalizeValue(true));
  149. $this->assertEquals(new Expr\ConstFetch(new Node\Name('false')), BuilderHelpers::normalizeValue(false));
  150. $this->assertEquals(new Scalar\LNumber(2), BuilderHelpers::normalizeValue(2));
  151. $this->assertEquals(new Scalar\DNumber(2.5), BuilderHelpers::normalizeValue(2.5));
  152. $this->assertEquals(new Scalar\String_('text'), BuilderHelpers::normalizeValue('text'));
  153. $this->assertEquals(
  154. new Expr\Array_([
  155. new Expr\ArrayItem(new Scalar\LNumber(0)),
  156. new Expr\ArrayItem(new Scalar\LNumber(1), new Scalar\String_('test')),
  157. ]),
  158. BuilderHelpers::normalizeValue([
  159. 0,
  160. 'test' => 1,
  161. ])
  162. );
  163. $this->expectException(\LogicException::class);
  164. $this->expectExceptionMessage('Invalid value');
  165. BuilderHelpers::normalizeValue(new \stdClass());
  166. }
  167. public function testNormalizeDocComment() {
  168. $docComment = new Comment\Doc('Some doc comment');
  169. $this->assertSame($docComment, BuilderHelpers::normalizeDocComment($docComment));
  170. $this->assertEquals($docComment, BuilderHelpers::normalizeDocComment('Some doc comment'));
  171. $this->expectException(\LogicException::class);
  172. $this->expectExceptionMessage('Doc comment must be a string or an instance of PhpParser\Comment\Doc');
  173. BuilderHelpers::normalizeDocComment(1);
  174. }
  175. public function testNormalizeAttribute() {
  176. $attribute = new Node\Attribute(new Node\Name('Test'));
  177. $attributeGroup = new Node\AttributeGroup([$attribute]);
  178. $this->assertEquals($attributeGroup, BuilderHelpers::normalizeAttribute($attribute));
  179. $this->assertSame($attributeGroup, BuilderHelpers::normalizeAttribute($attributeGroup));
  180. $this->expectException(\LogicException::class);
  181. $this->expectExceptionMessage('Attribute must be an instance of PhpParser\Node\Attribute or PhpParser\Node\AttributeGroup');
  182. BuilderHelpers::normalizeAttribute('test');
  183. }
  184. }