InterfaceTest.php (5892B)
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_INTERFACE class. 13 * 14 * @package PHP_TokenStream 15 * @subpackage Tests 16 * @author Sebastian Bergmann <sebastian@phpunit.de> 17 * @author Laurent Laville <pear@laurent-laville.org> 18 * @copyright Sebastian Bergmann <sebastian@phpunit.de> 19 * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License 20 * @version Release: @package_version@ 21 * @link http://github.com/sebastianbergmann/php-token-stream/ 22 * @since Class available since Release 1.0.0 23 */ 24 class PHP_Token_InterfaceTest extends PHPUnit_Framework_TestCase 25 { 26 protected $class; 27 protected $interfaces; 28 29 protected function setUp() 30 { 31 $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'source4.php'); 32 $i = 0; 33 foreach ($ts as $token) { 34 if ($token instanceof PHP_Token_CLASS) { 35 $this->class = $token; 36 } 37 elseif ($token instanceof PHP_Token_INTERFACE) { 38 $this->interfaces[$i] = $token; 39 $i++; 40 } 41 } 42 } 43 44 /** 45 * @covers PHP_Token_INTERFACE::getName 46 */ 47 public function testGetName() 48 { 49 $this->assertEquals( 50 'iTemplate', $this->interfaces[0]->getName() 51 ); 52 } 53 54 /** 55 * @covers PHP_Token_INTERFACE::getParent 56 */ 57 public function testGetParentNotExists() 58 { 59 $this->assertFalse( 60 $this->interfaces[0]->getParent() 61 ); 62 } 63 64 /** 65 * @covers PHP_Token_INTERFACE::hasParent 66 */ 67 public function testHasParentNotExists() 68 { 69 $this->assertFalse( 70 $this->interfaces[0]->hasParent() 71 ); 72 } 73 74 /** 75 * @covers PHP_Token_INTERFACE::getParent 76 */ 77 public function testGetParentExists() 78 { 79 $this->assertEquals( 80 'a', $this->interfaces[2]->getParent() 81 ); 82 } 83 84 /** 85 * @covers PHP_Token_INTERFACE::hasParent 86 */ 87 public function testHasParentExists() 88 { 89 $this->assertTrue( 90 $this->interfaces[2]->hasParent() 91 ); 92 } 93 94 /** 95 * @covers PHP_Token_INTERFACE::getInterfaces 96 */ 97 public function testGetInterfacesExists() 98 { 99 $this->assertEquals( 100 array('b'), 101 $this->class->getInterfaces() 102 ); 103 } 104 105 /** 106 * @covers PHP_Token_INTERFACE::hasInterfaces 107 */ 108 public function testHasInterfacesExists() 109 { 110 $this->assertTrue( 111 $this->class->hasInterfaces() 112 ); 113 } 114 /** 115 * @covers PHP_Token_INTERFACE::getPackage 116 */ 117 public function testGetPackageNamespace() { 118 $tokenStream = new PHP_Token_Stream(TEST_FILES_PATH . 'classInNamespace.php'); 119 foreach($tokenStream as $token) { 120 if($token instanceOf PHP_Token_INTERFACE) { 121 $package = $token->getPackage(); 122 $this->assertSame('Foo\\Bar', $package['namespace']); 123 } 124 } 125 } 126 127 128 public function provideFilesWithClassesWithinMultipleNamespaces() { 129 return array( 130 array(TEST_FILES_PATH . 'multipleNamespacesWithOneClassUsingBraces.php'), 131 array(TEST_FILES_PATH . 'multipleNamespacesWithOneClassUsingNonBraceSyntax.php'), 132 ); 133 } 134 135 /** 136 * @dataProvider provideFilesWithClassesWithinMultipleNamespaces 137 * @covers PHP_Token_INTERFACE::getPackage 138 */ 139 public function testGetPackageNamespaceForFileWithMultipleNamespaces($filepath) { 140 $tokenStream = new PHP_Token_Stream($filepath); 141 $firstClassFound = false; 142 foreach($tokenStream as $token) { 143 if($firstClassFound === false && $token instanceOf PHP_Token_INTERFACE) { 144 $package = $token->getPackage(); 145 $this->assertSame('TestClassInBar', $token->getName()); 146 $this->assertSame('Foo\\Bar', $package['namespace']); 147 $firstClassFound = true; 148 continue; 149 } 150 // Secound class 151 if($token instanceOf PHP_Token_INTERFACE) { 152 $package = $token->getPackage(); 153 $this->assertSame('TestClassInBaz', $token->getName()); 154 $this->assertSame('Foo\\Baz', $package['namespace']); 155 return; 156 } 157 } 158 $this->fail("Seachring for 2 classes failed"); 159 } 160 161 public function testGetPackageNamespaceIsEmptyForInterfacesThatAreNotWithinNamespaces() { 162 foreach($this->interfaces as $token) { 163 $package = $token->getPackage(); 164 $this->assertSame("", $package['namespace']); 165 } 166 } 167 168 /** 169 * @covers PHP_Token_INTERFACE::getPackage 170 */ 171 public function testGetPackageNamespaceWhenExtentingFromNamespaceClass() { 172 $tokenStream = new PHP_Token_Stream(TEST_FILES_PATH . 'classExtendsNamespacedClass.php'); 173 $firstClassFound = false; 174 foreach($tokenStream as $token) { 175 if($firstClassFound === false && $token instanceOf PHP_Token_INTERFACE) { 176 $package = $token->getPackage(); 177 $this->assertSame('Baz', $token->getName()); 178 $this->assertSame('Foo\\Bar', $package['namespace']); 179 $firstClassFound = true; 180 continue; 181 } 182 if($token instanceOf PHP_Token_INTERFACE) { 183 $package = $token->getPackage(); 184 $this->assertSame('Extender', $token->getName()); 185 $this->assertSame('Other\\Space', $package['namespace']); 186 return; 187 } 188 } 189 $this->fail("Searching for 2 classes failed"); 190 } 191 }