Compare commits

...

31 Commits
2.0.0 ... main

Author SHA1 Message Date
Daniel Seifert 8ee6b780a4
adjust changelog 2023-06-29 11:28:01 +02:00
Daniel Seifert 889584f5c8
bump psr/http-message to version 2 2023-06-29 11:24:06 +02:00
Daniel Seifert 14a26a602b
use assertion exception 2023-02-02 22:27:14 +01:00
Daniel Seifert e8da35995e
fix wrong changelog item date 2023-01-20 09:07:46 +01:00
Daniel Seifert 958ed9e83d
adjust changelog 2023-01-20 08:51:02 +01:00
Daniel Seifert 1e2582a76b
add middleware to retry request in some error cases 2023-01-19 09:35:02 +01:00
Daniel Seifert a50567fd0c
remove unused ApiException 2023-01-18 09:28:05 +01:00
Daniel Seifert 19f5fca18a
make installable in PHP > 8.0 2023-01-17 20:38:20 +01:00
Daniel Seifert 186bb2d622
add debug logger to default Guzzle client 2023-01-17 20:37:33 +01:00
Daniel Seifert 6c5a83a8a8
make deprecated client argument optional in recipients list 2023-01-17 15:36:50 +01:00
Daniel Seifert 54cfb2645c
add missing recipients list interface definition 2023-01-11 00:05:20 +01:00
Daniel Seifert 95384c5eba
mark client parameter in recipient list class as deprecated 2023-01-07 22:50:17 +01:00
Daniel Seifert a8756c7ade
allow guzzle dependency v7.3 for more backward compatibility 2022-12-26 00:33:57 +01:00
Daniel Seifert df33a97084
adjust README 2022-09-29 09:12:10 +02:00
Daniel Seifert 222770bec9
add support note 2022-09-08 15:45:21 +02:00
Daniel Seifert 8f70d40640
adjust version informations 2022-07-28 10:54:23 +02:00
Daniel Seifert cd4e3b6277
Merge remote-tracking branch 'remotes/origin/rel_1.x' into rel_2.x
# Conflicts:
#	CHANGELOG.md
2022-07-28 10:50:49 +02:00
Daniel Seifert f4d5e54448
adjust version informations 2022-07-28 10:47:20 +02:00
Daniel Seifert 0d589f3878
fix wrong return type of LoggerHandler::getInstance 2022-07-28 10:22:13 +02:00
Daniel Seifert 4cd0ab0d17
fix wrong return type of LoggerHandler::getInstance
# Conflicts:
#	src/LoggerHandler.php
2022-07-28 10:17:50 +02:00
Daniel Seifert 1a9389d763
improve changelog
# Conflicts:
#	CHANGELOG.md
2022-07-28 10:17:50 +02:00
Daniel Seifert 491c260870
add request methods to it's interface 2022-07-28 10:17:50 +02:00
Daniel Seifert 50d2276a78
adjust changelog
# Conflicts:
#	CHANGELOG.md
2022-07-28 10:17:50 +02:00
Daniel Seifert 3932246fbd
improve changelog 2022-07-27 09:35:25 +02:00
Daniel Seifert 3338d70562
add request methods to it's interface 2022-07-27 09:31:46 +02:00
Daniel Seifert 229d961bb8
adjust changelog 2022-07-26 11:04:07 +02:00
Daniel Seifert fed8040fcb
improve code 2022-07-21 14:07:10 +02:00
Daniel Seifert b33b6081ad
fix composer script commands 2022-07-21 11:03:37 +02:00
Daniel Seifert d07eb1f694
make package installable in PHP 8.0.x (dev mode) 2022-07-21 09:55:31 +02:00
Daniel Seifert c8ca412c14
fix phpstan level 2 and below issues 2022-07-21 00:33:39 +02:00
Daniel Seifert ff0784ce46
add phpstan and configuration 2022-07-21 00:15:04 +02:00
29 changed files with 478 additions and 214 deletions

View File

