File: PathRepositoryTest.php

package info (click to toggle)
composer 2.9.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,528 kB
  • sloc: php: 83,030; makefile: 59; xml: 39
file content (177 lines) | stat: -rw-r--r-- 6,288 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php declare(strict_types=1);

/*
 * This file is part of Composer.
 *
 * (c) Nils Adermann <naderman@naderman.de>
 *     Jordi Boggiano <j.boggiano@seld.be>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Composer\Test\Repository;

use Composer\Repository\PathRepository;
use Composer\Test\TestCase;
use Composer\Util\HttpDownloader;
use Composer\Util\Loop;
use Composer\Util\Platform;
use Composer\Util\ProcessExecutor;

class PathRepositoryTest extends TestCase
{
    public function testLoadPackageFromFileSystemWithIncorrectPath(): void
    {
        self::expectException('RuntimeException');

        $repositoryUrl = implode(DIRECTORY_SEPARATOR, [__DIR__, 'Fixtures', 'path', 'missing']);
        $repository = $this->createPathRepo(['url' => $repositoryUrl]);
        $repository->getPackages();
    }

    public function testLoadPackageFromFileSystemWithVersion(): void
    {
        $repositoryUrl = implode(DIRECTORY_SEPARATOR, [__DIR__, 'Fixtures', 'path', 'with-version']);
        $repository = $this->createPathRepo(['url' => $repositoryUrl]);
        $repository->getPackages();

        self::assertSame(1, $repository->count());
        self::assertTrue($repository->hasPackage(self::getPackage('test/path-versioned', '0.0.2')));
    }

    public function testLoadPackageFromFileSystemWithoutVersion(): void
    {
        $repositoryUrl = implode(DIRECTORY_SEPARATOR, [__DIR__, 'Fixtures', 'path', 'without-version']);
        $repository = $this->createPathRepo(['url' => $repositoryUrl]);
        $packages = $repository->getPackages();

        self::assertGreaterThanOrEqual(1, $repository->count());

        $package = $packages[0];
        self::assertSame('test/path-unversioned', $package->getName());

        $packageVersion = $package->getVersion();
        self::assertNotEmpty($packageVersion);
    }

    public function testLoadPackageFromFileSystemWithWildcard(): void
    {
        $repositoryUrl = implode(DIRECTORY_SEPARATOR, [__DIR__, 'Fixtures', 'path', '*']);
        $repository = $this->createPathRepo(['url' => $repositoryUrl]);
        $packages = $repository->getPackages();
        $names = [];

        self::assertGreaterThanOrEqual(2, $repository->count());

        $package = $packages[0];
        $names[] = $package->getName();

        $package = $packages[1];
        $names[] = $package->getName();

        sort($names);
        self::assertEquals(['test/path-unversioned', 'test/path-versioned'], $names);
    }

    public function testLoadPackageWithExplicitVersions(): void
    {
        $options = [
            'versions' => [
                'test/path-unversioned' => '4.3.2.1',
                'test/path-versioned' => '3.2.1.0',
            ],
        ];
        $repositoryUrl = implode(DIRECTORY_SEPARATOR, [__DIR__, 'Fixtures', 'path', '*']);
        $repository = $this->createPathRepo(['url' => $repositoryUrl, 'options' => $options]);
        $packages = $repository->getPackages();

        $versions = [];

        self::assertEquals(2, $repository->count());

        $package = $packages[0];
        $versions[$package->getName()] = $package->getVersion();

        $package = $packages[1];
        $versions[$package->getName()] = $package->getVersion();

        ksort($versions);
        self::assertSame(['test/path-unversioned' => '4.3.2.1', 'test/path-versioned' => '3.2.1.0'], $versions);
    }

    /**
     * Verify relative repository URLs remain relative, see #4439
     */
    public function testUrlRemainsRelative(): void
    {
        // realpath() does not fully expand the paths
        // PHP Bug https://bugs.php.net/bug.php?id=72642
        $repositoryUrl = implode(DIRECTORY_SEPARATOR, [realpath(realpath(__DIR__)), 'Fixtures', 'path', 'with-version']);
        // getcwd() not necessarily match __DIR__
        // PHP Bug https://bugs.php.net/bug.php?id=73797
        $relativeUrl = ltrim(substr($repositoryUrl, strlen(realpath(realpath(Platform::getCwd())))), DIRECTORY_SEPARATOR);

        $repository = $this->createPathRepo(['url' => $relativeUrl]);
        $packages = $repository->getPackages();

        self::assertSame(1, $repository->count());

        $package = $packages[0];
        self::assertSame('test/path-versioned', $package->getName());

        // Convert platform specific separators back to generic URL slashes
        $relativeUrl = str_replace(DIRECTORY_SEPARATOR, '/', $relativeUrl);
        self::assertSame($relativeUrl, $package->getDistUrl());
    }

    public function testReferenceNone(): void
    {
        $options = [
            'reference' => 'none',
        ];
        $repositoryUrl = implode(DIRECTORY_SEPARATOR, [__DIR__, 'Fixtures', 'path', '*']);
        $repository = $this->createPathRepo(['url' => $repositoryUrl, 'options' => $options]);
        $packages = $repository->getPackages();

        self::assertGreaterThanOrEqual(2, $repository->count());

        foreach ($packages as $package) {
            self::assertEquals($package->getDistReference(), null);
        }
    }

    public function testReferenceConfig(): void
    {
        $options = [
            'reference' => 'config',
            'relative' => true,
        ];
        $repositoryUrl = implode(DIRECTORY_SEPARATOR, [__DIR__, 'Fixtures', 'path', '*']);
        $repository = $this->createPathRepo(['url' => $repositoryUrl, 'options' => $options]);
        $packages = $repository->getPackages();

        self::assertGreaterThanOrEqual(2, $repository->count());

        foreach ($packages as $package) {
            self::assertEquals(
                $package->getDistReference(),
                hash('sha1', file_get_contents($package->getDistUrl() . '/composer.json') . serialize($options))
            );
        }
    }

    /**
     * @param array<mixed> $options
     */
    private function createPathRepo(array $options): PathRepository
    {
        $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();

        $config = new \Composer\Config();
        $proc = new ProcessExecutor();
        $loop = new Loop(new HttpDownloader($io, $config), $proc);

        return new PathRepository($options, $io, $config, null, null, $proc);
    }
}