菜谱项目

ParamTest.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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('object', 'object'),
  106. array('Array', 'array'),
  107. array('CALLABLE', 'callable'),
  108. array('Some\Class', new Node\Name('Some\Class')),
  109. array('\Foo', new Node\Name\FullyQualified('Foo')),
  110. array('self', new Node\Name('self')),
  111. array('?array', new Node\NullableType('array')),
  112. array('?Some\Class', new Node\NullableType(new Node\Name('Some\Class'))),
  113. array(new Node\Name('Some\Class'), new Node\Name('Some\Class')),
  114. array(new Node\NullableType('int'), new Node\NullableType('int')),
  115. array(
  116. new Node\NullableType(new Node\Name('Some\Class')),
  117. new Node\NullableType(new Node\Name('Some\Class'))
  118. ),
  119. );
  120. }
  121. /**
  122. * @expectedException \LogicException
  123. * @expectedExceptionMessage Parameter type cannot be void
  124. */
  125. public function testVoidTypeError() {
  126. $this->createParamBuilder('test')->setTypeHint('void');
  127. }
  128. /**
  129. * @expectedException \LogicException
  130. * @expectedExceptionMessage Type must be a string, or an instance of Name or NullableType
  131. */
  132. public function testInvalidTypeError() {
  133. $this->createParamBuilder('test')->setTypeHint(new \stdClass);
  134. }
  135. public function testByRef() {
  136. $node = $this->createParamBuilder('test')
  137. ->makeByRef()
  138. ->getNode()
  139. ;
  140. $this->assertEquals(
  141. new Node\Param('test', null, null, true),
  142. $node
  143. );
  144. }
  145. public function testVariadic() {
  146. $node = $this->createParamBuilder('test')
  147. ->makeVariadic()
  148. ->getNode()
  149. ;
  150. $this->assertEquals(
  151. new Node\Param('test', null, null, false, true),
  152. $node
  153. );
  154. }
  155. }