ParamTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  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 array(
  23. array(
  24. null,
  25. new Expr\ConstFetch(new Node\Name('null'))
  26. ),
  27. array(
  28. true,
  29. new Expr\ConstFetch(new Node\Name('true'))
  30. ),
  31. array(
  32. false,
  33. new Expr\ConstFetch(new Node\Name('false'))
  34. ),
  35. array(
  36. 31415,
  37. new Scalar\LNumber(31415)
  38. ),
  39. array(
  40. 3.1415,
  41. new Scalar\DNumber(3.1415)
  42. ),
  43. array(
  44. 'Hallo World',
  45. new Scalar\String_('Hallo World')
  46. ),
  47. array(
  48. array(1, 2, 3),
  49. new Expr\Array_(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. array(
  56. array('foo' => 'bar', 'bar' => 'foo'),
  57. new Expr\Array_(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. array(
  69. new Scalar\MagicConst\Dir,
  70. new Scalar\MagicConst\Dir
  71. )
  72. );
  73. }
  74. /**
  75. * @dataProvider provideTestTypeHints
  76. */
  77. public function testTypeHints($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. if ($expectedType instanceof Node\Name) {
  90. $this->assertInstanceOf(get_class($expectedType), $type);
  91. $this->assertEquals($expectedType, $type);
  92. } else {
  93. $this->assertSame($expectedType, $type);
  94. }
  95. }
  96. public function provideTestTypeHints() {
  97. return array(
  98. array('array', 'array'),
  99. array('callable', 'callable'),
  100. array('bool', 'bool'),
  101. array('int', 'int'),
  102. array('float', 'float'),
  103. array('string', 'string'),
  104. array('iterable', 'iterable'),
  105. array('Array', 'array'),
  106. array('CALLABLE', 'callable'),
  107. array('Some\Class', new Node\Name('Some\Class')),
  108. array('\Foo', new Node\Name\FullyQualified('Foo')),
  109. array('self', new Node\Name('self')),
  110. array('?array', new Node\NullableType('array')),
  111. array('?Some\Class', new Node\NullableType(new Node\Name('Some\Class'))),
  112. array(new Node\Name('Some\Class'), new Node\Name('Some\Class')),
  113. array(new Node\NullableType('int'), new Node\NullableType('int')),
  114. array(
  115. new Node\NullableType(new Node\Name('Some\Class')),
  116. new Node\NullableType(new Node\Name('Some\Class'))
  117. ),
  118. );
  119. }
  120. /**
  121. * @expectedException \LogicException
  122. * @expectedExceptionMessage Parameter type cannot be void
  123. */
  124. public function testVoidTypeError() {
  125. $this->createParamBuilder('test')->setTypeHint('void');
  126. }
  127. /**
  128. * @expectedException \LogicException
  129. * @expectedExceptionMessage Type must be a string, or an instance of Name or NullableType
  130. */
  131. public function testInvalidTypeError() {
  132. $this->createParamBuilder('test')->setTypeHint(new \stdClass);
  133. }
  134. public function testByRef() {
  135. $node = $this->createParamBuilder('test')
  136. ->makeByRef()
  137. ->getNode()
  138. ;
  139. $this->assertEquals(
  140. new Node\Param('test', null, null, true),
  141. $node
  142. );
  143. }
  144. }