LocateDependenciesViaComposerTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
eloc 60
c 5
b 0
f 1
dl 0
loc 104
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testWillNotLocateDependenciesForANonExistingPath() 0 12 1
A tearDown() 0 5 1
A setUp() 0 14 1
A testWillLocateDependencies() 0 47 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RoaveTest\BackwardCompatibility\LocateDependencies;
6
7
use Assert\AssertionFailedException;
8
use Composer\Installer;
9
use PHPUnit\Framework\MockObject\MockObject;
10
use PHPUnit\Framework\TestCase;
11
use ReflectionProperty;
12
use Roave\BackwardCompatibility\LocateDependencies\LocateDependenciesViaComposer;
13
use Roave\BetterReflection\BetterReflection;
14
use Roave\BetterReflection\SourceLocator\Ast\Locator;
15
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
16
use Roave\BetterReflection\SourceLocator\Type\PhpInternalSourceLocator;
17
use function Safe\getcwd;
18
use function Safe\realpath;
19
20
/**
21
 * @covers \Roave\BackwardCompatibility\LocateDependencies\LocateDependenciesViaComposer
22
 */
23
final class LocateDependenciesViaComposerTest extends TestCase
24
{
25
    /** @var string */
26
    private $originalCwd;
27
28
    /** @var callable */
29
    private $makeInstaller;
30
31
    /** @var Installer&MockObject */
32
    private $composerInstaller;
33
34
    /** @var string|null */
35
    private $expectedInstallatonPath;
36
37
    /** @var Locator */
38
    private $astLocator;
39
40
    /** @var LocateDependenciesViaComposer */
41
    private $locateDependencies;
42
43
    protected function setUp() : void
44
    {
45
        parent::setUp();
46
47
        $this->originalCwd       = getcwd();
48
        $this->composerInstaller = $this->createMock(Installer::class);
49
        $this->astLocator        = (new BetterReflection())->astLocator();
50
        $this->makeInstaller     = function (string $installationPath) : Installer {
51
            self::assertSame($this->expectedInstallatonPath, $installationPath);
52
53
            return $this->composerInstaller;
54
        };
55
56
        $this->locateDependencies = new LocateDependenciesViaComposer($this->makeInstaller, $this->astLocator);
57
    }
58
59
    protected function tearDown() : void
60
    {
61
        self::assertSame($this->originalCwd, getcwd());
62
63
        parent::tearDown();
64
    }
65
66
    public function testWillNotLocateDependenciesForANonExistingPath() : void
67
    {
68
        $this
69
            ->composerInstaller
70
            ->expects(self::never())
71
            ->method('run');
72
73
        $this->expectException(AssertionFailedException::class);
74
75
        $this
76
            ->locateDependencies
77
            ->__invoke(__DIR__ . '/non-existing');
78
    }
79
80
    public function testWillLocateDependencies() : void
81
    {
82
        $this->expectedInstallatonPath = realpath(__DIR__ . '/../../asset/composer-installation-structure');
83
84
        $this
85
            ->composerInstaller
86
            ->expects(self::atLeastOnce())
87
            ->method('setDevMode')
88
            ->with(false);
89
        $this
90
            ->composerInstaller
91
            ->expects(self::atLeastOnce())
92
            ->method('setDumpAutoloader')
93
            ->with(false);
94
        $this
95
            ->composerInstaller
96
            ->expects(self::atLeastOnce())
97
            ->method('setRunScripts')
98
            ->with(false);
99
        $this
100
            ->composerInstaller
101
            ->expects(self::atLeastOnce())
102
            ->method('setIgnorePlatformRequirements')
103
            ->with(true);
104
105
        $this
106
            ->composerInstaller
107
            ->expects(self::once())
108
            ->method('run')
109
            ->willReturnCallback(function () : void {
110
                self::assertSame($this->expectedInstallatonPath, getcwd());
111
            });
112
113
        $locator = $this
114
            ->locateDependencies
115
            ->__invoke($this->expectedInstallatonPath);
116
117
        self::assertInstanceOf(AggregateSourceLocator::class, $locator);
118
119
        $reflectionLocators = new ReflectionProperty(AggregateSourceLocator::class, 'sourceLocators');
120
121
        $reflectionLocators->setAccessible(true);
122
123
        $locators = $reflectionLocators->getValue($locator);
124
125
        self::assertCount(2, $locators);
126
        self::assertInstanceOf(PhpInternalSourceLocator::class, $locators[1]);
127
    }
128
}
129