Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't test throwing an exception. Help needed #392

Closed
bu4ak opened this issue Apr 13, 2021 · 0 comments
Closed

Can't test throwing an exception. Help needed #392

bu4ak opened this issue Apr 13, 2021 · 0 comments

Comments

@bu4ak
Copy link

bu4ak commented Apr 13, 2021

PHP version: 8.0.2

Description
After adding 'httplug.factory.mock', the HttpMethodsClient is returned from the container.

Additional context

services.yaml

Http\Client\HttpClient $PDAHttpClient: '@httplug.client.pda'

test/httplug.yaml

httplug:
  clients:
    pda:
      factory: 'httplug.factory.mock'
      plugins:
        - vcr:
            mode: replay

PDAService.php

...
    public function __construct(
        HttpClient $PDAHttpClient
    ) {
         $this->httpClient = $PDAHttpClient;
    }
...

    public function executeRequest(RequestInterface $request): string
    {
            ....
            $response = $this->httpClient->sendRequest($request);
            ...
    }

ClientTest.php

...
$exception = new NetworkException('Could not resolve host', $this->createMock(Request::class));
$this->getContainer()->get('httplug.client.mock')->addException($exception);

$this->getContainer()->get(PDAService::class)->executeRequest(...);
...

Inside the PDAService constructor comes Http\Client\Common\HttpMethodsClient instead of Http\Mock\Client.
And instead of throwing an exception, it tries to find for a vcr fixture.

Other tests that work with vcr are performed correctly.

How can I test a case where an http client should throws a transport error?

@bu4ak bu4ak closed this as completed Apr 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant