Compare commits

...

6 Commits
1.1.0 ... 1.2.0

18 changed files with 363 additions and 132 deletions

View File

@ -18,12 +18,13 @@ namespace D3\LinkmobilityClient\Tests;
use Assert\InvalidArgumentException;
use D3\LinkmobilityClient\Client;
use D3\LinkmobilityClient\Exceptions\ApiException;
use D3\LinkmobilityClient\LoggerHandler;
use D3\LinkmobilityClient\Request\RequestInterface;
use D3\LinkmobilityClient\Response\Response;
use D3\LinkmobilityClient\Response\ResponseInterface;
use D3\LinkmobilityClient\SMS\TextRequest;
use D3\LinkmobilityClient\Url;
use D3\LinkmobilityClient\UrlInterface;
use D3\LinkmobilityClient\Url\Url;
use D3\LinkmobilityClient\Url\UrlInterface;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\ClientInterface;
use PHPUnit\Framework\MockObject\MockObject;
@ -170,13 +171,12 @@ class ClientTest extends ApiTestCase
/**
* @test
* @param $useLogger
* @param $okStatus
* @return void
* @throws ReflectionException
* @dataProvider rawRequestDataProvider
*/
public function testRawRequest($useLogger, $okStatus)
public function testRawRequest($okStatus)
{
$statusCode = $okStatus ? '200' : '301';
@ -205,7 +205,7 @@ class ClientTest extends ApiTestCase
->disableOriginalConstructor()
->getMock();
$responseMock->expects($this->atLeastOnce())->method('getStatusCode')->willReturn($statusCode);
$responseMock->expects($useLogger && $okStatus ? $this->atLeastOnce() : $this->never())
$responseMock->expects($okStatus ? $this->atLeastOnce() : $this->never())
->method('getBody')->willReturn($streamMock);
/** @var GuzzleClient|MockObject $requestClientMock */
@ -219,17 +219,21 @@ class ClientTest extends ApiTestCase
->onlyMethods(['debug', 'error', 'log'])
->getMock();
/** @var LoggerHandler|MockObject $loggerHandlerMock */
$loggerHandlerMock = $this->getMockBuilder(LoggerHandler::class)
->onlyMethods(['getLogger'])
->getMock();
$loggerHandlerMock->method('getLogger')->willReturn($loggerMock);
/** @var Client|MockObject $clientMock */
$clientMock = $this->getMockBuilder(Client::class)
->disableOriginalConstructor()
->onlyMethods([
'hasLogger',
'getLogger'
'getLoggerHandler'
])
->getMock();
$clientMock->method('hasLogger')->willReturn($useLogger);
$clientMock->expects($useLogger ? $this->atLeastOnce() : $this->never())
->method('getLogger')->willReturn($loggerMock);
$clientMock->expects($this->atLeastOnce())
->method('getLoggerHandler')->willReturn($loggerHandlerMock);
$this->setValue($clientMock, 'requestClient', $requestClientMock);
if (false === $okStatus) {
@ -247,37 +251,8 @@ class ClientTest extends ApiTestCase
public function rawRequestDataProvider(): array
{
return [
'has logger, OK status' => [true, true],
'has no logger, OK status' => [false, true],
'has logger, NOK status' => [true, false],
'has no logger, NOK status' => [false, false],
'OK status' => [true],
'NOK status' => [false],
];
}
/**
* @test
* @return void
* @throws ReflectionException
*/
public function testLogger()
{
$this->assertFalse($this->callMethod($this->api, 'hasLogger'));
$this->assertNull($this->callMethod($this->api, 'getLogger'));
/** @var LoggerInterface|MockObject $loggerMock */
$loggerMock = $this->getMockBuilder(AbstractLogger::class)
->onlyMethods(['debug', 'error', 'log'])
->getMock();
$this->assertSame(
$this->api,
$this->callMethod($this->api, 'setLogger', [$loggerMock])
);
$this->assertTrue($this->callMethod($this->api, 'hasLogger'));
$this->assertSame(
$loggerMock,
$this->callMethod($this->api, 'getLogger')
);
}
}

View File

@ -0,0 +1,79 @@
<?php
/**
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* @link https://www.oxidmodule.com
*/
declare(strict_types=1);
namespace D3\LinkmobilityClient\Tests;
use D3\LinkmobilityClient\LoggerHandler;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\AbstractLogger;
use Psr\Log\LoggerInterface;
use ReflectionException;
class LoggerHandlerTest extends ApiTestCase
{
/** @var LoggerHandler */
public $loggerHandler;
/**
* @return void
*/
public function setUp(): void
{
parent::setUp();
$this->loggerHandler = new LoggerHandler();
}
/**
* @return void
*/
public function tearDown(): void
{
parent::tearDown();
unset($this->loggerHandler);
}
/**
* @test
* @throws ReflectionException
*/
public function testConstruct()
{
$this->assertInstanceOf(
LoggerInterface::class,
$this->callMethod($this->loggerHandler, 'getLogger')
);
}
/**
* @test
* @return void
* @throws ReflectionException
*/
public function testLogger()
{
/** @var LoggerInterface|MockObject $loggerMock */
$loggerMock = $this->getMockBuilder(AbstractLogger::class)
->onlyMethods(['debug', 'error', 'log'])
->getMock();
$this->callMethod($this->loggerHandler, 'setLogger', [ $loggerMock]);
$this->assertSame(
$loggerMock,
$this->callMethod($this->loggerHandler, 'getLogger')
);
}
}

View File

@ -16,6 +16,7 @@ declare(strict_types=1);
namespace D3\LinkmobilityClient\Tests\RecipientsList;
use D3\LinkmobilityClient\Client;
use D3\LinkmobilityClient\LoggerHandler;
use D3\LinkmobilityClient\RecipientsList\RecipientsList;
use D3\LinkmobilityClient\Tests\ApiTestCase;
use D3\LinkmobilityClient\ValueObject\Recipient;
@ -183,13 +184,18 @@ class RecipientsListTest extends ApiTestCase
->getMock();
$loggerMock->expects($this->atLeastOnce())->method('info')->willReturn(true);
/** @var LoggerHandler|MockObject $loggerHandlerMock */
$loggerHandlerMock = $this->getMockBuilder(LoggerHandler::class)
->onlyMethods(['getLogger'])
->getMock();
$loggerHandlerMock->method('getLogger')->willReturn($loggerMock);
/** @var Client|MockObject $clientMock */
$clientMock = $this->getMockBuilder(Client::class)
->disableOriginalConstructor()
->onlyMethods(['hasLogger', 'getLogger'])
->onlyMethods(['getLoggerHandler'])
->getMock();
$clientMock->method('hasLogger')->willReturn(true);
$clientMock->method('getLogger')->willReturn($loggerMock);
$clientMock->method('getLoggerHandler')->willReturn($loggerHandlerMock);
/** @var RecipientsList|MockObject $recListMock */
$recListMock = $this->getMockBuilder(RecipientsList::class)

View File

@ -92,6 +92,20 @@ abstract class AbstractRequest extends ApiTestCase
);
}
/**
* @test
* @throws ReflectionException
*/
public function testGetUri()
{
$this->assertIsString(
$this->callMethod(
$this->request,
'getUri'
)
);
}
/**
* @test
* @throws ReflectionException
@ -228,8 +242,6 @@ abstract class AbstractRequest extends ApiTestCase
];
}
/**
* @test
* @throws ReflectionException
@ -453,10 +465,55 @@ abstract class AbstractRequest extends ApiTestCase
/**
* @test
* @throws ReflectionException
* @dataProvider setGetSenderAddressTypeDataProvider
*/
public function setGetSenderAddressTypeTest()
public function testSetGetSenderAddressType($hasSender, $addressType, $expected)
{
$this->checkGetterSetter('fixture', 'setSenderAddressType', 'getSenderAddressType');
/** @var Request|MockObject $request */
$request = $this->getMockBuilder($this->testClassName)
->setConstructorArgs([$this->request->getMessage(), $this->request->getClient()])
->onlyMethods(['getSenderAddress'])
->getMock();
if ($hasSender) {
/** @var Sender|MockObject $senderMock */
$senderMock = $this->getMockBuilder(Sender::class)
->disableOriginalConstructor()
->onlyMethods(['get'])
->getMock();
$senderMock->method('get')->willReturn('fixture');
$request->method('getSenderAddress')->willReturn($senderMock);
} else {
$request->method('getSenderAddress')->willReturn(null);
}
$this->assertInstanceOf(
Request::class,
$this->callMethod(
$request,
'setSenderAddressType',
[$addressType]
)
);
$this->assertSame(
$expected,
$this->callMethod(
$request,
'getSenderAddressType'
)
);
}
/**
* @return array[]
*/
public function setGetSenderAddressTypeDataProvider(): array
{
return [
'has no sender' => [false, 'fixture', null],
'has sender and address type' => [true, 'fixture', 'fixture'],
];
}
/**

View File

@ -13,9 +13,10 @@
declare(strict_types=1);
namespace D3\LinkmobilityClient\Tests;
namespace D3\LinkmobilityClient\Tests\Url;
use D3\LinkmobilityClient\Url;
use D3\LinkmobilityClient\Tests\ApiTestCase;
use D3\LinkmobilityClient\Url\Url;
use ReflectionException;
class UrlTest extends ApiTestCase
@ -58,4 +59,34 @@ class UrlTest extends ApiTestCase
)
);
}
/**
* @test
* @throws ReflectionException
*/
public function testGetTextSmsUri()
{
$uri = $this->callMethod(
$this->url,
'getTextSmsUri'
);
$this->assertIsString($uri);
$this->assertStringStartsWith('/', $uri);
}
/**
* @test
* @throws ReflectionException
*/
public function testGetBinarySmsUri()
{
$uri = $this->callMethod(
$this->url,
'getBinarySmsUri'
);
$this->assertIsString($uri);
$this->assertStringStartsWith('/', $uri);
}
}

