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
|
|
|
// This test will always fail if there is nothing in the installed.json |
58
|
|
|
// due to this package being installed with `composer install --no-dev`. |
59
|
|
|
if (json_decode(file_get_contents(getcwd() . '/vendor/composer/installed.json', true)) === []) { |
60
|
|
|
$this->markTestSkipped('Empty installed.json (possible --no-dev)'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$lockData = json_decode(file_get_contents(__DIR__ . '/../../composer.lock'), true); |
64
|
|
|
|
65
|
|
|
$packages = array_merge($lockData['packages'], $lockData['packages-dev'] ?? []); |
66
|
|
|
|
67
|
|
|
self::assertNotEmpty($packages); |
68
|
|
|
|
69
|
|
|
$this->backupFile(__DIR__ . '/../../composer.lock'); |
70
|
|
|
foreach ($packages as $package) { |
71
|
|
|
self::assertSame( |
72
|
|
|
$package['version'] . '@' . $package['source']['reference'], |
73
|
|
|
FallbackVersions::getVersion($package['name']) |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testValidVersionsWithoutInstalledJson() : void |
79
|
|
|
{ |
80
|
|
|
if (($packages = json_decode(file_get_contents(__DIR__ . '/../../vendor/composer/installed.json'), true)) === []) { |
81
|
|
|
// In case of --no-dev flag |
82
|
|
|
$packages = json_decode(file_get_contents(getcwd() . '/composer.lock'), true); |
83
|
|
|
$packages = array_merge($packages['packages'], $packages['packages-dev'] ?? []); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
self::assertNotEmpty($packages); |
87
|
|
|
|
88
|
|
|
$this->backupFile(__DIR__ . '/../../vendor/composer/installed.json'); |
89
|
|
|
foreach ($packages as $package) { |
90
|
|
|
self::assertSame( |
91
|
|
|
$package['version'] . '@' . $package['source']['reference'], |
92
|
|
|
FallbackVersions::getVersion($package['name']) |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testInvalidVersionsAreRejected() : void |
98
|
|
|
{ |
99
|
|
|
$this->expectException(OutOfBoundsException::class); |
100
|
|
|
|
101
|
|
|
FallbackVersions::getVersion(uniqid('', true) . '/' . uniqid('', true)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
protected function tearDown() : void |
105
|
|
|
{ |
106
|
|
|
$this->revertFile(__DIR__ . '/../../composer.lock'); |
107
|
|
|
$this->revertFile(__DIR__ . '/../../vendor/composer/installed.json'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
private function backupFile(string $filename) : void |
111
|
|
|
{ |
112
|
|
|
rename($filename, $filename . '.backup'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private function revertFile(string $filename) : void |
116
|
|
|
{ |
117
|
|
|
if (! file_exists($filename . '.backup')) { |
118
|
|
|
return; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
rename($filename . '.backup', $filename); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|