don't show recipient issues and detailed request errors in frontend

This commit is contained in:
Daniel Seifert 2023-01-16 12:12:55 +01:00 committed by Daniel Seifert
parent 358805d12b
commit 2897097076
Signed by: DanielS
GPG Key ID: 6A513E13AEE66170
19 changed files with 392 additions and 174 deletions

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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
{
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 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) {
}
}
/**

View File

@ -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,11 +47,10 @@ 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) {
@ -60,14 +58,6 @@ class Sms extends AbstractMessage
}
$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);
}
return false;
}
/**
@ -101,7 +91,6 @@ 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) {
@ -109,14 +98,6 @@ class Sms extends AbstractMessage
}
$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);
}
return false;
}
/**
@ -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;
}
}

View File

@ -0,0 +1,29 @@
<?php
/**
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* @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

View File

@ -0,0 +1,29 @@
<?php
/**
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* @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

View File

@ -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

View File

@ -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

View File

@ -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 ) );
}
/**

View File

@ -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

View File

@ -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)
);

View File

@ -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);

View File

@ -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);

View File

@ -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',

View File

@ -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
* @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);

View File

@ -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]
];
}

View File

@ -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
* @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);

View File

@ -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
*/
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
*/
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
*/
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()],
];
}

View File

@ -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)