FunctionTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'source.php');
  20. foreach ($ts as $token) {
  21. if ($token instanceof PHP_Token_FUNCTION) {
  22. $this->functions[] = $token;
  23. }
  24. }
  25. }
  26. /**
  27. * @covers PHP_Token_FUNCTION::getArguments
  28. */
  29. public function testGetArguments()
  30. {
  31. $this->assertEquals([], $this->functions[0]->getArguments());
  32. $this->assertEquals(
  33. ['$baz' => 'Baz'], $this->functions[1]->getArguments()
  34. );
  35. $this->assertEquals(
  36. ['$foobar' => 'Foobar'], $this->functions[2]->getArguments()
  37. );
  38. $this->assertEquals(
  39. ['$barfoo' => 'Barfoo'], $this->functions[3]->getArguments()
  40. );
  41. $this->assertEquals([], $this->functions[4]->getArguments());
  42. $this->assertEquals(['$x' => null, '$y' => null], $this->functions[5]->getArguments());
  43. }
  44. /**
  45. * @covers PHP_Token_FUNCTION::getName
  46. */
  47. public function testGetName()
  48. {
  49. $this->assertEquals('foo', $this->functions[0]->getName());
  50. $this->assertEquals('bar', $this->functions[1]->getName());
  51. $this->assertEquals('foobar', $this->functions[2]->getName());
  52. $this->assertEquals('barfoo', $this->functions[3]->getName());
  53. $this->assertEquals('baz', $this->functions[4]->getName());
  54. }
  55. /**
  56. * @covers PHP_Token::getLine
  57. */
  58. public function testGetLine()
  59. {
  60. $this->assertEquals(5, $this->functions[0]->getLine());
  61. $this->assertEquals(10, $this->functions[1]->getLine());
  62. $this->assertEquals(17, $this->functions[2]->getLine());
  63. $this->assertEquals(21, $this->functions[3]->getLine());
  64. $this->assertEquals(29, $this->functions[4]->getLine());
  65. $this->assertEquals(37, $this->functions[6]->getLine());
  66. }
  67. /**
  68. * @covers PHP_TokenWithScope::getEndLine
  69. */
  70. public function testGetEndLine()
  71. {
  72. $this->assertEquals(5, $this->functions[0]->getEndLine());
  73. $this->assertEquals(12, $this->functions[1]->getEndLine());
  74. $this->assertEquals(19, $this->functions[2]->getEndLine());
  75. $this->assertEquals(23, $this->functions[3]->getEndLine());
  76. $this->assertEquals(31, $this->functions[4]->getEndLine());
  77. $this->assertEquals(41, $this->functions[6]->getEndLine());
  78. }
  79. /**
  80. * @covers PHP_Token_FUNCTION::getDocblock
  81. */
  82. public function testGetDocblock()
  83. {
  84. $this->assertNull($this->functions[0]->getDocblock());
  85. $this->assertEquals(
  86. "/**\n * @param Baz \$baz\n */",
  87. $this->functions[1]->getDocblock()
  88. );
  89. $this->assertEquals(
  90. "/**\n * @param Foobar \$foobar\n */",
  91. $this->functions[2]->getDocblock()
  92. );
  93. $this->assertNull($this->functions[3]->getDocblock());
  94. $this->assertNull($this->functions[4]->getDocblock());
  95. }
  96. public function testSignature()
  97. {
  98. $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'source5.php');
  99. $f = $ts->getFunctions();
  100. $c = $ts->getClasses();
  101. $i = $ts->getInterfaces();
  102. $this->assertEquals(
  103. 'foo($a, array $b, array $c = array())',
  104. $f['foo']['signature']
  105. );
  106. $this->assertEquals(
  107. 'm($a, array $b, array $c = array())',
  108. $c['c']['methods']['m']['signature']
  109. );
  110. $this->assertEquals(
  111. 'm($a, array $b, array $c = array())',
  112. $c['a']['methods']['m']['signature']
  113. );
  114. $this->assertEquals(
  115. 'm($a, array $b, array $c = array())',
  116. $i['i']['methods']['m']['signature']
  117. );
  118. }
  119. }