ClassTest.php (3209B)
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 /** 12 * Tests for the PHP_Token_CLASS class. 13 * 14 * @package PHP_TokenStream 15 * @subpackage Tests 16 * @author Laurent Laville <pear@laurent-laville.org> 17 * @copyright Sebastian Bergmann <sebastian@phpunit.de> 18 * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License 19 * @version Release: @package_version@ 20 * @link http://github.com/sebastianbergmann/php-token-stream/ 21 * @since Class available since Release 1.0.2 22 */ 23 class PHP_Token_ClassTest extends PHPUnit_Framework_TestCase 24 { 25 protected $class; 26 protected $function; 27 28 protected function setUp() 29 { 30 $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'source2.php'); 31 32 foreach ($ts as $token) { 33 if ($token instanceof PHP_Token_CLASS) { 34 $this->class = $token; 35 } 36 37 if ($token instanceof PHP_Token_FUNCTION) { 38 $this->function = $token; 39 break; 40 } 41 } 42 } 43 44 /** 45 * @covers PHP_Token_CLASS::getKeywords 46 */ 47 public function testGetClassKeywords() 48 { 49 $this->assertEquals('abstract', $this->class->getKeywords()); 50 } 51 52 /** 53 * @covers PHP_Token_FUNCTION::getKeywords 54 */ 55 public function testGetFunctionKeywords() 56 { 57 $this->assertEquals('abstract,static', $this->function->getKeywords()); 58 } 59 60 /** 61 * @covers PHP_Token_FUNCTION::getVisibility 62 */ 63 public function testGetFunctionVisibility() 64 { 65 $this->assertEquals('public', $this->function->getVisibility()); 66 } 67 68 public function testIssue19() 69 { 70 $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'issue19.php'); 71 72 foreach ($ts as $token) { 73 if ($token instanceof PHP_Token_CLASS) { 74 $this->assertFalse($token->hasInterfaces()); 75 } 76 } 77 } 78 79 public function testIssue30() 80 { 81 $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'issue30.php'); 82 $this->assertCount(1, $ts->getClasses()); 83 } 84 85 /** 86 * @requires PHP 7 87 */ 88 public function testAnonymousClassesAreHandledCorrectly() 89 { 90 $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'class_with_method_that_declares_anonymous_class.php'); 91 92 $classes = $ts->getClasses(); 93 94 $this->assertEquals(array('class_with_method_that_declares_anonymous_class'), array_keys($classes)); 95 } 96 97 /** 98 * @requires PHP 7 99 * @ticket https://github.com/sebastianbergmann/php-token-stream/issues/52 100 */ 101 public function testAnonymousClassesAreHandledCorrectly2() 102 { 103 $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'class_with_method_that_declares_anonymous_class2.php'); 104 105 $classes = $ts->getClasses(); 106 107 $this->assertEquals(array('Test'), array_keys($classes)); 108 $this->assertEquals(array('methodOne', 'methodTwo'), array_keys($classes['Test']['methods'])); 109 110 $this->assertEmpty($ts->getFunctions()); 111 } 112 }