@ -1,48 +1,97 @@
# Changelog
All notable changes to this project will be documented in this file.
---
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## 2.0.0.0 (2022-07-19)
## [Unreleased](https://git.d3data.de/D3Public/linkmobility-php-client/compare/2.2.0...rel_2.x)
## [2.2.0](https://git.d3data.de/D3Public/linkmobility-php-client/compare/2.1.0...2.2.0) - 2023-06-29
### Added
- bump psr/http-message dependency to version 2
### Changed
- use assertion exception
## [2.1.0](https://git.d3data.de/D3Public/linkmobility-php-client/compare/2.0.3...2.1.0) - 2023-01-20
### Added
- installable in PHP > 8.0
- debug logger to log all comunications in debug mode (default Guzzle client only)
- retry middleware to request again in defined error cases (default Guzzle client only)
### Fixed
- missing getRecipientsList() in RecipientsListInterface
### Deprecated
- unused client argument in recipient list class
### Removed
- unused ApiException class
## [2.0.3](https://git.d3data.de/D3Public/linkmobility-php-client/compare/2.0.2...2.0.3) - 2022-12-26
### Changed
- allow Guzzle v7.3 for more backward compatibility
## [2.0.2](https://git.d3data.de/D3Public/linkmobility-php-client/compare/2.0.1...2.0.2) - 2022-07-28
### Changed
- add support note
- adjust readme
## [2.0.1](https://git.d3data.de/D3Public/linkmobility-php-client/compare/2.0.0...2.0.1) - 2022-07-28
### Added
- phpstan code checks
### Changed
- improve changelog
- improve code quality
### Fixed
- wrong return type of LoggerHandler::getInstance
## [2.0.0](https://git.d3data.de/D3Public/linkmobility-php-client/compare/1.3.0...2.0.0) - 2022-07-19
### Changed
- adjust to PHP >= 7.3 and current dependency packages
---
## [1.3.1](https://git.d3data.de/D3Public/linkmobility-php-client/compare/1.3.0...1.3.1) - 2022-07-28
### Changed
- improve code quality
## 1.3.0.0 (2022-07-18)
### Fixed
- wrong return type of LoggerHandler::getInstance
- tests use generated example phone numbers
- move recipient checks from list to recipient itself
## [1.3.0](https://git.d3data.de/D3Public/linkmobility-php-client/compare/1.2.1...1.3.0) - 2022-07-18
### Added
- tests added
---
## 1.2.1.0 (2022-07-15)
### Changed
- tests use generated example phone numbers
- move recipient checks from list to recipient itself
## [1.2.1](https://git.d3data.de/D3Public/linkmobility-php-client/compare/1.2.0...1.2.1) - 2022-07-15
### Changed
- extend log messages
- sanitize special phone number format before request
---
## 1.2.0.0 (2022-07-14)
- make sender number optional
- assign sender address type only if sender is set
## [1.2.0](https://git.d3data.de/D3Public/linkmobility-php-client/compare/1.1.0...1.2.0) - 2022-07-14
### Added
- collect exception messages in a class
- collect URI parts in a class
### Changed
- make sender number optional
- assign sender address type only if sender is set
- extract logger handler from client
---
## 1.1.0.0 (2022-07-13)
## [1.1.0](https://git.d3data.de/D3Public/linkmobility-php-client/compare/1.0.0...1.1.0) - 2022-07-13
### Added
- make installable in PHP 8
### Removed
- remove unused dependency
---
## 1.0.0.0 (2022-07-13)
## [1.0.0](https://git.d3data.de/D3Public/linkmobility-php-client/releases/tag/1.0.0) - 2022-07-13
### Added
- initial implementation
- SMS requests (text or binary)
- SMS responses
- recipient managing
- recipient managing

View File

