linkmobility-php-client/Tests/Response/AbstractResponse.php

233 lignes
6.8 KiB
PHP
Brut Vue normale Historique

2022-07-12 14:54:06 +02:00
<?php
/**
2022-07-13 10:50:45 +02:00
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* https://www.d3data.de
2022-07-12 14:54:06 +02:00
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
2022-07-13 10:50:45 +02:00
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* @link https://www.oxidmodule.com
2022-07-12 14:54:06 +02:00
*/
2022-07-13 10:41:23 +02:00
declare(strict_types=1);
2022-07-12 14:54:06 +02:00
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
* @covers \D3\LinkmobilityClient\SMS\Response::__construct
* @covers \D3\LinkmobilityClient\SMS\Response::getRawResponse
* @covers \D3\LinkmobilityClient\SMS\Response::getContent
2022-07-12 14:54:06 +02:00
*/
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', ])
2022-07-12 14:54:06 +02:00
->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', ])
2022-07-12 14:54:06 +02:00
->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",
2022-07-13 10:41:23 +02:00
],
2022-07-12 14:54:06 +02:00
$this->callMethod(
$response,
'getContent'
)
);
}
/**
* @throws ReflectionException
*/
protected function checkProperties($expected, $propertyName, $methodName)
{
2022-07-21 00:33:29 +02:00
/** @var Response|MockObject $responseMock */
2022-07-12 14:54:06 +02:00
$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
* @covers \D3\LinkmobilityClient\SMS\Response::getInternalStatus
2022-07-12 14:54:06 +02:00
*/
public function testGetInternalStatus()
{
$this->checkProperties(200, 'statusCode', 'getInternalStatus');
}
/**
* @test
* @throws ReflectionException
* @covers \D3\LinkmobilityClient\SMS\Response::getStatusMessage
2022-07-12 14:54:06 +02:00
*/
public function testGetStatusMessage()
{
$this->checkProperties('statusMessage', 'statusMessage', 'getStatusMessage');
}
2022-07-16 00:42:41 +02:00
/**
* @test
* @throws ReflectionException
* @covers \D3\LinkmobilityClient\SMS\Response::getClientMessageId
2022-07-16 00:42:41 +02:00
*/
public function testGetClientMessageId()
{
$this->checkProperties('clientMessageId', 'clientMessageId', 'getClientMessageId');
}
2022-07-12 14:54:06 +02:00
/**
* @test
* @throws ReflectionException
* @covers \D3\LinkmobilityClient\SMS\Response::getTransferId
2022-07-12 14:54:06 +02:00
*/
public function testGetTransferId()
{
$this->checkProperties('transferId', 'transferId', 'getTransferId');
}
/**
* @test
* @throws ReflectionException
* @covers \D3\LinkmobilityClient\SMS\Response::getSmsCount
2022-07-12 14:54:06 +02:00
*/
public function testGetSmsCount()
{
$this->checkProperties(5, 'smsCount', 'getSmsCount');
}
/**
* @test
* @param $statusCode
* @param $expected
*
* @throws ReflectionException
* @dataProvider isSuccessfulDataProvider
* @covers \D3\LinkmobilityClient\SMS\Response::isSuccessful
2022-07-12 14:54:06 +02:00
*/
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
* @covers \D3\LinkmobilityClient\SMS\Response::getErrorMessage
2022-07-12 14:54:06 +02:00
*/
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'],
2022-07-12 14:54:06 +02:00
];
}
2022-07-13 10:41:23 +02:00
}