View File

@ -18,11 +18,12 @@ namespace D3\LinkmobilityClient;
use D3\LinkmobilityClient\Exceptions\ApiException;
use D3\LinkmobilityClient\Exceptions\ExceptionMessages;
use D3\LinkmobilityClient\Request\RequestInterface;
use D3\LinkmobilityClient\Url\Url;
use D3\LinkmobilityClient\Url\UrlInterface;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;
use InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
class Client
{
@ -30,8 +31,6 @@ class Client
public $apiUrl;
public $requestClient;
private $logger;
public function __construct(string $accessToken, UrlInterface $apiUrl = null, ClientInterface $client = null)
{
$this->accessToken = $accessToken;
@ -69,9 +68,7 @@ class Client
{
$options['headers']['Authorization'] = 'Bearer '.$this->accessToken;
if ($this->hasLogger()) {
$this->getLogger()->debug('request '.$url, $options);
}
$this->getLoggerHandler()->getLogger()->debug('request '.$url, $options);
$response = $this->requestClient->request(
$method,
@ -81,45 +78,21 @@ class Client
if ($response->getStatusCode() != 200) {
$message = sprintf(ExceptionMessages::NOK_REQUEST_RETURN, $url, $response->getStatusCode());
if ($this->hasLogger()) {
$this->getLogger()->error($message);
}
$this->getLoggerHandler()->getLogger()->error($message);
throw new ApiException($message);
}
if ($this->hasLogger()) {
$response->getBody()->rewind();
$this->getLogger()->debug('response', [$response->getBody()->getContents()]);
}
$response->getBody()->rewind();
$this->getLoggerHandler()->getLogger()->debug('response', [$response->getBody()->getContents()]);
return $response;
}
/**
* @param mixed $logger
*
* @return Client
* @return LoggerHandler
*/
public function setLogger(LoggerInterface $logger): Client
public function getLoggerHandler(): LoggerHandler
{
$this->logger = $logger;
return $this;
}
/**
* @return bool
*/
public function hasLogger(): bool
{
return $this->logger instanceof LoggerInterface;
}
/**
* @return LoggerInterface|null
*/
public function getLogger()
{
return $this->hasLogger() ? $this->logger : null;
return LoggerHandler::getInstance();
}
}

View File

@ -17,11 +17,15 @@ namespace D3\LinkmobilityClient\Exceptions;
class ExceptionMessages
{
public const INVALID_SENDER = 'invalid sender phone number';
public const INVALID_SENDER = 'invalid sender phone number';
public const NOK_REQUEST_RETURN = 'request %1$s returns status code %2$s';
public const NOK_REQUEST_RETURN = 'request %1$s returns status code %2$s';
public const INVALID_RECIPIENT_PHONE = 'invalid recipient phone number';
public const INVALID_RECIPIENT_PHONE = 'invalid recipient phone number';
public const NOT_A_MOBILE_NUMBER = 'not a mobile number';
public const NOT_A_MOBILE_NUMBER = 'not a mobile number';
public const EMPTY_RECIPIENT_LIST = 'request must contain a valid recipient';
public const DEBUG_NOSENDERORCOUNTRYCODE= 'no sender number or sender country code defined, use fallback to account default';
}

View File

@ -0,0 +1,20 @@
<?php
/**
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* @link https://www.oxidmodule.com
*/
declare(strict_types=1);
namespace D3\LinkmobilityClient\Exceptions;
class NoSenderDefinedException extends LinkmobilityException
{
}

55
src/LoggerHandler.php Normal file
View File

@ -0,0 +1,55 @@
<?php
/**
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* @link https://www.oxidmodule.com
*/
declare(strict_types=1);
namespace D3\LinkmobilityClient;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
class LoggerHandler
{
private static $instance = null;
private $logger;
public static function getInstance()
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
public function __construct()
{
$this->setLogger(new NullLogger());
}
/**
* @param mixed $logger
*/
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}
/**
* @return LoggerInterface
*/
public function getLogger(): LoggerInterface
{
return $this->logger;
}
}

View File

@ -76,13 +76,9 @@ class RecipientsList implements RecipientsListInterface, Iterator
$this->recipients[ md5(serialize($recipient)) ] = $recipient;
} catch (NumberParseException $e) {
if ($this->getClient()->hasLogger()) {
$this->getClient()->getLogger()->info($e->getMessage());
}
$this->client->getLoggerHandler()->getLogger()->info($e->getMessage());
} catch (RecipientException $e) {
if ($this->getClient()->hasLogger()) {
$this->getClient()->getLogger()->info($e->getMessage());
}
$this->client->getLoggerHandler()->getLogger()->info($e->getMessage());
}
return $this;

