Sen descrición

FunctionTest.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /*
  3. * This file is part of php-token-stream.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. use PHPUnit\Framework\TestCase;
  11. class PHP_Token_FunctionTest extends TestCase
  12. {
  13. /**
  14. * @var PHP_Token_FUNCTION[]
  15. */
  16. private $functions;
  17. protected function setUp()
  18. {
  19. foreach (new PHP_Token_Stream(TEST_FILES_PATH . 'source.php') as $token) {
  20. if ($token instanceof PHP_Token_FUNCTION) {
  21. $this->functions[] = $token;
  22. }
  23. }
  24. }
  25. public function testGetArguments()
  26. {
  27. $this->assertEquals([], $this->functions[0]->getArguments());
  28. $this->assertEquals(
  29. ['$baz' => 'Baz'], $this->functions[1]->getArguments()
  30. );
  31. $this->assertEquals(
  32. ['$foobar' => 'Foobar'], $this->functions[2]->getArguments()
  33. );
  34. $this->assertEquals(
  35. ['$barfoo' => 'Barfoo'], $this->functions[3]->getArguments()
  36. );
  37. $this->assertEquals([], $this->functions[4]->getArguments());
  38. $this->assertEquals(['$x' => null, '$y' => null], $this->functions[5]->getArguments());
  39. }
  40. public function testGetName()
  41. {
  42. $this->assertEquals('foo', $this->functions[0]->getName());
  43. $this->assertEquals('bar', $this->functions[1]->getName());
  44. $this->assertEquals('foobar', $this->functions[2]->getName());
  45. $this->assertEquals('barfoo', $this->functions[3]->getName());
  46. $this->assertEquals('baz', $this->functions[4]->getName());
  47. }
  48. public function testGetLine()
  49. {
  50. $this->assertEquals(5, $this->functions[0]->getLine());
  51. $this->assertEquals(10, $this->functions[1]->getLine());
  52. $this->assertEquals(17, $this->functions[2]->getLine());
  53. $this->assertEquals(21, $this->functions[3]->getLine());
  54. $this->assertEquals(29, $this->functions[4]->getLine());
  55. $this->assertEquals(37, $this->functions[6]->getLine());
  56. }
  57. public function testGetEndLine()
  58. {
  59. $this->assertEquals(5, $this->functions[0]->getEndLine());
  60. $this->assertEquals(12, $this->functions[1]->getEndLine());
  61. $this->assertEquals(19, $this->functions[2]->getEndLine());
  62. $this->assertEquals(23, $this->functions[3]->getEndLine());
  63. $this->assertEquals(31, $this->functions[4]->getEndLine());
  64. $this->assertEquals(41, $this->functions[6]->getEndLine());
  65. }
  66. public function testGetDocblock()
  67. {
  68. $this->assertNull($this->functions[0]->getDocblock());
  69. $this->assertEquals(
  70. "/**\n * @param Baz \$baz\n */",
  71. $this->functions[1]->getDocblock()
  72. );
  73. $this->assertEquals(
  74. "/**\n * @param Foobar \$foobar\n */",
  75. $this->functions[2]->getDocblock()
  76. );
  77. $this->assertNull($this->functions[3]->getDocblock());
  78. $this->assertNull($this->functions[4]->getDocblock());
  79. }
  80. public function testSignature()
  81. {
  82. $tokens = new PHP_Token_Stream(TEST_FILES_PATH . 'source5.php');
  83. $functions = $tokens->getFunctions();
  84. $classes = $tokens->getClasses();
  85. $interfaces = $tokens->getInterfaces();
  86. $this->assertEquals(
  87. 'foo($a, array $b, array $c = array())',
  88. $functions['foo']['signature']
  89. );
  90. $this->assertEquals(
  91. 'm($a, array $b, array $c = array())',
  92. $classes['c']['methods']['m']['signature']
  93. );
  94. $this->assertEquals(
  95. 'm($a, array $b, array $c = array())',
  96. $classes['a']['methods']['m']['signature']
  97. );
  98. $this->assertEquals(
  99. 'm($a, array $b, array $c = array())',
  100. $interfaces['i']['methods']['m']['signature']
  101. );
  102. }
  103. }