diff --git a/lib/DTO/PhpConfiguration.php b/lib/DTO/PhpConfiguration.php index 3ebe9ee..0ec8c52 100644 --- a/lib/DTO/PhpConfiguration.php +++ b/lib/DTO/PhpConfiguration.php @@ -16,4 +16,25 @@ public function __construct( public ?array $composerLockContent = null, ) { } + + /** + * This only parses prod dependencies from composer.lock. + * + * @return array + */ + public function getExactInstalledDependencies(): array + { + if (null === $this->composerLockContent || !isset($this->composerLockContent['packages'])) { + return []; + } + + $dependencies = []; + foreach ($this->composerLockContent['packages'] as $package) { + if (isset($package['name'], $package['version'])) { + $dependencies[$package['name']] = $package['version']; + } + } + + return $dependencies; + } } diff --git a/tests/Unit/DetectorTest.php b/tests/Unit/DetectorTest.php index 2cb619d..f2b51c2 100644 --- a/tests/Unit/DetectorTest.php +++ b/tests/Unit/DetectorTest.php @@ -451,6 +451,50 @@ public function it_returns_composer_lock_content_if_both_composer_config_files_a ); } + /** @test */ + public function it_returns_no_exact_installed_dependencies_if_no_composer_lock_is_found() + { + $fullConfig = $this->sut->getFullConfiguration( + sprintf('%s/../fixtures/%s', __DIR__, 'composer-config/composer-json') + ); + + $this->assertNotNull($fullConfig); + $this->assertNotNull($fullConfig->phpConfiguration); + $this->assertIsArray( + $fullConfig->phpConfiguration->getExactInstalledDependencies() + ); + $this->assertCount( + 0, + $fullConfig->phpConfiguration->getExactInstalledDependencies() + ); + } + + /** @test */ + public function it_returns_exact_installed_dependencies_if_composer_lock_is_found() + { + $fullConfig = $this->sut->getFullConfiguration( + sprintf('%s/../fixtures/%s', __DIR__, 'composer-config/composer-both') + ); + + $this->assertNotNull($fullConfig); + $this->assertNotNull($fullConfig->phpConfiguration); + $this->assertIsArray( + $fullConfig->phpConfiguration->getExactInstalledDependencies() + ); + $this->assertCount( + 3, + $fullConfig->phpConfiguration->getExactInstalledDependencies() + ); + $this->assertSame( + [ + 'someframework/foo' => 'v2.4.1', + 'symfony/config' => 'v6.3.2', + 'symfony/framework-bundle' => 'v6.3.5', + ], + $fullConfig->phpConfiguration->getExactInstalledDependencies() + ); + } + public static function packagesDataProvider(): array { return [