From c117e9509d0ee2f939e48d32bbdf09bd8094330f Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Wed, 4 Jan 2023 23:58:37 +0100 Subject: [PATCH] move shared code to separate classes to prevent errors while phpunit coverage test run --- .../Controller/Admin/AdminOrder.php | 101 +++++++++++- .../Controller/Admin/AdminSendController.php | 144 ------------------ .../Controller/Admin/AdminUser.php | 101 +++++++++++- .../Controller/Admin/AdminOrderTest.php | 1 - 4 files changed, 197 insertions(+), 150 deletions(-) delete mode 100644 src/Application/Controller/Admin/AdminSendController.php diff --git a/src/Application/Controller/Admin/AdminOrder.php b/src/Application/Controller/Admin/AdminOrder.php index 0a89a6e..ea0bcf4 100644 --- a/src/Application/Controller/Admin/AdminOrder.php +++ b/src/Application/Controller/Admin/AdminOrder.php @@ -15,14 +15,22 @@ declare(strict_types=1); namespace D3\Linkmobility4OXID\Application\Controller\Admin; -use D3\DIContainerHandler\d3DicHandler; 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\Response\ResponseInterface; +use D3\LinkmobilityClient\ValueObject\Recipient; use D3\TestingTools\Production\IsMockable; +use InvalidArgumentException; +use OxidEsales\Eshop\Application\Controller\Admin\AdminController; use OxidEsales\Eshop\Application\Model\Order; +use OxidEsales\Eshop\Core\Language; +use OxidEsales\Eshop\Core\Registry; +use OxidEsales\Eshop\Core\Request; +use OxidEsales\Eshop\Core\UtilsView; -class AdminOrder extends AdminSendController +class AdminOrder extends AdminController { use IsMockable; @@ -39,6 +47,11 @@ class AdminOrder extends AdminSendController $this->item = d3GetOxidDIC()->get('d3ox.linkmobility.'.Order::class); d3GetOxidDIC()->set(OrderRecipients::class.'.args.order', $this->item); $this->itemRecipients = d3GetOxidDIC()->get(OrderRecipients::class); + + $this->item->load($this->getEditObjectId()); + + $this->addTplParam('recipient', $this->getRecipientFromCurrentItem()); + parent::__construct(); } @@ -55,4 +68,88 @@ class AdminOrder extends AdminSendController (string) $this->getSuccessSentMessage($sms) : $this->getUnsuccessfullySentMessage($sms); } + + /*** duplicated code but errors while phpunit coverage run if code is in shared abstract class or trait ***/ + + /** + * @return Recipient|false + */ + public function getRecipientFromCurrentItem() + { + try { + return $this->itemRecipients->getSmsRecipient(); + } catch (noRecipientFoundException $e) { + /** @var Language $lang */ + $lang = d3GetOxidDIC()->get('d3ox.linkmobility.'.Language::class); + $message = $lang->translateString($e->getMessage()); + /** @var UtilsView $utilsView */ + $utilsView = d3GetOxidDIC()->get('d3ox.linkmobility.'.UtilsView::class); + $utilsView->addErrorToDisplay($message); + } + + return false; + } + + /** + * @return void + */ + public function send(): void + { + /** @var UtilsView $utilsView */ + $utilsView = d3GetOxidDIC()->get('d3ox.linkmobility.'.UtilsView::class); + + try { + $utilsView->addErrorToDisplay($this->sendMessage()); + } catch (noRecipientFoundException|InvalidArgumentException $e) { + $utilsView->addErrorToDisplay($e); + } + } + + /** + * @return string + * @throws InvalidArgumentException + */ + protected function getMessageBody(): string + { + /** @var Request $request */ + $request = d3GetOxidDIC()->get('d3ox.linkmobility.'.Request::class); + $messageBody = $request->getRequestEscapedParameter('messagebody'); + + if (false === is_string($messageBody) || strlen(trim($messageBody)) <= 1) { + d3GetOxidDIC()->setParameter( + 'd3ox.linkmobility.'.InvalidArgumentException::class.'.args.message', + Registry::getLang()->translateString('D3LM_EXC_MESSAGE_NO_LENGTH') + ); + /** @var InvalidArgumentException $exc */ + $exc = d3GetOxidDIC()->get('d3ox.linkmobility.'.InvalidArgumentException::class); + throw $exc; + } + + return $messageBody; + } + + /** + * @param Sms $sms + * @return successfullySentException + */ + protected function getSuccessSentMessage(Sms $sms): successfullySentException + { + $smsCount = $sms->getResponse() ? $sms->getResponse()->getSmsCount() : 0; + d3GetOxidDIC()->setParameter(successfullySentException::class.'.args.smscount', $smsCount); + /** @var successfullySentException $exc */ + $exc = d3GetOxidDIC()->get(successfullySentException::class); + return $exc; + } + + /** + * @param Sms $sms + * @return string + */ + protected function getUnsuccessfullySentMessage(Sms $sms): string + { + $errorMsg = $sms->getResponse() instanceof ResponseInterface ? $sms->getResponse()->getErrorMessage() : 'no response'; + /** @var string $format */ + $format = Registry::getLang()->translateString('D3LM_EXC_MESSAGE_UNEXPECTED_ERR_SEND'); + return sprintf($format, $errorMsg); + } } diff --git a/src/Application/Controller/Admin/AdminSendController.php b/src/Application/Controller/Admin/AdminSendController.php deleted file mode 100644 index 74a1fa2..0000000 --- a/src/Application/Controller/Admin/AdminSendController.php +++ /dev/null @@ -1,144 +0,0 @@ - - * @link https://www.oxidmodule.com - */ - -declare(strict_types=1); - -namespace D3\Linkmobility4OXID\Application\Controller\Admin; - -use D3\DIContainerHandler\d3DicHandler; -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\Linkmobility4OXID\Application\Model\UserRecipients; -use D3\LinkmobilityClient\Response\ResponseInterface; -use D3\LinkmobilityClient\ValueObject\Recipient; -use D3\TestingTools\Production\IsMockable; -use InvalidArgumentException; -use OxidEsales\Eshop\Application\Controller\Admin\AdminController; -use OxidEsales\Eshop\Application\Model\Order; -use OxidEsales\Eshop\Application\Model\User; -use OxidEsales\Eshop\Core\Language; -use OxidEsales\Eshop\Core\Registry; -use OxidEsales\Eshop\Core\Request; -use OxidEsales\Eshop\Core\UtilsView; - -abstract class AdminSendController extends AdminController -{ - use IsMockable; - - /** - * @var Order|User - */ - protected $item; - - /** @var OrderRecipients|UserRecipients */ - protected $itemRecipients; - - public function __construct() - { - $this->item->load($this->getEditObjectId()); - - $this->addTplParam('recipient', $this->getRecipientFromCurrentItem()); - - parent::__construct(); - } - - /** - * @return Recipient|false - */ - public function getRecipientFromCurrentItem() - { - try { - return $this->itemRecipients->getSmsRecipient(); - } catch (noRecipientFoundException $e) { - /** @var Language $lang */ - $lang = d3GetOxidDIC()->get('d3ox.linkmobility.'.Language::class); - $message = $lang->translateString($e->getMessage()); - /** @var UtilsView $utilsView */ - $utilsView = d3GetOxidDIC()->get('d3ox.linkmobility.'.UtilsView::class); - $utilsView->addErrorToDisplay($message); - } - - return false; - } - - /** - * @return void - */ - public function send(): void - { - /** @var UtilsView $utilsView */ - $utilsView = d3GetOxidDIC()->get('d3ox.linkmobility.'.UtilsView::class); - - try { - $utilsView->addErrorToDisplay($this->sendMessage()); - } catch (noRecipientFoundException|InvalidArgumentException $e) { - $utilsView->addErrorToDisplay($e); - } - } - - /** - * @return string - * @throws InvalidArgumentException - */ - protected function getMessageBody(): string - { - /** @var Request $request */ - $request = d3GetOxidDIC()->get('d3ox.linkmobility.'.Request::class); - $messageBody = $request->getRequestEscapedParameter('messagebody'); - - if (false === is_string($messageBody) || strlen(trim($messageBody)) <= 1) { - d3GetOxidDIC()->setParameter( - 'd3ox.linkmobility.'.InvalidArgumentException::class.'.args.message', - Registry::getLang()->translateString('D3LM_EXC_MESSAGE_NO_LENGTH') - ); - /** @var InvalidArgumentException $exc */ - $exc = d3GetOxidDIC()->get('d3ox.linkmobility.'.InvalidArgumentException::class); - throw $exc; - } - - return $messageBody; - } - - /** - * @return string - * @throws noRecipientFoundException - */ - abstract protected function sendMessage(): string; - - /** - * @param Sms $sms - * @return successfullySentException - */ - protected function getSuccessSentMessage(Sms $sms): successfullySentException - { - $smsCount = $sms->getResponse() ? $sms->getResponse()->getSmsCount() : 0; - d3GetOxidDIC()->setParameter(successfullySentException::class.'.args.smscount', $smsCount); - /** @var successfullySentException $exc */ - $exc = d3GetOxidDIC()->get(successfullySentException::class); - return $exc; - } - - /** - * @param Sms $sms - * @return string - */ - protected function getUnsuccessfullySentMessage(Sms $sms): string - { - $errorMsg = $sms->getResponse() instanceof ResponseInterface ? $sms->getResponse()->getErrorMessage() : 'no response'; - /** @var string $format */ - $format = Registry::getLang()->translateString('D3LM_EXC_MESSAGE_UNEXPECTED_ERR_SEND'); - return sprintf($format, $errorMsg); - } -} \ No newline at end of file diff --git a/src/Application/Controller/Admin/AdminUser.php b/src/Application/Controller/Admin/AdminUser.php index c969f7c..c23f5c2 100644 --- a/src/Application/Controller/Admin/AdminUser.php +++ b/src/Application/Controller/Admin/AdminUser.php @@ -15,14 +15,21 @@ declare(strict_types=1); namespace D3\Linkmobility4OXID\Application\Controller\Admin; -use D3\DIContainerHandler\d3DicHandler; 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 OxidEsales\Eshop\Application\Model\Order; +use D3\LinkmobilityClient\Response\ResponseInterface; +use D3\LinkmobilityClient\ValueObject\Recipient; +use InvalidArgumentException; +use OxidEsales\Eshop\Application\Controller\Admin\AdminController; use OxidEsales\Eshop\Application\Model\User; +use OxidEsales\Eshop\Core\Language; +use OxidEsales\Eshop\Core\Registry; +use OxidEsales\Eshop\Core\Request; +use OxidEsales\Eshop\Core\UtilsView; -class AdminUser extends AdminSendController +class AdminUser extends AdminController { protected $_sThisTemplate = 'd3adminuser.tpl'; @@ -42,6 +49,10 @@ class AdminUser extends AdminSendController $this->item = d3GetOxidDIC()->get('d3ox.linkmobility.'.User::class); d3GetOxidDIC()->set(UserRecipients::class.".args.user", $this->item); $this->itemRecipients = d3GetOxidDIC()->get(UserRecipients::class); + $this->item->load($this->getEditObjectId()); + + $this->addTplParam('recipient', $this->getRecipientFromCurrentItem()); + parent::__construct(); } @@ -58,4 +69,88 @@ class AdminUser extends AdminSendController (string) $this->getSuccessSentMessage($sms) : $this->getUnsuccessfullySentMessage($sms); } + + /*** duplicated code but errors while phpunit coverage run if code is in shared abstract class or trait ***/ + + /** + * @return Recipient|false + */ + public function getRecipientFromCurrentItem() + { + try { + return $this->itemRecipients->getSmsRecipient(); + } catch (noRecipientFoundException $e) { + /** @var Language $lang */ + $lang = d3GetOxidDIC()->get('d3ox.linkmobility.'.Language::class); + $message = $lang->translateString($e->getMessage()); + /** @var UtilsView $utilsView */ + $utilsView = d3GetOxidDIC()->get('d3ox.linkmobility.'.UtilsView::class); + $utilsView->addErrorToDisplay($message); + } + + return false; + } + + /** + * @return void + */ + public function send(): void + { + /** @var UtilsView $utilsView */ + $utilsView = d3GetOxidDIC()->get('d3ox.linkmobility.'.UtilsView::class); + + try { + $utilsView->addErrorToDisplay($this->sendMessage()); + } catch (noRecipientFoundException|InvalidArgumentException $e) { + $utilsView->addErrorToDisplay($e); + } + } + + /** + * @return string + * @throws InvalidArgumentException + */ + protected function getMessageBody(): string + { + /** @var Request $request */ + $request = d3GetOxidDIC()->get('d3ox.linkmobility.'.Request::class); + $messageBody = $request->getRequestEscapedParameter('messagebody'); + + if (false === is_string($messageBody) || strlen(trim($messageBody)) <= 1) { + d3GetOxidDIC()->setParameter( + 'd3ox.linkmobility.'.InvalidArgumentException::class.'.args.message', + Registry::getLang()->translateString('D3LM_EXC_MESSAGE_NO_LENGTH') + ); + /** @var InvalidArgumentException $exc */ + $exc = d3GetOxidDIC()->get('d3ox.linkmobility.'.InvalidArgumentException::class); + throw $exc; + } + + return $messageBody; + } + + /** + * @param Sms $sms + * @return successfullySentException + */ + protected function getSuccessSentMessage(Sms $sms): successfullySentException + { + $smsCount = $sms->getResponse() ? $sms->getResponse()->getSmsCount() : 0; + d3GetOxidDIC()->setParameter(successfullySentException::class.'.args.smscount', $smsCount); + /** @var successfullySentException $exc */ + $exc = d3GetOxidDIC()->get(successfullySentException::class); + return $exc; + } + + /** + * @param Sms $sms + * @return string + */ + protected function getUnsuccessfullySentMessage(Sms $sms): string + { + $errorMsg = $sms->getResponse() instanceof ResponseInterface ? $sms->getResponse()->getErrorMessage() : 'no response'; + /** @var string $format */ + $format = Registry::getLang()->translateString('D3LM_EXC_MESSAGE_UNEXPECTED_ERR_SEND'); + return sprintf($format, $errorMsg); + } } diff --git a/src/tests/unit/Application/Controller/Admin/AdminOrderTest.php b/src/tests/unit/Application/Controller/Admin/AdminOrderTest.php index c5e4c6c..5d7bf3d 100644 --- a/src/tests/unit/Application/Controller/Admin/AdminOrderTest.php +++ b/src/tests/unit/Application/Controller/Admin/AdminOrderTest.php @@ -38,7 +38,6 @@ class AdminOrderTest extends AdminSend * @return void * @throws ReflectionException * @covers \D3\Linkmobility4OXID\Application\Controller\Admin\AdminOrder::__construct - * @covers \D3\Linkmobility4OXID\Application\Controller\Admin\AdminSendController::__construct */ public function canConstruct() {