Completed
Pull Request — master (#48)
by Marco
02:32
created

LocateDependenciesViaComposerTest::setUp()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 40
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 35
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RoaveTest\ApiCompare\LocateDependencies;
6
7
use Composer\Installer;
8
use PHPUnit\Framework\MockObject\MockObject;
9
use PHPUnit\Framework\TestCase;
10
use Roave\ApiCompare\LocateDependencies\LocateDependenciesViaComposer;
11
use Roave\ApiCompare\SourceLocator\StaticClassMapSourceLocator;
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 Roave\BetterReflection\SourceLocator\Type\SingleFileSourceLocator;
17
18
/**
19
 * @covers \Roave\ApiCompare\LocateDependencies\LocateDependenciesViaComposer
20
 */
21
final class LocateDependenciesViaComposerTest extends TestCase
22
{
23
    /** @var string */
24
    private $originalCwd;
25
26
    /** @var Installer|MockObject */
27
    private $composerInstaller;
28
29
    /** @var Locator */
30
    private $astLocator;
31
32
    /** @var LocateDependenciesViaComposer */
33
    private $locateDependencies;
34
35
    protected function setUp() : void
36
    {
37
        parent::setUp();
38
39
        $this->originalCwd       = getcwd();
40
        $this->composerInstaller = $this->createMock(Installer::class);
41
        $this->astLocator        = (new BetterReflection())->astLocator();
42
43
        $this
44
            ->composerInstaller
45
            ->expects(self::atLeastOnce())
46
            ->method('setDevMode')
47
            ->with(false);
48
        $this
49
            ->composerInstaller
50
            ->expects(self::atLeastOnce())
51
            ->method('setDumpAutoloader')
52
            ->with(true);
53
        $this
54
            ->composerInstaller
55
            ->expects(self::atLeastOnce())
56
            ->method('setRunScripts')
57
            ->with(false);
58
        $this
59
            ->composerInstaller
60
            ->expects(self::atLeastOnce())
61
            ->method('setOptimizeAutoloader')
62
            ->with(true);
63
        $this
64
            ->composerInstaller
65
            ->expects(self::atLeastOnce())
66
            ->method('setClassMapAuthoritative')
67
            ->with(true);
68
        $this
69
            ->composerInstaller
70
            ->expects(self::atLeastOnce())
71
            ->method('setIgnorePlatformRequirements')
72
            ->with(true);
73
74
        $this->locateDependencies = new LocateDependenciesViaComposer($this->composerInstaller, $this->astLocator);
75
    }
76
77
    protected function tearDown() : void
78
    {
79
        self::assertSame($this->originalCwd, getcwd());
80
81
        parent::tearDown();
82
    }
83
84
    public function testWillLocateDependencies() : void
85
    {
86
        $composerInstallationStructure = realpath(__DIR__ . '/../../asset/composer-installation-structure');
87
88
        $this
89
            ->composerInstaller
90
            ->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

90
            ->/** @scrutinizer ignore-call */ 
91
              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...
91
            ->method('run')
92
            ->willReturnCallback(function () use ($composerInstallationStructure) : void {
93
                self::assertSame($composerInstallationStructure, getcwd());
94
            });
95
96
        $locator = $this
97
            ->locateDependencies
98
            ->__invoke($composerInstallationStructure);
99
100
        self::assertInstanceOf(AggregateSourceLocator::class, $locator);
101
102
        $reflectionLocators = new \ReflectionProperty(AggregateSourceLocator::class, 'sourceLocators');
103
104
        $reflectionLocators->setAccessible(true);
105
106
        $locators = $reflectionLocators->getValue($locator);
107
108
        self::assertCount(3, $locators);
109
        self::assertEquals(
110
            new StaticClassMapSourceLocator(
111
                [
112
                    'A\\ClassName' => realpath(__DIR__ . '/../../asset/composer-installation-structure/AClassName.php'),
113
                    'B\\ClassName' => realpath(__DIR__ . '/../../asset/composer-installation-structure/BClassName.php'),
114
                ],
115
                $this->astLocator
116
            ),
117
            $locators[0]
118
        );
119
        self::assertEquals(
120
            new AggregateSourceLocator([
121
                new SingleFileSourceLocator(
122
                    realpath(__DIR__ . '/../../asset/composer-installation-structure/included-file-1.php'),
123
                    $this->astLocator
124
                ),
125
                new SingleFileSourceLocator(
126
                    realpath(__DIR__ . '/../../asset/composer-installation-structure/included-file-2.php'),
127
                    $this->astLocator
128
                ),
129
            ]),
130
            $locators[1]
131
        );
132
        self::assertInstanceOf(PhpInternalSourceLocator::class, $locators[2]);
133
    }
134
}
135