Completed
Push — master ( 4eb33b...9d87b5 )
by Marco
11s queued 10s
created

StaticClassMapSourceLocatorTest::acceptsEmptySet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RoaveTest\BackwardCompatibility\SourceLocator;
6
7
use InvalidArgumentException;
8
use PHPUnit\Framework\MockObject\MockObject;
9
use PHPUnit\Framework\TestCase;
10
use Roave\BackwardCompatibility\SourceLocator\StaticClassMapSourceLocator;
11
use Roave\BetterReflection\Identifier\Identifier;
12
use Roave\BetterReflection\Identifier\IdentifierType;
13
use Roave\BetterReflection\Reflection\Reflection;
14
use Roave\BetterReflection\Reflector\Reflector;
15
use Roave\BetterReflection\SourceLocator\Ast\Locator;
16
use Roave\BetterReflection\SourceLocator\Located\LocatedSource;
17
use function Safe\file_get_contents;
18
19
/**
20
 * @covers \Roave\BackwardCompatibility\SourceLocator\StaticClassMapSourceLocator
21
 */
22
final class StaticClassMapSourceLocatorTest extends TestCase
23
{
24
    /** @var Locator&MockObject */
25
    private $astLocator;
26
27
    /** @var Reflector&MockObject */
28
    private $reflector;
29
30
    protected function setUp() : void
31
    {
32
        parent::setUp();
0 ignored issues
show
Bug introduced by
The method setUp() does not exist on PHPUnit\Framework\TestCase. It seems like you code against a sub-type of PHPUnit\Framework\TestCase such as Symfony\Component\Yaml\Tests\InlineTest or SebastianBergmann\Diff\ParserTest or PHP_Token_FunctionTest or SebastianBergmann\Compar...ockObjectComparatorTest or PharIo\Manifest\BundlesElementTest or SebastianBergmann\Diff\O...tBuilderIntegrationTest or PharIo\Manifest\LicenseElementTest or RoaveTest\BackwardCompat...ed\OpenClassChangedTest or ExceptionInSetUpTest or PharIo\Manifest\ManifestTest or SebastianBergmann\GlobalState\BlacklistTest or PHPUnit\Runner\DefaultTestResultCacheTest or RoaveTest\BackwardCompat...tectedMethodChangedTest or PHPUnit\Util\ConfigurationTest or PHPUnit\Framework\TestListenerTest or SebastianBergmann\Diff\U...ertTraitIntegrationTest or BankAccountWithCustomExtensionTest or RoaveTest\BackwardCompat...ipMethodBasedErrorsTest or PHP_Token_IncludeTest or RoaveTest\BackwardCompat...essibleMethodChangeTest or SebastianBergmann\Comparator\DoubleComparatorTest or SebastianBergmann\Compar...\DateTimeComparatorTest or PharIo\Manifest\RequiresElementTest or RoaveTest\BackwardCompat...PropertyBasedErrorsTest or RoaveTest\BackwardCompat...nterfaceBasedErrorsTest or Symfony\Component\Consol...s\Question\QuestionTest or SebastianBergmann\Comparator\ScalarComparatorTest or SebastianBergmann\Comparator\ObjectComparatorTest or Symfony\Component\Consol...\Helper\ProgressBarTest or PHP_Token_ClassTest or PharIo\Manifest\CopyrightElementTest or Symfony\Component\Filesy...ests\FilesystemTestCase or RoaveTest\BackwardCompat...kipTraitBasedErrorsTest or Symfony\Component\Console\Tests\ApplicationTest or PHP_CodeSniffer\Tests\St...s\AbstractSniffUnitTest or PHP_CodeSniffer\Tests\Co...ndExtendedClassNameTest or PHP_CodeSniffer\Tests\Co...GetMethodParametersTest or SebastianBergmann\CodeUnitReverseLookup\WizardTest or PharIo\Manifest\BundledComponentCollectionTest or PHP_CodeSniffer\Tests\Co...entedInterfaceNamesTest or TheSeer\Tokenizer\XMLSerializerTest or PHPUnit\Runner\PhptTestCaseTest or SebastianBergmann\Environment\ConsoleTest or RoaveTest\BackwardCompat...ctedPropertyChangedTest or SebastianBergmann\Type\ObjectTypeTest or RoaveTest\BackwardCompat...lassConstantChangedTest or ExceptionInAssertPreConditionsTest or ExceptionInTearDownTest or RoaveTest\BackwardCompat...d\FinalClassChangedTest or ExceptionInAssertPostConditionsTest or PHPUnit\Framework\TestSuiteTest or TheSeer\Tokenizer\TokenCollectionTest or SebastianBergmann\Object...tor\ObjectReflectorTest or RoaveTest\BackwardCompat...blicPropertyChangedTest or SebastianBergmann\RecursionContext\ContextTest or PHPUnit\SelfTest\Basic\SetUpTest or Symfony\Component\Consol...\Style\SymfonyStyleTest or PharIo\Manifest\ComponentElementTest or Symfony\Component\Consol...ester\CommandTesterTest or PHPUnit\Util\PHP\AbstractPhpProcessTest or SebastianBergmann\Type\IterableTypeTest or SebastianBergmann\Environment\OperatingSystemTest or PharIo\Manifest\LicenseTest or SebastianBergmann\Comparator\TypeComparatorTest or PharIo\Manifest\ExtensionTest or PHPUnit\SelfTest\Basic\SetUpBeforeClassTest or RoaveTest\BackwardCompatibility\CompareClassesTest or SebastianBergmann\CodeCoverage\FilterTest or RoaveTest\BackwardCompat...iblePropertyChangedTest or Humbug\Test\FunctionTest or Symfony\Component\Consol...Output\StreamOutputTest or PharIo\Manifest\ExtElementTest or Symfony\Component\Consol...onsoleSectionOutputTest or SebastianBergmann\Compar...\ResourceComparatorTest or GeneratorTest or ExceptionInTest or RoaveTest\BackwardCompat...FunctionBasedErrorsTest or TemplateMethodsTest or RoaveTest\BackwardCompat...onFromGitRepositoryTest or PharIo\Manifest\AuthorCollectionTest or SebastianBergmann\Comparator\DOMNodeComparatorTest or RoaveTest\BackwardCompat...onDefinitionChangedTest or SebastianBergmann\Exporter\ExporterTest or PharIo\Manifest\RequirementCollectionTest or RoaveTest\BackwardCompat...BackwardsCompatibleTest or PharIo\Manifest\AuthorElementTest or Symfony\Component\Consol...r\ApplicationTesterTest or PHP_CodeSniffer\Tests\Co...GetMemberPropertiesTest or RoaveTest\BackwardCompat...assMapSourceLocatorTest or SebastianBergmann\FileIterator\FactoryTest or PharIo\Manifest\BundledComponentTest or SebastianBergmann\Diff\O...tBuilderIntegrationTest or PharIo\Manifest\AuthorTest or RoaveTest\BackwardCompat...kipClassBasedErrorsTest or PharIo\Manifest\PhpVersionRequirementTest or RoaveE2ETest\BackwardCom...BackwardsCompatibleTest or PHP_CodeSniffer\Tests\Co...GetMethodPropertiesTest or SebastianBergmann\GlobalState\SnapshotTest or Symfony\Component\Console\Tests\TerminalTest or PHP_CodeSniffer\Tests\Co...\FindEndOfStatementTest or PHP_CodeSniffer\Tests\Core\File\IsReferenceTest or PHPUnit\Runner\ResultCacheExtensionTest or TheSeer\Tokenizer\TokenTest or PHPUnit\Util\TestDox\NamePrettifierTest or SebastianBergmann\Comparator\NumericComparatorTest or PHPUnit\Util\TestDox\CliTestDoxPrinterColorTest or PHP_Token_ClosureTest or SebastianBergmann\Diff\LineTest or Symfony\Component\Yaml\Tests\DumperTest or SebastianBergmann\Compar...ctStorageComparatorTest or PharIo\Manifest\LibraryTest or SebastianBergmann\Diff\DifferTest or RoaveTest\BackwardCompat...ConstantBasedErrorsTest or SebastianBergmann\Comparator\ArrayComparatorTest or PhpParser\Builder\InterfaceTest or PharIo\Manifest\CopyrightInformationTest or Symfony\Component\Yaml\T...Command\LintCommandTest or PHPUnit\Util\TestDox\CliTestDoxPrinterTest or SebastianBergmann\Environment\RuntimeTest or RoaveTest\BackwardCompat...rcesViaComposerJsonTest or SebastianBergmann\ObjectEnumerator\EnumeratorTest or SebastianBergmann\Diff\L...stCommonSubsequenceTest or PharIo\Manifest\ContainsElementTest or Issue1374Test or PharIo\Manifest\ApplicationTest or PharIo\Manifest\ExtensionElementTest or BankAccountTest or SebastianBergmann\Type\CallableTypeTest or PharIo\Manifest\PhpElementTest or RoaveTest\BackwardCompat...ndenciesViaComposerTest or SebastianBergmann\Type\NullTypeTest or SebastianBergmann\Type\UnknownTypeTest or PHP_Token_InterfaceTest or RoaveTest\BackwardCompat...lassConstantChangedTest or SebastianBergmann\Compar...ExceptionComparatorTest or SebastianBergmann\Diff\ChunkTest or RoaveTest\BackwardCompat...PublicMethodChangedTest or SebastianBergmann\CodeCoverage\CodeCoverageTest or SebastianBergmann\CodeCoverage\Report\BuilderTest. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
        parent::/** @scrutinizer ignore-call */ 
33
                setUp();
Loading history...
33
34
        $this->astLocator = $this->createMock(Locator::class);
35
        $this->reflector  = $this->createMock(Reflector::class);
36
    }
37
38
    public function testRejectsEmptyKeys() : void
39
    {
40
        $this->expectException(InvalidArgumentException::class);
41
42
        new StaticClassMapSourceLocator(
43
            ['' => __FILE__],
44
            $this->astLocator
45
        );
46
    }
47
48
    public function testRejectsEmptyStringFiles() : void
49
    {
50
        $this->expectException(InvalidArgumentException::class);
51
52
        new StaticClassMapSourceLocator(
53
            ['foo' => ''],
54
            $this->astLocator
55
        );
56
    }
57
58
    public function testAcceptsEmptySet() : void
59
    {
60
        $locator = new StaticClassMapSourceLocator([], $this->astLocator);
61
62
        self::assertNull($locator->locateIdentifier(
63
            $this->reflector,
64
            new Identifier(self::class, new IdentifierType(IdentifierType::IDENTIFIER_CLASS))
65
        ));
66
    }
67
68
    /**
69
     * @dataProvider thisClassPossiblePaths
70
     */
71
    public function testWillLocateThisClass(string $thisClassFilePath) : void
72
    {
73
        $locator    = new StaticClassMapSourceLocator([self::class => $thisClassFilePath], $this->astLocator);
74
        $reflection = $this->createMock(Reflection::class);
75
76
        $this
77
            ->astLocator
78
            ->expects(self::once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Roave\BetterReflection\SourceLocator\Ast\Locator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
            ->/** @scrutinizer ignore-call */ 
79
              expects(self::once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method once() does not exist on RoaveTest\BackwardCompat...assMapSourceLocatorTest. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
            ->expects(self::/** @scrutinizer ignore-call */ once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method expects() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
            ->/** @scrutinizer ignore-call */ 
79
              expects(self::once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
79
            ->method('findReflection')
80
            ->with($this->reflector, self::callback(static function (LocatedSource $source) : bool {
0 ignored issues
show
Bug introduced by
The method callback() does not exist on RoaveTest\BackwardCompat...assMapSourceLocatorTest. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

80
            ->with($this->reflector, self::/** @scrutinizer ignore-call */ callback(static function (LocatedSource $source) : bool {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
81
                self::assertSame(file_get_contents(__FILE__), $source->getSource());
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\Assert::assertSame() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
                self::/** @scrutinizer ignore-call */ 
82
                      assertSame(file_get_contents(__FILE__), $source->getSource());
Loading history...
Bug introduced by
file_get_contents(__FILE__) of type string is incompatible with the type PHPUnit\Framework\T expected by parameter $expected of PHPUnit\Framework\Assert::assertSame(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

81
                self::assertSame(/** @scrutinizer ignore-type */ file_get_contents(__FILE__), $source->getSource());
Loading history...
82
                self::assertSame(__FILE__, $source->getFileName());
83
                self::assertNull($source->getExtensionName());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $source->getExtensionName() targeting Roave\BetterReflection\S...rce::getExtensionName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
84
85
                return true;
86
            }))
87
            ->willReturn($reflection);
88
89
        self::assertSame($reflection, $locator->locateIdentifier(
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\Assert::assertSame() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

89
        self::/** @scrutinizer ignore-call */ 
90
              assertSame($reflection, $locator->locateIdentifier(
Loading history...
90
            $this->reflector,
91
            new Identifier(self::class, new IdentifierType(IdentifierType::IDENTIFIER_CLASS))
92
        ));
93
    }
94
95
    /** @return array<int, array<int, string>> */
96
    public static function thisClassPossiblePaths() : array
97
    {
98
        return [
99
            [__FILE__],
100
            [__DIR__ . '/../SourceLocator/StaticClassMapSourceLocatorTest.php'],
101
        ];
102
    }
103
104
    public function testWillNotLocateUnknownClass() : void
105
    {
106
        $locator = new StaticClassMapSourceLocator([self::class => __FILE__], $this->astLocator);
107
108
        $this
109
            ->astLocator
110
            ->expects(self::never())
0 ignored issues
show
Bug introduced by
The method never() does not exist on RoaveTest\BackwardCompat...assMapSourceLocatorTest. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

110
            ->expects(self::/** @scrutinizer ignore-call */ never())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
111
            ->method('findReflection');
112
113
        self::assertNull($locator->locateIdentifier(
114
            $this->reflector,
115
            new Identifier('Unknown\\ClassName', new IdentifierType(IdentifierType::IDENTIFIER_CLASS))
116
        ));
117
    }
118
119
    public function testWillNotLocateFunctions() : void
120
    {
121
        $locator = new StaticClassMapSourceLocator([self::class => __FILE__], $this->astLocator);
122
123
        $this
124
            ->astLocator
125
            ->expects(self::never())
126
            ->method('findReflection');
127
128
        self::assertNull($locator->locateIdentifier(
129
            $this->reflector,
130
            new Identifier(self::class, new IdentifierType(IdentifierType::IDENTIFIER_FUNCTION))
131
        ));
132
    }
133
}
134