Passed
Pull Request — master (#120)
by Marco
06:44
created

LocateDependenciesViaComposerTest::testWillLocateDependenciesEvenWithoutAutoloadFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 23
nc 1
nop 0
dl 0
loc 37
rs 9.552
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RoaveTest\BackwardCompatibility\LocateDependencies;
6
7
use Composer\Installer;
8
use PHPUnit\Framework\MockObject\MockObject;
9
use PHPUnit\Framework\TestCase;
10
use ReflectionProperty;
11
use Roave\BackwardCompatibility\LocateDependencies\LocateDependenciesViaComposer;
12
use Roave\BetterReflection\BetterReflection;
13
use Roave\BetterReflection\SourceLocator\Ast\Locator;
14
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
15
use Roave\BetterReflection\SourceLocator\Type\PhpInternalSourceLocator;
16
use function Safe\getcwd;
17
use function Safe\realpath;
18
19
/**
20
 * @covers \Roave\BackwardCompatibility\LocateDependencies\LocateDependenciesViaComposer
21
 */
22
final class LocateDependenciesViaComposerTest extends TestCase
23
{
24
    /** @var string */
25
    private $originalCwd;
26
27
    /** @var callable */
28
    private $makeInstaller;
29
30
    /** @var Installer|MockObject */
31
    private $composerInstaller;
32
33
    /** @var string|null */
34
    private $expectedInstallatonPath;
35
36
    /** @var Locator */
37
    private $astLocator;
38
39
    /** @var LocateDependenciesViaComposer */
40
    private $locateDependencies;
41
42
    protected function setUp() : void
43
    {
44
        parent::setUp();
45
46
        $originalCwd = getcwd();
47
48
        self::assertInternalType('string', $originalCwd);
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertInternalType() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3369 ( Ignorable by Annotation )

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

48
        /** @scrutinizer ignore-deprecated */ self::assertInternalType('string', $originalCwd);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
49
50
        $this->originalCwd       = $originalCwd;
51
        $this->composerInstaller = $this->createMock(Installer::class);
52
        $this->astLocator        = (new BetterReflection())->astLocator();
53
        $this->makeInstaller     = function (string $installationPath) : Installer {
54
            self::assertSame($this->expectedInstallatonPath, $installationPath);
55
56
            return $this->composerInstaller;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->composerInstaller could return the type PHPUnit\Framework\MockObject\MockObject which is incompatible with the type-hinted return Composer\Installer. Consider adding an additional type-check to rule them out.
Loading history...
57
        };
58
59
        $this
60
            ->composerInstaller
61
            ->expects(self::atLeastOnce())
62
            ->method('setDevMode')
63
            ->with(false);
64
        $this
65
            ->composerInstaller
66
            ->expects(self::atLeastOnce())
67
            ->method('setDumpAutoloader')
68
            ->with(false);
69
        $this
70
            ->composerInstaller
71
            ->expects(self::atLeastOnce())
72
            ->method('setRunScripts')
73
            ->with(false);
74
        $this
75
            ->composerInstaller
76
            ->expects(self::atLeastOnce())
77
            ->method('setIgnorePlatformRequirements')
78
            ->with(true);
79
80
        $this->locateDependencies = new LocateDependenciesViaComposer($this->makeInstaller, $this->astLocator);
81
    }
82
83
    protected function tearDown() : void
84
    {
85
        self::assertSame($this->originalCwd, getcwd());
86
87
        parent::tearDown();
88
    }
89
90
    public function testWillLocateDependencies() : void
91
    {
92
        $this->expectedInstallatonPath = $this->realpath(__DIR__ . '/../../asset/composer-installation-structure');
93
94
        $this
95
            ->composerInstaller
96
            ->expects(self::once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Composer\Installer. ( Ignorable by Annotation )

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

96
            ->/** @scrutinizer ignore-call */ 
97
              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...
97
            ->method('run')
98
            ->willReturnCallback(function () : void {
99
                self::assertSame($this->expectedInstallatonPath, getcwd());
100
            });
101
102
        $locator = $this
103
            ->locateDependencies
104
            ->__invoke($this->expectedInstallatonPath);
105
106
        self::assertInstanceOf(AggregateSourceLocator::class, $locator);
107
108
        $reflectionLocators = new ReflectionProperty(AggregateSourceLocator::class, 'sourceLocators');
109
110
        $reflectionLocators->setAccessible(true);
111
112
        $locators = $reflectionLocators->getValue($locator);
113
114
        self::assertCount(2, $locators);
115
        self::assertInstanceOf(PhpInternalSourceLocator::class, $locators[1]);
116
    }
117
118
    private function realpath(string $path) : string
119
    {
120
        $realPath = realpath($path);
121
122
        self::assertInternalType('string', $realPath);
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertInternalType() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3369 ( Ignorable by Annotation )

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

122
        /** @scrutinizer ignore-deprecated */ self::assertInternalType('string', $realPath);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
123
124
        return $realPath;
125
    }
126
}
127