@ -1,7 +1,7 @@
[![deutsche Version](https://logos.oxidmodule.com/de2_xs.svg)](README.md)
[![english version](https://logos.oxidmodule.com/en2_xs.svg)](README.en.md)
# LINK Mobility Austria PHP API Client
# LINK Mobility PHP API Client
[LINK Mobility](https://www.linkmobility.de/) provides a service for sending mobile messages (SMS, Whatsapp, RCS, Chatbot, ...).
@ -18,8 +18,8 @@ composer require d3/linkmobility-php-client
```
```
$client = new Client('personal accesstoken');
$client->setLogger($logger); // optional
$client = new \D3\LinkmobilityClient\Client('personal accesstoken');
\D3\LinkmobilityClient\LoggerHandler::getInstance()->setLogger($logger); // optional
$request = new D3\LinkmobilityClient\SMS\RequestFactory($message, $client)->getSmsRequest())
->addRecipient(new D3\LinkmobilityClient\ValueObject\Recipient('recipient number', 'DE'));
$response = $client->request($request)
@ -39,6 +39,12 @@ If you have a suggestion that would make this better, please fork the repo and c
- Push to the Branch (git push origin feature/AmazingFeature)
- Open a Pull Request
## Support
If you have any questions about the *messaging service* and its *contracts*, please contact the [LINK Mobility Team](https://www.linkmobility.de/kontakt).
For *technical inquiries* you will find the contact options in the [composer.json](composer.json).
## License
(status: 2022-07-13)

View File

@ -1,7 +1,7 @@
[![deutsche Version](https://logos.oxidmodule.com/de2_xs.svg)](README.md)
[![english version](https://logos.oxidmodule.com/en2_xs.svg)](README.en.md)
# LINK Mobility Austria PHP API Client
# LINK Mobility PHP API Client
[LINK Mobility](https://www.linkmobility.de/) stellt einen Service zum Versenden von mobilen Nachrichten (SMS, Whatsapp, RCS, Chatbot, ...) zur VerfĂĽgung.
@ -18,8 +18,8 @@ composer require d3/linkmobility-php-client
```
```
$client = new Client('personal accesstoken');
$client->setLogger($logger); // optional
$client = new \D3\LinkmobilityClient\Client('personal accesstoken');
\D3\LinkmobilityClient\LoggerHandler::getInstance()->setLogger($logger); // optional
$request = new D3\LinkmobilityClient\SMS\RequestFactory($message, $client)->getSmsRequest())
->addRecipient(new D3\LinkmobilityClient\ValueObject\Recipient('recipient number', 'DE'));
$response = $client->request($request)
@ -39,6 +39,12 @@ Wenn Sie eine Verbesserungsvorschlag haben, legen Sie einen Fork des Respoitorie
- Ăśbertragen Sie den Branch (git push origin feature/AmazingFeature)
- Ă–ffnen Sie einen Pull Request
## Support
Bei Fragen zum *Messaging Service* und dessen *Verträgen* kontaktieren Sie bitte das [LINK Mobility Team](https://www.linkmobility.de/kontakt).
Zu *technischen Anfragen* finden Sie die Kontaktmöglichkeiten in der [composer.json](composer.json).
## Lizenz
(Stand: 13.07.2022)

View File

@ -31,7 +31,7 @@ abstract class ApiTestCase extends TestCase
* @return mixed
* @throws ReflectionException
*/
public function callMethod(object $object, string $methodName, array $arguments = [])
public function callMethod($object, string $methodName, array $arguments = [])
{
$class = new ReflectionClass($object);
$method = $class->getMethod($methodName);
@ -47,7 +47,7 @@ abstract class ApiTestCase extends TestCase
* @param $value
* @throws ReflectionException
*/
public function setValue(object $object, string $valueName, $value)
public function setValue($object, string $valueName, $value)
{
$reflection = new ReflectionClass($object);
$property = $reflection->getProperty($valueName);
@ -63,7 +63,7 @@ abstract class ApiTestCase extends TestCase
* @return mixed
* @throws ReflectionException
*/
public function getValue(object $object, string $valueName)
public function getValue($object, string $valueName)
{
$reflection = new ReflectionClass($object);
$property = $reflection->getProperty($valueName);

View File

@ -17,7 +17,6 @@ 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;
@ -27,11 +26,9 @@ use D3\LinkmobilityClient\Url\Url;
use D3\LinkmobilityClient\Url\UrlInterface;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Psr7\Response as GuzzleResponse;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Http\Message\ResponseInterface as MessageResponseInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Log\AbstractLogger;
use Psr\Log\LoggerInterface;
use ReflectionException;
class ClientTest extends ApiTestCase
@ -100,6 +97,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
@ -173,41 +192,20 @@ class ClientTest extends ApiTestCase
/**
* @test
* @param $okStatus
* @return void
* @throws ReflectionException
* @dataProvider rawRequestDataProvider
* @covers \D3\LinkmobilityClient\Client::rawRequest
*/
public function testRawRequest($okStatus)
public function testRawRequest()
{
$statusCode = $okStatus ? '200' : '301';
/** @var StreamInterface|MockObject $streamMock */
$streamMock = $this->getMockBuilder(StreamInterface::class)
->getMock();
/** @var MessageResponseInterface|MockObject $responseMock */
$responseMock = $this->getMockBuilder(MessageResponseInterface::class)
->onlyMethods([
'getStatusCode',
'getBody',
'withStatus',
'getReasonPhrase',
'getProtocolVersion',
'withProtocolVersion',
'getHeaders',
'hasHeader',
'getHeader',
'getHeaderLine',
'withHeader',
'withAddedHeader',
'withoutHeader',
'withBody',
])
/** @var GuzzleResponse|MockObject $responseMock */
$responseMock = $this->getMockBuilder( GuzzleResponse::class)
->disableOriginalConstructor()
->getMock();
$responseMock->expects($this->atLeastOnce())->method('getStatusCode')->willReturn($statusCode);
$responseMock->expects($this->atLeastOnce())
->method('getBody')->willReturn($streamMock);
@ -217,17 +215,6 @@ class ClientTest extends ApiTestCase
->getMock();
$requestClientMock->expects($this->once())->method('request')->willReturn($responseMock);
/** @var LoggerInterface|MockObject $loggerMock */
$loggerMock = $this->getMockBuilder(AbstractLogger::class)
->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()
@ -235,30 +222,14 @@ class ClientTest extends ApiTestCase
'getLoggerHandler',
])
->getMock();
$clientMock->expects($this->atLeastOnce())
->method('getLoggerHandler')->willReturn($loggerHandlerMock);
$this->setValue($clientMock, 'requestClient', $requestClientMock);
if (false === $okStatus) {
$this->expectException(ApiException::class);
}
$this->assertSame(
$responseMock,
$this->callMethod($clientMock, 'rawRequest', ['myUrl'])
);
}
/**
* @return array
*/
public function rawRequestDataProvider(): array
{
return [
'OK status' => [true],
'NOK status' => [false],
];
}
/**
* @test
* @return void
@ -275,4 +246,38 @@ class ClientTest extends ApiTestCase
)
);
}
/**
* @test
* @return void
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
* @covers \D3\LinkmobilityClient\Client::getLoggerMiddleware
*/
public function testGetLoggerMiddleware()
{
$this->assertIsCallable(
$this->callMethod(
$this->api,
'getLoggerMiddleware'
)
);
}
/**
* @test
* @return void
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
* @covers \D3\LinkmobilityClient\Client::getRetryMiddleware
*/
public function testGetRetryMiddleware()
{
$this->assertIsCallable(
$this->callMethod(
$this->api,
'getRetryMiddleware'
)
);
}
}

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

@ -89,13 +89,10 @@ abstract class AbstractRequest extends ApiTestCase
$requestMock->expects($this->atLeastOnce())->method('setMessage')->with($this->equalTo($messageMock))->willReturnSelf();
$requestMock->expects($this->atLeastOnce())->method('setClient')->with($this->equalTo($clientMock))->willReturnSelf();
$this->assertInstanceOf(
Request::class,
$this->callMethod(
$requestMock,
'__construct',
[$messageMock, $clientMock]
)
$this->callMethod(
$requestMock,
'__construct',
[$messageMock, $clientMock]
);
}

View File

@ -86,7 +86,7 @@ abstract class AbstractResponse extends ApiTestCase
*/
protected function checkProperties($expected, $propertyName, $methodName)
{
/** @var Response $response */
/** @var Response|MockObject $responseMock */
$responseMock = $this->getMockBuilder($this->testClassName)
->disableOriginalConstructor()
->onlyMethods(['getContent'])

View File

@ -149,7 +149,7 @@ class RecipientTest extends ApiTestCase
}
/**
* @return string[][]
* @return array[]
*/
public function constructInvalidDataProvider(): array
{

View File

@ -29,7 +29,7 @@ use ReflectionException;
class SenderTest extends ApiTestCase
{
/** @var Sender */
/** @var Sender|MockObject */
public $sender;
private $phoneNumberFixture;
@ -48,7 +48,6 @@ class SenderTest extends ApiTestCase
$example = $phoneUtil->getExampleNumberForType($this->phoneCountryFixture, PhoneNumberType::MOBILE);
$this->phoneNumberFixture = $phoneUtil->format($example, PhoneNumberFormat::NATIONAL);
/** @var Sender|MockObject sender */
$this->sender = new Sender($this->phoneNumberFixture, $this->phoneCountryFixture);
}
@ -159,7 +158,7 @@ class SenderTest extends ApiTestCase
}
/**
* @return string[][]
* @return array[]
*/
public function constructInvalidDataProvider(): array
{

View File

@ -15,14 +15,13 @@ declare(strict_types=1);
namespace D3\LinkmobilityClient\Tests\ValueObject;
use Assert\InvalidArgumentException;
use D3\LinkmobilityClient\Tests\ApiTestCase;
use D3\LinkmobilityClient\ValueObject\SmsBinaryMessage;
use Phlib\SmsLength\Exception\InvalidArgumentException;
use Phlib\SmsLength\SmsLength;
use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException;
class SmsBinaryMessageTest extends ApiTestCase
class SmsBinaryMessageTest extends SmsMessageAbstractTest
{
/** @var SmsBinaryMessage */
public $message;
@ -39,21 +38,12 @@ class SmsBinaryMessageTest extends ApiTestCase
$this->message = new SmsBinaryMessage($this->messageFixture);
}
/**
* @return void
*/
public function tearDown(): void
{
parent::tearDown();
unset($this->message);
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\LinkmobilityClient\ValueObject\SmsBinaryMessage::__construct
* @covers \D3\LinkmobilityClient\ValueObject\SmsMessageAbstract::__construct
*/
public function testConstructValid()
{
@ -91,6 +81,7 @@ class SmsBinaryMessageTest extends ApiTestCase
* @throws ReflectionException
* @dataProvider constructInvalidDataProvider
* @covers \D3\LinkmobilityClient\ValueObject\SmsBinaryMessage::__construct
* @covers \D3\LinkmobilityClient\ValueObject\SmsMessageAbstract::__construct
*/
public function testConstructInvalid($binaryMessage, $valid, $expectedException)
{
@ -102,7 +93,7 @@ class SmsBinaryMessageTest extends ApiTestCase
if ($valid) {
$smsLengthMock->expects($this->never())->method('validate')->willReturn(true);
} else {
$smsLengthMock->expects($this->atLeastOnce())->method('validate')->willThrowException(new \Phlib\SmsLength\Exception\InvalidArgumentException());
$smsLengthMock->expects($this->atLeastOnce())->method('validate')->willThrowException(new InvalidArgumentException());
}
/** @var SmsBinaryMessage|MockObject $message */
@ -124,37 +115,11 @@ class SmsBinaryMessageTest extends ApiTestCase
);
}
/**
* @return string[][]
*/
public function constructInvalidDataProvider(): array
{
return [
'empty message' => ['', true, InvalidArgumentException::class],
'invalid sms message' => ['abc', false, \Phlib\SmsLength\Exception\InvalidArgumentException::class],
];
}
/**
* @test
* @throws ReflectionException
* @covers \D3\LinkmobilityClient\ValueObject\SmsBinaryMessage::getSmsLength
*/
public function testGetSmsLengthInstance()
{
$this->assertInstanceOf(
SmsLength::class,
$this->callMethod(
$this->message,
'getSmsLength'
)
);
}
/**
* @test
* @throws ReflectionException
* @covers \D3\LinkmobilityClient\ValueObject\SmsBinaryMessage::chunkCount
* @covers \D3\LinkmobilityClient\ValueObject\SmsMessageAbstract::chunkCount
*/
public function testGetChunkCount()
{
@ -170,9 +135,9 @@ class SmsBinaryMessageTest extends ApiTestCase
/** @var SmsBinaryMessage|MockObject $message */
$message = $this->getMockBuilder(SmsBinaryMessage::class)
->onlyMethods(['getSmsLength'])
->disableOriginalConstructor()
->getMock();
->onlyMethods(['getSmsLength'])
->disableOriginalConstructor()
->getMock();
$message->method('getSmsLength')->willReturn($smsLengthMock);
$this->assertSame(
@ -188,6 +153,7 @@ class SmsBinaryMessageTest extends ApiTestCase
* @test
* @throws ReflectionException
* @covers \D3\LinkmobilityClient\ValueObject\SmsBinaryMessage::length
* @covers \D3\LinkmobilityClient\ValueObject\SmsMessageAbstract::length
*/
public function testGetSize()
{

View File

@ -0,0 +1,65 @@
<?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\ValueObject;
use Assert\InvalidArgumentException;
use D3\LinkmobilityClient\Tests\ApiTestCase;
use D3\LinkmobilityClient\ValueObject\SmsBinaryMessage;
use Phlib\SmsLength\SmsLength;
use ReflectionException;
abstract class SmsMessageAbstractTest extends ApiTestCase
{
public $message;
/**
* @return void
*/
public function tearDown(): void
{
parent::tearDown();
unset($this->message);
}
/**
* @return array[]
*/
public function constructInvalidDataProvider(): array
{
return [
'empty message' => ['', true, InvalidArgumentException::class],
'invalid sms message' => ['abc', false, \Phlib\SmsLength\Exception\InvalidArgumentException::class],
];
}
/**
* @test
* @throws ReflectionException
* @covers \D3\LinkmobilityClient\ValueObject\SmsBinaryMessage::getSmsLength
* @covers \D3\LinkmobilityClient\ValueObject\SmsTextMessage::getSmsLength
*/
public function testGetSmsLengthInstance()
{
$this->assertInstanceOf(
SmsLength::class,
$this->callMethod(
$this->message,
'getSmsLength'
)
);
}
}

View File

@ -21,7 +21,7 @@ use Phlib\SmsLength\SmsLength;
use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException;
class SmsTextMessageTest extends SmsBinaryMessageTest
class SmsTextMessageTest extends SmsMessageAbstractTest
{
/** @var SmsTextMessage */
public $message;
@ -43,6 +43,7 @@ class SmsTextMessageTest extends SmsBinaryMessageTest
* @return void
* @throws ReflectionException
* @covers \D3\LinkmobilityClient\ValueObject\SmsTextMessage::__construct
* @covers \D3\LinkmobilityClient\ValueObject\SmsMessageAbstract::__construct
*/
public function testConstructValid()
{
@ -80,6 +81,7 @@ class SmsTextMessageTest extends SmsBinaryMessageTest
* @throws ReflectionException
* @dataProvider constructInvalidDataProvider
* @covers \D3\LinkmobilityClient\ValueObject\SmsTextMessage::__construct
* @covers \D3\LinkmobilityClient\ValueObject\SmsMessageAbstract::__construct
*/
public function testConstructInvalid($binaryMessage, $valid, $expectedException)
{
@ -112,4 +114,104 @@ class SmsTextMessageTest extends SmsBinaryMessageTest
)
);
}
/**
* @test
* @throws ReflectionException
* @covers \D3\LinkmobilityClient\ValueObject\SmsTextMessage::chunkCount
* @covers \D3\LinkmobilityClient\ValueObject\SmsMessageAbstract::chunkCount
*/
public function testGetChunkCount()
{
$expected = 2;
/** @var SmsLength|MockObject $smsLengthMock */
$smsLengthMock = $this->getMockBuilder(SmsLength::class)
->onlyMethods(['getMessageCount', 'validate'])
->disableOriginalConstructor()
->getMock();
$smsLengthMock->expects($this->once())->method('getMessageCount')->willReturn($expected);
$smsLengthMock->method('validate')->willReturn(true);
/** @var SmsTextMessage|MockObject $message */
$message = $this->getMockBuilder(SmsTextMessage::class)
->onlyMethods(['getSmsLength'])
->disableOriginalConstructor()
->getMock();
$message->method('getSmsLength')->willReturn($smsLengthMock);
$this->assertSame(
$expected,
$this->callMethod(
$message,
'chunkCount'
)
);
}
/**
* @test
* @throws ReflectionException
* @covers \D3\LinkmobilityClient\ValueObject\SmsTextMessage::length
* @covers \D3\LinkmobilityClient\ValueObject\SmsMessageAbstract::length
*/
public function testGetSize()
{
$expected = 55;
/** @var SmsLength|MockObject $smsLengthMock */
$smsLengthMock = $this->getMockBuilder(SmsLength::class)
->onlyMethods(['getSize', 'validate'])
->disableOriginalConstructor()
->getMock();
$smsLengthMock->expects($this->once())->method('getSize')->willReturn($expected);
$smsLengthMock->method('validate')->willReturn(true);
/** @var SmsTextMessage|MockObject $message */
$message = $this->getMockBuilder(SmsTextMessage::class)
->onlyMethods(['getSmsLength'])
->disableOriginalConstructor()
->getMock();
$message->method('getSmsLength')->willReturn($smsLengthMock);
$this->assertSame(
$expected,
$this->callMethod(
$message,
'length'
)
);
}
/**
* @test
* @throws ReflectionException
* @covers \D3\LinkmobilityClient\ValueObject\SmsTextMessage::getMessageContent
* @covers \D3\LinkmobilityClient\ValueObject\SmsMessageAbstract::getMessageContent
*/
public function testGetMessageContent()
{
/** @var SmsLength|MockObject $smsLengthMock */
$smsLengthMock = $this->getMockBuilder(SmsLength::class)
->onlyMethods(['validate'])
->disableOriginalConstructor()
->getMock();
$smsLengthMock->method('validate')->willReturn(true);
/** @var SmsTextMessage|MockObject $message */
$message = $this->getMockBuilder(SmsTextMessage::class)
->onlyMethods(['getSmsLength'])
->disableOriginalConstructor()
->getMock();
$message->method('getSmsLength')->willReturn($smsLengthMock);
$message->__construct($this->messageFixture);
$this->assertSame(
'testMessage',
$this->callMethod(
$message,
'getMessageContent'
)
);
}
}

View File

@ -26,11 +26,13 @@ use ReflectionException;
class ValueObjectTest extends ApiTestCase
{
/** @var ValueObject|MockObject */
public $value;
public function setUp(): void
{
parent::setUp();
/** @var ValueObject|MockObject value */
$this->value = $this->getMockBuilder(ValueObject::class)
->disableOriginalConstructor()
->getMock();

View File

@ -1,7 +1,7 @@
{
"name": "d3/linkmobility-php-client",
"type": "library",
"description": "PHP client for LINK Mobility Austria API",
"description": "PHP client for LINK Mobility API",
"keywords": [
"LINK Mobility",
"php",
@ -21,18 +21,20 @@
"GPL-3.0-or-later"
],
"require": {
"php": "^7.3 || ^8.0",
"php": "^7.3 || ^8.0.3",
"beberlei/assert": "^3.3",
"guzzlehttp/guzzle": "^7.4",
"psr/http-message": "~1.0",
"guzzlehttp/guzzle": "^7.3",
"psr/http-message": "^1.0 || ^2.0",
"phlib/sms-length": "^2.0",
"giggsey/libphonenumber-for-php": "^8.12.50",
"caseyamcl/guzzle_retry_middleware": "^2.8",
"ext-json": "*"
},
"require-dev": {
"php": "^7.4",
"php": "^7.4 || ~8.0.3",
"phpunit/phpunit" : "^9.5",
"friendsofphp/php-cs-fixer": "^3.9"
"friendsofphp/php-cs-fixer": "^3.9",
"phpstan/phpstan": "^1.8"
},
"autoload": {
"psr-4": {
@ -41,8 +43,9 @@
}
},
"scripts": {
"test": "phpunit",
"test": "./vendor/bin/phpunit",
"check-style": "./vendor/bin/php-cs-fixer fix --verbose --dry-run",
"fix-style": "./vendor/bin/php-cs-fixer fix --verbose"
"fix-style": "./vendor/bin/php-cs-fixer fix --verbose",
"check-code": "./vendor/bin/phpstan analyse -c phpstan.neon --no-progress --ansi"
}
}

6
phpstan.neon Normal file
View File

@ -0,0 +1,6 @@
parameters:
paths:
- src
- Tests
level: 5
phpVersion: 70300

View File

@ -15,14 +15,17 @@ declare(strict_types=1);
namespace D3\LinkmobilityClient;
use D3\LinkmobilityClient\Exceptions\ApiException;
use D3\LinkmobilityClient\Exceptions\ExceptionMessages;
use Assert\InvalidArgumentException;
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 InvalidArgumentException;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\MessageFormatter;
use GuzzleHttp\Middleware;
use GuzzleRetry\GuzzleRetryMiddleware;
use Psr\Http\Message\ResponseInterface;
class Client
@ -35,14 +38,28 @@ 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
{
$handlerStack = HandlerStack::create();
$handlerStack->push($this->getLoggerMiddleware());
$handlerStack->push($this->getRetryMiddleware());
return new GuzzleClient( [
'base_uri' => $this->apiUrl->getBaseUri(),
'handler' => $handlerStack
]);
}
/**
* @param RequestInterface $request
*
* @return Response\ResponseInterface
* @throws ApiException
* @throws GuzzleException
* @throws InvalidArgumentException
*/
@ -61,30 +78,14 @@ class Client
* @param array $options
*
* @return ResponseInterface
* @throws ApiException
* @throws GuzzleException
*/
protected function rawRequest($url, string $method = RequestInterface::METHOD_GET, array $options = []): ResponseInterface
{
$options['headers']['Authorization'] = 'Bearer '.$this->accessToken;
$this->getLoggerHandler()->getLogger()->debug('linkmobility request: '.$url, $options);
$response = $this->requestClient->request(
$method,
$url,
$options
);
if ($response->getStatusCode() != 200) {
$message = sprintf(ExceptionMessages::NOK_REQUEST_RETURN, $url, $response->getStatusCode());
$response->getBody()->rewind();
$this->getLoggerHandler()->getLogger()->error($message, [$response->getBody()->getContents()]);
throw new ApiException($message);
}
$response = $this->requestClient->request($method, $url, $options);
$response->getBody()->rewind();
$this->getLoggerHandler()->getLogger()->debug('response', [$response->getBody()->getContents()]);
return $response;
}
@ -96,4 +97,28 @@ class Client
{
return LoggerHandler::getInstance();
}
/**
* @param string $loglevel
*
* @return callable
*/
protected function getLoggerMiddleware(string $loglevel = 'debug'): callable
{
return Middleware::log(
$this->getLoggerHandler()->getLogger(),
new MessageFormatter(MessageFormatter::DEBUG),
$loglevel
);
}
/**
* @return callable
*/
protected function getRetryMiddleware(): callable
{
return GuzzleRetryMiddleware::factory([
'max_retry_attempts' => 3
]);
}
}

View File

@ -1,20 +0,0 @@
<?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 ApiException extends LinkmobilityException
{
}

View File

@ -20,10 +20,14 @@ use Psr\Log\NullLogger;
class LoggerHandler
{
/** @var LoggerHandler */
private static $instance = null;
private $logger;
public static function getInstance(): ?LoggerHandler
/**
* @return LoggerHandler
*/
public static function getInstance(): LoggerHandler
{
if (self::$instance === null) {
self::$instance = new self();
@ -38,7 +42,7 @@ class LoggerHandler
}
/**
* @param mixed $logger
* @param LoggerInterface $logger
*/
public function setLogger(LoggerInterface $logger)
{

View File

@ -31,9 +31,15 @@ class RecipientsList implements RecipientsListInterface, Iterator
*/
private $recipients = [];
public function __construct(Client $client)
/**
* @deprecated unused client parameter will remove
* @param Client|null $client
*/
public function __construct(Client $client = null)
{
$this->setClient($client);
if ($client) {
$this->setClient( $client );
}
}
/**
@ -113,6 +119,7 @@ class RecipientsList implements RecipientsListInterface, Iterator
}
/**
* @deprecated
* @return Client
*/
public function getClient(): Client
@ -121,6 +128,7 @@ class RecipientsList implements RecipientsListInterface, Iterator
}
/**
* @deprecated
* @param Client $client
*
* @return RecipientsList

View File

@ -20,6 +20,10 @@ use D3\LinkmobilityClient\ValueObject\Recipient;
interface RecipientsListInterface
{
/**
* @deprecated unused client parameter will remove
* @param Client $client
*/
public function __construct(Client $client);
public function add(Recipient $recipient): RecipientsListInterface;
@ -27,4 +31,6 @@ interface RecipientsListInterface
public function clearRecipents(): RecipientsListInterface;
public function getRecipients(): array;
public function getRecipientsList(): array;
}

View File

@ -16,6 +16,7 @@ declare(strict_types=1);
namespace D3\LinkmobilityClient\Request;
use Assert\Assert;
use Assert\InvalidArgumentException;
use D3\LinkmobilityClient\Client;
use D3\LinkmobilityClient\Exceptions\ExceptionMessages;
use D3\LinkmobilityClient\RecipientsList\RecipientsList;
@ -25,9 +26,7 @@ use D3\LinkmobilityClient\ValueObject\Recipient;
use D3\LinkmobilityClient\ValueObject\Sender;
use D3\LinkmobilityClient\ValueObject\SmsMessageAbstract;
use D3\LinkmobilityClient\ValueObject\SmsMessageInterface;
use D3\LinkmobilityClient\ValueObject\StringValueObject;
use GuzzleHttp\RequestOptions;
use InvalidArgumentException;
abstract class Request implements RequestInterface
{
@ -107,7 +106,7 @@ abstract class Request implements RequestInterface
private $test = false;
/**
* @var bool
* @var int
*/
private $maxSmsPerMessage = 0;
@ -119,8 +118,6 @@ abstract class Request implements RequestInterface
$this->recipientsList = new RecipientsList($client);
$this->setMessage($message);
$this->setClient($client);
return $this;
}
/**
@ -207,11 +204,11 @@ abstract class Request implements RequestInterface
}
/**
* @param StringValueObject $message
* @param SmsMessageInterface $message
*
* @return $this
*/
public function setMessage(StringValueObject $message): Request
public function setMessage(SmsMessageInterface $message): Request
{
$this->message = $message;
@ -422,7 +419,7 @@ abstract class Request implements RequestInterface
}
/**
* @return bool|null
* @return bool
*/
public function doSendAsFlashSms(): bool
{

View File

@ -15,14 +15,16 @@ declare(strict_types=1);
namespace D3\LinkmobilityClient\Request;
use Assert\InvalidArgumentException;
use D3\LinkmobilityClient\Client;
use D3\LinkmobilityClient\RecipientsList\RecipientsListInterface;
use D3\LinkmobilityClient\ValueObject\SmsMessageInterface;
use InvalidArgumentException;
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
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';
@ -41,10 +43,20 @@ interface RequestInterface
public const SENDERADDRESSTYPE_INTERNATIONAL = 'international';
public const SENDERADDRESSTYPE_ALPHANUMERIC = 'alphanumeric';
public const SENDERADDRESSTYPE_SHORTCODE = 'shortcode';
// @codeCoverageIgnoreEnd
/**
* @param SmsMessageInterface $message
* @param Client $client
*/
public function __construct(SmsMessageInterface $message, Client $client);
public function setMethod(string $method);
/**
* @param string $method
*
* @return Request
*/
public function setMethod(string $method): Request;
/**
* Must return the HTTP verb for this request, i.e. GET, POST, PUT
@ -53,6 +65,18 @@ interface RequestInterface
*/
public function getMethod(): string;
/**
* @param bool $test
*
* @return Request
*/
public function setTestMode(bool $test): Request;
/**
* @return bool
*/
public function getTestMode(): bool;
/**
* Must return the URI for the request with a leading slash, i.e. /messages.json
*
@ -81,6 +105,11 @@ interface RequestInterface
*/
public function getResponseInstance(PsrResponseInterface $rawResponse): LMResponseInterface;
/**
* @return RecipientsListInterface
*/
public function getRecipientsList(): RecipientsListInterface;
/**
* Must return the options for this request. If there are none, return [] (empty array)
*

View File

@ -16,10 +16,10 @@ declare(strict_types=1);
namespace D3\LinkmobilityClient\SMS;
use Assert\Assert;
use Assert\InvalidArgumentException;
use D3\LinkmobilityClient\Request\Request;
use D3\LinkmobilityClient\Url\Url;
use D3\LinkmobilityClient\ValueObject\SmsBinaryMessage;
use InvalidArgumentException;
class BinaryRequest extends Request implements SmsRequestInterface
{

View File

@ -16,10 +16,10 @@ declare(strict_types=1);
namespace D3\LinkmobilityClient\SMS;
use Assert\Assert;
use Assert\InvalidArgumentException;
use D3\LinkmobilityClient\Request\Request;
use D3\LinkmobilityClient\Url\Url;
use D3\LinkmobilityClient\ValueObject\SmsTextMessage;
use InvalidArgumentException;
class TextRequest extends Request implements SmsRequestInterface
{

View File

@ -16,6 +16,7 @@ declare(strict_types=1);
namespace D3\LinkmobilityClient\ValueObject;
use Assert\Assert;
use Assert\InvalidArgumentException;
use D3\LinkmobilityClient\Exceptions\ExceptionMessages;
use D3\LinkmobilityClient\Exceptions\RecipientException;
use libphonenumber\NumberParseException;
@ -44,6 +45,7 @@ class Recipient extends StringValueObject
*
* @throws NumberParseException
* @throws RecipientException
* @throws InvalidArgumentException
*/
public function __construct(string $number, string $iso2CountryCode)
{

View File

@ -16,6 +16,7 @@ declare(strict_types=1);
namespace D3\LinkmobilityClient\ValueObject;
use Assert\Assert;
use Assert\InvalidArgumentException;
use D3\LinkmobilityClient\Exceptions\ExceptionMessages;
use D3\LinkmobilityClient\Exceptions\NoSenderDefinedException;
use D3\LinkmobilityClient\Exceptions\RecipientException;
@ -32,6 +33,7 @@ class Sender extends ValueObject
*
* @throws NumberParseException
* @throws RecipientException
* @throws InvalidArgumentException
*/
public function __construct(string $number = null, string $iso2CountryCode = null)
{

View File

@ -15,7 +15,7 @@ declare(strict_types=1);
namespace D3\LinkmobilityClient\ValueObject;
use InvalidArgumentException;
use Assert\InvalidArgumentException;
class SmsTextMessage extends SmsMessageAbstract
{

View File

@ -16,11 +16,16 @@ declare(strict_types=1);
namespace D3\LinkmobilityClient\ValueObject;
use Assert\Assert;
use Assert\InvalidArgumentException;
abstract class ValueObject
{
protected $value;
/**
* @param string $number
* @throws InvalidArgumentException
*/
public function __construct(string $number)
{
Assert::that($number)->notEmpty();