Passed
Pull Request — master (#48)
by Marco
02:59
created

LocateDependenciesViaComposerTest::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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

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