View File

@ -17,6 +17,7 @@ namespace D3\LinkmobilityClient\Request;
use Assert\Assert;
use D3\LinkmobilityClient\Client;
use D3\LinkmobilityClient\Exceptions\ExceptionMessages;
use D3\LinkmobilityClient\RecipientsList\RecipientsList;
use D3\LinkmobilityClient\RecipientsList\RecipientsListInterface;
use D3\LinkmobilityClient\Response\ResponseInterface;
@ -122,6 +123,11 @@ abstract class Request implements RequestInterface
return $this;
}
/**
* @return string
*/
abstract public function getUri(): string;
/**
* @throws InvalidArgumentException
*/
@ -135,7 +141,7 @@ abstract class Request implements RequestInterface
Assert::that($this->getOptions())->isArray();
Assert::that($this->getRecipientsList())->isInstanceOf(RecipientsList::class)->notEmpty();
Assert::that($this->getRecipientsList()->getRecipients())->notEmpty('request must contain a valid recipient');
Assert::that($this->getRecipientsList()->getRecipients())->notEmpty(ExceptionMessages::EMPTY_RECIPIENT_LIST);
Assert::thatAll($this->getRecipientsList())->isInstanceOf(Recipient::class)->notEmpty();
// optional properties
@ -460,7 +466,7 @@ abstract class Request implements RequestInterface
*/
public function getSenderAddressType()
{
return $this->senderAddressType;
return $this->getSenderAddress() && $this->getSenderAddress()->get() ? $this->senderAddressType : null;
}
/**

View File

@ -17,6 +17,7 @@ namespace D3\LinkmobilityClient\SMS;
use Assert\Assert;
use D3\LinkmobilityClient\Request\Request;
use D3\LinkmobilityClient\Url\Url;
use D3\LinkmobilityClient\ValueObject\SmsBinaryMessage;
use InvalidArgumentException;
@ -27,7 +28,7 @@ class BinaryRequest extends Request implements SmsRequestInterface
*/
public function getUri(): string
{
return '/rest/smsmessaging/binary';
return (new Url())->getBinarySmsUri();
}
public function getRawBody(): array

