diff --git a/Tests/Response/AbstractResponse.php b/Tests/Response/AbstractResponse.php new file mode 100644 index 0000000..4fcf196 --- /dev/null +++ b/Tests/Response/AbstractResponse.php @@ -0,0 +1,216 @@ + + * @link http://www.oxidmodule.com + */ + +declare( strict_types = 1 ); + +namespace D3\LinkmobilityClient\Tests\Response; + +use D3\LinkmobilityClient\Response\Response; +use D3\LinkmobilityClient\Tests\ApiTestCase; +use PHPUnit\Framework\MockObject\MockObject; +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\StreamInterface; +use ReflectionException; + +abstract class AbstractResponse extends ApiTestCase +{ + protected $testClassName; + + /** + * @test + * @throws ReflectionException + */ + public function testConstruct() + { + /** @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->expects($this->atLeastOnce())->method('getContents')->willReturn( + '{ + "contentCategory": "informational", + "messageContent": "fixture", + "senderAddressType": "international" + }' + ); + + /** @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); + + /** @var Response $response */ + $response = new $this->testClassName($rawResponseMock); + + $this->assertSame( + $rawResponseMock, + $this->callMethod( + $response, + 'getRawResponse' + ) + ); + + $this->assertSame( + [ + "contentCategory" => "informational", + "messageContent" => "fixture", + "senderAddressType" => "international", + ] + , + $this->callMethod( + $response, + 'getContent' + ) + ); + } + + /** + * @throws ReflectionException + */ + protected function checkProperties($expected, $propertyName, $methodName) + { + /** @var Response $response */ + $responseMock = $this->getMockBuilder($this->testClassName) + ->disableOriginalConstructor() + ->onlyMethods(['getContent']) + ->getMock(); + $responseMock->method('getContent')->willReturn([$propertyName => $expected]); + + $this->assertSame( + $expected, + $this->callMethod( + $responseMock, + $methodName + ) + ); + } + + /** + * @test + * @throws ReflectionException + */ + public function testGetInternalStatus() + { + $this->checkProperties(200, 'statusCode', 'getInternalStatus'); + } + + /** + * @test + * @throws ReflectionException + */ + public function testGetStatusMessage() + { + $this->checkProperties('statusMessage', 'statusMessage', 'getStatusMessage'); + } + + /** + * @test + * @throws ReflectionException + */ + public function testGetTransferId() + { + $this->checkProperties('transferId', 'transferId', 'getTransferId'); + } + + /** + * @test + * @throws ReflectionException + */ + public function testGetSmsCount() + { + $this->checkProperties(5, 'smsCount', 'getSmsCount'); + } + + /** + * @test + * @param $statusCode + * @param $expected + * + * @throws ReflectionException + * @dataProvider isSuccessfulDataProvider + */ + public function testIsSuccessful($statusCode, $expected) + { + /** @var Response|MockObject $responseMock */ + $responseMock = $this->getMockBuilder($this->testClassName) + ->disableOriginalConstructor() + ->onlyMethods(['getInternalStatus']) + ->getMock(); + $responseMock->method('getInternalStatus')->willReturn($statusCode); + + $this->assertSame( + $expected, + $this->callMethod( + $responseMock, + 'isSuccessful' + ) + ); + } + + /** + * @return array[] + */ + public function isSuccessfulDataProvider(): array + { + return [ + 'below 2000' => [1999, false], + 'between 2000' => [2000, true], + 'above 3000' => [3000, false], + ]; + } + + /** + * @test + * @param $successful + * @param $expected + * + * @throws ReflectionException + * @dataProvider getErrorMessageDataProvider + */ + public function testGetErrorMessage($successful, $expected) + { + /** @var Response|MockObject $responseMock */ + $responseMock = $this->getMockBuilder($this->testClassName) + ->disableOriginalConstructor() + ->onlyMethods(['isSuccessful', 'getStatusMessage']) + ->getMock(); + $responseMock->method('isSuccessful')->willReturn($successful); + $responseMock->method('getStatusMessage')->willReturn('fixtureMessage'); + + $this->assertSame( + $expected, + $this->callMethod( + $responseMock, + 'getErrorMessage' + ) + ); + } + + /** + * @return array[] + */ + public function getErrorMessageDataProvider(): array + { + return [ + 'successful' => [true, ''], + 'not successful'=> [false, 'fixtureMessage'] + ]; + } +} \ No newline at end of file diff --git a/Tests/SMS/ResponseTest.php b/Tests/SMS/ResponseTest.php new file mode 100644 index 0000000..c36966d --- /dev/null +++ b/Tests/SMS/ResponseTest.php @@ -0,0 +1,26 @@ + + * @link http://www.oxidmodule.com + */ + +declare( strict_types = 1 ); + +namespace D3\LinkmobilityClient\Tests\SMS; + +use D3\LinkmobilityClient\SMS\Response; +use D3\LinkmobilityClient\Tests\Response\AbstractResponse; + +class ResponseTest extends AbstractResponse +{ + protected $testClassName = Response::class; +} \ No newline at end of file diff --git a/src/SMS/SmsRequestInterface.php b/src/SMS/SmsRequestInterface.php index 02bce86..9241587 100644 --- a/src/SMS/SmsRequestInterface.php +++ b/src/SMS/SmsRequestInterface.php @@ -15,4 +15,6 @@ namespace D3\LinkmobilityClient\SMS; -interface SmsRequestInterface {} \ No newline at end of file +use D3\LinkmobilityClient\Request\RequestInterface; + +interface SmsRequestInterface extends RequestInterface {} \ No newline at end of file