diff --git a/Tests/ClientTest.php b/Tests/ClientTest.php index d41a36d..20f093d 100644 --- a/Tests/ClientTest.php +++ b/Tests/ClientTest.php @@ -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()], ]; } diff --git a/Tests/README.md b/Tests/README.md index 24ffa53..89bcd93 100644 --- a/Tests/README.md +++ b/Tests/README.md @@ -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] ``` diff --git a/src/Client.php b/src/Client.php index af39b9d..b05aaa7 100644 --- a/src/Client.php +++ b/src/Client.php @@ -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; } diff --git a/src/Request/RequestInterface.php b/src/Request/RequestInterface.php index 129aa6e..90185f2 100644 --- a/src/Request/RequestInterface.php +++ b/src/Request/RequestInterface.php @@ -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