function ComposerInspectorTest::testGetInstalledPackagesList

@covers ::getInstalledPackagesList

File

core/modules/package_manager/tests/src/Kernel/ComposerInspectorTest.php, line 104

Class

ComposerInspectorTest
@coversDefaultClass \Drupal\package_manager\ComposerInspector

Namespace

Drupal\Tests\package_manager\Kernel

Code

public function testGetInstalledPackagesList() : void {
    $project_root = $this->container
        ->get(PathLocator::class)
        ->getProjectRoot();
    
    /** @var \Drupal\package_manager\ComposerInspector $inspector */
    $inspector = $this->container
        ->get(ComposerInspector::class);
    $list = $inspector->getInstalledPackagesList($project_root);
    $expected_list = new InstalledPackagesList([
        'drupal/core' => InstalledPackage::createFromArray([
            'name' => 'drupal/core',
            'type' => 'drupal-core',
            'version' => '9.8.0',
            'path' => "{$project_root}/vendor/drupal/core",
        ]),
        'drupal/core-recommended' => InstalledPackage::createFromArray([
            'name' => 'drupal/core-recommended',
            'type' => 'project',
            'version' => '9.8.0',
            'path' => "{$project_root}/vendor/drupal/core-recommended",
        ]),
        'drupal/core-dev' => InstalledPackage::createFromArray([
            'name' => 'drupal/core-dev',
            'type' => 'package',
            'version' => '9.8.0',
            'path' => "{$project_root}/vendor/drupal/core-dev",
        ]),
    ]);
    $this->assertPackageListsEqual($expected_list, $list);
    // Since the lock file hasn't changed, we should get the same package list
    // back if we call getInstalledPackageList() again.
    $this->assertSame($list, $inspector->getInstalledPackagesList($project_root));
    // If we change the lock file, we should get a different package list.
    $lock_file = new JsonFile($project_root . '/composer.lock');
    $lock_data = $lock_file->read();
    $this->assertArrayHasKey('_readme', $lock_data);
    unset($lock_data['_readme']);
    $lock_file->write($lock_data);
    $this->assertNotSame($list, $inspector->getInstalledPackagesList($project_root));
    // If composer.lock is removed, we should get an exception because
    // getInstalledPackagesList() should validate that $project_root is
    // Composer-ready.
    unlink($lock_file->getPath());
    $this->expectExceptionMessage("composer.lock not found in {$project_root}.");
    $inspector->getInstalledPackagesList($project_root);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.