function ComposerInspectorTest::testVersionCheck

@covers ::validate

@testWith ["2.2.12", "<default>"] ["2.2.13", "<default>"] ["2.5.0", "<default>"] ["2.5.5", "<default>"] ["2.5.11", "<default>"] ["2.6.0", null] ["2.2.11", "<default>"] ["2.2.0-dev", "<default>"] ["2.3.6", "<default>"] ["2.4.1", "<default>"] ["2.3.4", "<default>"] ["2.1.6", "<default>"] ["1.10.22", "<default>"] ["1.7.3", "<default>"] ["2.0.0-alpha3", "<default>"] ["2.1.0-RC1", "<default>"] ["1.0.0-RC", "<default>"] ["1.0.0-beta1", "<default>"] ["1.9-dev", "<default>"] ["@package_version@", "Invalid version string \"@package_version@\""] [null, "Unable to determine Composer version"]

Parameters

string|null $reported_version: The version of Composer that will be returned by ::getVersion().

string|null $expected_message: The error message that should be generated for the reported version of Composer. If not passed, will default to the message format defined in ::validate().

File

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

Class

ComposerInspectorTest
@coversDefaultClass \Drupal\package_manager\ComposerInspector

Namespace

Drupal\Tests\package_manager\Kernel

Code

public function testVersionCheck(?string $reported_version, ?string $expected_message) : void {
    $runner = $this->mockComposerRunner($reported_version);
    // Mock the ComposerIsAvailableInterface so that if it uses the Composer
    // runner it will not affect the test expectations.
    $composerPrecondition = $this->prophesize(ComposerIsAvailableInterface::class);
    $composerPrecondition->assertIsFulfilled(Argument::cetera())
        ->shouldBeCalledOnce();
    $this->container
        ->set(ComposerIsAvailableInterface::class, $composerPrecondition->reveal());
    // The result of the version check is statically cached, so the runner
    // should only be called once, even though we call validate() twice in this
    // test.
    $runner->getMethodProphecies('run')[0]
        ->withArguments([
        [
            '--format=json',
        ],
        NULL,
        [],
        Argument::any(),
    ])
        ->shouldBeCalledOnce();
    // The runner should be called with `validate` as the first argument, but
    // it won't affect the outcome of this test.
    $runner->run(Argument::withEntry(0, 'validate'));
    $this->container
        ->set(ComposerProcessRunnerInterface::class, $runner->reveal());
    if ($expected_message === '<default>') {
        $expected_message = "The detected Composer version, {$reported_version}, does not satisfy <code>" . ComposerInspector::SUPPORTED_VERSION . '</code>.';
    }
    $project_root = $this->container
        ->get(PathLocator::class)
        ->getProjectRoot();
    
    /** @var \Drupal\package_manager\ComposerInspector $inspector */
    $inspector = $this->container
        ->get(ComposerInspector::class);
    try {
        $inspector->validate($project_root);
        // If we expected the version check to succeed, ensure we did not expect
        // an exception message.
        $this->assertNull($expected_message, 'Expected an exception, but none was thrown.');
    } catch (ComposerNotReadyException $e) {
        $this->assertNull($e->workingDir);
        $this->assertSame($expected_message, $e->getMessage());
    }
    if (isset($expected_message)) {
        $this->expectException(ComposerNotReadyException::class);
        $this->expectExceptionMessage($expected_message);
    }
    $inspector->validate($project_root);
}

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