Problem/Motivation

In unit tests that mock the guzzle client interface, the SecurityAdvisoriesFetcher throws an error like

TypeError: Argument 3 passed to Drupal\system\SecurityAdvisories\SecurityAdvisoriesFetcher::__construct() must be an instance of GuzzleHttp\Client, instance of Mock_ClientInterface_f0228a1b given

Steps to reproduce

Create a Kernel test that sets the http_client service to a mocked object using a GuzzleHttp\ClientInterface::class. For example,

    $guzzle = $this->createMock(ClientInterface::class);
    $guzzle->method('request')->will($this->returnCallback([
      $this,
      'guzzleCallback',
    ]));
    \Drupal::getContainer()->set('http_client', $guzzle);
    Role::create(['label' => "Foo", 'id' => "foo"])->save();

Proposed resolution

Simply replace the constructor type hint to use ClientInterface instead of Client

Remaining tasks

Replace the Type Hint

User interface changes

None

API changes

None

Data model changes

None

Release notes snippet

Issue fork drupal-3221171

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

pookmish created an issue. See original summary.

pookmish’s picture

Status: Active » Needs review
longwave’s picture

Status: Needs review » Reviewed & tested by the community

We always use interfaces where possible, this is just an oversight in the original patch.

alexpott’s picture

Version: 9.2.x-dev » 9.3.x-dev
Status: Reviewed & tested by the community » Closed (duplicate)

I merged this as part of #3222168: Replace \GuzzleHttp\Client with \GuzzleHttp\ClientInterface. These shouldn't have been separate issues. It's the same problem - a constructor not using an available interface. I credited @pookmish on that issue.