Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions lib/DTO/PhpConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,25 @@ public function __construct(
public ?array $composerLockContent = null,
) {
}

/**
* This only parses prod dependencies from composer.lock.
*
* @return array<string, string>
*/
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;
}
}
44 changes: 44 additions & 0 deletions tests/Unit/DetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down
Loading