function FixtureManipulatorTest::testAddPackage

@covers ::addPackage

File

core/modules/package_manager/tests/src/Kernel/FixtureManipulatorTest.php, line 83

Class

FixtureManipulatorTest
@coversDefaultClass \Drupal\fixture_manipulator\FixtureManipulator

Namespace

Drupal\Tests\package_manager\Kernel

Code

public function testAddPackage() : void {
    // Packages cannot be added without a name.
    foreach ([
        'name',
        'type',
    ] as $require_key) {
        // Make a package that is missing the required key.
        $package = array_diff_key([
            'name' => 'Any old name',
            'type' => 'Any old type',
        ], [
            $require_key => '',
        ]);
        try {
            $manipulator = new ActiveFixtureManipulator();
            $manipulator->addPackage($package)
                ->commitChanges();
            $this->fail("Adding a package without the '{$require_key}' should raise an error.");
        } catch (\UnexpectedValueException $e) {
            $this->assertSame("The '{$require_key}' is required when calling ::addPackage().", $e->getMessage());
        }
    }
    // We should get a helpful error if the name is not a valid package name.
    try {
        $manipulator = new ActiveFixtureManipulator();
        $manipulator->addPackage([
            'name' => 'my_drupal_module',
            'type' => 'drupal-module',
        ])
            ->commitChanges();
        $this->fail('Trying to add a package with an invalid name should raise an error.');
    } catch (\UnexpectedValueException $e) {
        $this->assertSame("'my_drupal_module' is not a valid package name.", $e->getMessage());
    }
    // We should not be able to add an existing package.
    try {
        $manipulator = new ActiveFixtureManipulator();
        $manipulator->addPackage([
            'name' => 'my/package',
            'type' => 'library',
        ])
            ->commitChanges();
        $this->fail('Trying to add an existing package should raise an error.');
    } catch (\LogicException $e) {
        $this->assertStringContainsString("Expected package 'my/package' to not be installed, but it was.", $e->getMessage());
    }
    // Ensure that none of the failed calls to ::addPackage() changed the installed
    // packages.
    $this->assertPackageListsEqual($this->originalFixturePackages, $this->inspector
        ->getInstalledPackagesList($this->dir));
    $root_info = $this->inspector
        ->getRootPackageInfo($this->dir);
    $this->assertSame([
        'drupal/core-dev',
        'my/dev-package',
    ], array_keys($root_info['devRequires']));
}

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