diff --git a/Tests/Request/AbstractRequest.php b/Tests/Request/AbstractRequest.php new file mode 100644 index 0000000..2f96cab --- /dev/null +++ b/Tests/Request/AbstractRequest.php @@ -0,0 +1,557 @@ + + * @link http://www.oxidmodule.com + */ + +declare( strict_types = 1 ); + +namespace D3\LinkmobilityClient\Tests\Request; + +use Assert\InvalidArgumentException; +use D3\LinkmobilityClient\Client; +use D3\LinkmobilityClient\RecipientsList\RecipientsListInterface; +use D3\LinkmobilityClient\Request\Request; +use D3\LinkmobilityClient\Request\RequestInterface; +use D3\LinkmobilityClient\SMS\Response; +use D3\LinkmobilityClient\Tests\ApiTestCase; +use D3\LinkmobilityClient\ValueObject\Recipient; +use D3\LinkmobilityClient\ValueObject\Sender; +use PHPUnit\Framework\MockObject\MockObject; +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\StreamInterface; +use ReflectionException; + +abstract class AbstractRequest extends ApiTestCase +{ + /** @var Request */ + protected $request; + protected $testClassName; + protected $messageClassName; + + public function setUp(): void + { + parent::setUp(); + + $messageMock = $this->getMockBuilder($this->messageClassName) + ->disableOriginalConstructor() + ->getMock(); + /** @var Client|MockObject $clientMock */ + $clientMock = $this->getMockBuilder(Client::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->request = new $this->testClassName($messageMock, $clientMock); + } + + public function tearDown(): void + { + parent::tearDown(); + + unset($this->request); + } + + /** + * @test + * @return void + * @throws ReflectionException + */ + public function testConstruct() + { + $messageMock = $this->getMockBuilder($this->messageClassName) + ->disableOriginalConstructor() + ->getMock(); + + /** @var Client|MockObject $clientMock */ + $clientMock = $this->getMockBuilder(Client::class) + ->disableOriginalConstructor() + ->getMock(); + + /** @var Request|MockObject $requestMock */ + $requestMock = $this->getMockBuilder($this->testClassName) + ->disableOriginalConstructor() + ->onlyMethods(['setMessage', 'setClient']) + ->getMock(); + $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] + ) + ); + } + + /** + * @test + * @throws ReflectionException + */ + public function validatePassedTest() + { + $recipient = new Recipient('015792300219', 'DE'); + + /** @var Request|MockObject $requestMock */ + $requestMock = $this->getMockBuilder($this->testClassName) + ->setConstructorArgs([$this->request->getMessage(), $this->request->getClient()]) + ->onlyMethods(['getBody', 'getMethod', 'getUri', 'getResponseClass', 'getOptions']) + ->getMock(); + $requestMock->expects($this->atLeastOnce())->method('getMethod')->willReturn(RequestInterface::METHOD_GET); + $requestMock->expects($this->atLeastOnce())->method('getBody')->willReturn(['fixture']); + $requestMock->expects($this->atLeastOnce())->method('getUri')->willReturn('/uri'); + $requestMock->expects($this->atLeastOnce())->method('getResponseClass')->willReturn(Response::class); + $requestMock->expects($this->atLeastOnce())->method('getOptions')->willReturn([]); + + $requestMock->getRecipientsList()->add($recipient); + + $this->callMethod( + $requestMock, + 'validate' + ); + } + + /** + * @test + * @throws ReflectionException + */ + public function validateFailedTest() + { + $recipient = new Recipient('015792300219', 'DE'); + + /** @var Request|MockObject $requestMock */ + $requestMock = $this->getMockBuilder($this->testClassName) + ->setConstructorArgs([$this->request->getMessage(), $this->request->getClient()]) + ->onlyMethods(['getBody', 'getMethod', 'getUri', 'getResponseClass', 'getOptions']) + ->getMock(); + $requestMock->expects($this->atLeastOnce())->method('getMethod')->willReturn('otherMethod'); + $requestMock->expects($this->any())->method('getBody')->willReturn(['fixture']); + $requestMock->expects($this->any())->method('getUri')->willReturn('/uri'); + $requestMock->expects($this->any())->method('getResponseClass')->willReturn(Response::class); + $requestMock->expects($this->any())->method('getOptions')->willReturn([]); + + $requestMock->getRecipientsList()->add($recipient); + + $this->expectException(InvalidArgumentException::class); + + $this->callMethod( + $requestMock, + 'validate' + ); + } + + /** + * @test + * @throws ReflectionException + */ + public function testGetRawBody() + { + /** @var Request|MockObject $requestMock */ + $requestMock = $this->getMockBuilder($this->testClassName) + ->setConstructorArgs([$this->request->getMessage(), $this->request->getClient()]) + ->onlyMethods(['getMessage', 'getTestMode']) + ->getMock(); + $requestMock->expects($this->atLeastOnce())->method('getMessage'); + $requestMock->expects($this->atLeastOnce())->method('getTestMode'); + + $rawBody = $this->callMethod( + $requestMock, + 'getRawBody' + ); + + $this->assertIsArray($rawBody); + $this->assertContains('messageContent', array_keys($rawBody)); + } + + /** + * @test + * @throws ReflectionException + * @dataProvider getBodyDataProvider + */ + public function testGetBody($contentType, $expected) + { + /** @var Request|MockObject $requestMock */ + $requestMock = $this->getMockBuilder($this->testClassName) + ->setConstructorArgs([$this->request->getMessage(), $this->request->getClient()]) + ->onlyMethods(['getRawBody', 'getContentType']) + ->getMock(); + $requestMock->expects($this->atLeastOnce())->method('getRawBody')->willReturn( + [ + 'clientMessageId' => null, + 'contentCategory' => 'informational', + 'messageContent' => 'messageContent', + 'notificationCallbackUrl' => null, + 'priority' => 0, + ] + ); + $requestMock->expects($this->atLeastOnce())->method('getContentType')->willReturn($contentType); + + $this->assertSame( + $expected, + $this->callMethod( + $requestMock, + 'getBody' + ) + ); + } + + /** + * @return array + */ + public function getBodyDataProvider(): array + { + return [ + 'json format' => [ + RequestInterface::CONTENTTYPE_JSON, + ['json' => [ + 'contentCategory' => 'informational', + 'messageContent' => 'messageContent', + 'priority' => 0 + ]] + ], + 'other' => [ + 'other', + [ + 'contentCategory' => 'informational', + 'messageContent' => 'messageContent', + 'priority' => 0 + ] + ] + ]; + } + + + + /** + * @test + * @throws ReflectionException + */ + public function testGetOptions() + { + /** @var Request|MockObject $requestMock */ + $requestMock = $this->getMockBuilder($this->testClassName) + ->setConstructorArgs([$this->request->getMessage(), $this->request->getClient()]) + ->onlyMethods(['getBody']) + ->getMock(); + $requestMock->expects($this->atLeastOnce())->method('getBody')->willReturn( + ['json' => [ + 'contentCategory' => 'informational', + 'messageContent' => 'messageContent', + 'priority' => 0 + ]] + ); + + $this->assertSame( + ['headers' => [ + 'Accept' => 'application/json', + 'Content-Type' => 'application/json' + ], + 'json' => [ + 'contentCategory' => 'informational', + 'messageContent' => 'messageContent', + 'priority' => 0 + ]], + $this->callMethod( + $requestMock, + 'getOptions' + ) + ); + } + + /** + * @throws ReflectionException + */ + public function checkGetterSetter($content, $setter, $getter) + { + $this->assertInstanceOf( + Request::class, + $this->callMethod( + $this->request, + $setter, + [$content] + ) + ); + + $this->assertSame( + $content, + $this->callMethod( + $this->request, + $getter + ) + ); + } + + /** + * @test + * @throws ReflectionException + */ + public function setGetMessageTest() + { + $messageMock = $this->getMockBuilder($this->messageClassName) + ->disableOriginalConstructor() + ->getMock(); + + $this->checkGetterSetter($messageMock, 'setMessage', 'getMessage'); + } + + /** + * @test + * @throws ReflectionException + */ + public function setGetMethodTest() + { + $this->checkGetterSetter('fixture', 'setMethod', 'getMethod'); + } + + /** + * @test + * @throws ReflectionException + */ + public function getMethodsTest() + { + $methods = $this->callMethod( + $this->request, + 'getMethods' + ); + + $this->assertIsArray($methods); + $this->assertTrue((bool) count($methods)); + } + + /** + * @test + * @throws ReflectionException + */ + public function setGetContentTypeTest() + { + $this->checkGetterSetter('fixture', 'setContentType', 'getContentType'); + } + + /** + * @test + * @throws ReflectionException + */ + public function setGetClientMessageIdTest() + { + $this->checkGetterSetter('fixture', 'setClientMessageId', 'getClientMessageId'); + } + + /** + * @test + * @throws ReflectionException + */ + public function setGetContentCategoryTest() + { + $this->checkGetterSetter('fixture', 'setContentCategory', 'getContentCategory'); + } + + /** + * @test + * @throws ReflectionException + */ + public function getContentCategoriesTest() + { + $contentCategories = $this->callMethod( + $this->request, + 'getContentCategories' + ); + + $this->assertIsArray($contentCategories); + $this->assertTrue((bool) count($contentCategories)); + } + + /** + * @test + * @throws ReflectionException + */ + public function setGetTestModeTest() + { + $this->checkGetterSetter(true, 'setTestMode', 'getTestMode'); + } + + /** + * @test + * @throws ReflectionException + */ + public function setGetMaxSmsPerMessageTest() + { + $this->checkGetterSetter(2, 'setMaxSmsPerMessage', 'getMaxSmsPerMessage'); + } + + /** + * @test + * @throws ReflectionException + */ + public function setGetMessageTypeTest() + { + $this->checkGetterSetter('fixture', 'setMessageType', 'getMessageType'); + } + + /** + * @test + * @throws ReflectionException + */ + public function setGetNotificationCallbackUrlTest() + { + $this->checkGetterSetter('fixture', 'setNotificationCallbackUrl', 'getNotificationCallbackUrl'); + } + + /** + * @test + * @throws ReflectionException + */ + public function setGetPriorityTest() + { + $this->checkGetterSetter(200, 'setPriority', 'getPriority'); + } + + /** + * @test + * @throws ReflectionException + */ + public function getRecipientsListTest() + { + $this->assertInstanceOf( + RecipientsListInterface::class, + $this->callMethod( + $this->request, + 'getRecipientsList' + ) + ); + } + + /** + * @test + * @throws ReflectionException + */ + public function setGetFlashSmsTest() + { + $this->checkGetterSetter(true, 'sendAsFlashSms', 'doSendAsFlashSms'); + } + + /** + * @test + * @throws ReflectionException + */ + public function setGetSenderAddressTest() + { + $senderMock = $this->getMockBuilder(Sender::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->checkGetterSetter($senderMock, 'setSenderAddress', 'getSenderAddress'); + } + + /** + * @test + * @throws ReflectionException + */ + public function setGetSenderAddressTypeTest() + { + $this->checkGetterSetter('fixture', 'setSenderAddressType', 'getSenderAddressType'); + } + + /** + * @test + * @throws ReflectionException + */ + public function getSenderAddressTypesTest() + { + $senderAddressTypes = $this->callMethod( + $this->request, + 'getSenderAddressTypes' + ); + + $this->assertIsArray($senderAddressTypes); + $this->assertTrue((bool) count($senderAddressTypes)); + } + + /** + * @test + * @throws ReflectionException + */ + public function setGetValidityPeriodeTest() + { + $this->checkGetterSetter(1, 'setValidityPeriode', 'getValidityPeriode'); + } + + /** + * @test + * @throws ReflectionException + * @dataProvider getResponseInstanceDataProvider + */ + public function testGetResponseInstance(ResponseInterface $response) + { + /** @var Request|MockObject $requestMock */ + $requestMock = $this->getMockBuilder(Request::class) + ->onlyMethods(['getResponseClass', 'getUri']) + ->disableOriginalConstructor() + ->getMock(); + $requestMock->expects($this->atLeastOnce())->method('getResponseClass')->willReturn(Response::class); + + $instance = $this->callMethod( + $requestMock, + 'getResponseInstance', + [$response] + ); + + $this->assertInstanceOf( + Response::class, + $instance + ); + $this->assertSame( + $response, + $instance->getRawResponse() + ); + } + + /** + * @return array[] + */ + public function getResponseInstanceDataProvider(): array + { + /** @var StreamInterface|MockObject $streamMock */ + $streamMock = $this->getMockBuilder(StreamInterface::class) + ->onlyMethods(['getContents', '__toString', 'close', 'detach', 'getSize', 'tell', 'eof', 'isSeekable', + 'seek', 'rewind', 'isWritable', 'write', 'isReadable', 'read', 'getMetadata']) + ->getMock(); + $streamMock->method('getContents')->willReturn('{}'); + + /** @var ResponseInterface|MockObject $rawResponseMock */ + $rawResponseMock = $this->getMockBuilder(ResponseInterface::class) + ->onlyMethods([ + 'getBody', 'getStatusCode', 'withStatus', 'getReasonphrase', 'getProtocolVersion', + 'withProtocolVersion', 'getHeaders', 'hasHeader', 'getHeader', 'getHeaderLine', + 'withHeader', 'withAddedHeader', 'withoutHeader', 'withBody']) + ->getMock(); + $rawResponseMock->method('getBody')->willReturn($streamMock); + + return [ + 'SMS Response' => [$rawResponseMock] + ]; + } + + /** + * @test + * @throws ReflectionException + */ + public function setGetClientTest() + { + /** @var Client|MockObject $clientMock */ + $clientMock = $this->getMockBuilder(Client::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->checkGetterSetter($clientMock, 'setClient', 'getClient'); + } +} \ No newline at end of file diff --git a/Tests/SMS/BinaryRequestTest.php b/Tests/SMS/BinaryRequestTest.php new file mode 100644 index 0000000..5e15ac6 --- /dev/null +++ b/Tests/SMS/BinaryRequestTest.php @@ -0,0 +1,28 @@ + + * @link http://www.oxidmodule.com + */ + +declare( strict_types = 1 ); + +namespace D3\LinkmobilityClient\Tests\SMS; + +use D3\LinkmobilityClient\SMS\BinaryRequest; +use D3\LinkmobilityClient\Tests\Request\AbstractRequest; +use D3\LinkmobilityClient\ValueObject\SmsBinaryMessage; + +class BinaryRequestTest extends AbstractRequest +{ + protected $testClassName = BinaryRequest::class; + protected $messageClassName = SmsBinaryMessage::class; +} \ No newline at end of file diff --git a/Tests/SMS/TextRequestTest.php b/Tests/SMS/TextRequestTest.php new file mode 100644 index 0000000..6e666f2 --- /dev/null +++ b/Tests/SMS/TextRequestTest.php @@ -0,0 +1,28 @@ + + * @link http://www.oxidmodule.com + */ + +declare( strict_types = 1 ); + +namespace D3\LinkmobilityClient\Tests\SMS; + +use D3\LinkmobilityClient\SMS\TextRequest; +use D3\LinkmobilityClient\Tests\Request\AbstractRequest; +use D3\LinkmobilityClient\ValueObject\SmsTextMessage; + +class TextRequestTest extends AbstractRequest +{ + protected $testClassName = TextRequest::class; + protected $messageClassName = SmsTextMessage::class; +} \ No newline at end of file diff --git a/src/Request/Request.php b/src/Request/Request.php index 4244136..8c9e6a4 100644 --- a/src/Request/Request.php +++ b/src/Request/Request.php @@ -169,6 +169,9 @@ abstract class Request implements RequestInterface ]; } + /** + * @return array + */ public function getBody(): array { $body = array_filter( $this->getRawBody(), function( $elm ) { @@ -183,6 +186,9 @@ abstract class Request implements RequestInterface } } + /** + * @return array + */ public function getOptions(): array { return array_merge( @@ -514,8 +520,10 @@ abstract class Request implements RequestInterface /** * @param Client $client */ - public function setClient( Client $client ) + public function setClient( Client $client ): Request { $this->client = $client; + + return $this; } } \ No newline at end of file