View File

@ -17,6 +17,7 @@ namespace D3\LinkmobilityClient\SMS;
use Assert\Assert;
use D3\LinkmobilityClient\Request\Request;
use D3\LinkmobilityClient\Url\Url;
use D3\LinkmobilityClient\ValueObject\SmsTextMessage;
use InvalidArgumentException;
@ -27,7 +28,7 @@ class TextRequest extends Request implements SmsRequestInterface
*/
public function getUri(): string
{
return '/rest/smsmessaging/text';
return (new Url())->getTextSmsUri();
}
/**

View File

@ -13,7 +13,7 @@
declare(strict_types=1);
namespace D3\LinkmobilityClient;
namespace D3\LinkmobilityClient\Url;
class Url implements UrlInterface
{
@ -26,4 +26,20 @@ class Url implements UrlInterface
{
return $this->baseUri;
}
/**
* @return string
*/
public function getTextSmsUri()
{
return '/rest/smsmessaging/text';
}
/**
* @return string
*/
public function getBinarySmsUri()
{
return '/rest/smsmessaging/binary';
}
}

View File

@ -13,7 +13,7 @@
declare(strict_types=1);
namespace D3\LinkmobilityClient;
namespace D3\LinkmobilityClient\Url;
interface UrlInterface
{

View File

@ -17,12 +17,14 @@ namespace D3\LinkmobilityClient\ValueObject;
use Assert\Assert;
use D3\LinkmobilityClient\Exceptions\ExceptionMessages;
use D3\LinkmobilityClient\Exceptions\NoSenderDefinedException;
use D3\LinkmobilityClient\Exceptions\RecipientException;
use D3\LinkmobilityClient\LoggerHandler;
use libphonenumber\NumberParseException;
use libphonenumber\PhoneNumberFormat;
use libphonenumber\PhoneNumberUtil;
class Sender extends StringValueObject
class Sender extends ValueObject
{
/**
* @param string $number
@ -31,20 +33,30 @@ class Sender extends StringValueObject
* @throws RecipientException
* @throws NumberParseException
*/
public function __construct(string $number, string $iso2CountryCode)
public function __construct(string $number = null, string $iso2CountryCode = null)
{
Assert::that($iso2CountryCode)->string()->length(2);
try {
if (is_null($number) || is_null($iso2CountryCode)) {
throw new NoSenderDefinedException();
}
$phoneUtil = $this->getPhoneNumberUtil();
Assert::that($iso2CountryCode)->string()->length(2);
$phoneNumber = $phoneUtil->parse($number, strtoupper($iso2CountryCode));
$number = ltrim($phoneUtil->format($phoneNumber, PhoneNumberFormat::E164), '+');
$phoneUtil = $this->getPhoneNumberUtil();
if (false === $phoneUtil->isValidNumber($phoneNumber)) {
throw new RecipientException(ExceptionMessages::INVALID_SENDER);
$phoneNumber = $phoneUtil->parse($number, strtoupper($iso2CountryCode));
$number = ltrim($phoneUtil->format($phoneNumber, PhoneNumberFormat::E164), '+');
if (false === $phoneUtil->isValidNumber($phoneNumber)) {
throw new RecipientException(ExceptionMessages::INVALID_SENDER);
}
parent::__construct($number);
} catch (NoSenderDefinedException $e) {
LoggerHandler::getInstance()->getLogger()->debug(
ExceptionMessages::DEBUG_NOSENDERORCOUNTRYCODE
);
}
parent::__construct($number);
}
/**
@ -54,12 +66,4 @@ class Sender extends StringValueObject
{
return PhoneNumberUtil::getInstance();
}
/**
* @return int
*/
public function getFormatted()
{
return parent::getFormatted();
}
}

View File

@ -19,13 +19,6 @@ use Assert\Assert;
abstract class StringValueObject extends ValueObject
{
public function __construct(string $number)
{
Assert::that($number)->notEmpty();
$this->value = $number;
}
public function __toString()
{
return $this->get();
@ -35,9 +28,4 @@ abstract class StringValueObject extends ValueObject
{
return $this->value;
}
public function getFormatted()
{
return $this->get();
}
}

View File

@ -15,7 +15,26 @@ declare(strict_types=1);
namespace D3\LinkmobilityClient\ValueObject;
use Assert\Assert;
abstract class ValueObject
{
protected $value;
public function __construct(string $number)
{
Assert::that($number)->notEmpty();
$this->value = $number;
}
public function get()
{
return $this->value;
}
public function getFormatted()
{
return $this->get();
}
}