|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace PackageVersionsTest; |
|
6
|
|
|
|
|
7
|
|
|
use OutOfBoundsException; |
|
8
|
|
|
use PackageVersions\FallbackVersions; |
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
|
10
|
|
|
use UnexpectedValueException; |
|
11
|
|
|
use function array_merge; |
|
12
|
|
|
use function file_exists; |
|
13
|
|
|
use function file_get_contents; |
|
14
|
|
|
use function json_decode; |
|
15
|
|
|
use function rename; |
|
16
|
|
|
use function uniqid; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @covers \PackageVersions\FallbackVersions |
|
20
|
|
|
*/ |
|
21
|
|
|
final class FallbackVersionsTest extends TestCase |
|
22
|
|
|
{ |
|
23
|
|
|
public function testWillFailWithoutValidPackageData() : void |
|
24
|
|
|
{ |
|
25
|
|
|
$this->backupFile(__DIR__ . '/../../vendor/composer/installed.json'); |
|
26
|
|
|
$this->backupFile(__DIR__ . '/../../composer.lock'); |
|
27
|
|
|
|
|
28
|
|
|
$this->expectException(UnexpectedValueException::class); |
|
29
|
|
|
$this->expectExceptionMessageRegExp( |
|
30
|
|
|
'@PackageVersions could not locate the `vendor/composer/installed\.json` or your `composer\.lock` ' |
|
31
|
|
|
. 'location\. This is assumed to be in \[[^]]+?\]\. If you customized your composer vendor directory and ran composer ' |
|
32
|
|
|
. 'installation with --no-scripts or if you deployed without the required composer files, then you are on ' |
|
33
|
|
|
. 'your own, and we can\'t really help you\. Fix your shit and cut the tooling some slack\.@' |
|
34
|
|
|
); |
|
35
|
|
|
|
|
36
|
|
|
FallbackVersions::getVersion('phpunit/phpunit'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function testValidVersions() : void |
|
40
|
|
|
{ |
|
41
|
|
|
$lockData = json_decode(file_get_contents(__DIR__ . '/../../composer.lock'), true); |
|
42
|
|
|
|
|
43
|
|
|
$packages = array_merge($lockData['packages'], $lockData['packages-dev']); |
|
44
|
|
|
|
|
45
|
|
|
self::assertNotEmpty($packages); |
|
46
|
|
|
|
|
47
|
|
|
foreach ($packages as $package) { |
|
48
|
|
|
self::assertSame( |
|
49
|
|
|
$package['version'] . '@' . $package['source']['reference'], |
|
50
|
|
|
FallbackVersions::getVersion($package['name']) |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function testValidVersionsWithoutComposerLock() : void |
|
56
|
|
|
{ |
|
57
|
|
|
$lockData = json_decode(file_get_contents(__DIR__ . '/../../composer.lock'), true); |
|
58
|
|
|
|
|
59
|
|
|
$packages = array_merge($lockData['packages'], $lockData['packages-dev'] ?? []); |
|
60
|
|
|
|
|
61
|
|
|
self::assertNotEmpty($packages); |
|
62
|
|
|
|
|
63
|
|
|
$this->backupFile(__DIR__ . '/../../composer.lock'); |
|
64
|
|
|
foreach ($packages as $package) { |
|
65
|
|
|
self::assertSame( |
|
66
|
|
|
$package['version'] . '@' . $package['source']['reference'], |
|
67
|
|
|
FallbackVersions::getVersion($package['name']) |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function testValidVersionsWithoutInstalledJson() : void |
|
73
|
|
|
{ |
|
74
|
|
|
if (($packages = json_decode(file_get_contents(__DIR__ . '/../../vendor/composer/installed.json'), true)) === []) { |
|
75
|
|
|
// In case of --no-dev flag |
|
76
|
|
|
$packages = json_decode(file_get_contents(getcwd() . '/composer.lock'), true); |
|
77
|
|
|
$packages = array_merge($packages['packages'], $packages['packages-dev'] ?? []); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
self::assertNotEmpty($packages); |
|
81
|
|
|
|
|
82
|
|
|
$this->backupFile(__DIR__ . '/../../vendor/composer/installed.json'); |
|
83
|
|
|
foreach ($packages as $package) { |
|
84
|
|
|
self::assertSame( |
|
85
|
|
|
$package['version'] . '@' . $package['source']['reference'], |
|
86
|
|
|
FallbackVersions::getVersion($package['name']) |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function testInvalidVersionsAreRejected() : void |
|
92
|
|
|
{ |
|
93
|
|
|
$this->expectException(OutOfBoundsException::class); |
|
94
|
|
|
|
|
95
|
|
|
FallbackVersions::getVersion(uniqid('', true) . '/' . uniqid('', true)); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
protected function tearDown() : void |
|
99
|
|
|
{ |
|
100
|
|
|
$this->revertFile(__DIR__ . '/../../composer.lock'); |
|
101
|
|
|
$this->revertFile(__DIR__ . '/../../vendor/composer/installed.json'); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
private function backupFile(string $filename) : void |
|
105
|
|
|
{ |
|
106
|
|
|
rename($filename, $filename . '.backup'); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
private function revertFile(string $filename) : void |
|
110
|
|
|
{ |
|
111
|
|
|
if (! file_exists($filename . '.backup')) { |
|
112
|
|
|
return; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
rename($filename . '.backup', $filename); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|