44
55namespace Rector \Tests \Composer ;
66
7+ use PHPUnit \Framework \Attributes \DataProvider ;
78use PHPUnit \Framework \TestCase ;
89use Rector \Composer \InstalledPackageResolver ;
910use Rector \Composer \ValueObject \InstalledPackage ;
@@ -47,11 +48,10 @@ public function testComposerJsonHasPriorityOverOutdatedInstalledJson(): void
4748 $ this ->assertSame ('1.11.0.0 ' , $ installedPackageResolver ->resolvePackageVersion ('webmozart/assert ' ));
4849 }
4950
50- public function testLibraryTargetsLowestDeclaredVersionEvenWhenInstalledSatisfies (): void
51+ #[DataProvider('provideLibraryTypeFixtureDirectory ' )]
52+ public function testLibraryTargetsLowestDeclaredVersionEvenWhenInstalledSatisfies (string $ fixtureDirectory ): void
5153 {
52- $ installedPackageResolver = new InstalledPackageResolver (
53- __DIR__ . '/Fixture/InstalledPackageResolver/library_composer_json '
54- );
54+ $ installedPackageResolver = new InstalledPackageResolver ($ fixtureDirectory );
5555
5656 // installed 12.1.0 satisfies "^10.5 || ^11.0 || ^12.0", yet a library targets the lowest declared version
5757 $ this ->assertSame ('10.5.0.0 ' , $ installedPackageResolver ->resolvePackageVersion ('phpunit/phpunit ' ));
@@ -66,6 +66,31 @@ public function testLibraryTargetsLowestDeclaredVersionEvenWhenInstalledSatisfie
6666 $ this ->assertSame ('1.11.0.0 ' , $ installedPackageResolver ->resolvePackageVersion ('webmozart/assert ' ));
6767 }
6868
69+ /**
70+ * @return iterable<string, array{string}>
71+ */
72+ public static function provideLibraryTypeFixtureDirectory (): iterable
73+ {
74+ // "library" is the default distributed type
75+ yield 'library ' => [__DIR__ . '/Fixture/InstalledPackageResolver/library_composer_json ' ];
76+
77+ // any other non-"project" distributed type behaves the same way
78+ yield 'symfony-bundle ' => [__DIR__ . '/Fixture/InstalledPackageResolver/symfony_bundle_composer_json ' ];
79+ }
80+
81+ public function testProjectTypeKeepsInstalledVersionOverDeclaredRange (): void
82+ {
83+ $ installedPackageResolver = new InstalledPackageResolver (
84+ __DIR__ . '/Fixture/InstalledPackageResolver/project_composer_json '
85+ );
86+
87+ // an explicit "project" is an application, so the installed version stands even within a wider range
88+ $ this ->assertSame ('12.1.0.0 ' , $ installedPackageResolver ->resolvePackageVersion ('phpunit/phpunit ' ));
89+
90+ // installed 7.2.0 satisfies "^7.0", kept as installed
91+ $ this ->assertSame ('7.2.0.0 ' , $ installedPackageResolver ->resolvePackageVersion ('symfony/console ' ));
92+ }
93+
6994 public function testStandaloneComposerJsonResolvesVersionsWithoutVendor (): void
7095 {
7196 $ installedPackageResolver = new InstalledPackageResolver (
0 commit comments