InterfaceTest.php (7911B)
1 <?php 2 /** 3 * php-token-stream 4 * 5 * Copyright (c) 2009-2013, Sebastian Bergmann <sebastian@phpunit.de>. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * * Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * * Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 20 * * Neither the name of Sebastian Bergmann nor the names of his 21 * contributors may be used to endorse or promote products derived 22 * from this software without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 28 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 * 37 * @package PHP_TokenStream 38 * @subpackage Tests 39 * @author Sebastian Bergmann <sebastian@phpunit.de> 40 * @author Laurent Laville <pear@laurent-laville.org> 41 * @copyright 2009-2013 Sebastian Bergmann <sebastian@phpunit.de> 42 * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License 43 * @since File available since Release 1.0.0 44 */ 45 46 if (!defined('TEST_FILES_PATH')) { 47 define( 48 'TEST_FILES_PATH', 49 dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 50 '_files' . DIRECTORY_SEPARATOR 51 ); 52 } 53 54 require_once 'PHP/Token/Stream.php'; 55 56 /** 57 * Tests for the PHP_Token_INTERFACE class. 58 * 59 * @package PHP_TokenStream 60 * @subpackage Tests 61 * @author Sebastian Bergmann <sebastian@phpunit.de> 62 * @author Laurent Laville <pear@laurent-laville.org> 63 * @copyright 2009-2013 Sebastian Bergmann <sebastian@phpunit.de> 64 * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License 65 * @version Release: @package_version@ 66 * @link http://github.com/sebastianbergmann/php-token-stream/ 67 * @since Class available since Release 1.0.0 68 */ 69 class PHP_Token_InterfaceTest extends PHPUnit_Framework_TestCase 70 { 71 protected $class; 72 protected $interfaces; 73 74 protected function setUp() 75 { 76 $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'source4.php'); 77 $i = 0; 78 foreach ($ts as $token) { 79 if ($token instanceof PHP_Token_CLASS) { 80 $this->class = $token; 81 } 82 elseif ($token instanceof PHP_Token_INTERFACE) { 83 $this->interfaces[$i] = $token; 84 $i++; 85 } 86 } 87 } 88 89 /** 90 * @covers PHP_Token_INTERFACE::getName 91 */ 92 public function testGetName() 93 { 94 $this->assertEquals( 95 'iTemplate', $this->interfaces[0]->getName() 96 ); 97 } 98 99 /** 100 * @covers PHP_Token_INTERFACE::getParent 101 */ 102 public function testGetParentNotExists() 103 { 104 $this->assertFalse( 105 $this->interfaces[0]->getParent() 106 ); 107 } 108 109 /** 110 * @covers PHP_Token_INTERFACE::hasParent 111 */ 112 public function testHasParentNotExists() 113 { 114 $this->assertFalse( 115 $this->interfaces[0]->hasParent() 116 ); 117 } 118 119 /** 120 * @covers PHP_Token_INTERFACE::getParent 121 */ 122 public function testGetParentExists() 123 { 124 $this->assertEquals( 125 'a', $this->interfaces[2]->getParent() 126 ); 127 } 128 129 /** 130 * @covers PHP_Token_INTERFACE::hasParent 131 */ 132 public function testHasParentExists() 133 { 134 $this->assertTrue( 135 $this->interfaces[2]->hasParent() 136 ); 137 } 138 139 /** 140 * @covers PHP_Token_INTERFACE::getInterfaces 141 */ 142 public function testGetInterfacesExists() 143 { 144 $this->assertEquals( 145 array('b'), 146 $this->class->getInterfaces() 147 ); 148 } 149 150 /** 151 * @covers PHP_Token_INTERFACE::hasInterfaces 152 */ 153 public function testHasInterfacesExists() 154 { 155 $this->assertTrue( 156 $this->class->hasInterfaces() 157 ); 158 } 159 /** 160 * @covers PHP_Token_INTERFACE::getPackage 161 */ 162 public function testGetPackageNamespace() { 163 $tokenStream = new PHP_Token_Stream(TEST_FILES_PATH . 'classInNamespace.php'); 164 foreach($tokenStream as $token) { 165 if($token instanceOf PHP_Token_INTERFACE) { 166 $package = $token->getPackage(); 167 $this->assertSame('Foo\\Bar', $package['namespace']); 168 } 169 } 170 } 171 172 173 public function provideFilesWithClassesWithinMultipleNamespaces() { 174 return array( 175 array(TEST_FILES_PATH . 'multipleNamespacesWithOneClassUsingBraces.php'), 176 array(TEST_FILES_PATH . 'multipleNamespacesWithOneClassUsingNonBraceSyntax.php'), 177 ); 178 } 179 180 /** 181 * @dataProvider provideFilesWithClassesWithinMultipleNamespaces 182 * @covers PHP_Token_INTERFACE::getPackage 183 */ 184 public function testGetPackageNamespaceForFileWithMultipleNamespaces($filepath) { 185 $tokenStream = new PHP_Token_Stream($filepath); 186 $firstClassFound = false; 187 foreach($tokenStream as $token) { 188 if($firstClassFound === false && $token instanceOf PHP_Token_INTERFACE) { 189 $package = $token->getPackage(); 190 $this->assertSame('TestClassInBar', $token->getName()); 191 $this->assertSame('Foo\\Bar', $package['namespace']); 192 $firstClassFound = true; 193 continue; 194 } 195 // Secound class 196 if($token instanceOf PHP_Token_INTERFACE) { 197 $package = $token->getPackage(); 198 $this->assertSame('TestClassInBaz', $token->getName()); 199 $this->assertSame('Foo\\Baz', $package['namespace']); 200 return; 201 } 202 } 203 $this->fail("Seachring for 2 classes failed"); 204 } 205 206 public function testGetPackageNamespaceIsEmptyForInterfacesThatAreNotWithinNamespaces() { 207 foreach($this->interfaces as $token) { 208 $package = $token->getPackage(); 209 $this->assertSame("", $package['namespace']); 210 } 211 } 212 213 /** 214 * @covers PHP_Token_INTERFACE::getPackage 215 */ 216 public function testGetPackageNamespaceWhenExtentingFromNamespaceClass() { 217 $tokenStream = new PHP_Token_Stream(TEST_FILES_PATH . 'classExtendsNamespacedClass.php'); 218 $firstClassFound = false; 219 foreach($tokenStream as $token) { 220 if($firstClassFound === false && $token instanceOf PHP_Token_INTERFACE) { 221 $package = $token->getPackage(); 222 $this->assertSame('Baz', $token->getName()); 223 $this->assertSame('Foo\\Bar', $package['namespace']); 224 $firstClassFound = true; 225 continue; 226 } 227 if($token instanceOf PHP_Token_INTERFACE) { 228 $package = $token->getPackage(); 229 $this->assertSame('Extender', $token->getName()); 230 $this->assertSame('Other\\Space', $package['namespace']); 231 return; 232 } 233 } 234 $this->fail("Searching for 2 classes failed"); 235 } 236 }