add debug logger to default Guzzle client

This commit is contained in:
Daniel Seifert 2023-01-17 15:37:53 +01:00 committed by Daniel Seifert
parent 6c5a83a8a8
commit 186bb2d622
Signed by: DanielS
GPG Key ID: 6A513E13AEE66170
4 changed files with 54 additions and 10 deletions

View File

@ -100,6 +100,28 @@ class ClientTest extends ApiTestCase
];
}
/**
* @test
* @throws ReflectionException
* @return void
* @covers \D3\LinkmobilityClient\Client::getDefaultClient
*/
public function testGetDefaultClient()
{
/** @var Client|MockObject $sut */
$sut = $this->getMockBuilder(Client::class)
->setConstructorArgs(['accessTokenFixture'])
->getMock();
$this->assertInstanceOf(
GuzzleClient::class,
$this->callMethod(
$sut,
'getDefaultClient'
)
);
}
/**
* @test
* @return void
@ -179,7 +201,7 @@ class ClientTest extends ApiTestCase
* @dataProvider rawRequestDataProvider
* @covers \D3\LinkmobilityClient\Client::rawRequest
*/
public function testRawRequest($okStatus)
public function testRawRequest($okStatus, $logInvocationCount)
{
$statusCode = $okStatus ? '200' : '301';
@ -235,7 +257,7 @@ class ClientTest extends ApiTestCase
'getLoggerHandler',
])
->getMock();
$clientMock->expects($this->atLeastOnce())
$clientMock->expects($logInvocationCount)
->method('getLoggerHandler')->willReturn($loggerHandlerMock);
$this->setValue($clientMock, 'requestClient', $requestClientMock);
@ -254,8 +276,8 @@ class ClientTest extends ApiTestCase
public function rawRequestDataProvider(): array
{
return [
'OK status' => [true],
'NOK status' => [false],
'OK status' => [true, $this->never()],
'NOK status' => [false, $this->once()],
];
}

View File

@ -7,5 +7,5 @@ composer create-project -s dev --prefer-source [--repository '{"type": "vcs", "u
# Run tests
```
./vendor/bin/phpunit [--no-coverage]
./vendor/bin/phpunit [--no-coverage] [--coverage-html=cov]
```

View File

@ -20,8 +20,12 @@ use D3\LinkmobilityClient\Exceptions\ExceptionMessages;
use D3\LinkmobilityClient\Request\RequestInterface;
use D3\LinkmobilityClient\Url\Url;
use D3\LinkmobilityClient\Url\UrlInterface;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\MessageFormatter;
use GuzzleHttp\Middleware;
use InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
@ -35,7 +39,26 @@ class Client
{
$this->accessToken = $accessToken;
$this->apiUrl = $apiUrl ?: new Url();
$this->requestClient = $client ?: new \GuzzleHttp\Client([ 'base_uri' => $this->apiUrl->getBaseUri() ]);
$this->requestClient = $client ?: $this->getDefaultClient();
}
/**
* @return GuzzleClient
*/
protected function getDefaultClient(): GuzzleClient
{
$logger = Middleware::log(
$this->getLoggerHandler()->getLogger(),
new MessageFormatter(MessageFormatter::DEBUG),
'debug'
);
$handlerStack = HandlerStack::create();
$handlerStack->push($logger);
return new GuzzleClient( [
'base_uri' => $this->apiUrl->getBaseUri(),
'handler' => $handlerStack
]);
}
/**
@ -68,8 +91,6 @@ class Client
{
$options['headers']['Authorization'] = 'Bearer '.$this->accessToken;
$this->getLoggerHandler()->getLogger()->debug('linkmobility request: '.$url, $options);
$response = $this->requestClient->request(
$method,
$url,
@ -79,12 +100,11 @@ class Client
if ($response->getStatusCode() != 200) {
$message = sprintf(ExceptionMessages::NOK_REQUEST_RETURN, $url, $response->getStatusCode());
$response->getBody()->rewind();
$this->getLoggerHandler()->getLogger()->error($message, [$response->getBody()->getContents()]);
$this->getLoggerHandler()->getLogger()->error('linkmobility error: '.$message, [$response->getBody()->getContents()]);
throw new ApiException($message);
}
$response->getBody()->rewind();
$this->getLoggerHandler()->getLogger()->debug('response', [$response->getBody()->getContents()]);
return $response;
}

View File

@ -24,6 +24,7 @@ use D3\LinkmobilityClient\Response\ResponseInterface as LMResponseInterface;
interface RequestInterface
{
// @codeCoverageIgnoreStart
public const METHOD_GET = 'GET';
public const METHOD_POST = 'POST';
public const METHOD_PUT = 'PUT';
@ -42,6 +43,7 @@ interface RequestInterface
public const SENDERADDRESSTYPE_INTERNATIONAL = 'international';
public const SENDERADDRESSTYPE_ALPHANUMERIC = 'alphanumeric';
public const SENDERADDRESSTYPE_SHORTCODE = 'shortcode';
// @codeCoverageIgnoreEnd
/**
* @param SmsMessageInterface $message