Skip to content

Commit 3305810

Browse files
authored
[ComposerProcessor] Skip change under "suggest" config in composer.json for RaiseToInstalledComposerProcessor (#27)
* [ComposerProcessor] Skip change under "suggest" config in composer.json for RaiseToInstalledComposerProcessor * more fixtures * space * Fix * Fix phpstan
1 parent 298cb9d commit 3305810

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed

src/FileSystem/ComposerJsonPackageVersionUpdater.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,24 @@ final class ComposerJsonPackageVersionUpdater
1111
public static function update(string $composerJsonContents, string $packageName, string $newVersion): string
1212
{
1313
// replace using regex, to keep original composer.json format
14-
return Strings::replace(
14+
$allChanges = Strings::replace(
1515
$composerJsonContents,
1616
// find
1717
sprintf('#"%s": "(.*?)"#', $packageName),
1818
// replace
1919
sprintf('"%s": "%s"', $packageName, $newVersion)
2020
);
21+
22+
$suggestContent = Strings::match($composerJsonContents, '#"suggest"\s*:\s*{[^}]*}#');
23+
24+
if ($suggestContent !== null) {
25+
$allChanges = Strings::replace(
26+
$allChanges,
27+
'#"suggest"\s*:\s*{[^}]*}#',
28+
$suggestContent[0]
29+
);
30+
}
31+
32+
return $allChanges;
2133
}
2234
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"suggest": {
3+
"illuminate/container": "to use container"
4+
},
5+
"require-dev": {
6+
"illuminate/container": "^9.0"
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"require-dev": {
3+
"illuminate/container": "^9.0"
4+
},
5+
"suggest": {
6+
"illuminate/container": "to use container"
7+
}
8+
}

tests/ComposerProcessor/RaiseToInstalledComposerProcessor/RaiseToInstalledComposerProcessorTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Rector\Jack\Tests\ComposerProcessor\RaiseToInstalledComposerProcessor;
66

77
use Nette\Utils\FileSystem;
8+
use PHPUnit\Framework\Attributes\DataProvider;
89
use Rector\Jack\ComposerProcessor\RaiseToInstalledComposerProcessor;
910
use Rector\Jack\Tests\AbstractTestCase;
1011
use Rector\Jack\ValueObject\ChangedPackageVersion;
@@ -49,6 +50,58 @@ public function testSkipDev(): void
4950
$this->assertEmpty($changedPackageVersionsResult->getChangedPackageVersions());
5051
}
5152

53+
/**
54+
* @return iterable<array{string, string}>
55+
*/
56+
public static function provideSkipSuggestChangeFiles(): iterable
57+
{
58+
yield [
59+
__DIR__ . '/Fixture/skip-suggest.json',
60+
<<<'JSON'
61+
{
62+
"require-dev": {
63+
"illuminate/container": "^12.19"
64+
},
65+
"suggest": {
66+
"illuminate/container": "to use container"
67+
}
68+
}
69+
70+
JSON
71+
];
72+
73+
yield [
74+
__DIR__ . '/Fixture/skip-suggest-early-definition.json',
75+
<<<'JSON'
76+
{
77+
"suggest": {
78+
"illuminate/container": "to use container"
79+
},
80+
"require-dev": {
81+
"illuminate/container": "^12.19"
82+
}
83+
}
84+
85+
JSON
86+
];
87+
}
88+
89+
#[DataProvider('provideSkipSuggestChangeFiles')]
90+
public function testSkipSuggestChange(string $file, string $changedFileContent): void
91+
{
92+
$composerJsonContents = FileSystem::read($file);
93+
94+
$changedPackageVersionsResult = $this->raiseToInstalledComposerProcessor->process($composerJsonContents);
95+
96+
$changedPackageVersion = $changedPackageVersionsResult->getChangedPackageVersions()[0];
97+
98+
$this->assertSame('illuminate/container', $changedPackageVersion->getPackageName());
99+
$this->assertSame('^9.0', $changedPackageVersion->getOldVersion());
100+
$this->assertSame('^12.19', $changedPackageVersion->getNewVersion());
101+
102+
$this->assertSame($changedFileContent, $changedPackageVersionsResult->getComposerJsonContents());
103+
}
104+
52105
public function testSinglePiped(): void
53106
{
54107
$composerJsonContents = FileSystem::read(__DIR__ . '/Fixture/single-piped.json');

0 commit comments

Comments
 (0)