ClassTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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_CLASS class.
  12. *
  13. * @package PHP_TokenStream
  14. * @subpackage Tests
  15. * @author Laurent Laville <pear@laurent-laville.org>
  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.2
  21. */
  22. class PHP_Token_ClassTest extends PHPUnit_Framework_TestCase
  23. {
  24. protected $class;
  25. protected $function;
  26. protected function setUp()
  27. {
  28. $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'source2.php');
  29. foreach ($ts as $token) {
  30. if ($token instanceof PHP_Token_CLASS) {
  31. $this->class = $token;
  32. }
  33. if ($token instanceof PHP_Token_FUNCTION) {
  34. $this->function = $token;
  35. break;
  36. }
  37. }
  38. }
  39. /**
  40. * @covers PHP_Token_CLASS::getKeywords
  41. */
  42. public function testGetClassKeywords()
  43. {
  44. $this->assertEquals('abstract', $this->class->getKeywords());
  45. }
  46. /**
  47. * @covers PHP_Token_FUNCTION::getKeywords
  48. */
  49. public function testGetFunctionKeywords()
  50. {
  51. $this->assertEquals('abstract,static', $this->function->getKeywords());
  52. }
  53. /**
  54. * @covers PHP_Token_FUNCTION::getVisibility
  55. */
  56. public function testGetFunctionVisibility()
  57. {
  58. $this->assertEquals('public', $this->function->getVisibility());
  59. }
  60. public function testIssue19()
  61. {
  62. $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'issue19.php');
  63. foreach ($ts as $token) {
  64. if ($token instanceof PHP_Token_CLASS) {
  65. $this->assertFalse($token->hasInterfaces());
  66. }
  67. }
  68. }
  69. public function testIssue30()
  70. {
  71. $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'issue30.php');
  72. $this->assertCount(1, $ts->getClasses());
  73. }
  74. /**
  75. * @requires PHP 7
  76. */
  77. public function testAnonymousClassesAreHandledCorrectly()
  78. {
  79. $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'class_with_method_that_declares_anonymous_class.php');
  80. $classes = $ts->getClasses();
  81. $this->assertEquals(array('class_with_method_that_declares_anonymous_class'), array_keys($classes));
  82. }
  83. /**
  84. * @requires PHP 7
  85. * @ticket https://github.com/sebastianbergmann/php-token-stream/issues/52
  86. */
  87. public function testAnonymousClassesAreHandledCorrectly2()
  88. {
  89. $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'class_with_method_that_declares_anonymous_class2.php');
  90. $classes = $ts->getClasses();
  91. $this->assertEquals(array('Test'), array_keys($classes));
  92. $this->assertEquals(array('methodOne', 'methodTwo'), array_keys($classes['Test']['methods']));
  93. $this->assertEmpty($ts->getFunctions());
  94. }
  95. /**
  96. * @requires PHP 5.6
  97. */
  98. public function testImportedFunctionsAreHandledCorrectly()
  99. {
  100. $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'classUsesNamespacedFunction.php');
  101. $this->assertEmpty($ts->getFunctions());
  102. $this->assertCount(1, $ts->getClasses());
  103. }
  104. }