ParamTest.php 5.2 KB

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