From e4393803bfbdd72d30d7053fa59739b8d832403d Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Wed, 4 Jan 2023 23:08:05 +0100 Subject: [PATCH] add tests for RequestFactory class --- src/Application/Model/RequestFactory.php | 14 +++- src/Config/linkmobilityphpclient.yaml | 11 +++ .../Application/Model/RequestFactoryTest.php | 75 +++++++++++++++++++ 3 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 src/tests/unit/Application/Model/RequestFactoryTest.php diff --git a/src/Application/Model/RequestFactory.php b/src/Application/Model/RequestFactory.php index 7f0004c..5f26836 100644 --- a/src/Application/Model/RequestFactory.php +++ b/src/Application/Model/RequestFactory.php @@ -18,17 +18,25 @@ namespace D3\Linkmobility4OXID\Application\Model; use D3\LinkmobilityClient\Request\RequestInterface; use D3\LinkmobilityClient\SMS\SmsRequestInterface; use D3\LinkmobilityClient\ValueObject\Sender; +use D3\TestingTools\Production\IsMockable; class RequestFactory extends \D3\LinkmobilityClient\SMS\RequestFactory { + use IsMockable; + public function getSmsRequest(): SmsRequestInterface { - $configuration = oxNew(Configuration::class); + /** @var Configuration $configuration */ + $configuration = d3GetOxidDIC()->get(Configuration::class); - $request = parent::getSmsRequest(); + /** parent call */ + $request = $this->d3CallMockableFunction([\D3\LinkmobilityClient\SMS\RequestFactory::class, 'getSmsRequest']); + + d3GetOxidDIC()->setParameter(Sender::class.'.args.number', $configuration->getSmsSenderNumber()); + d3GetOxidDIC()->setParameter(Sender::class.'.args.iso2countrycode', $configuration->getSmsSenderCountry()); $request->setTestMode($configuration->getTestMode()) ->setSenderAddress( - oxNew(Sender::class, $configuration->getSmsSenderNumber(), $configuration->getSmsSenderCountry()) + d3GetOxidDIC()->get(Sender::class) ) ->setSenderAddressType(RequestInterface::SENDERADDRESSTYPE_INTERNATIONAL); diff --git a/src/Config/linkmobilityphpclient.yaml b/src/Config/linkmobilityphpclient.yaml index 548b971..f97ef66 100644 --- a/src/Config/linkmobilityphpclient.yaml +++ b/src/Config/linkmobilityphpclient.yaml @@ -10,6 +10,8 @@ parameters: D3\LinkmobilityClient\ValueObject\Recipient.args.number: D3\LinkmobilityClient\ValueObject\Recipient.args.iso2countrycode: + D3\LinkmobilityClient\ValueObject\Sender.args.number: + D3\LinkmobilityClient\ValueObject\Sender.args.iso2countrycode: services: _defaults: @@ -32,6 +34,15 @@ services: - '%D3\LinkmobilityClient\ValueObject\Recipient.args.number%' - '%D3\LinkmobilityClient\ValueObject\Recipient.args.iso2countrycode%' + D3\LinkmobilityClient\ValueObject\Sender: + class: D3\LinkmobilityClient\ValueObject\Sender + factory: 'oxNew' + shared: false + arguments: + - D3\LinkmobilityClient\ValueObject\Sender + - '%D3\LinkmobilityClient\ValueObject\Sender.args.number%' + - '%D3\LinkmobilityClient\ValueObject\Sender.args.iso2countrycode%' + D3\LinkmobilityClient\Exceptions\RecipientException: class: D3\LinkmobilityClient\Exceptions\RecipientException factory: 'oxNew' diff --git a/src/tests/unit/Application/Model/RequestFactoryTest.php b/src/tests/unit/Application/Model/RequestFactoryTest.php new file mode 100644 index 0000000..3e95b5c --- /dev/null +++ b/src/tests/unit/Application/Model/RequestFactoryTest.php @@ -0,0 +1,75 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\Linkmobility4OXID\tests\unit\Application\Model; + +use D3\Linkmobility4OXID\Application\Model\Configuration; +use D3\Linkmobility4OXID\Application\Model\RequestFactory; +use D3\Linkmobility4OXID\tests\unit\LMUnitTestCase; +use D3\LinkmobilityClient\SMS\SmsRequestInterface; +use D3\LinkmobilityClient\SMS\TextRequest; +use D3\LinkmobilityClient\ValueObject\Sender; +use D3\TestingTools\Development\CanAccessRestricted; +use PHPUnit\Framework\MockObject\MockObject; +use ReflectionException; + +class RequestFactoryTest extends LMUnitTestCase +{ + use CanAccessRestricted; + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\Linkmobility4OXID\Application\Model\RequestFactory::getSmsRequest + */ + public function canGetSmsRequest() + { + /** @var Configuration|MockObject $configurationMock */ + $configurationMock = $this->getMockBuilder(Configuration::class) + ->onlyMethods(['getTestMode', 'getSmsSenderNumber', 'getSmsSenderCountry']) + ->getMock(); + $configurationMock->expects($this->once())->method('getTestMode')->willReturn(true); + $configurationMock->expects($this->once())->method('getSmsSenderNumber')->willReturn('01512 3456789'); + $configurationMock->expects($this->once())->method('getSmsSenderCountry')->willReturn('DE'); + d3GetOxidDIC()->set(Configuration::class, $configurationMock); + + /** @var Sender|MockObject $senderMock */ + $senderMock = $this->getMockBuilder(Sender::class) + ->disableOriginalConstructor() + ->getMock(); + d3GetOxidDIC()->set(Sender::class, $senderMock); + + /** @var TextRequest|MockObject $textRequestMock */ + $textRequestMock = $this->getMockBuilder(TextRequest::class) + ->disableOriginalConstructor() + ->getMock(); + + /** @var RequestFactory|MockObject $sut */ + $sut = $this->getMockBuilder(RequestFactory::class) + ->disableOriginalConstructor() + ->onlyMethods(['d3CallMockableFunction']) + ->getMock(); + $sut->method('d3CallMockableFunction')->willReturn($textRequestMock); + + $this->assertInstanceOf( + SmsRequestInterface::class, + $this->callMethod( + $sut, + 'getSmsRequest' + ) + ); + } +} \ No newline at end of file