Completed
Push — master ( 85028e...e22ffb )
by Marco
15s queued 10s
created

LocateSourcesViaComposerJsonTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 78
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A realPath() 0 7 1
A locatedSourcesExamples() 0 36 1
A astLocator() 0 3 1
A setUp() 0 5 1
A testExpectedLocatedSourcesPaths() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RoaveTest\BackwardCompatibility\LocateSources;
6
7
use PHPUnit\Framework\TestCase;
8
use Roave\BackwardCompatibility\LocateSources\LocateSourcesViaComposerJson;
9
use Roave\BetterReflection\BetterReflection;
10
use Roave\BetterReflection\SourceLocator\Ast\Locator;
11
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
12
use Roave\BetterReflection\SourceLocator\Type\DirectoriesSourceLocator;
13
use Roave\BetterReflection\SourceLocator\Type\SingleFileSourceLocator;
14
use Roave\BetterReflection\SourceLocator\Type\SourceLocator;
15
use function realpath;
16
17
/**
18
 * @covers \Roave\BackwardCompatibility\LocateSources\LocateSourcesViaComposerJson
19
 */
20
final class LocateSourcesViaComposerJsonTest extends TestCase
21
{
22
    /** @var Locator|null */
23
    private static $astLocator;
24
25
    /** @var LocateSourcesViaComposerJson */
26
    private $locateSources;
27
28
    protected function setUp() : void
29
    {
30
        parent::setUp();
31
32
        $this->locateSources = new LocateSourcesViaComposerJson($this->astLocator());
33
    }
34
35
    /** @dataProvider locatedSourcesExamples */
36
    public function testExpectedLocatedSourcesPaths(
37
        string $installationPath,
38
        SourceLocator $expectedSourceLocator
39
    ) : void {
40
        self::assertEquals($expectedSourceLocator, $this->locateSources->__invoke($installationPath));
41
    }
42
43
    /** @return string[][]|SourceLocator[][] */
44
    public function locatedSourcesExamples() : array
45
    {
46
        return [
47
            'empty composer definition' => [
48
                __DIR__ . '/../../asset/located-sources/empty',
49
                new AggregateSourceLocator([
50
                    new DirectoriesSourceLocator([], $this->astLocator()),
51
                ]),
52
            ],
53
            'composer definition with everything' => [
54
                __DIR__ . '/../../asset/located-sources/composer-definition-with-everything',
55
                new AggregateSourceLocator([
56
                    new DirectoriesSourceLocator(
57
                        [
58
                            $this->realPath(__DIR__ . '/../../asset/located-sources/composer-definition-with-everything/foo0'),
59
                            $this->realPath(__DIR__ . '/../../asset/located-sources/composer-definition-with-everything/bar4'),
60
                            $this->realPath(__DIR__ . '/../../asset/located-sources/composer-definition-with-everything/baz4_0'),
61
                            $this->realPath(__DIR__ . '/../../asset/located-sources/composer-definition-with-everything/baz4_1'),
62
                            $this->realPath(__DIR__ . '/../../asset/located-sources/composer-definition-with-everything/baz4_2'),
63
                            $this->realPath(__DIR__ . '/../../asset/located-sources/composer-definition-with-everything/baz4_3'),
64
                            $this->realPath(__DIR__ . '/../../asset/located-sources/composer-definition-with-everything/classmap0'),
65
                            $this->realPath(__DIR__ . '/../../asset/located-sources/composer-definition-with-everything/classmap1'),
66
                        ],
67
                        $this->astLocator()
68
                    ),
69
                    new SingleFileSourceLocator(
70
                        $this->realPath(__DIR__ . '/../../asset/located-sources/composer-definition-with-everything/classmap2/file.php'),
71
                        $this->astLocator()
72
                    ),
73
                    new SingleFileSourceLocator(
74
                        $this->realPath(__DIR__ . '/../../asset/located-sources/composer-definition-with-everything/files/foo.php'),
75
                        $this->astLocator()
76
                    ),
77
                    new SingleFileSourceLocator(
78
                        $this->realPath(__DIR__ . '/../../asset/located-sources/composer-definition-with-everything/files/bar.php'),
79
                        $this->astLocator()
80
                    ),
81
                ]),
82
            ],
83
        ];
84
    }
85
86
    private function astLocator() : Locator
87
    {
88
        return self::$astLocator ?? self::$astLocator = (new BetterReflection())->astLocator();
89
    }
90
91
    private function realPath(string $path) : string
92
    {
93
        $realPath = realpath($path);
94
95
        self::assertInternalType('string', $realPath);
96
97
        return $realPath;
98
    }
99
}
100