function UnpackRecipeTest::testUnpackCommand

Tests dependency unpacking using drupal:recipe-unpack.

File

core/tests/Drupal/Tests/Composer/Plugin/Unpack/Functional/UnpackRecipeTest.php, line 171

Class

UnpackRecipeTest
Tests recipe unpacking.

Namespace

Drupal\Tests\Composer\Plugin\Unpack\Functional

Code

public function testUnpackCommand() : void {
    $root_project_path = $this->fixturesDir . '/composer-root';
    // Run composer install and confirm the composer.lock was created.
    $this->runComposer('install');
    // Disable automatic unpacking as it is the default behavior,
    $this->runComposer('config --merge --json extra.drupal-recipe-unpack.on-require false');
    // Install a module in require-dev.
    $this->runComposer('require --dev fixtures/module-a');
    // Install a module in require.
    $this->runComposer('require fixtures/module-b:*');
    // Ensure we have added the dependencies.
    $root_composer_json = $this->getFileContents($root_project_path . '/composer.json');
    $this->assertArrayHasKey('fixtures/module-b', $root_composer_json['require']);
    $this->assertArrayHasKey('fixtures/module-a', $root_composer_json['require-dev']);
    // Install a recipe and check it is not unpacked.
    $stdout = $this->runComposer('require fixtures/recipe-a');
    $root_composer_json = $this->getFileContents($root_project_path . '/composer.json');
    // When the package is unpacked, the unpacked dependencies should be logged
    // in the stdout.
    $this->assertStringNotContainsString("unpacked.", $stdout);
    $this->assertArrayHasKey('fixtures/recipe-a', $root_composer_json['require']);
    // Ensure the resulting Composer files are valid.
    $this->runComposer('validate');
    // The package dependencies should not be in the root composer.json.
    $this->assertArrayNotHasKey('fixtures/recipe-b', $root_composer_json['require']);
    // Try unpacking a recipe that in not in the root composer.json.
    try {
        $this->runComposer('drupal:recipe-unpack fixtures/recipe-b');
        $this->fail('Unpacking a non-existent dependency should fail');
    } catch (\RuntimeException $e) {
        $this->assertStringContainsString('fixtures/recipe-b not found in the root composer.json.', $e->getMessage());
    }
    // The dev dependency has not moved.
    $this->assertArrayHasKey('fixtures/module-a', $root_composer_json['require-dev']);
    $stdout = $this->runComposer('drupal:recipe-unpack fixtures/recipe-a');
    $this->doTestRecipeAUnpacked($root_project_path, $stdout);
    $root_composer_json = $this->getFileContents($root_project_path . '/composer.json');
    // The more specific constraints has been used.
    $this->assertSame("^2.0", $root_composer_json['require']['fixtures/module-b']);
    // Try unpacking something that is not a recipe.
    try {
        $this->runComposer('drupal:recipe-unpack fixtures/module-a');
        $this->fail('Unpacking a module should fail');
    } catch (\RuntimeException $e) {
        $this->assertStringContainsString('fixtures/module-a is not a recipe.', $e->getMessage());
    }
    // Try unpacking something that in not in the root composer.json.
    try {
        $this->runComposer('drupal:recipe-unpack fixtures/module-c');
        $this->fail('Unpacking a non-existent dependency should fail');
    } catch (\RuntimeException $e) {
        $this->assertStringContainsString('fixtures/module-c not found in the root composer.json.', $e->getMessage());
    }
}

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