From 5ebe4009dd24e639a4bcc0461b6d0ad2cdd239f0 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Sat, 7 Jan 2023 22:45:24 +0100 Subject: [PATCH] add tests for SMS message type classes --- .../Model/MessageTypes/AbstractMessage.php | 6 +- src/Application/Model/MessageTypes/Sms.php | 187 ++-- src/Config/linkmobility4oxid.yaml | 20 + src/Config/linkmobilityphpclient.yaml | 11 + src/Config/oxid.yaml | 12 +- .../Model/MessageTypes/SmsTest.php | 818 ++++++++++++++++++ 6 files changed, 992 insertions(+), 62 deletions(-) create mode 100644 src/tests/unit/Application/Model/MessageTypes/SmsTest.php diff --git a/src/Application/Model/MessageTypes/AbstractMessage.php b/src/Application/Model/MessageTypes/AbstractMessage.php index e315666..4247d6a 100644 --- a/src/Application/Model/MessageTypes/AbstractMessage.php +++ b/src/Application/Model/MessageTypes/AbstractMessage.php @@ -17,6 +17,7 @@ namespace D3\Linkmobility4OXID\Application\Model\MessageTypes; use D3\LinkmobilityClient\RecipientsList\RecipientsList; use D3\LinkmobilityClient\Response\ResponseInterface; +use D3\LinkmobilityClient\ValueObject\Recipient; use Exception; use OxidEsales\Eshop\Application\Model\Remark; @@ -65,7 +66,7 @@ abstract class AbstractMessage */ protected function setRemark(string $userId, string $recipients, string $message): void { - $remark = oxNew(Remark::class); + $remark = d3GetOxidDIC()->get('d3ox.linkmobility.'.Remark::class); $remark->assign([ 'oxtype' => self::REMARK_IDENT, 'oxparentid' => $userId, @@ -97,6 +98,7 @@ abstract class AbstractMessage public function getRecipientsList(): string { $list = []; + /** @var Recipient $recipient */ foreach ($this->recipients->getRecipientsList() as $recipient) { $list[] = $recipient->get(); } @@ -113,7 +115,7 @@ abstract class AbstractMessage { $message = trim(strip_tags($message)); $message = $this->removeLineBreaks ? str_replace(["\r", "\n"], ' ', $message) : $message; - $regexp = '/\s{2,}/m'; + $regexp = '/[^\S\r\n]{2,}/m'; return $this->removeMultipleSpaces ? (string) preg_replace($regexp, ' ', $message) : $message; } diff --git a/src/Application/Model/MessageTypes/Sms.php b/src/Application/Model/MessageTypes/Sms.php index 9c273b5..37ffd71 100644 --- a/src/Application/Model/MessageTypes/Sms.php +++ b/src/Application/Model/MessageTypes/Sms.php @@ -22,9 +22,12 @@ use D3\Linkmobility4OXID\Application\Model\MessageClient; use D3\Linkmobility4OXID\Application\Model\OrderRecipients; use D3\Linkmobility4OXID\Application\Model\RequestFactory; use D3\Linkmobility4OXID\Application\Model\UserRecipients; +use D3\LinkmobilityClient\Client; use D3\LinkmobilityClient\Exceptions\ApiException; use D3\LinkmobilityClient\RecipientsList\RecipientsList; +use D3\LinkmobilityClient\RecipientsList\RecipientsListInterface; use D3\LinkmobilityClient\Request\RequestInterface; +use D3\LinkmobilityClient\Response\ResponseInterface; use D3\LinkmobilityClient\SMS\SmsRequestInterface; use D3\LinkmobilityClient\ValueObject\Recipient; use D3\LinkmobilityClient\ValueObject\Sender; @@ -34,6 +37,8 @@ use InvalidArgumentException; use OxidEsales\Eshop\Application\Model\Order; use OxidEsales\Eshop\Application\Model\User; use OxidEsales\Eshop\Core\Registry; +use OxidEsales\Eshop\Core\UtilsView; +use Psr\Log\LoggerInterface; class Sms extends AbstractMessage { @@ -46,24 +51,43 @@ class Sms extends AbstractMessage public function sendUserAccountMessage(User $user): bool { try { - Registry::getLogger()->debug('startRequest', ['userId' => $user->getId()]); - $return = $this->sendCustomRecipientMessage( - oxNew(RecipientsList::class, oxNew(MessageClient::class)->getClient()) - ->add(oxNew(UserRecipients::class, $user)->getSmsRecipient()) - ); + $this->getLogger()->debug('startRequest', ['userId' => $user->getId()]); + $return = $this->sendCustomRecipientMessage($this->getUserRecipientsList($user)); if ($return) { $this->setRemark($user->getId(), $this->getRecipientsList(), $this->getMessage()); } - Registry::getLogger()->debug('finishRequest', ['userId' => $user->getId()]); + $this->getLogger()->debug('finishRequest', ['userId' => $user->getId()]); return $return; } catch (noRecipientFoundException $e) { - Registry::getLogger()->warning($e->getMessage()); - Registry::getUtilsView()->addErrorToDisplay($e); + $this->getLogger()->warning($e->getMessage()); + d3GetOxidDIC()->get('d3ox.linkmobility.'.UtilsView::class)->addErrorToDisplay($e); } return false; } + /** + * @param User $user + * @return RecipientsListInterface + * @throws noRecipientFoundException + */ + protected function getUserRecipientsList(User $user): RecipientsListInterface + { + /** @var MessageClient $messageClient */ + $messageClient = d3GetOxidDIC()->get(MessageClient::class); + + d3GetOxidDIC()->set(RecipientsList::class.'.args.client', $messageClient->getClient()); + /** @var RecipientsList $recipientsList */ + $recipientsList = d3GetOxidDIC()->get(RecipientsList::class); + + d3GetOxidDIC()->set(UserRecipients::class.'.args.user', $user); + /** @var UserRecipients $userRecipients */ + $userRecipients = d3GetOxidDIC()->get(UserRecipients::class); + return $recipientsList->add( + $userRecipients->getSmsRecipient() + ); + } + /** * @param Order $order * @@ -74,20 +98,38 @@ class Sms extends AbstractMessage public function sendOrderMessage(Order $order): bool { try { - Registry::getLogger()->debug('startRequest', ['orderId' => $order->getId()]); - $return = $this->sendCustomRecipientMessage( - oxNew(RecipientsList::class, oxNew(MessageClient::class)->getClient()) - ->add($this->getOrderRecipient($order)) - ); + $this->getLogger()->debug('startRequest', ['orderId' => $order->getId()]); + $return = $this->sendCustomRecipientMessage($this->getOrderRecipientsList($order)); if ($return) { $this->setRemark($order->getOrderUser()->getId(), $this->getRecipientsList(), $this->getMessage()); } - Registry::getLogger()->debug('finishRequest', ['orderId' => $order->getId()]); + $this->getLogger()->debug('finishRequest', ['orderId' => $order->getId()]); return $return; } catch (noRecipientFoundException $e) { - Registry::getLogger()->warning($e->getMessage()); - throw $e; + $this->getLogger()->warning($e->getMessage()); + d3GetOxidDIC()->get('d3ox.linkmobility.'.UtilsView::class)->addErrorToDisplay($e); } + + return false; + } + + /** + * @param Order $order + * @return RecipientsListInterface + * @throws noRecipientFoundException + */ + protected function getOrderRecipientsList(Order $order): RecipientsListInterface + { + /** @var MessageClient $messageClient */ + $messageClient = d3GetOxidDIC()->get(MessageClient::class); + + d3GetOxidDIC()->set(RecipientsList::class.'.args.client', $messageClient->getClient()); + /** @var RecipientsList $recipientsList */ + $recipientsList = d3GetOxidDIC()->get(RecipientsList::class); + + return $recipientsList->add( + $this->getOrderRecipient(($order)) + ); } /** @@ -97,7 +139,10 @@ class Sms extends AbstractMessage */ protected function getOrderRecipient(Order $order): Recipient { - return oxNew(OrderRecipients::class, $order)->getSmsRecipient(); + d3GetOxidDIC()->set(OrderRecipients::class.'.args.order', $order); + /** @var OrderRecipients $orderRecipients */ + $orderRecipients = d3GetOxidDIC()->get(OrderRecipients::class); + return $orderRecipients->getSmsRecipient(); } /** @@ -108,54 +153,74 @@ class Sms extends AbstractMessage public function sendCustomRecipientMessage(RecipientsList $recipientsList): bool { try { - $this->setRecipients($recipientsList); - $configuration = oxNew(Configuration::class); - $client = oxNew(MessageClient::class)->getClient(); - - /** @var SmsRequestInterface $request */ - $request = oxNew(RequestFactory::class, $this->getMessage(), $client)->getSmsRequest(); - $request->setTestMode($configuration->getTestMode())->setMethod(RequestInterface::METHOD_POST) - ->setSenderAddress( - oxNew( - Sender::class, - $configuration->getSmsSenderNumber(), - $configuration->getSmsSenderCountry() - ) - ) - ->setSenderAddressType(RequestInterface::SENDERADDRESSTYPE_INTERNATIONAL); - - $requestRecipientsList = $request->getRecipientsList(); - foreach ($recipientsList as $recipient) { - $requestRecipientsList->add($recipient); - } - - $response = $client->request($request); - - $this->response = $response; - - if (false === $response->isSuccessful()) { - Registry::getLogger()->warning($response->getErrorMessage(), [$request->getBody()]); - } + $this->response = $response = $this->submitMessage($recipientsList); return $response->isSuccessful(); - } catch (abortSendingExceptionInterface $e) { - Registry::getLogger()->warning($e->getMessage()); + } catch (abortSendingExceptionInterface|GuzzleException|ApiException|InvalidArgumentException $e) { + $this->getLogger()->warning($e->getMessage()); // Oxid does not accept throwable interface only exceptions according to definition - Registry::getUtilsView()->addErrorToDisplay($e->getMessage()); - } catch (GuzzleException $e) { - Registry::getLogger()->warning($e->getMessage()); - Registry::getUtilsView()->addErrorToDisplay($e->getMessage()); - } catch (ApiException $e) { - Registry::getLogger()->warning($e->getMessage()); - Registry::getUtilsView()->addErrorToDisplay($e->getMessage()); - } catch (InvalidArgumentException $e) { - Registry::getLogger()->warning($e->getMessage()); - Registry::getUtilsView()->addErrorToDisplay($e->getMessage()); + d3GetOxidDIC()->get('d3ox.linkmobility.'.UtilsView::class)->addErrorToDisplay($e->getMessage()); } return false; } + /** + * @param Configuration $configuration + * @param Client $client + * @return SmsRequestInterface + */ + protected function getRequest(Configuration $configuration, Client $client): SmsRequestInterface + { + d3GetOxidDIC()->setParameter(RequestFactory::class.'.args.message', $this->getMessage()); + d3GetOxidDIC()->set(RequestFactory::class.'.args.client', $client); + /** @var RequestFactory $requestFactory */ + $requestFactory = d3GetOxidDIC()->get(RequestFactory::class); + + d3GetOxidDIC()->setParameter(Sender::class.'.args.number', $configuration->getSmsSenderNumber()); + d3GetOxidDIC()->setParameter(Sender::class.'.args.iso2countrycode', $configuration->getSmsSenderCountry()); + /** @var Sender $sender */ + $sender = d3GetOxidDIC()->get(Sender::class); + + /** @var SmsRequestInterface $request */ + $request = $requestFactory->getSmsRequest(); + $request->setTestMode($configuration->getTestMode())->setMethod(RequestInterface::METHOD_POST) + ->setSenderAddress($sender) + ->setSenderAddressType(RequestInterface::SENDERADDRESSTYPE_INTERNATIONAL); + + return $request; + } + + /** + * @param RecipientsList $recipientsList + * @return ResponseInterface + * @throws ApiException + * @throws GuzzleException + */ + protected function submitMessage(RecipientsList $recipientsList): ResponseInterface + { + $this->setRecipients($recipientsList); + /** @var Configuration $configuration */ + $configuration = d3GetOxidDIC()->get(Configuration::class); + /** @var MessageClient $messageClient */ + $messageClient = d3GetOxidDIC()->get(MessageClient::class); + $client = $messageClient->getClient(); + + $request = $this->getRequest($configuration, $client); + $requestRecipientsList = $request->getRecipientsList(); + foreach ($recipientsList->getRecipients() as $recipient) { + $requestRecipientsList->add($recipient); + } + + $response = $client->request($request); + + if (false === $response->isSuccessful()) { + $this->getLogger()->warning($response->getErrorMessage(), [$request->getBody()]); + } + + return $response; + } + /** * @return string */ @@ -163,4 +228,12 @@ class Sms extends AbstractMessage { return 'SMS'; } + + /** + * @return LoggerInterface + */ + public function getLogger() + { + return Registry::getLogger(); + } } diff --git a/src/Config/linkmobility4oxid.yaml b/src/Config/linkmobility4oxid.yaml index 7ee6a6e..5554cda 100644 --- a/src/Config/linkmobility4oxid.yaml +++ b/src/Config/linkmobility4oxid.yaml @@ -8,6 +8,7 @@ # @link https://www.oxidmodule.com parameters: + D3\Linkmobility4OXID\Application\Model\RequestFactory.args.message: D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms.args.message: D3\Linkmobility4OXID\Application\Model\Exceptions\successfullySentException.args.smscount: @@ -45,6 +46,25 @@ services: arguments: - D3\Linkmobility4OXID\Application\Model\Configuration + D3\Linkmobility4OXID\Application\Model\MessageClient: + class: D3\Linkmobility4OXID\Application\Model\MessageClient + factory: 'oxNew' + shared: true + arguments: + - D3\Linkmobility4OXID\Application\Model\MessageClient + + D3\Linkmobility4OXID\Application\Model\RequestFactory: + class: D3\Linkmobility4OXID\Application\Model\RequestFactory + factory: 'oxNew' + shared: true + arguments: + - D3\Linkmobility4OXID\Application\Model\RequestFactory + - '%D3\Linkmobility4OXID\Application\Model\RequestFactory.args.message%' + - '@D3\Linkmobility4OXID\Application\Model\RequestFactory.args.client' + D3\Linkmobility4OXID\Application\Model\RequestFactory.args.client: + synthetic: true + shared: false + D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms: class: D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms factory: 'oxNew' diff --git a/src/Config/linkmobilityphpclient.yaml b/src/Config/linkmobilityphpclient.yaml index 2444d1c..1ae2eff 100644 --- a/src/Config/linkmobilityphpclient.yaml +++ b/src/Config/linkmobilityphpclient.yaml @@ -43,6 +43,17 @@ services: - '%D3\LinkmobilityClient\ValueObject\Recipient.args.number%' - '%D3\LinkmobilityClient\ValueObject\Recipient.args.iso2countrycode%' + D3\LinkmobilityClient\RecipientsList\RecipientsList: + class: D3\LinkmobilityClient\RecipientsList\RecipientsList + factory: 'oxNew' + shared: false + arguments: + - D3\LinkmobilityClient\RecipientsList\RecipientsList + - '@D3\LinkmobilityClient\RecipientsList\RecipientsList.args.client' + D3\LinkmobilityClient\RecipientsList\RecipientsList.args.client: + synthetic: true + shared: false + D3\LinkmobilityClient\ValueObject\Sender: class: D3\LinkmobilityClient\ValueObject\Sender factory: 'oxNew' diff --git a/src/Config/oxid.yaml b/src/Config/oxid.yaml index 8d2912d..82b06b0 100644 --- a/src/Config/oxid.yaml +++ b/src/Config/oxid.yaml @@ -37,9 +37,7 @@ services: class: 'OxidEsales\Eshop\Core\UtilsView' factory: - 'OxidEsales\Eshop\Core\Registry' - - 'get' - arguments: - - 'OxidEsales\Eshop\Core\UtilsView' + - 'getUtilsView' shared: true # Order @@ -66,6 +64,14 @@ services: - 'OxidEsales\Eshop\Application\Model\Country' shared: false + # Remark + d3ox.linkmobility.OxidEsales\Eshop\Application\Model\Remark: + class: 'OxidEsales\Eshop\Application\Model\Remark' + factory: 'oxNew' + arguments: + - 'OxidEsales\Eshop\Application\Model\Remark' + shared: false + d3ox.linkmobility.Psr\Log\LoggerInterface: class: Psr\Log\LoggerInterface factory: diff --git a/src/tests/unit/Application/Model/MessageTypes/SmsTest.php b/src/tests/unit/Application/Model/MessageTypes/SmsTest.php new file mode 100644 index 0000000..dc8234e --- /dev/null +++ b/src/tests/unit/Application/Model/MessageTypes/SmsTest.php @@ -0,0 +1,818 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\Linkmobility4OXID\tests\unit\Application\Model\MessageTypes; + +use D3\Linkmobility4OXID\Application\Model\Configuration; +use D3\Linkmobility4OXID\Application\Model\Exceptions\noRecipientFoundException; +use D3\Linkmobility4OXID\Application\Model\MessageClient; +use D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms; +use D3\Linkmobility4OXID\Application\Model\OrderRecipients; +use D3\Linkmobility4OXID\Application\Model\RequestFactory; +use D3\Linkmobility4OXID\Application\Model\UserRecipients; +use D3\Linkmobility4OXID\tests\unit\LMUnitTestCase; +use D3\LinkmobilityClient\Client; +use D3\LinkmobilityClient\Exceptions\ApiException; +use D3\LinkmobilityClient\RecipientsList\RecipientsList; +use D3\LinkmobilityClient\SMS\BinaryRequest; +use D3\LinkmobilityClient\SMS\Response; +use D3\LinkmobilityClient\ValueObject\Recipient; +use D3\TestingTools\Development\CanAccessRestricted; +use Monolog\Logger; +use OxidEsales\Eshop\Application\Model\Order; +use OxidEsales\Eshop\Application\Model\Remark; +use OxidEsales\Eshop\Application\Model\User; +use OxidEsales\Eshop\Core\UtilsView; +use PHPUnit\Framework\MockObject\MockObject; +use Psr\Log\LoggerInterface; +use ReflectionException; + +class SmsTest extends LMUnitTestCase +{ + use CanAccessRestricted; + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms::__construct + * @covers \D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms::getMessage + */ + public function canConstruct() + { + /** @var Sms|MockObject $sut */ + $sut = $this->getMockBuilder(Sms::class) + ->disableOriginalConstructor() + ->onlyMethods(['sanitizeMessage']) + ->getMock(); + $sut->expects($this->once())->method('sanitizeMessage')->willReturnArgument(0); + + $this->callMethod( + $sut, + '__construct', + ['messageFixture'] + ); + + $this->assertSame( + 'messageFixture', + $this->callMethod( + $sut, + 'getMessage' + ) + ); + } + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms::setRemark + */ + public function canSetRemark() + { + /** @var Remark|MockObject $remarkMock */ + $remarkMock = $this->getMockBuilder(Remark::class) + ->onlyMethods(['assign', 'save']) + ->getMock(); + $remarkMock->expects($this->atLeastOnce())->method('assign'); + $remarkMock->expects($this->atLeastOnce())->method('save')->willReturn(true); + d3GetOxidDIC()->set('d3ox.linkmobility.'.Remark::class, $remarkMock); + + /** @var Sms|MockObject $sut */ + $sut = $this->getMockBuilder(Sms::class) + ->setConstructorArgs(['messageFixture']) + ->onlyMethods(['getTypeName']) + ->getMock(); + $sut->method('getTypeName')->willReturn('typeFixture'); + + $this->callMethod( + $sut, + 'setRemark', + ['userIdFixture', 'recipients', 'messageFixture'] + ); + } + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms::getResponse + */ + public function canGetResponse() + { + /** @var Response|MockObject $responseMock */ + $responseMock = $this->getMockBuilder(Response::class) + ->disableOriginalConstructor() + ->getMock(); + + /** @var Sms $sut */ + $sut = oxNew(Sms::class, 'messageFixture'); + + $this->setValue( + $sut, + 'response', + $responseMock + ); + + $this->assertSame( + $responseMock, + $this->callMethod( + $sut, + 'getResponse' + ) + ); + } + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms::setRecipients + */ + public function canSetRecipients() + { + /** @var RecipientsList|MockObject $recipientsListMock */ + $recipientsListMock = $this->getMockBuilder(RecipientsList::class) + ->disableOriginalConstructor() + ->getMock(); + + /** @var Sms $sut */ + $sut = oxNew(Sms::class, 'messageFixture'); + + $this->callMethod( + $sut, + 'setRecipients', + [$recipientsListMock] + ); + + $this->assertSame( + $recipientsListMock, + $this->getValue( + $sut, + 'recipients' + ) + ); + } + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms::getRecipientsList + */ + public function canGetRecipientsList() + { + /** @var Recipient|MockObject $recipientsMock1 */ + $recipientsMock1 = $this->getMockBuilder(Recipient::class) + ->setConstructorArgs(['01512 3456788', 'DE']) + ->onlyMethods(['getFormatted']) + ->getMock(); + + /** @var Recipient|MockObject $recipientsMock1 */ + $recipientsMock2 = $this->getMockBuilder(Recipient::class) + ->setConstructorArgs(['01512 3456789', 'DE']) + ->onlyMethods(['getFormatted']) + ->getMock(); + + /** @var RecipientsList|MockObject $recipientsListMock */ + $recipientsListMock = $this->getMockBuilder(RecipientsList::class) + ->disableOriginalConstructor() + ->onlyMethods(['clearRecipents']) + ->getMock(); + $recipientsListMock->add($recipientsMock1)->add($recipientsMock2); + + /** @var Sms $sut */ + $sut = oxNew(Sms::class, 'messageFixture'); + $this->callMethod( + $sut, + 'setRecipients', + [$recipientsListMock] + ); + + $this->assertSame( + '+4915123456788, +4915123456789', + $this->callMethod( + $sut, + 'getRecipientsList' + ) + ); + } + + /** + * @test + * @param $message + * @param $removeLineBreaks + * @param $removeMultiSpaces + * @param $expected + * @return void + * @throws ReflectionException + * @dataProvider canSanitizeMessageDataProvider + * @covers \D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms::sanitizeMessage + */ + public function canSanitizeMessage($message, $removeLineBreaks, $removeMultiSpaces, $expected) + { + /** @var Sms|MockObject $sut */ + $sut = $this->getMockBuilder(Sms::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->setValue( + $sut, + 'removeLineBreaks', + $removeLineBreaks + ); + + $this->setValue( + $sut, + 'removeMultipleSpaces', + $removeMultiSpaces + ); + + $this->assertSame( + $expected, + $this->callMethod( + $sut, + 'sanitizeMessage', + [$message] + ) + ); + } + + /** + * @return array[] + */ + public function canSanitizeMessageDataProvider(): array + { + $message = " ab
cd ef\r\ngh "; + + return [ + 'keep linebreaks, keep multispaces' => [$message, false, false, "abcd ef\r\ngh"], + 'rem linebreaks, keep multispaces' => [$message, true, false, "abcd ef gh"], + 'keep linebreaks, rem multispaces' => [$message, false, true, "abcd ef\r\ngh"], + 'rem linebreaks, rem multispaces' => [$message, true, true, 'abcd ef gh'], + ]; + } + + /** + * @test + * @param $sendReturn + * @param $throwException + * @param $setRemark + * @return void + * @throws ReflectionException + * @dataProvider canSendUserAccountMessageDataProvider + * @covers \D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms::sendUserAccountMessage + */ + public function canSendUserAccountMessage($sendReturn, $throwException, $setRemark) + { + /** @var User|MockObject $userMock */ + $userMock = $this->getMockBuilder(User::class) + ->disableOriginalConstructor() + ->onlyMethods(['getId']) + ->getMock(); + $userMock->method('getId')->willReturn('userIdFixture'); + + /** @var Logger|MockObject $loggerMock */ + $loggerMock = $this->getMockBuilder(Logger::class) + ->onlyMethods(['warning']) + ->disableOriginalConstructor() + ->getMock(); + $loggerMock->expects($this->exactly((int) $throwException))->method('warning'); + + /** @var UtilsView|MockObject $utilsViewMock */ + $utilsViewMock = $this->getMockBuilder(UtilsView::class) + ->onlyMethods(['addErrorToDisplay']) + ->getMock(); + $utilsViewMock->expects($this->exactly((int) $throwException))->method('addErrorToDisplay'); + d3GetOxidDIC()->set('d3ox.linkmobility.'.UtilsView::class, $utilsViewMock); + + /** @var RecipientsList|MockObject $recipientsListMock */ + $recipientsListMock = $this->getMockBuilder(RecipientsList::class) + ->disableOriginalConstructor() + ->getMock(); + + /** @var Sms|MockObject $sut */ + $sut = $this->getMockBuilder(Sms::class) + ->onlyMethods(['getLogger', 'sendCustomRecipientMessage', 'getUserRecipientsList', 'setRemark', + 'getRecipientsList', 'getMessage']) + ->disableOriginalConstructor() + ->getMock(); + $sut->method('getLogger')->willReturn($loggerMock); + $sut->method('sendCustomRecipientMessage')->will( + $throwException ? + $this->throwException(oxNew(noRecipientFoundException::class)) : + $this->returnValue($sendReturn) + ); + $sut->expects($setRemark ? $this->once() : $this->never())->method('setRemark'); + $sut->method('getUserRecipientsList')->willReturn($recipientsListMock); + $sut->method('getRecipientsList')->willReturn('abc,def'); + $sut->method('getMessage')->willReturn('messageFixture'); + + $this->assertSame( + $sendReturn, + $this->callMethod( + $sut, + 'sendUserAccountMessage', + [$userMock] + ) + ); + } + + /** + * @return array + */ + public function canSendUserAccountMessageDataProvider(): array + { + return [ + 'can send' => [true, false, true], + 'cant send' => [false, false, false], + 'no recipient' => [false, true, false] + ]; + } + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms::getUserRecipientsList + */ + public function canGetUserRecipientsList() + { + /** @var User|MockObject $userMock */ + $userMock = $this->getMockBuilder(User::class) + ->disableOriginalConstructor() + ->getMock(); + + /** @var Recipient|MockObject $recipientMock */ + $recipientMock = $this->getMockBuilder(Recipient::class) + ->disableOriginalConstructor() + ->getMock(); + + /** @var RecipientsList|MockObject $recipientsListMock */ + $recipientsListMock = $this->getMockBuilder(RecipientsList::class) + ->disableOriginalConstructor() + ->onlyMethods(['add']) + ->getMock(); + $recipientsListMock->expects($this->once())->method('add')->willReturnSelf(); + d3GetOxidDIC()->set(RecipientsList::class, $recipientsListMock); + + $userRecipientsMock = $this->getMockBuilder(UserRecipients::class) + ->disableOriginalConstructor() + ->onlyMethods(['getSmsRecipient']) + ->getMock(); + $userRecipientsMock->method('getSmsRecipient')->willReturn($recipientMock); + d3GetOxidDIC()->set(UserRecipients::class, $userRecipientsMock); + + /** @var MessageClient|MockObject $messageClientMock */ + $messageClientMock = $this->getMockBuilder(MessageClient::class) + ->getMock(); + d3GetOxidDIC()->set(MessageClient::class, $messageClientMock); + + /** @var Sms|MockObject $sut */ + $sut = $this->getMockBuilder(Sms::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->assertSame( + $recipientsListMock, + $this->callMethod( + $sut, + 'getUserRecipientsList', + [$userMock] + ) + ); + } + + /** + * @test + * @param $sendReturn + * @param $throwException + * @param $setRemark + * @return void + * @throws ReflectionException + * @dataProvider canSendUserAccountMessageDataProvider + * @covers \D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms::sendOrderMessage + */ + public function canSendOrderMessage($sendReturn, $throwException, $setRemark) + { + /** @var User|MockObject $userMock */ + $userMock = $this->getMockBuilder(User::class) + ->disableOriginalConstructor() + ->onlyMethods(['getId']) + ->getMock(); + $userMock->method('getId')->willReturn('userIdFixture'); + + /** @var Order|MockObject $orderMock */ + $orderMock = $this->getMockBuilder(Order::class) + ->disableOriginalConstructor() + ->onlyMethods(['getId', 'getOrderUser']) + ->getMock(); + $orderMock->method('getId')->willReturn('userIdFixture'); + $orderMock->method('getOrderUser')->willReturn($userMock); + + /** @var Logger|MockObject $loggerMock */ + $loggerMock = $this->getMockBuilder(Logger::class) + ->onlyMethods(['warning']) + ->disableOriginalConstructor() + ->getMock(); + $loggerMock->expects($this->exactly((int) $throwException))->method('warning'); + + /** @var UtilsView|MockObject $utilsViewMock */ + $utilsViewMock = $this->getMockBuilder(UtilsView::class) + ->onlyMethods(['addErrorToDisplay']) + ->getMock(); + $utilsViewMock->expects($this->exactly((int) $throwException))->method('addErrorToDisplay'); + d3GetOxidDIC()->set('d3ox.linkmobility.'.UtilsView::class, $utilsViewMock); + + /** @var RecipientsList|MockObject $recipientsListMock */ + $recipientsListMock = $this->getMockBuilder(RecipientsList::class) + ->disableOriginalConstructor() + ->getMock(); + + /** @var Sms|MockObject $sut */ + $sut = $this->getMockBuilder(Sms::class) + ->onlyMethods(['getLogger', 'sendCustomRecipientMessage', 'getOrderRecipientsList', 'setRemark', + 'getRecipientsList', 'getMessage']) + ->disableOriginalConstructor() + ->getMock(); + $sut->method('getLogger')->willReturn($loggerMock); + $sut->method('sendCustomRecipientMessage')->will( + $throwException ? + $this->throwException(oxNew(noRecipientFoundException::class)) : + $this->returnValue($sendReturn) + ); + $sut->expects($setRemark ? $this->once() : $this->never())->method('setRemark'); + $sut->method('getOrderRecipientsList')->willReturn($recipientsListMock); + $sut->method('getRecipientsList')->willReturn('abc,def'); + $sut->method('getMessage')->willReturn('messageFixture'); + + $this->assertSame( + $sendReturn, + $this->callMethod( + $sut, + 'sendOrderMessage', + [$orderMock] + ) + ); + } + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms::getOrderRecipientsList + */ + public function canGetOrderRecipientsList() + { + /** @var Order|MockObject $orderMock */ + $orderMock = $this->getMockBuilder(Order::class) + ->disableOriginalConstructor() + ->getMock(); + + /** @var Recipient|MockObject $recipientMock */ + $recipientMock = $this->getMockBuilder(Recipient::class) + ->disableOriginalConstructor() + ->getMock(); + + /** @var RecipientsList|MockObject $recipientsListMock */ + $recipientsListMock = $this->getMockBuilder(RecipientsList::class) + ->disableOriginalConstructor() + ->onlyMethods(['add']) + ->getMock(); + $recipientsListMock->expects($this->once())->method('add')->willReturnSelf(); + d3GetOxidDIC()->set(RecipientsList::class, $recipientsListMock); + + /** @var MessageClient|MockObject $messageClientMock */ + $messageClientMock = $this->getMockBuilder(MessageClient::class) + ->getMock(); + d3GetOxidDIC()->set(MessageClient::class, $messageClientMock); + + /** @var Sms|MockObject $sut */ + $sut = $this->getMockBuilder(Sms::class) + ->disableOriginalConstructor() + ->onlyMethods(['getOrderRecipient']) + ->getMock(); + $sut->method('getOrderRecipient')->willReturn($recipientMock); + + $this->assertSame( + $recipientsListMock, + $this->callMethod( + $sut, + 'getOrderRecipientsList', + [$orderMock] + ) + ); + } + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms::getOrderRecipient + */ + public function canGetOrderRecipient() + { + /** @var Order|MockObject $orderMock */ + $orderMock = $this->getMockBuilder(Order::class) + ->disableOriginalConstructor() + ->getMock(); + + /** @var Recipient|MockObject $recipientMock */ + $recipientMock = $this->getMockBuilder(Recipient::class) + ->disableOriginalConstructor() + ->getMock(); + + /** @var OrderRecipients|MockObject $orderRecipientsMock */ + $orderRecipientsMock = $this->getMockBuilder(OrderRecipients::class) + ->disableOriginalConstructor() + ->onlyMethods(['getSmsRecipient']) + ->getMock(); + $orderRecipientsMock->method('getSmsRecipient')->willReturn($recipientMock); + d3GetOxidDIC()->set(OrderRecipients::class, $orderRecipientsMock); + + /** @var Sms|MockObject $sut */ + $sut = $this->getMockBuilder(Sms::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->assertSame( + $recipientMock, + $this->callMethod( + $sut, + 'getOrderRecipient', + [$orderMock] + ) + ); + } + + /** + * @test + * @param $sendSuccessful + * @param $throwException + * @param $expected + * @return void + * @throws ReflectionException + * @dataProvider canSendCustomRecipientMessageDataProvider + * @covers \D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms::sendCustomRecipientMessage() + */ + public function canSendCustomRecipientMessage($sendSuccessful, $throwException, $expected) + { + /** @var RecipientsList|MockObject $recipientsListMock */ + $recipientsListMock = $this->getMockBuilder(RecipientsList::class) + ->disableOriginalConstructor() + ->getMock(); + + /** @var Response|MockObject $smsResponseMock */ + $smsResponseMock = $this->getMockBuilder(Response::class) + ->onlyMethods(['isSuccessful']) + ->disableOriginalConstructor() + ->getMock(); + $smsResponseMock->method('isSuccessful')->willReturn($sendSuccessful); + + /** @var Logger|MockObject $loggerMock */ + $loggerMock = $this->getMockBuilder(Logger::class) + ->onlyMethods(['warning']) + ->disableOriginalConstructor() + ->getMock(); + $loggerMock->expects($this->exactly((int) $throwException))->method('warning'); + + /** @var UtilsView|MockObject $utilsViewMock */ + $utilsViewMock = $this->getMockBuilder(UtilsView::class) + ->onlyMethods(['addErrorToDisplay']) + ->getMock(); + $utilsViewMock->expects($this->exactly((int) $throwException))->method('addErrorToDisplay'); + d3GetOxidDIC()->set('d3ox.linkmobility.'.UtilsView::class, $utilsViewMock); + + /** @var Sms|MockObject $sut */ + $sut = $this->getMockBuilder(Sms::class) + ->onlyMethods(['submitMessage', 'getLogger']) + ->disableOriginalConstructor() + ->getMock(); + $sut->method('submitMessage')->will( + $throwException ? + $this->throwException(oxNew(ApiException::class)) : + $this->returnValue($smsResponseMock) + ); + $sut->method('getLogger')->willReturn($loggerMock); + + $this->assertSame( + $expected, + $this->callMethod( + $sut, + 'sendCustomRecipientMessage', + [$recipientsListMock] + ) + ); + } + + /** + * @return array + */ + public function canSendCustomRecipientMessageDataProvider(): array + { + return [ + 'is successful' => [true, false, true], + 'is not successful' => [false, false, false], + 'no recipient' => [false, true, false], + ]; + } + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms::getRequest() + */ + public function canGetRequest() + { + /** @var Client|MockObject $clientMock */ + $clientMock = $this->getMockBuilder(Client::class) + ->disableOriginalConstructor() + ->getMock(); + + /** @var Configuration|MockObject $configurationMock */ + $configurationMock = $this->getMockBuilder(Configuration::class) + ->getMock(); + + /** @var BinaryRequest|MockObject $binaryRequestMock */ + $binaryRequestMock = $this->getMockBuilder(BinaryRequest::class) + ->disableOriginalConstructor() + ->getMock(); + + /** @var RequestFactory|MockObject $requestFactoryMock */ + $requestFactoryMock = $this->getMockBuilder(RequestFactory::class) + ->disableOriginalConstructor() + ->onlyMethods(['getSmsRequest']) + ->getMock(); + $requestFactoryMock->method('getSmsRequest')->willReturn($binaryRequestMock); + d3GetOxidDIC()->set(RequestFactory::class, $requestFactoryMock); + + /** @var Sms|MockObject $sut */ + $sut = $this->getMockBuilder(Sms::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->assertSame( + $binaryRequestMock, + $this->callMethod( + $sut, + 'getRequest', + [$configurationMock, $clientMock] + ) + ); + } + + /** + * @test + * @param $submitSuccessful + * @return void + * @throws ReflectionException + * @dataProvider canSubmitMessageDataProvider + * @covers \D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms::submitMessage() + */ + public function canSubmitMessage($submitSuccessful) + { + /** @var Recipient|MockObject $recipientMock */ + $recipientMock = $this->getMockBuilder(Recipient::class) + ->disableOriginalConstructor() + ->getMock(); + + /** @var RecipientsList|MockObject $recipientListMock */ + $recipientListMock = $this->getMockBuilder(RecipientsList::class) + ->onlyMethods(['getRecipients']) + ->disableOriginalConstructor() + ->getMock(); + $recipientListMock->method('getRecipients')->willReturn([$recipientMock]); + + /** @var RecipientsList|MockObject $requestRecipientListMock */ + $requestRecipientListMock = $this->getMockBuilder(RecipientsList::class) + ->disableOriginalConstructor() + ->onlyMethods(['add']) + ->getMock(); + $requestRecipientListMock->expects($this->once())->method('add')->with( + $this->identicalTo($recipientMock) + ); + + /** @var BinaryRequest|MockObject $binaryRequestMock */ + $binaryRequestMock = $this->getMockBuilder(BinaryRequest::class) + ->onlyMethods(['getRecipientsList', 'getBody']) + ->disableOriginalConstructor() + ->getMock(); + $binaryRequestMock->method('getRecipientsList')->willReturn($requestRecipientListMock); + $binaryRequestMock->method('getBody')->willReturn(['bodyFixture']); + + /** @var Response|MockObject $smsResponseMock */ + $smsResponseMock = $this->getMockBuilder(Response::class) + ->disableOriginalConstructor() + ->onlyMethods(['isSuccessful', 'getErrorMessage']) + ->getMock(); + $smsResponseMock->method('isSuccessful')->willReturn($submitSuccessful); + $smsResponseMock->method('getErrorMessage')->willReturn('errorMessageFixture'); + + /** @var Logger|MockObject $loggerMock */ + $loggerMock = $this->getMockBuilder(Logger::class) + ->onlyMethods(['warning']) + ->disableOriginalConstructor() + ->getMock(); + $loggerMock->expects($this->exactly((int) !$submitSuccessful))->method('warning'); + + /** @var Client|MockObject $clientMock */ + $clientMock = $this->getMockBuilder(Client::class) + ->disableOriginalConstructor() + ->onlyMethods(['request']) + ->getMock(); + $clientMock->method('request')->willReturn($smsResponseMock); + + /** @var MessageClient|MockObject $messageClientMock */ + $messageClientMock = $this->getMockBuilder(MessageClient::class) + ->onlyMethods(['getClient']) + ->getMock(); + $messageClientMock->method('getClient')->willReturn($clientMock); + d3GetOxidDIC()->set(MessageClient::class, $messageClientMock); + + /** @var Sms|MockObject $sut */ + $sut = $this->getMockBuilder(Sms::class) + ->disableOriginalConstructor() + ->onlyMethods(['getRequest', 'getLogger']) + ->getMock(); + $sut->method('getRequest')->willReturn($binaryRequestMock); + $sut->method('getLogger')->willReturn($loggerMock); + + $this->assertSame( + $smsResponseMock, + $this->callMethod( + $sut, + 'submitMessage', + [$recipientListMock] + ) + ); + } + + /** + * @return array + */ + public function canSubmitMessageDataProvider(): array + { + return [ + 'successful' => [true], + 'not successful'=> [false] + ]; + } + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms::getTypeName + */ + public function canGetTypeName() + { + /** @var Sms|MockObject $sut */ + $sut = $this->getMockBuilder(Sms::class) + ->disableOriginalConstructor() + ->onlyMethods(['getMessage']) + ->getMock(); + + $this->assertIsString( + $this->callMethod( + $sut, + 'getTypeName' + ) + ); + } + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms::getLogger() + */ + public function canGetLogger() + { + /** @var Sms|MockObject $sut */ + $sut = $this->getMockBuilder(Sms::class) + ->disableOriginalConstructor() + ->onlyMethods(['getMessage']) + ->getMock(); + + $this->assertInstanceOf( + LoggerInterface::class, + $this->callMethod( + $sut, + 'getLogger' + ) + ); + } +} \ No newline at end of file