No Description

FunctionTest.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /*
  3. * This file is part of the PHP_TokenStream package.
  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. /**
  11. * Tests for the PHP_Token_FUNCTION class.
  12. *
  13. * @package PHP_TokenStream
  14. * @subpackage Tests
  15. * @author Sebastian Bergmann <sebastian@phpunit.de>
  16. * @copyright Sebastian Bergmann <sebastian@phpunit.de>
  17. * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
  18. * @version Release: @package_version@
  19. * @link http://github.com/sebastianbergmann/php-token-stream/
  20. * @since Class available since Release 1.0.0
  21. */
  22. class PHP_Token_FunctionTest extends PHPUnit_Framework_TestCase
  23. {
  24. protected $functions;
  25. protected function setUp()
  26. {
  27. $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'source.php');
  28. foreach ($ts as $token) {
  29. if ($token instanceof PHP_Token_FUNCTION) {
  30. $this->functions[] = $token;
  31. }
  32. }
  33. }
  34. /**
  35. * @covers PHP_Token_FUNCTION::getArguments
  36. */
  37. public function testGetArguments()
  38. {
  39. $this->assertEquals(array(), $this->functions[0]->getArguments());
  40. $this->assertEquals(
  41. array('$baz' => 'Baz'), $this->functions[1]->getArguments()
  42. );
  43. $this->assertEquals(
  44. array('$foobar' => 'Foobar'), $this->functions[2]->getArguments()
  45. );
  46. $this->assertEquals(
  47. array('$barfoo' => 'Barfoo'), $this->functions[3]->getArguments()
  48. );
  49. $this->assertEquals(array(), $this->functions[4]->getArguments());
  50. $this->assertEquals(array('$x' => null, '$y' => null), $this->functions[5]->getArguments());
  51. }
  52. /**
  53. * @covers PHP_Token_FUNCTION::getName
  54. */
  55. public function testGetName()
  56. {
  57. $this->assertEquals('foo', $this->functions[0]->getName());
  58. $this->assertEquals('bar', $this->functions[1]->getName());
  59. $this->assertEquals('foobar', $this->functions[2]->getName());
  60. $this->assertEquals('barfoo', $this->functions[3]->getName());
  61. $this->assertEquals('baz', $this->functions[4]->getName());
  62. }
  63. /**
  64. * @covers PHP_Token::getLine
  65. */
  66. public function testGetLine()
  67. {
  68. $this->assertEquals(5, $this->functions[0]->getLine());
  69. $this->assertEquals(10, $this->functions[1]->getLine());
  70. $this->assertEquals(17, $this->functions[2]->getLine());
  71. $this->assertEquals(21, $this->functions[3]->getLine());
  72. $this->assertEquals(29, $this->functions[4]->getLine());
  73. }
  74. /**
  75. * @covers PHP_TokenWithScope::getEndLine
  76. */
  77. public function testGetEndLine()
  78. {
  79. $this->assertEquals(5, $this->functions[0]->getEndLine());
  80. $this->assertEquals(12, $this->functions[1]->getEndLine());
  81. $this->assertEquals(19, $this->functions[2]->getEndLine());
  82. $this->assertEquals(23, $this->functions[3]->getEndLine());
  83. $this->assertEquals(31, $this->functions[4]->getEndLine());
  84. }
  85. /**
  86. * @covers PHP_Token_FUNCTION::getDocblock
  87. */
  88. public function testGetDocblock()
  89. {
  90. $this->assertNull($this->functions[0]->getDocblock());
  91. $this->assertEquals(
  92. "/**\n * @param Baz \$baz\n */",
  93. $this->functions[1]->getDocblock()
  94. );
  95. $this->assertEquals(
  96. "/**\n * @param Foobar \$foobar\n */",
  97. $this->functions[2]->getDocblock()
  98. );
  99. $this->assertNull($this->functions[3]->getDocblock());
  100. $this->assertNull($this->functions[4]->getDocblock());
  101. }
  102. public function testSignature()
  103. {
  104. $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'source5.php');
  105. $f = $ts->getFunctions();
  106. $c = $ts->getClasses();
  107. $i = $ts->getInterfaces();
  108. $this->assertEquals(
  109. 'foo($a, array $b, array $c = array())',
  110. $f['foo']['signature']
  111. );
  112. $this->assertEquals(
  113. 'm($a, array $b, array $c = array())',
  114. $c['c']['methods']['m']['signature']
  115. );
  116. $this->assertEquals(
  117. 'm($a, array $b, array $c = array())',
  118. $c['a']['methods']['m']['signature']
  119. );
  120. $this->assertEquals(
  121. 'm($a, array $b, array $c = array())',
  122. $i['i']['methods']['m']['signature']
  123. );
  124. }
  125. }