ParamTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. public function testTypeHints() {
  75. $node = $this->createParamBuilder('test')
  76. ->setTypeHint('array')
  77. ->getNode()
  78. ;
  79. $this->assertEquals(
  80. new Node\Param('test', null, 'array'),
  81. $node
  82. );
  83. $node = $this->createParamBuilder('test')
  84. ->setTypeHint('callable')
  85. ->getNode()
  86. ;
  87. $this->assertEquals(
  88. new Node\Param('test', null, 'callable'),
  89. $node
  90. );
  91. $node = $this->createParamBuilder('test')
  92. ->setTypeHint('Some\Class')
  93. ->getNode()
  94. ;
  95. $this->assertEquals(
  96. new Node\Param('test', null, new Node\Name('Some\Class')),
  97. $node
  98. );
  99. }
  100. public function testByRef() {
  101. $node = $this->createParamBuilder('test')
  102. ->makeByRef()
  103. ->getNode()
  104. ;
  105. $this->assertEquals(
  106. new Node\Param('test', null, null, true),
  107. $node
  108. );
  109. }
  110. }