1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
<?php
/**
* Tests for the \PHP_CodeSniffer\Files\File::findImplementedInterfaceNames method.
*
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/
namespace PHP_CodeSniffer\Tests\Core\File;
use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTestCase;
/**
* Tests for the \PHP_CodeSniffer\Files\File::findImplementedInterfaceNames method.
*
* @covers \PHP_CodeSniffer\Files\File::findImplementedInterfaceNames
*/
final class FindImplementedInterfaceNamesTest extends AbstractMethodUnitTestCase
{
/**
* Test getting a `false` result when a non-existent token is passed.
*
* @return void
*/
public function testNonExistentToken()
{
$result = self::$phpcsFile->findImplementedInterfaceNames(100000);
$this->assertFalse($result);
}//end testNonExistentToken()
/**
* Test getting a `false` result when a token other than one of the supported tokens is passed.
*
* @return void
*/
public function testNotAClass()
{
$token = $this->getTargetToken('/* testNotAClass */', [T_FUNCTION]);
$result = self::$phpcsFile->findImplementedInterfaceNames($token);
$this->assertFalse($result);
}//end testNotAClass()
/**
* Test retrieving the name(s) of the interfaces being implemented by a class.
*
* @param string $identifier Comment which precedes the test case.
* @param array<string>|false $expected Expected function output.
*
* @dataProvider dataImplementedInterface
*
* @return void
*/
public function testFindImplementedInterfaceNames($identifier, $expected)
{
$OOToken = $this->getTargetToken($identifier, [T_CLASS, T_ANON_CLASS, T_INTERFACE, T_ENUM]);
$result = self::$phpcsFile->findImplementedInterfaceNames($OOToken);
$this->assertSame($expected, $result);
}//end testFindImplementedInterfaceNames()
/**
* Data provider for the FindImplementedInterfaceNames test.
*
* @see testFindImplementedInterfaceNames()
*
* @return array<string, array<string, string|array<string>>>
*/
public static function dataImplementedInterface()
{
return [
'interface declaration, no implements' => [
'identifier' => '/* testPlainInterface */',
'expected' => false,
],
'class does not implement' => [
'identifier' => '/* testNonImplementedClass */',
'expected' => false,
],
'class implements single interface, unqualified' => [
'identifier' => '/* testClassImplementsSingle */',
'expected' => [
'testFIINInterface',
],
],
'class implements multiple interfaces' => [
'identifier' => '/* testClassImplementsMultiple */',
'expected' => [
'testFIINInterface',
'testFIINInterface2',
],
],
'class implements single interface, fully qualified' => [
'identifier' => '/* testImplementsFullyQualified */',
'expected' => [
'\PHP_CodeSniffer\Tests\Core\File\testFIINInterface',
],
],
'class implements single interface, partially qualified' => [
'identifier' => '/* testImplementsPartiallyQualified */',
'expected' => [
'Core\File\RelativeInterface',
],
],
'class extends and implements' => [
'identifier' => '/* testClassThatExtendsAndImplements */',
'expected' => [
'InterfaceA',
'\NameSpaced\Cat\InterfaceB',
],
],
'class implements and extends' => [
'identifier' => '/* testClassThatImplementsAndExtends */',
'expected' => [
'\InterfaceA',
'InterfaceB',
],
],
'enum does not implement' => [
'identifier' => '/* testBackedEnumWithoutImplements */',
'expected' => false,
],
'enum implements single interface, unqualified' => [
'identifier' => '/* testEnumImplementsSingle */',
'expected' => [
'Colorful',
],
],
'enum implements multiple interfaces, unqualified + fully qualified' => [
'identifier' => '/* testBackedEnumImplementsMulti */',
'expected' => [
'Colorful',
'\Deck',
],
],
'anon class implements single interface, unqualified' => [
'identifier' => '/* testAnonClassImplementsSingle */',
'expected' => [
'testFIINInterface',
],
],
'parse error - implements keyword, but no interface name' => [
'identifier' => '/* testMissingImplementsName */',
'expected' => false,
],
'parse error - live coding - no curly braces' => [
'identifier' => '/* testParseError */',
'expected' => false,
],
];
}//end dataImplementedInterface()
}//end class
|