add tests for client and url class

This commit is contained in:
2022-07-10 21:52:37 +02:00
parent 740b14f868
commit 3a3a940a01
7 changed files with 339 additions and 54 deletions

View File

@ -20,13 +20,11 @@ namespace D3\LinkmobilityClient;
use D3\LinkmobilityClient\Exceptions\ApiException;
use D3\LinkmobilityClient\Exceptions\ExceptionMessages;
use D3\LinkmobilityClient\Request\RequestInterface;
use D3\LinkmobilityClient\ValueObject\ValueObject;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;
use InvalidArgumentException;
use phpDocumentor\Reflection\Types\Mixed_;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use RuntimeException;
class Client
{
@ -35,14 +33,9 @@ class Client
public $requestClient;
private $logger;
private $configuration = [];
public function __construct(string $accessToken, $apiUrl = false, $client = false)
public function __construct(string $accessToken, UrlInterface $apiUrl = null, ClientInterface $client = null)
{
if ($apiUrl !== false && false === $apiUrl instanceof UrlInterface) {
throw new RuntimeException(ExceptionMessages::WRONG_APIURL_INTERFACE);
}
$this->accessToken = $accessToken;
$this->apiUrl = $apiUrl ?: new Url();
$this->requestClient = $client ?: new \GuzzleHttp\Client( [ 'base_uri' => $this->apiUrl->getBaseUri() ] );
@ -87,7 +80,7 @@ class Client
);
if ($response->getStatusCode() != 200) {
$message = sprintf(ExceptionMessages::NOK_REQUEST_RETURN, [$url, $response->getStatusCode()]);
$message = sprintf(ExceptionMessages::NOK_REQUEST_RETURN, $url, $response->getStatusCode());
if ($this->hasLogger()) $this->getLogger()->error($message);
throw new ApiException($message);
}
@ -123,40 +116,8 @@ class Client
/**
* @return LoggerInterface
*/
public function getLogger(): LoggerInterface
public function getLogger(): ?LoggerInterface
{
return $this->logger;
}
/**
* @param string $name
* @param $configuration
*
* @return $this
*/
public function setConfiguration( string $name, $configuration ): Client
{
$this->configuration[$name] = oxNew(ValueObject::class, $configuration);
return $this;
}
public function hasConfiguration(string $name)
{
return isset($this->configuration[$name]);
}
/**
* @param string $name
*
* @return mixed
*/
public function getConfiguration(string $name)
{
if (false === isset($this->configuration)) {
throw new InvalidArgumentException('configuration '.$name.' is not set');
}
return $this->configuration[$name];
return $this->hasLogger() ? $this->logger : null;
}
}

View File

@ -19,8 +19,6 @@ class ExceptionMessages
{
const INVALID_SENDER = 'invalid sender phone number';
const WRONG_APIURL_INTERFACE = 'ApiUrl instance must implement UrlInterface';
const NOK_REQUEST_RETURN = 'request %1$s returns status code %2$s';
const INVALID_RECIPIENT_PHONE = 'invalid recipient phone number';