add test for RequestFactory

This commit is contained in:
Daniel Seifert 2022-07-11 23:30:44 +02:00
bovenliggende 67291c19da
commit 81236bcf2c
Getekend door: DanielS
GPG sleutel-ID: 6A513E13AEE66170
2 gewijzigde bestanden met toevoegingen van 144 en 2 verwijderingen

Bestand weergeven

@ -0,0 +1,135 @@
<?php
/**
* This Software is the property of Data Development and is protected
* by copyright law - it is NOT Freeware.
* Any unauthorized use of this software without a valid license
* is a violation of the license agreement and will be prosecuted by
* civil and criminal law.
* http://www.shopmodule.com
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* @link http://www.oxidmodule.com
*/
declare( strict_types = 1 );
namespace D3\LinkmobilityClient\Tests\SMS;
use D3\LinkmobilityClient\Client;
use D3\LinkmobilityClient\SMS\BinaryRequest;
use D3\LinkmobilityClient\SMS\RequestFactory;
use D3\LinkmobilityClient\SMS\TextRequest;
use D3\LinkmobilityClient\Tests\ApiTestCase;
use Phlib\SmsLength\SmsLength;
use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException;
class RequestFactoryTest extends ApiTestCase
{
/**
* @test
* @return void
* @throws ReflectionException
*/
public function testConstruct()
{
$message = 'fixtureMessage';
/** @var Client|MockObject $clientMock */
$clientMock = $this->getMockBuilder(Client::class)
->disableOriginalConstructor()
->getMock();
$requestFactory = new RequestFactory($message, $clientMock);
$this->assertSame(
$message,
$this->getValue($requestFactory, 'message')
);
$this->assertSame(
$clientMock,
$this->getValue($requestFactory, 'client')
);
}
/**
* @test
* @param $encoding
* @param $expectedClass
* @return void
* @throws ReflectionException
* @dataProvider getTextSmsRequestDataProvider
*/
public function testGetTextSmsRequest($encoding, $expectedClass)
{
$message = 'fixtureMessage';
/** @var Client|MockObject $clientMock */
$clientMock = $this->getMockBuilder(Client::class)
->disableOriginalConstructor()
->getMock();
/** @var SmsLength|MockObject $smsLengthMock */
$smsLengthMock = $this->getMockBuilder(SmsLength::class)
->onlyMethods(['getEncoding'])
->disableOriginalConstructor()
->getMock();
$smsLengthMock->method('getEncoding')->willReturn($encoding);
/** @var RequestFactory|MockObject $requestFactoryMock */
$requestFactoryMock = $this->getMockBuilder(RequestFactory::class)
->setConstructorArgs([$message, $clientMock])
->onlyMethods(['getSmsLength'])
->getMock();
$requestFactoryMock->method('getSmsLength')->willReturn($smsLengthMock);
$this->assertInstanceOf(
$expectedClass,
$this->callMethod(
$requestFactoryMock,
'getSmsRequest'
)
);
}
/**
* @return array[]
*/
public function getTextSmsRequestDataProvider(): array
{
return [
'binary' => [RequestFactory::GSM_UCS2, BinaryRequest::class],
'ascii' => [RequestFactory::GSM_7BIT, TextRequest::class]
];
}
/**
* @test
* @return void
* @throws ReflectionException
*/
public function testGetSmsLengthInstance()
{
$message = 'fixtureMessage';
/** @var Client|MockObject $clientMock */
$clientMock = $this->getMockBuilder(Client::class)
->disableOriginalConstructor()
->getMock();
/** @var RequestFactory|MockObject $requestFactoryMock */
$requestFactoryMock = $this->getMockBuilder(RequestFactory::class)
->setConstructorArgs([$message, $clientMock])
->getMock();
$this->assertInstanceOf(
SmsLength::class,
$this->callMethod(
$requestFactoryMock,
'getSmsLength'
)
);
}
}

Bestand weergeven

@ -48,8 +48,7 @@ class RequestFactory
*/
public function getSmsRequest() : SmsRequestInterface
{
$smsLength = new SmsLength($this->message);
if ($smsLength->getEncoding() === self::GSM_7BIT) {
if ($this->getSmsLength()->getEncoding() === self::GSM_7BIT) {
$message = new SmsTextMessage($this->message);
return new TextRequest($message, $this->client);
}
@ -57,4 +56,12 @@ class RequestFactory
$message = new SmsBinaryMessage($this->message);
return new BinaryRequest($message, $this->client);
}
/**
* @return SmsLength
*/
protected function getSmsLength(): SmsLength
{
return new SmsLength($this->message);
}
}