ClassTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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_ClassTest extends TestCase
  12. {
  13. /**
  14. * @var PHP_Token_CLASS
  15. */
  16. private $class;
  17. /**
  18. * @var PHP_Token_FUNCTION
  19. */
  20. private $function;
  21. protected function setUp()
  22. {
  23. $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'source2.php');
  24. foreach ($ts as $token) {
  25. if ($token instanceof PHP_Token_CLASS) {
  26. $this->class = $token;
  27. }
  28. if ($token instanceof PHP_Token_FUNCTION) {
  29. $this->function = $token;
  30. break;
  31. }
  32. }
  33. }
  34. /**
  35. * @covers PHP_Token_CLASS::getKeywords
  36. */
  37. public function testGetClassKeywords()
  38. {
  39. $this->assertEquals('abstract', $this->class->getKeywords());
  40. }
  41. /**
  42. * @covers PHP_Token_FUNCTION::getKeywords
  43. */
  44. public function testGetFunctionKeywords()
  45. {
  46. $this->assertEquals('abstract,static', $this->function->getKeywords());
  47. }
  48. /**
  49. * @covers PHP_Token_FUNCTION::getVisibility
  50. */
  51. public function testGetFunctionVisibility()
  52. {
  53. $this->assertEquals('public', $this->function->getVisibility());
  54. }
  55. public function testIssue19()
  56. {
  57. $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'issue19.php');
  58. foreach ($ts as $token) {
  59. if ($token instanceof PHP_Token_CLASS) {
  60. $this->assertFalse($token->hasInterfaces());
  61. }
  62. }
  63. }
  64. public function testIssue30()
  65. {
  66. $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'issue30.php');
  67. $this->assertCount(1, $ts->getClasses());
  68. }
  69. public function testAnonymousClassesAreHandledCorrectly()
  70. {
  71. $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'class_with_method_that_declares_anonymous_class.php');
  72. $classes = $ts->getClasses();
  73. $this->assertEquals(
  74. [
  75. 'class_with_method_that_declares_anonymous_class',
  76. 'AnonymousClass:9#31',
  77. 'AnonymousClass:10#55',
  78. 'AnonymousClass:11#75',
  79. 'AnonymousClass:12#91',
  80. 'AnonymousClass:13#107'
  81. ],
  82. array_keys($classes)
  83. );
  84. }
  85. /**
  86. * @ticket https://github.com/sebastianbergmann/php-token-stream/issues/52
  87. */
  88. public function testAnonymousClassesAreHandledCorrectly2()
  89. {
  90. $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'class_with_method_that_declares_anonymous_class2.php');
  91. $classes = $ts->getClasses();
  92. $this->assertEquals(['Test', 'AnonymousClass:4#23'], array_keys($classes));
  93. $this->assertEquals(['methodOne', 'methodTwo'], array_keys($classes['Test']['methods']));
  94. $this->assertEmpty($ts->getFunctions());
  95. }
  96. public function testImportedFunctionsAreHandledCorrectly()
  97. {
  98. $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'classUsesNamespacedFunction.php');
  99. $this->assertEmpty($ts->getFunctions());
  100. $this->assertCount(1, $ts->getClasses());
  101. }
  102. /**
  103. * @ticket https://github.com/sebastianbergmann/php-code-coverage/issues/543
  104. */
  105. public function testClassWithMultipleAnonymousClassesAndFunctionsIsHandledCorrectly()
  106. {
  107. $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'class_with_multiple_anonymous_classes_and_functions.php');
  108. $classes = $ts->getClasses();
  109. $this->assertArrayHasKey('class_with_multiple_anonymous_classes_and_functions', $classes);
  110. $this->assertArrayHasKey('AnonymousClass:6#23', $classes);
  111. $this->assertArrayHasKey('AnonymousClass:12#53', $classes);
  112. $this->assertArrayHasKey('m', $classes['class_with_multiple_anonymous_classes_and_functions']['methods']);
  113. $this->assertArrayHasKey('anonymousFunction:18#81', $classes['class_with_multiple_anonymous_classes_and_functions']['methods']);
  114. $this->assertArrayHasKey('anonymousFunction:22#108', $classes['class_with_multiple_anonymous_classes_and_functions']['methods']);
  115. }
  116. /**
  117. * @ticket https://github.com/sebastianbergmann/php-token-stream/issues/68
  118. */
  119. public function testClassWithMethodNamedEmptyIsHandledCorrectly()
  120. {
  121. $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'class_with_method_named_empty.php');
  122. $classes = $ts->getClasses();
  123. $this->assertArrayHasKey('class_with_method_named_empty', $classes);
  124. $this->assertArrayHasKey('empty', $classes['class_with_method_named_empty']['methods']);
  125. }
  126. /**
  127. * @ticket https://github.com/sebastianbergmann/php-code-coverage/issues/424
  128. */
  129. public function testAnonymousFunctionDoesNotAffectStartAndEndLineOfMethod()
  130. {
  131. $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'php-code-coverage-issue-424.php');
  132. $classes = $ts->getClasses();
  133. $this->assertSame(5, $classes['Example']['methods']['even']['startLine']);
  134. $this->assertSame(12, $classes['Example']['methods']['even']['endLine']);
  135. $this->assertSame(7, $classes['Example']['methods']['anonymousFunction:7#28']['startLine']);
  136. $this->assertSame(9, $classes['Example']['methods']['anonymousFunction:7#28']['endLine']);
  137. }
  138. }