3 * This file is part of the PHP_TokenStream package.
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
12 * Tests for the PHP_Token_INTERFACE class.
14 * @package PHP_TokenStream
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
24 class PHP_Token_InterfaceTest extends PHPUnit_Framework_TestCase
27 protected $interfaces;
29 protected function setUp()
31 $ts = new PHP_Token_Stream(TEST_FILES_PATH . 'source4.php');
33 foreach ($ts as $token) {
34 if ($token instanceof PHP_Token_CLASS) {
35 $this->class = $token;
37 elseif ($token instanceof PHP_Token_INTERFACE) {
38 $this->interfaces[$i] = $token;
45 * @covers PHP_Token_INTERFACE::getName
47 public function testGetName()
50 'iTemplate', $this->interfaces[0]->getName()
55 * @covers PHP_Token_INTERFACE::getParent
57 public function testGetParentNotExists()
60 $this->interfaces[0]->getParent()
65 * @covers PHP_Token_INTERFACE::hasParent
67 public function testHasParentNotExists()
70 $this->interfaces[0]->hasParent()
75 * @covers PHP_Token_INTERFACE::getParent
77 public function testGetParentExists()
80 'a', $this->interfaces[2]->getParent()
85 * @covers PHP_Token_INTERFACE::hasParent
87 public function testHasParentExists()
90 $this->interfaces[2]->hasParent()
95 * @covers PHP_Token_INTERFACE::getInterfaces
97 public function testGetInterfacesExists()
101 $this->class->getInterfaces()
106 * @covers PHP_Token_INTERFACE::hasInterfaces
108 public function testHasInterfacesExists()
111 $this->class->hasInterfaces()
115 * @covers PHP_Token_INTERFACE::getPackage
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']);
128 public function provideFilesWithClassesWithinMultipleNamespaces() {
130 array(TEST_FILES_PATH . 'multipleNamespacesWithOneClassUsingBraces.php'),
131 array(TEST_FILES_PATH . 'multipleNamespacesWithOneClassUsingNonBraceSyntax.php'),
136 * @dataProvider provideFilesWithClassesWithinMultipleNamespaces
137 * @covers PHP_Token_INTERFACE::getPackage
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;
151 if($token instanceOf PHP_Token_INTERFACE) {
152 $package = $token->getPackage();
153 $this->assertSame('TestClassInBaz', $token->getName());
154 $this->assertSame('Foo\\Baz', $package['namespace']);
158 $this->fail("Seachring for 2 classes failed");
161 public function testGetPackageNamespaceIsEmptyForInterfacesThatAreNotWithinNamespaces() {
162 foreach($this->interfaces as $token) {
163 $package = $token->getPackage();
164 $this->assertSame("", $package['namespace']);
169 * @covers PHP_Token_INTERFACE::getPackage
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;
182 if($token instanceOf PHP_Token_INTERFACE) {
183 $package = $token->getPackage();
184 $this->assertSame('Extender', $token->getName());
185 $this->assertSame('Other\\Space', $package['namespace']);
189 $this->fail("Searching for 2 classes failed");