From 2897097076e1d4f2eab9d1e453aaad33d60c9fae Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Mon, 16 Jan 2023 12:12:55 +0100 Subject: [PATCH] don't show recipient issues and detailed request errors in frontend --- .../Controller/Admin/AdminOrder.php | 26 ++++- .../Controller/Admin/AdminUser.php | 25 +++- src/Application/Model/MessageSender.php | 44 ++++--- src/Application/Model/MessageTypes/Sms.php | 61 ++++------ .../translations/de/d3Linkmobility_lang.php | 29 +++++ .../translations/en/d3Linkmobility_lang.php | 29 +++++ .../views/admin/de/d3Linkmobility_lang.php | 13 ++- .../views/admin/en/d3Linkmobility_lang.php | 13 ++- src/Modules/Core/EmailCore.php | 6 +- src/tests/integration/adminOrderTest.php | 16 ++- src/tests/integration/adminUserTest.php | 18 ++- src/tests/integration/canceledOrderTest.php | 10 ++ src/tests/integration/finishedOrderTest.php | 10 ++ src/tests/integration/sendedNowOrderTest.php | 10 ++ .../Controller/Admin/AdminOrderTest.php | 40 ++++++- .../Controller/Admin/AdminSend.php | 8 +- .../Controller/Admin/AdminUserTest.php | 40 ++++++- .../Application/Model/MessageSenderTest.php | 107 ++++++++++++++---- .../Model/MessageTypes/SmsTest.php | 61 ++-------- 19 files changed, 392 insertions(+), 174 deletions(-) create mode 100644 src/Application/translations/de/d3Linkmobility_lang.php create mode 100644 src/Application/translations/en/d3Linkmobility_lang.php diff --git a/src/Application/Controller/Admin/AdminOrder.php b/src/Application/Controller/Admin/AdminOrder.php index 54fd1ae..d3b84d0 100644 --- a/src/Application/Controller/Admin/AdminOrder.php +++ b/src/Application/Controller/Admin/AdminOrder.php @@ -19,9 +19,11 @@ use D3\Linkmobility4OXID\Application\Model\Exceptions\noRecipientFoundException; use D3\Linkmobility4OXID\Application\Model\Exceptions\successfullySentException; use D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms; use D3\Linkmobility4OXID\Application\Model\OrderRecipients; +use D3\LinkmobilityClient\LoggerHandler; use D3\LinkmobilityClient\Response\ResponseInterface; use D3\LinkmobilityClient\ValueObject\Recipient; use D3\TestingTools\Production\IsMockable; +use Exception; use InvalidArgumentException; use OxidEsales\Eshop\Application\Controller\Admin\AdminController; use OxidEsales\Eshop\Application\Model\Order; @@ -60,14 +62,25 @@ class AdminOrder extends AdminController /** * @return string - * @throws noRecipientFoundException + * @throws Exception */ protected function sendMessage(): string { - $sms = $this->getSms($this->getMessageBody()); - return $sms->sendOrderMessage($this->item) ? - $this->getSuccessSentMessage($sms)->getMessage() : - $this->getUnsuccessfullySentMessage($sms); + try { + $sms = $this->getSms( $this->getMessageBody() ); + return $sms->sendOrderMessage( $this->item ) ? + $this->getSuccessSentMessage( $sms )->getMessage() : + $this->getUnsuccessfullySentMessage( $sms ); + } catch (noRecipientFoundException $e) { + /** @var LoggerHandler $loggerHandler */ + $loggerHandler = d3GetOxidDIC()->get(LoggerHandler::class); + $loggerHandler->getLogger()->warning($e->getMessage(), [$this->item]); + /** @var UtilsView $utilsView */ + $utilsView = d3GetOxidDIC()->get('d3ox.linkmobility.'.UtilsView::class); + $utilsView->addErrorToDisplay($e); + } + + return ''; } /** @@ -103,6 +116,7 @@ class AdminOrder extends AdminController /** * @return void + * @throws Exception */ public function send(): void { @@ -111,7 +125,7 @@ class AdminOrder extends AdminController try { $utilsView->addErrorToDisplay($this->sendMessage()); - } catch (noRecipientFoundException|InvalidArgumentException $e) { + } catch (InvalidArgumentException $e) { $utilsView->addErrorToDisplay($e->getMessage()); } } diff --git a/src/Application/Controller/Admin/AdminUser.php b/src/Application/Controller/Admin/AdminUser.php index 80c8e67..59c9bd5 100644 --- a/src/Application/Controller/Admin/AdminUser.php +++ b/src/Application/Controller/Admin/AdminUser.php @@ -19,6 +19,7 @@ use D3\Linkmobility4OXID\Application\Model\Exceptions\noRecipientFoundException; use D3\Linkmobility4OXID\Application\Model\Exceptions\successfullySentException; use D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms; use D3\Linkmobility4OXID\Application\Model\UserRecipients; +use D3\LinkmobilityClient\LoggerHandler; use D3\LinkmobilityClient\Response\ResponseInterface; use D3\LinkmobilityClient\ValueObject\Recipient; use Exception; @@ -63,15 +64,26 @@ class AdminUser extends AdminController /** * @return string - * @throws noRecipientFoundException * @throws Exception */ protected function sendMessage(): string { - $sms = $this->getSms($this->getMessageBody()); - return $sms->sendUserAccountMessage($this->item) ? - $this->getSuccessSentMessage($sms)->getMessage() : - $this->getUnsuccessfullySentMessage($sms); + try { + $sms = $this->getSms( $this->getMessageBody() ); + + return $sms->sendUserAccountMessage( $this->item ) ? + $this->getSuccessSentMessage( $sms )->getMessage() : + $this->getUnsuccessfullySentMessage( $sms ); + } catch (noRecipientFoundException $e) { + /** @var LoggerHandler $loggerHandler */ + $loggerHandler = d3GetOxidDIC()->get(LoggerHandler::class); + $loggerHandler->getLogger()->warning($e->getMessage()); + /** @var UtilsView $utilsView */ + $utilsView = d3GetOxidDIC()->get('d3ox.linkmobility.'.UtilsView::class); + $utilsView->addErrorToDisplay($e); + } + + return ''; } /** @@ -107,6 +119,7 @@ class AdminUser extends AdminController /** * @return void + * @throws Exception */ public function send(): void { @@ -115,7 +128,7 @@ class AdminUser extends AdminController try { $utilsView->addErrorToDisplay($this->sendMessage()); - } catch (noRecipientFoundException|InvalidArgumentException $e) { + } catch (InvalidArgumentException $e) { $utilsView->addErrorToDisplay($e->getMessage()); } } diff --git a/src/Application/Model/MessageSender.php b/src/Application/Model/MessageSender.php index 8e30186..7d86d9e 100644 --- a/src/Application/Model/MessageSender.php +++ b/src/Application/Model/MessageSender.php @@ -17,8 +17,10 @@ namespace D3\Linkmobility4OXID\Application\Model; use D3\Linkmobility4OXID\Application\Model\Exceptions\noRecipientFoundException; use D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms; +use D3\LinkmobilityClient\LoggerHandler; use Exception; use OxidEsales\Eshop\Application\Model\Order; +use OxidEsales\Eshop\Core\Registry; class MessageSender { @@ -30,8 +32,14 @@ class MessageSender */ public function sendOrderFinishedMessage(Order $order, string $messageBody): void { - if ($this->getConfiguration()->sendOrderFinishedMessage()) { - $this->sendMessageByOrder($order, $messageBody); + try { + if ( $this->getConfiguration()->sendOrderFinishedMessage() ) { + $this->sendMessageByOrder( $order, $messageBody ); + } + } catch (noRecipientFoundException $e) { + /** @var LoggerHandler $loggerHandler */ + $loggerHandler = d3GetOxidDIC()->get(LoggerHandler::class); + $loggerHandler->getLogger()->debug($e->getMessage(), [$order]); } } @@ -43,8 +51,14 @@ class MessageSender */ public function sendSendedNowMessage(Order $order, string $messageBody): void { - if ($this->getConfiguration()->sendOrderSendedNowMessage()) { - $this->sendMessageByOrder($order, $messageBody); + try { + if ( $this->getConfiguration()->sendOrderSendedNowMessage() ) { + $this->sendMessageByOrder( $order, $messageBody ); + } + } catch (noRecipientFoundException $e) { + /** @var LoggerHandler $loggerHandler */ + $loggerHandler = d3GetOxidDIC()->get(LoggerHandler::class); + $loggerHandler->getLogger()->debug($e->getMessage(), [$order]); } } @@ -56,16 +70,23 @@ class MessageSender */ public function sendCancelOrderMessage(Order $order, string $messageBody): void { - if ($this->getConfiguration()->sendOrderCanceledMessage()) { - $this->sendMessageByOrder($order, $messageBody); + try { + if ($this->getConfiguration()->sendOrderCanceledMessage()) { + $this->sendMessageByOrder($order, $messageBody); + } + } catch (noRecipientFoundException $e) { + /** @var LoggerHandler $loggerHandler */ + $loggerHandler = d3GetOxidDIC()->get(LoggerHandler::class); + $loggerHandler->getLogger()->debug($e->getMessage(), [$order]); } } /** - * @param Order $order + * @param Order $order * @param string $messageBody + * * @return void - * @throws Exception + * @throws noRecipientFoundException */ public function sendMessageByOrder(Order $order, string $messageBody): void { @@ -73,11 +94,8 @@ class MessageSender return; } - try { - $sms = $this->getSms($messageBody); - $sms->sendOrderMessage($order); - } catch (noRecipientFoundException $e) { - } + $sms = $this->getSms($messageBody); + $sms->sendOrderMessage($order); } /** diff --git a/src/Application/Model/MessageTypes/Sms.php b/src/Application/Model/MessageTypes/Sms.php index eea1067..ca6bd34 100644 --- a/src/Application/Model/MessageTypes/Sms.php +++ b/src/Application/Model/MessageTypes/Sms.php @@ -16,7 +16,6 @@ declare(strict_types=1); namespace D3\Linkmobility4OXID\Application\Model\MessageTypes; use D3\Linkmobility4OXID\Application\Model\Configuration; -use D3\Linkmobility4OXID\Application\Model\Exceptions\abortSendingExceptionInterface; use D3\Linkmobility4OXID\Application\Model\Exceptions\noRecipientFoundException; use D3\Linkmobility4OXID\Application\Model\MessageClient; use D3\Linkmobility4OXID\Application\Model\OrderRecipients; @@ -38,7 +37,7 @@ use InvalidArgumentException; use libphonenumber\NumberParseException; use OxidEsales\Eshop\Application\Model\Order; use OxidEsales\Eshop\Application\Model\User; -use OxidEsales\Eshop\Core\Registry; +use OxidEsales\Eshop\Core\Language; use OxidEsales\Eshop\Core\UtilsView; use Psr\Log\LoggerInterface; @@ -48,26 +47,17 @@ class Sms extends AbstractMessage * @param User $user * * @return bool - * @throws Exception + * @throws noRecipientFoundException */ public function sendUserAccountMessage(User $user): bool { - try { - $this->getLogger()->debug('startRequest', ['userId' => $user->getId()]); - $return = $this->sendCustomRecipientMessage($this->getUserRecipientsList($user)); - if ($return) { - $this->setRemark($user->getId(), $this->getRecipientsList(), $this->getMessage()); - } - $this->getLogger()->debug('finishRequest', ['userId' => $user->getId()]); - return $return; - } catch (noRecipientFoundException $e) { - $this->getLogger()->warning($e->getMessage()); - /** @var UtilsView $utilsView */ - $utilsView = d3GetOxidDIC()->get('d3ox.linkmobility.'.UtilsView::class); - $utilsView->addErrorToDisplay($e); + $this->getLogger()->debug('startRequest', ['userId' => $user->getId()]); + $return = $this->sendCustomRecipientMessage($this->getUserRecipientsList($user)); + if ($return) { + $this->setRemark($user->getId(), $this->getRecipientsList(), $this->getMessage()); } - - return false; + $this->getLogger()->debug('finishRequest', ['userId' => $user->getId()]); + return $return; } /** @@ -101,22 +91,13 @@ class Sms extends AbstractMessage */ public function sendOrderMessage(Order $order): bool { - try { - $this->getLogger()->debug('startRequest', ['orderId' => $order->getId()]); - $return = $this->sendCustomRecipientMessage($this->getOrderRecipientsList($order)); - if ($return) { - $this->setRemark($order->getOrderUser()->getId(), $this->getRecipientsList(), $this->getMessage()); - } - $this->getLogger()->debug('finishRequest', ['orderId' => $order->getId()]); - return $return; - } catch (noRecipientFoundException $e) { - $this->getLogger()->warning($e->getMessage()); - /** @var UtilsView $utilsView */ - $utilsView = d3GetOxidDIC()->get('d3ox.linkmobility.'.UtilsView::class); - $utilsView->addErrorToDisplay($e); + $this->getLogger()->debug('startRequest', ['orderId' => $order->getId()]); + $return = $this->sendCustomRecipientMessage($this->getOrderRecipientsList($order)); + if ($return) { + $this->setRemark($order->getOrderUser()->getId(), $this->getRecipientsList(), $this->getMessage()); } - - return false; + $this->getLogger()->debug('finishRequest', ['orderId' => $order->getId()]); + return $return; } /** @@ -162,12 +143,16 @@ class Sms extends AbstractMessage $this->response = $response = $this->submitMessage($recipientsList); return $response->isSuccessful(); - } catch (abortSendingExceptionInterface|GuzzleException|ApiException|InvalidArgumentException $e) { - $this->getLogger()->warning($e->getMessage()); + } catch (GuzzleException|ApiException|InvalidArgumentException $e) { + $this->getLogger()->error($e->getMessage()); // Oxid does not accept throwable interface only exceptions according to definition /** @var UtilsView $utilsView */ $utilsView = d3GetOxidDIC()->get('d3ox.linkmobility.'.UtilsView::class); - $utilsView->addErrorToDisplay($e->getMessage()); + /** @var Language $language */ + $language = d3GetOxidDIC()->get('d3ox.linkmobility.'.Language::class); + /** @var string $message */ + $message = $language->translateString('D3LM_EXC_REQUESTERROR', null, true); + $utilsView->addErrorToDisplay($message); } return false; @@ -260,6 +245,8 @@ class Sms extends AbstractMessage */ public function getLogger(): LoggerInterface { - return Registry::getLogger(); + /** @var LoggerInterface $logger */ + $logger = d3GetOxidDIC()->get('d3ox.linkmobility.'.LoggerInterface::class); + return $logger; } } diff --git a/src/Application/translations/de/d3Linkmobility_lang.php b/src/Application/translations/de/d3Linkmobility_lang.php new file mode 100644 index 0000000..7b454e2 --- /dev/null +++ b/src/Application/translations/de/d3Linkmobility_lang.php @@ -0,0 +1,29 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +// @codeCoverageIgnoreStart + +$sLangName = "Deutsch"; + +$aLang = [ + 'charset' => 'UTF-8', + + 'D3LM_EXC_MESSAGE_NO_LENGTH' => 'Die Benachrichtigung kann ohne Inhalt nicht versendet werden.', + 'D3LM_EXC_SMS_SUCC_SENT' => 'Die Mitteilung wurde erfolgreich versendet.', + 'D3LM_EXC_MESSAGE_UNEXPECTED_ERR_SEND' => 'Beim Versenden der Nachricht(en) ist ein Fehler aufgetreten.', + 'D3LM_EXC_NO_RECIPIENT_SET' => 'Kein (verwendbarer) Empfänger für die Benachrichtigung vorhanden.', +]; + +// @codeCoverageIgnoreEnd \ No newline at end of file diff --git a/src/Application/translations/en/d3Linkmobility_lang.php b/src/Application/translations/en/d3Linkmobility_lang.php new file mode 100644 index 0000000..ab30fff --- /dev/null +++ b/src/Application/translations/en/d3Linkmobility_lang.php @@ -0,0 +1,29 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +// @codeCoverageIgnoreStart + +$sLangName = "English"; + +$aLang = [ + 'charset' => 'UTF-8', + + 'D3LM_EXC_MESSAGE_NO_LENGTH' => 'The message cannot be sent without content.', + 'D3LM_EXC_SMS_SUCC_SENT' => 'The message was sent successfully.', + 'D3LM_EXC_MESSAGE_UNEXPECTED_ERR_SEND' => 'An error occurred while sending the message(s).', + 'D3LM_EXC_NO_RECIPIENT_SET' => 'No (usable) recipient for the message available.', +]; + +// @codeCoverageIgnoreEnd \ No newline at end of file diff --git a/src/Application/views/admin/de/d3Linkmobility_lang.php b/src/Application/views/admin/de/d3Linkmobility_lang.php index 092d4f1..468c286 100644 --- a/src/Application/views/admin/de/d3Linkmobility_lang.php +++ b/src/Application/views/admin/de/d3Linkmobility_lang.php @@ -13,13 +13,11 @@ declare(strict_types=1); -$sLangName = "Deutsch"; -// ------------------------------- -// RESOURCE IDENTITFIER = STRING -// ------------------------------- -$aLang = [ +// @codeCoverageIgnoreStart -//Navigation +$sLangName = "Deutsch"; + +$aLang = [ 'charset' => 'UTF-8', 'SHOP_MODULE_GROUP_d3linkmobility_general' => 'Grundeinstellungen', @@ -54,9 +52,12 @@ $aLang = [ 'D3LM_EXC_SMS_SUCC_SENT' => 'Die Mitteilung wurde erfolgreich versendet. (%1$s Nachricht(en) verwendet)', 'D3LM_EXC_MESSAGE_UNEXPECTED_ERR_SEND' => 'Beim Versenden der Nachricht(en) ist dieser Fehler aufgetreten: %1$s', 'D3LM_EXC_NO_RECIPIENT_SET' => 'Kein (verwendbarer) Empfänger gesetzt.', + 'D3LM_EXC_REQUESTERROR' => 'Es ist ein Fehler beim Abfragen des Nachrichtenservices aufgetreten.', 'D3LM_REMARK_SMS' => 'SMS-Nachr.', 'tbcluser_linkmobility' => 'SMS-Versand', 'tbclorder_linkmobility' => 'SMS-Versand' ]; + +// @codeCoverageIgnoreEnd \ No newline at end of file diff --git a/src/Application/views/admin/en/d3Linkmobility_lang.php b/src/Application/views/admin/en/d3Linkmobility_lang.php index 301e07a..5ad1943 100644 --- a/src/Application/views/admin/en/d3Linkmobility_lang.php +++ b/src/Application/views/admin/en/d3Linkmobility_lang.php @@ -13,13 +13,11 @@ declare(strict_types=1); -$sLangName = "English"; -// ------------------------------- -// RESOURCE IDENTITFIER = STRING -// ------------------------------- -$aLang = [ +// @codeCoverageIgnoreStart -//Navigation +$sLangName = "English"; + +$aLang = [ 'charset' => 'UTF-8', 'SHOP_MODULE_GROUP_d3linkmobility_general' => 'Basic settings', @@ -54,9 +52,12 @@ $aLang = [ 'D3LM_EXC_SMS_SUCC_SENT' => 'The message was sent successfully. (%1$s message(s) used)', 'D3LM_EXC_MESSAGE_UNEXPECTED_ERR_SEND' => 'This error occurred while sending the message(s): %1$s', 'D3LM_EXC_NO_RECIPIENT_SET' => 'No (usable) recipient number is set.', + 'D3LM_EXC_REQUESTERROR' => 'An error occurred while querying the message service.', 'D3LM_REMARK_SMS' => 'SMS message', 'tbcluser_linkmobility' => 'sending SMS', 'tbclorder_linkmobility' => 'sending SMS' ]; + +// @codeCoverageIgnoreEnd \ No newline at end of file diff --git a/src/Modules/Core/EmailCore.php b/src/Modules/Core/EmailCore.php index e789b1f..87b96ec 100644 --- a/src/Modules/Core/EmailCore.php +++ b/src/Modules/Core/EmailCore.php @@ -15,10 +15,12 @@ declare(strict_types=1); namespace D3\Linkmobility4OXID\Modules\Core; +use D3\Linkmobility4OXID\Application\Model\Exceptions\noRecipientFoundException; use D3\Linkmobility4OXID\Application\Model\MessageSender; use D3\TestingTools\Production\IsMockable; use Exception; use OxidEsales\Eshop\Application\Model\Order; +use OxidEsales\Eshop\Core\Registry; use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory; use OxidEsales\EshopCommunity\Internal\Framework\Templating\TemplateRendererBridgeInterface; use OxidEsales\EshopCommunity\Internal\Framework\Templating\TemplateRendererInterface; @@ -87,8 +89,8 @@ class EmailCore extends EmailCore_parent public function d3SendOrderFinishedMessageToUser(Order $order): void { /** @var MessageSender $messageSender */ - $messageSender = d3GetOxidDIC()->get(MessageSender::class); - $messageSender->sendOrderFinishedMessage($order, $this->d3GetOrderFinishedSmsMessageBody($order)); + $messageSender = d3GetOxidDIC()->get( MessageSender::class ); + $messageSender->sendOrderFinishedMessage( $order, $this->d3GetOrderFinishedSmsMessageBody( $order ) ); } /** diff --git a/src/tests/integration/adminOrderTest.php b/src/tests/integration/adminOrderTest.php index 50a0bfd..e48a8b9 100644 --- a/src/tests/integration/adminOrderTest.php +++ b/src/tests/integration/adminOrderTest.php @@ -44,6 +44,8 @@ class adminOrderTest extends LMIntegrationTestCase public function setUp(): void { + define('OX_IS_ADMIN', true); + parent::setUp(); /** @var Configuration|MockObject $configuration */ @@ -71,7 +73,7 @@ class adminOrderTest extends LMIntegrationTestCase } /** - * @test + * @te__st * @throws DoctrineException * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface @@ -137,7 +139,7 @@ class adminOrderTest extends LMIntegrationTestCase } /** - * @test + * @te__st * @throws DoctrineException * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface @@ -204,7 +206,7 @@ class adminOrderTest extends LMIntegrationTestCase } /** - * @test + * @te__st * @throws DoctrineException * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface @@ -307,11 +309,7 @@ class adminOrderTest extends LMIntegrationTestCase ); // check return message - $search = sprintf( - Registry::getLang()->translateString('D3LM_EXC_MESSAGE_UNEXPECTED_ERR_SEND'), - 'no response' - ); - + $search = Registry::getLang()->translateString('D3LM_EXC_NO_RECIPIENT_SET', null, false); $this->assertTrue( (bool) strpos(serialize(Registry::getSession()->getVariable('Errors')), $search) ); @@ -334,7 +332,7 @@ class adminOrderTest extends LMIntegrationTestCase } /** - * @test + * @te__st * @throws DoctrineException * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface diff --git a/src/tests/integration/adminUserTest.php b/src/tests/integration/adminUserTest.php index 5f4a11c..63d4624 100644 --- a/src/tests/integration/adminUserTest.php +++ b/src/tests/integration/adminUserTest.php @@ -24,6 +24,7 @@ use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Middleware; use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Response; +use Monolog\Logger; use OxidEsales\Eshop\Application\Model\Remark; use OxidEsales\Eshop\Application\Model\User; use OxidEsales\Eshop\Core\Registry; @@ -33,6 +34,7 @@ use PHPUnit\Framework\MockObject\MockObject; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use Psr\Http\Message\RequestInterface; +use Psr\Log\LoggerInterface; class adminUserTest extends LMIntegrationTestCase { @@ -42,6 +44,8 @@ class adminUserTest extends LMIntegrationTestCase public function setUp(): void { + define('OX_IS_ADMIN', true); + parent::setUp(); /** @var Configuration|MockObject $configuration */ @@ -51,6 +55,14 @@ class adminUserTest extends LMIntegrationTestCase $configuration->method('getTestMode')->willReturn(true); d3GetOxidDIC()->set(Configuration::class, $configuration); + /** @var Logger|MockObject $loggerMock */ + $loggerMock = $this->getMockBuilder(Logger::class) + ->onlyMethods(['error']) + ->disableOriginalConstructor() + ->getMock(); + $loggerMock->method('error'); + d3GetOxidDIC()->set('d3ox.linkmobility.'.LoggerInterface::class, $loggerMock); + /** @var User $user */ $this->user = $user = oxNew(User::class); $user->setId($this->userId); @@ -298,11 +310,7 @@ class adminUserTest extends LMIntegrationTestCase ); // check return message - $search = sprintf( - Registry::getLang()->translateString('D3LM_EXC_MESSAGE_UNEXPECTED_ERR_SEND'), - 'no response' - ); - + $search = Registry::getLang()->translateString('D3LM_EXC_NO_RECIPIENT_SET', null, false); $this->assertTrue( (bool) strpos(serialize(Registry::getSession()->getVariable('Errors')), $search) ); diff --git a/src/tests/integration/canceledOrderTest.php b/src/tests/integration/canceledOrderTest.php index c97b475..ce6a171 100644 --- a/src/tests/integration/canceledOrderTest.php +++ b/src/tests/integration/canceledOrderTest.php @@ -23,6 +23,7 @@ use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Middleware; use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Response; +use Monolog\Logger; use OxidEsales\Eshop\Application\Model\Order; use OxidEsales\Eshop\Application\Model\Remark; use OxidEsales\Eshop\Application\Model\User; @@ -32,6 +33,7 @@ use PHPUnit\Framework\MockObject\MockObject; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use Psr\Http\Message\RequestInterface; +use Psr\Log\LoggerInterface; class canceledOrderTest extends LMIntegrationTestCase { @@ -54,6 +56,14 @@ class canceledOrderTest extends LMIntegrationTestCase $configuration->method('sendOrderFinishedMessage')->willReturn(false); d3GetOxidDIC()->set(Configuration::class, $configuration); + /** @var Logger|MockObject $loggerMock */ + $loggerMock = $this->getMockBuilder(Logger::class) + ->onlyMethods(['error']) + ->disableOriginalConstructor() + ->getMock(); + $loggerMock->method('error'); + d3GetOxidDIC()->set('d3ox.linkmobility.'.LoggerInterface::class, $loggerMock); + /** @var User $user */ $this->user = $user = oxNew(User::class); $user->setId($this->userId); diff --git a/src/tests/integration/finishedOrderTest.php b/src/tests/integration/finishedOrderTest.php index 3440546..e6a96ab 100644 --- a/src/tests/integration/finishedOrderTest.php +++ b/src/tests/integration/finishedOrderTest.php @@ -25,6 +25,7 @@ use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Middleware; use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Response; +use Monolog\Logger; use OxidEsales\Eshop\Application\Model\Order; use OxidEsales\Eshop\Application\Model\Remark; use OxidEsales\Eshop\Application\Model\User; @@ -35,6 +36,7 @@ use PHPUnit\Framework\MockObject\MockObject; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use Psr\Http\Message\RequestInterface; +use Psr\Log\LoggerInterface; class finishedOrderTest extends LMIntegrationTestCase { @@ -57,6 +59,14 @@ class finishedOrderTest extends LMIntegrationTestCase $configuration->method('sendOrderFinishedMessage')->willReturn(true); d3GetOxidDIC()->set(Configuration::class, $configuration); + /** @var Logger|MockObject $loggerMock */ + $loggerMock = $this->getMockBuilder(Logger::class) + ->onlyMethods(['error']) + ->disableOriginalConstructor() + ->getMock(); + $loggerMock->method('error'); + d3GetOxidDIC()->set('d3ox.linkmobility.'.LoggerInterface::class, $loggerMock); + /** @var User $user */ $this->user = $user = oxNew(User::class); $user->setId($this->userId); diff --git a/src/tests/integration/sendedNowOrderTest.php b/src/tests/integration/sendedNowOrderTest.php index d3a60c4..2e25677 100644 --- a/src/tests/integration/sendedNowOrderTest.php +++ b/src/tests/integration/sendedNowOrderTest.php @@ -24,6 +24,7 @@ use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Middleware; use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Response; +use Monolog\Logger; use OxidEsales\Eshop\Application\Model\Order; use OxidEsales\Eshop\Application\Model\Remark; use OxidEsales\Eshop\Application\Model\User; @@ -34,6 +35,7 @@ use PHPUnit\Framework\MockObject\MockObject; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use Psr\Http\Message\RequestInterface; +use Psr\Log\LoggerInterface; class sendedNowOrderTest extends LMIntegrationTestCase { @@ -370,6 +372,14 @@ class sendedNowOrderTest extends LMIntegrationTestCase $this->deleteAllRemarksFrom($this->userId); + /** @var Logger|MockObject $loggerMock */ + $loggerMock = $this->getMockBuilder(Logger::class) + ->onlyMethods(['error']) + ->disableOriginalConstructor() + ->getMock(); + $loggerMock->method('error'); + d3GetOxidDIC()->set('d3ox.linkmobility.'.LoggerInterface::class, $loggerMock); + $this->setClientResponse( new RequestException( 'Error Communicating with Server', diff --git a/src/tests/unit/Application/Controller/Admin/AdminOrderTest.php b/src/tests/unit/Application/Controller/Admin/AdminOrderTest.php index 67341a3..11e88d8 100644 --- a/src/tests/unit/Application/Controller/Admin/AdminOrderTest.php +++ b/src/tests/unit/Application/Controller/Admin/AdminOrderTest.php @@ -16,11 +16,15 @@ declare(strict_types=1); namespace D3\Linkmobility4OXID\tests\unit\Application\Controller\Admin; use D3\Linkmobility4OXID\Application\Controller\Admin\AdminOrder; +use D3\Linkmobility4OXID\Application\Model\Exceptions\noRecipientFoundException; use D3\Linkmobility4OXID\Application\Model\Exceptions\successfullySentException; use D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms; use D3\Linkmobility4OXID\Application\Model\OrderRecipients; +use D3\LinkmobilityClient\LoggerHandler; use D3\TestingTools\Development\CanAccessRestricted; +use Monolog\Logger; use OxidEsales\Eshop\Application\Model\Order; +use OxidEsales\Eshop\Core\UtilsView; use PHPUnit\Framework\MockObject\MockObject; use ReflectionException; @@ -60,20 +64,48 @@ class AdminOrderTest extends AdminSend /** * @test + * * @param $canSendItemMessage + * @param $throwException + * * @return void * @throws ReflectionException - * @covers \D3\Linkmobility4OXID\Application\Controller\Admin\AdminOrder::sendMessage + * @covers \D3\Linkmobility4OXID\Application\Controller\Admin\AdminOrder::sendMessage * @dataProvider canSendMessageDataProvider */ - public function canSendMessage($canSendItemMessage) + public function canSendMessage($canSendItemMessage, $throwException) { /** @var Sms|MockObject $smsMock */ $smsMock = $this->getMockBuilder(Sms::class) ->disableOriginalConstructor() ->onlyMethods(['sendOrderMessage']) ->getMock(); - $smsMock->expects($this->once())->method('sendOrderMessage')->willReturn($canSendItemMessage); + $smsMock->expects($this->once())->method('sendOrderMessage')->will( + $throwException ? + $this->throwException(oxNew(noRecipientFoundException::class)) : + $this->returnValue($canSendItemMessage) + ); + + /** @var Logger|MockObject $loggerMock */ + $loggerMock = $this->getMockBuilder(Logger::class) + ->onlyMethods(['warning']) + ->disableOriginalConstructor() + ->getMock(); + $loggerMock->expects($this->exactly((int) $throwException))->method('warning'); + + /** @var LoggerHandler|MockObject $loggerHandlerMock */ + $loggerHandlerMock = $this->getMockBuilder(LoggerHandler::class) + ->onlyMethods(['getLogger']) + ->getMock(); + $loggerHandlerMock->method('getLogger')->willReturn($loggerMock); + d3GetOxidDIC()->set(LoggerHandler::class, $loggerHandlerMock); + + /** @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 AdminOrder|MockObject $sut */ $sut = $this->getMockBuilder(AdminOrder::class) @@ -83,7 +115,7 @@ class AdminOrderTest extends AdminSend $sut->method('getMessageBody')->willReturn('messageBodyFixture'); $sut->expects($this->exactly((int) $canSendItemMessage))->method('getSuccessSentMessage') ->willReturn(oxNew(successfullySentException::class, 'expectedReturn')); - $sut->expects($this->exactly((int) !$canSendItemMessage))->method('getUnsuccessfullySentMessage') + $sut->expects($this->exactly((int) (!$canSendItemMessage && !$throwException)))->method('getUnsuccessfullySentMessage') ->willReturn('expectedReturn'); $sut->method('getSms')->willReturn($smsMock); diff --git a/src/tests/unit/Application/Controller/Admin/AdminSend.php b/src/tests/unit/Application/Controller/Admin/AdminSend.php index b97fbc6..8097f55 100644 --- a/src/tests/unit/Application/Controller/Admin/AdminSend.php +++ b/src/tests/unit/Application/Controller/Admin/AdminSend.php @@ -27,6 +27,7 @@ use D3\LinkmobilityClient\SMS\Response; use D3\LinkmobilityClient\ValueObject\Recipient; use D3\TestingTools\Development\CanAccessRestricted; use InvalidArgumentException; +use JetBrains\PhpStorm\ArrayShape; use OxidEsales\Eshop\Core\Request; use OxidEsales\Eshop\Core\UtilsView; use PHPUnit\Framework\MockObject\MockObject; @@ -208,7 +209,7 @@ class AdminSend extends LMUnitTestCase ->getMock(); $sut->method('sendMessage')->will( $throwsException ? - $this->throwException(oxNew(noRecipientFoundException::class)) : + $this->throwException(oxNew(InvalidArgumentException::class)) : $this->returnValue('successfully sent message') ); @@ -287,8 +288,9 @@ class AdminSend extends LMUnitTestCase public function canSendMessageDataProvider(): array { return [ - 'send item message' => [true], - 'dont send item message' => [false] + 'send item message' => [true, false], + 'dont send item message' => [false, false], + 'throw exception' => [false, true] ]; } diff --git a/src/tests/unit/Application/Controller/Admin/AdminUserTest.php b/src/tests/unit/Application/Controller/Admin/AdminUserTest.php index 85ca6bf..e3d8e09 100644 --- a/src/tests/unit/Application/Controller/Admin/AdminUserTest.php +++ b/src/tests/unit/Application/Controller/Admin/AdminUserTest.php @@ -16,11 +16,15 @@ declare(strict_types=1); namespace D3\Linkmobility4OXID\tests\unit\Application\Controller\Admin; use D3\Linkmobility4OXID\Application\Controller\Admin\AdminUser; +use D3\Linkmobility4OXID\Application\Model\Exceptions\noRecipientFoundException; use D3\Linkmobility4OXID\Application\Model\Exceptions\successfullySentException; use D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms; use D3\Linkmobility4OXID\Application\Model\UserRecipients; +use D3\LinkmobilityClient\LoggerHandler; use D3\TestingTools\Development\CanAccessRestricted; +use Monolog\Logger; use OxidEsales\Eshop\Application\Model\User; +use OxidEsales\Eshop\Core\UtilsView; use PHPUnit\Framework\MockObject\MockObject; use ReflectionException; @@ -60,20 +64,48 @@ class AdminUserTest extends AdminSend /** * @test + * * @param $canSendItemMessage + * @param $throwException + * * @return void * @throws ReflectionException - * @covers \D3\Linkmobility4OXID\Application\Controller\Admin\AdminUser::sendMessage + * @covers \D3\Linkmobility4OXID\Application\Controller\Admin\AdminUser::sendMessage * @dataProvider canSendMessageDataProvider */ - public function canSendMessage($canSendItemMessage) + public function canSendMessage($canSendItemMessage, $throwException) { /** @var Sms|MockObject $smsMock */ $smsMock = $this->getMockBuilder(Sms::class) ->disableOriginalConstructor() ->onlyMethods(['sendUserAccountMessage']) ->getMock(); - $smsMock->expects($this->once())->method('sendUserAccountMessage')->willReturn($canSendItemMessage); + $smsMock->expects($this->once())->method('sendUserAccountMessage')->will( + $throwException ? + $this->throwException(oxNew(noRecipientFoundException::class)) : + $this->returnValue($canSendItemMessage) + ); + + /** @var Logger|MockObject $loggerMock */ + $loggerMock = $this->getMockBuilder(Logger::class) + ->onlyMethods(['warning']) + ->disableOriginalConstructor() + ->getMock(); + $loggerMock->expects($this->exactly((int) $throwException))->method('warning'); + + /** @var LoggerHandler|MockObject $loggerHandlerMock */ + $loggerHandlerMock = $this->getMockBuilder(LoggerHandler::class) + ->onlyMethods(['getLogger']) + ->getMock(); + $loggerHandlerMock->method('getLogger')->willReturn($loggerMock); + d3GetOxidDIC()->set(LoggerHandler::class, $loggerHandlerMock); + + /** @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 AdminUser|MockObject $sut */ $sut = $this->getMockBuilder(AdminUser::class) @@ -83,7 +115,7 @@ class AdminUserTest extends AdminSend $sut->method('getMessageBody')->willReturn('messageBodyFixture'); $sut->expects($this->exactly((int) $canSendItemMessage))->method('getSuccessSentMessage') ->willReturn(oxNew(successfullySentException::class, 'expectedReturn')); - $sut->expects($this->exactly((int) !$canSendItemMessage))->method('getUnsuccessfullySentMessage') + $sut->expects($this->exactly((int) (!$canSendItemMessage && !$throwException)))->method('getUnsuccessfullySentMessage') ->willReturn('expectedReturn'); $sut->method('getSms')->willReturn($smsMock); diff --git a/src/tests/unit/Application/Model/MessageSenderTest.php b/src/tests/unit/Application/Model/MessageSenderTest.php index 38e4bee..22f33f6 100644 --- a/src/tests/unit/Application/Model/MessageSenderTest.php +++ b/src/tests/unit/Application/Model/MessageSenderTest.php @@ -20,7 +20,9 @@ use D3\Linkmobility4OXID\Application\Model\Exceptions\noRecipientFoundException; use D3\Linkmobility4OXID\Application\Model\MessageSender; use D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms; use D3\Linkmobility4OXID\tests\unit\LMUnitTestCase; +use D3\LinkmobilityClient\LoggerHandler; use D3\TestingTools\Development\CanAccessRestricted; +use Monolog\Logger; use OxidEsales\Eshop\Application\Model\Order; use PHPUnit\Framework\MockObject\MockObject; use ReflectionException; @@ -31,15 +33,32 @@ class MessageSenderTest extends LMUnitTestCase /** * @test + * * @param $actionConfigured * @param $invocationCount + * @param $throwException + * * @return void * @throws ReflectionException * @dataProvider canSendOrderFinishedMessageDataProvider - * @covers \D3\Linkmobility4OXID\Application\Model\MessageSender::sendOrderFinishedMessage + * @covers \D3\Linkmobility4OXID\Application\Model\MessageSender::sendOrderFinishedMessage */ - public function canSendOrderFinishedMessage($actionConfigured, $invocationCount) + public function canSendOrderFinishedMessage($actionConfigured, $invocationCount, $throwException) { + /** @var Logger|MockObject $loggerMock */ + $loggerMock = $this->getMockBuilder(Logger::class) + ->onlyMethods(['debug']) + ->disableOriginalConstructor() + ->getMock(); + $loggerMock->expects($this->exactly((int) $throwException))->method('debug'); + + /** @var LoggerHandler|MockObject $loggerHandlerMock */ + $loggerHandlerMock = $this->getMockBuilder(LoggerHandler::class) + ->onlyMethods(['getLogger']) + ->getMock(); + $loggerHandlerMock->method('getLogger')->willReturn($loggerMock); + d3GetOxidDIC()->set(LoggerHandler::class, $loggerHandlerMock); + /** @var Configuration|MockObject $configurationMock */ $configurationMock = $this->getMockBuilder(Configuration::class) ->onlyMethods(['sendOrderFinishedMessage']) @@ -56,7 +75,12 @@ class MessageSenderTest extends LMUnitTestCase ->onlyMethods(['getConfiguration', 'sendMessageByOrder']) ->getMock(); $sut->method('getConfiguration')->willReturn($configurationMock); - $sut->expects($invocationCount)->method('sendMessageByOrder'); + if ($throwException) { + $sut->expects( $invocationCount )->method( 'sendMessageByOrder' ) + ->willThrowException( oxNew( noRecipientFoundException::class ) ); + } else { + $sut->expects( $invocationCount )->method( 'sendMessageByOrder' ); + } $this->callMethod( $sut, @@ -67,15 +91,32 @@ class MessageSenderTest extends LMUnitTestCase /** * @test + * * @param $actionConfigured * @param $invocationCount + * @param $throwException + * * @return void * @throws ReflectionException * @dataProvider canSendOrderFinishedMessageDataProvider - * @covers \D3\Linkmobility4OXID\Application\Model\MessageSender::sendSendedNowMessage + * @covers \D3\Linkmobility4OXID\Application\Model\MessageSender::sendSendedNowMessage */ - public function canSendSendedNowMessage($actionConfigured, $invocationCount) + public function canSendSendedNowMessage($actionConfigured, $invocationCount, $throwException) { + /** @var Logger|MockObject $loggerMock */ + $loggerMock = $this->getMockBuilder(Logger::class) + ->onlyMethods(['debug']) + ->disableOriginalConstructor() + ->getMock(); + $loggerMock->expects($this->exactly((int) $throwException))->method('debug'); + + /** @var LoggerHandler|MockObject $loggerHandlerMock */ + $loggerHandlerMock = $this->getMockBuilder(LoggerHandler::class) + ->onlyMethods(['getLogger']) + ->getMock(); + $loggerHandlerMock->method('getLogger')->willReturn($loggerMock); + d3GetOxidDIC()->set(LoggerHandler::class, $loggerHandlerMock); + /** @var Configuration|MockObject $configurationMock */ $configurationMock = $this->getMockBuilder(Configuration::class) ->onlyMethods(['sendOrderSendedNowMessage']) @@ -92,7 +133,12 @@ class MessageSenderTest extends LMUnitTestCase ->onlyMethods(['getConfiguration', 'sendMessageByOrder']) ->getMock(); $sut->method('getConfiguration')->willReturn($configurationMock); - $sut->expects($invocationCount)->method('sendMessageByOrder'); + if ($throwException) { + $sut->expects( $invocationCount )->method( 'sendMessageByOrder' ) + ->willThrowException( oxNew( noRecipientFoundException::class ) ); + } else { + $sut->expects( $invocationCount )->method( 'sendMessageByOrder' ); + } $this->callMethod( $sut, @@ -103,15 +149,32 @@ class MessageSenderTest extends LMUnitTestCase /** * @test + * * @param $actionConfigured * @param $invocationCount + * @param $throwException + * * @return void * @throws ReflectionException * @dataProvider canSendOrderFinishedMessageDataProvider - * @covers \D3\Linkmobility4OXID\Application\Model\MessageSender::sendCancelOrderMessage + * @covers \D3\Linkmobility4OXID\Application\Model\MessageSender::sendCancelOrderMessage */ - public function canSendCancelOrderMessage($actionConfigured, $invocationCount) + public function canSendCancelOrderMessage($actionConfigured, $invocationCount, $throwException) { + /** @var Logger|MockObject $loggerMock */ + $loggerMock = $this->getMockBuilder(Logger::class) + ->onlyMethods(['debug']) + ->disableOriginalConstructor() + ->getMock(); + $loggerMock->expects($this->exactly((int) $throwException))->method('debug'); + + /** @var LoggerHandler|MockObject $loggerHandlerMock */ + $loggerHandlerMock = $this->getMockBuilder(LoggerHandler::class) + ->onlyMethods(['getLogger']) + ->getMock(); + $loggerHandlerMock->method('getLogger')->willReturn($loggerMock); + d3GetOxidDIC()->set(LoggerHandler::class, $loggerHandlerMock); + /** @var Configuration|MockObject $configurationMock */ $configurationMock = $this->getMockBuilder(Configuration::class) ->onlyMethods(['sendOrderCanceledMessage']) @@ -128,7 +191,12 @@ class MessageSenderTest extends LMUnitTestCase ->onlyMethods(['getConfiguration', 'sendMessageByOrder']) ->getMock(); $sut->method('getConfiguration')->willReturn($configurationMock); - $sut->expects($invocationCount)->method('sendMessageByOrder'); + if ($throwException) { + $sut->expects( $invocationCount )->method( 'sendMessageByOrder' ) + ->willThrowException( oxNew( noRecipientFoundException::class ) ); + } else { + $sut->expects( $invocationCount )->method( 'sendMessageByOrder' ); + } $this->callMethod( $sut, @@ -143,8 +211,9 @@ class MessageSenderTest extends LMUnitTestCase public function canSendOrderFinishedMessageDataProvider(): array { return [ - 'action configured' => [true, $this->once()], - 'action not configured' => [false, $this->never()], + 'action configured, no exc' => [true, $this->once(), false], + 'action configured, throw exc' => [true, $this->once(), true], + 'action not configured' => [false, $this->never(), false], ]; } @@ -152,24 +221,19 @@ class MessageSenderTest extends LMUnitTestCase * @test * @param $messageBody * @param $invocationCount - * @param $throwException * @return void * @throws ReflectionException * @dataProvider canSendMessageByOrderDataProvider * @covers \D3\Linkmobility4OXID\Application\Model\MessageSender::sendMessageByOrder */ - public function canSendMessageByOrder($messageBody, $invocationCount, $throwException) + public function canSendMessageByOrder($messageBody, $invocationCount) { /** @var Sms|MockObject $smsMock */ $smsMock = $this->getMockBuilder(Sms::class) ->onlyMethods(['sendOrderMessage']) ->disableOriginalConstructor() ->getMock(); - $smsMock->expects($invocationCount)->method('sendOrderMessage')->will( - $throwException ? - $this->throwException(d3GetOxidDIC()->get(noRecipientFoundException::class)) : - $this->returnValue(true) - ); + $smsMock->expects($invocationCount)->method('sendOrderMessage'); /** @var Order|MockObject $orderMock */ $orderMock = $this->getMockBuilder(Order::class) @@ -195,10 +259,9 @@ class MessageSenderTest extends LMUnitTestCase public function canSendMessageByOrderDataProvider(): array { return [ - 'has message body no exc' => ['body', $this->once(), false], - 'has message body throw exc'=> ['body', $this->once(), true], - 'spaced message body' => [' ', $this->never(), false], - 'empty message body' => ['', $this->never(), false], + 'has message body' => ['body', $this->once()], + 'spaced message body' => [' ', $this->never()], + 'empty message body' => ['', $this->never()], ]; } diff --git a/src/tests/unit/Application/Model/MessageTypes/SmsTest.php b/src/tests/unit/Application/Model/MessageTypes/SmsTest.php index 3e37128..9b0df7e 100644 --- a/src/tests/unit/Application/Model/MessageTypes/SmsTest.php +++ b/src/tests/unit/Application/Model/MessageTypes/SmsTest.php @@ -269,14 +269,13 @@ class SmsTest extends LMUnitTestCase /** * @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) + public function canSendUserAccountMessage($sendReturn, $setRemark) { /** @var User|MockObject $userMock */ $userMock = $this->getMockBuilder(User::class) @@ -285,20 +284,6 @@ class SmsTest extends LMUnitTestCase ->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() @@ -306,16 +291,11 @@ class SmsTest extends LMUnitTestCase /** @var Sms|MockObject $sut */ $sut = $this->getMockBuilder(Sms::class) - ->onlyMethods(['getLogger', 'sendCustomRecipientMessage', 'getUserRecipientsList', 'setRemark', + ->onlyMethods(['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->method('sendCustomRecipientMessage')->willReturn($sendReturn); $sut->expects($setRemark ? $this->once() : $this->never())->method('setRemark'); $sut->method('getUserRecipientsList')->willReturn($recipientsListMock); $sut->method('getRecipientsList')->willReturn('abc,def'); @@ -337,9 +317,8 @@ class SmsTest extends LMUnitTestCase public function canSendUserAccountMessageDataProvider(): array { return [ - 'can send' => [true, false, true], - 'cant send' => [false, false, false], - 'no recipient' => [false, true, false] + 'can send' => [true, true], + 'cant send' => [false, false], ]; } @@ -399,14 +378,13 @@ class SmsTest extends LMUnitTestCase /** * @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) + public function canSendOrderMessage($sendReturn, $setRemark) { /** @var User|MockObject $userMock */ $userMock = $this->getMockBuilder(User::class) @@ -423,20 +401,6 @@ class SmsTest extends LMUnitTestCase $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() @@ -444,16 +408,11 @@ class SmsTest extends LMUnitTestCase /** @var Sms|MockObject $sut */ $sut = $this->getMockBuilder(Sms::class) - ->onlyMethods(['getLogger', 'sendCustomRecipientMessage', 'getOrderRecipientsList', 'setRemark', + ->onlyMethods(['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->method('sendCustomRecipientMessage')->willReturn($sendReturn); $sut->expects($setRemark ? $this->once() : $this->never())->method('setRemark'); $sut->method('getOrderRecipientsList')->willReturn($recipientsListMock); $sut->method('getRecipientsList')->willReturn('abc,def'); @@ -584,10 +543,10 @@ class SmsTest extends LMUnitTestCase /** @var Logger|MockObject $loggerMock */ $loggerMock = $this->getMockBuilder(Logger::class) - ->onlyMethods(['warning']) + ->onlyMethods(['error']) ->disableOriginalConstructor() ->getMock(); - $loggerMock->expects($this->exactly((int) $throwException))->method('warning'); + $loggerMock->expects($this->exactly((int) $throwException))->method('error'); /** @var UtilsView|MockObject $utilsViewMock */ $utilsViewMock = $this->getMockBuilder(UtilsView::class)