ParamTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\Builder;
  3. use PhpParser\Node;
  4. use PhpParser\Node\Expr;
  5. use PhpParser\Node\Scalar;
  6. use PHPUnit\Framework\TestCase;
  7. class ParamTest extends TestCase
  8. {
  9. public function createParamBuilder($name) {
  10. return new Param($name);
  11. }
  12. /**
  13. * @dataProvider provideTestDefaultValues
  14. */
  15. public function testDefaultValues($value, $expectedValueNode) {
  16. $node = $this->createParamBuilder('test')
  17. ->setDefault($value)
  18. ->getNode()
  19. ;
  20. $this->assertEquals($expectedValueNode, $node->default);
  21. }
  22. public function provideTestDefaultValues() {
  23. return [
  24. [
  25. null,
  26. new Expr\ConstFetch(new Node\Name('null'))
  27. ],
  28. [
  29. true,
  30. new Expr\ConstFetch(new Node\Name('true'))
  31. ],
  32. [
  33. false,
  34. new Expr\ConstFetch(new Node\Name('false'))
  35. ],
  36. [
  37. 31415,
  38. new Scalar\LNumber(31415)
  39. ],
  40. [
  41. 3.1415,
  42. new Scalar\DNumber(3.1415)
  43. ],
  44. [
  45. 'Hallo World',
  46. new Scalar\String_('Hallo World')
  47. ],
  48. [
  49. [1, 2, 3],
  50. new Expr\Array_([
  51. new Expr\ArrayItem(new Scalar\LNumber(1)),
  52. new Expr\ArrayItem(new Scalar\LNumber(2)),
  53. new Expr\ArrayItem(new Scalar\LNumber(3)),
  54. ])
  55. ],
  56. [
  57. ['foo' => 'bar', 'bar' => 'foo'],
  58. new Expr\Array_([
  59. new Expr\ArrayItem(
  60. new Scalar\String_('bar'),
  61. new Scalar\String_('foo')
  62. ),
  63. new Expr\ArrayItem(
  64. new Scalar\String_('foo'),
  65. new Scalar\String_('bar')
  66. ),
  67. ])
  68. ],
  69. [
  70. new Scalar\MagicConst\Dir,
  71. new Scalar\MagicConst\Dir
  72. ]
  73. ];
  74. }
  75. /**
  76. * @dataProvider provideTestTypes
  77. */
  78. public function testTypes($typeHint, $expectedType) {
  79. $node = $this->createParamBuilder('test')
  80. ->setTypeHint($typeHint)
  81. ->getNode()
  82. ;
  83. $type = $node->type;
  84. /* Manually implement comparison to avoid __toString stupidity */
  85. if ($expectedType instanceof Node\NullableType) {
  86. $this->assertInstanceOf(get_class($expectedType), $type);
  87. $expectedType = $expectedType->type;
  88. $type = $type->type;
  89. }
  90. $this->assertInstanceOf(get_class($expectedType), $type);
  91. $this->assertEquals($expectedType, $type);
  92. }
  93. public function provideTestTypes() {
  94. return [
  95. ['array', new Node\Identifier('array')],
  96. ['callable', new Node\Identifier('callable')],
  97. ['bool', new Node\Identifier('bool')],
  98. ['int', new Node\Identifier('int')],
  99. ['float', new Node\Identifier('float')],
  100. ['string', new Node\Identifier('string')],
  101. ['iterable', new Node\Identifier('iterable')],
  102. ['object', new Node\Identifier('object')],
  103. ['Array', new Node\Identifier('array')],
  104. ['CALLABLE', new Node\Identifier('callable')],
  105. ['Some\Class', new Node\Name('Some\Class')],
  106. ['\Foo', new Node\Name\FullyQualified('Foo')],
  107. ['self', new Node\Name('self')],
  108. ['?array', new Node\NullableType(new Node\Identifier('array'))],
  109. ['?Some\Class', new Node\NullableType(new Node\Name('Some\Class'))],
  110. [new Node\Name('Some\Class'), new Node\Name('Some\Class')],
  111. [
  112. new Node\NullableType(new Node\Identifier('int')),
  113. new Node\NullableType(new Node\Identifier('int'))
  114. ],
  115. [
  116. new Node\NullableType(new Node\Name('Some\Class')),
  117. new Node\NullableType(new Node\Name('Some\Class'))
  118. ],
  119. ];
  120. }
  121. public function testVoidTypeError() {
  122. $this->expectException(\LogicException::class);
  123. $this->expectExceptionMessage('Parameter type cannot be void');
  124. $this->createParamBuilder('test')->setType('void');
  125. }
  126. public function testInvalidTypeError() {
  127. $this->expectException(\LogicException::class);
  128. $this->expectExceptionMessage('Type must be a string, or an instance of Name, Identifier or NullableType');
  129. $this->createParamBuilder('test')->setType(new \stdClass);
  130. }
  131. public function testByRef() {
  132. $node = $this->createParamBuilder('test')
  133. ->makeByRef()
  134. ->getNode()
  135. ;
  136. $this->assertEquals(
  137. new Node\Param(new Expr\Variable('test'), null, null, true),
  138. $node
  139. );
  140. }
  141. public function testVariadic() {
  142. $node = $this->createParamBuilder('test')
  143. ->makeVariadic()
  144. ->getNode()
  145. ;
  146. $this->assertEquals(
  147. new Node\Param(new Expr\Variable('test'), null, null, false, true),
  148. $node
  149. );
  150. }
  151. }