ClassTest.php 5.1 KB

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