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