From d88dee4876c0b6d9d7df82342cd86d215e8f4d1d Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Wed, 28 Dec 2022 00:09:24 +0100 Subject: [PATCH] remove dupclicates, add tests --- .../Controller/Admin/AdminOrder.php | 106 +--- .../Controller/Admin/AdminSendController.php | 132 +++++ .../Controller/Admin/AdminUser.php | 75 +-- .../Controller/Admin/AdminOrderTest.php | 447 +---------------- .../Controller/Admin/AdminSend.php | 471 ++++++++++++++++++ .../Controller/Admin/AdminUserTest.php | 87 ++++ 6 files changed, 731 insertions(+), 587 deletions(-) create mode 100644 src/Application/Controller/Admin/AdminSendController.php create mode 100644 src/tests/unit/Application/Controller/Admin/AdminSend.php create mode 100644 src/tests/unit/Application/Controller/Admin/AdminUserTest.php diff --git a/src/Application/Controller/Admin/AdminOrder.php b/src/Application/Controller/Admin/AdminOrder.php index 34a5b86..b92ffd9 100644 --- a/src/Application/Controller/Admin/AdminOrder.php +++ b/src/Application/Controller/Admin/AdminOrder.php @@ -16,93 +16,28 @@ declare(strict_types=1); namespace D3\Linkmobility4OXID\Application\Controller\Admin; 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 AdminController +class AdminOrder extends AdminSendController { use IsMockable; protected $_sThisTemplate = 'd3adminorder.tpl'; - /** - * @var Sms - */ - protected $sms; + /** @var Order */ + protected $item; - /** - * @var Order - */ - protected $order; + /** @var OrderRecipients */ + protected $itemRecipients; public function __construct() { - $this->order = $order = $this->d3GetMockableOxNewObject(Order::class); - $order->load($this->getEditObjectId()); - - $this->addTplParam('recipient', $this->getRecipientFromCurrentOrder()); - + $this->item = $this->d3GetMockableOxNewObject(Order::class); + $this->itemRecipients = $this->d3GetMockableOxNewObject(OrderRecipients::class, $this->item); parent::__construct(); - - } - - /** - * @return Recipient|false - */ - public function getRecipientFromCurrentOrder() - { - try { - return $this->d3GetMockableOxNewObject(OrderRecipients::class, $this->order)->getSmsRecipient(); - } catch (noRecipientFoundException $e) { - /** @var string $message */ - $message = $this->d3GetMockableRegistryObject(Language::class)->translateString($e->getMessage()); - $this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($message); - } - return false; - } - - /** - * @return void - */ - public function send(): void - { - $utilsView = $this->d3GetMockableRegistryObject(UtilsView::class); - - try { - $utilsView->addErrorToDisplay($this->sendMessage()); - } catch (noRecipientFoundException|InvalidArgumentException $e) { - $utilsView->addErrorToDisplay($e); - } - } - - /** - * @return string - * @throws InvalidArgumentException - */ - protected function getMessageBody(): string - { - $messageBody = $this->d3GetMockableRegistryObject(Request::class) - ->getRequestEscapedParameter('messagebody'); - - if (false === is_string($messageBody) || strlen(trim($messageBody)) <= 1) { - throw $this->d3GetMockableOxNewObject( - InvalidArgumentException::class, - Registry::getLang()->translateString('D3LM_EXC_MESSAGE_NO_LENGTH') - ); - } - - return $messageBody; } /** @@ -111,34 +46,9 @@ class AdminOrder extends AdminController */ protected function sendMessage(): string { - $order = $this->d3GetMockableOxNewObject(Order::class); - $order->load($this->getEditObjectId()); - $sms = $this->d3GetMockableOxNewObject(Sms::class, $this->getMessageBody()); - return $sms->sendOrderMessage($order) ? + return $sms->sendOrderMessage($this->item) ? (string) $this->getSuccessSentMessage($sms) : $this->getUnsuccessfullySentMessage($sms); } - - /** - * @param Sms $sms - * @return successfullySentException - */ - protected function getSuccessSentMessage(Sms $sms): successfullySentException - { - $smsCount = $sms->getResponse() ? $sms->getResponse()->getSmsCount() : 0; - return $this->d3GetMockableOxNewObject(successfullySentException::class, $smsCount); - } - - /** - * @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 new file mode 100644 index 0000000..376e9ba --- /dev/null +++ b/src/Application/Controller/Admin/AdminSendController.php @@ -0,0 +1,132 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\Linkmobility4OXID\Application\Controller\Admin; + +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 string $message */ + $message = $this->d3GetMockableRegistryObject(Language::class)->translateString($e->getMessage()); + $this->d3GetMockableRegistryObject(UtilsView::class)->addErrorToDisplay($message); + } + + return false; + } + + /** + * @return void + */ + public function send(): void + { + $utilsView = $this->d3GetMockableRegistryObject(UtilsView::class); + + try { + $utilsView->addErrorToDisplay($this->sendMessage()); + } catch (noRecipientFoundException|InvalidArgumentException $e) { + $utilsView->addErrorToDisplay($e); + } + } + + /** + * @return string + * @throws InvalidArgumentException + */ + protected function getMessageBody(): string + { + $messageBody = $this->d3GetMockableRegistryObject(Request::class) + ->getRequestEscapedParameter('messagebody'); + + if (false === is_string($messageBody) || strlen(trim($messageBody)) <= 1) { + throw $this->d3GetMockableOxNewObject( + InvalidArgumentException::class, + Registry::getLang()->translateString('D3LM_EXC_MESSAGE_NO_LENGTH') + ); + } + + 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; + return $this->d3GetMockableOxNewObject(successfullySentException::class, $smsCount); + } + + /** + * @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 3aba0fb..d884210 100644 --- a/src/Application/Controller/Admin/AdminUser.php +++ b/src/Application/Controller/Admin/AdminUser.php @@ -18,14 +18,10 @@ namespace D3\Linkmobility4OXID\Application\Controller\Admin; use D3\Linkmobility4OXID\Application\Model\Exceptions\noRecipientFoundException; use D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms; use D3\Linkmobility4OXID\Application\Model\UserRecipients; -use D3\LinkmobilityClient\Response\ResponseInterface; -use D3\LinkmobilityClient\ValueObject\Recipient; -use Exception; -use OxidEsales\Eshop\Application\Controller\Admin\AdminController; +use OxidEsales\Eshop\Application\Model\Order; use OxidEsales\Eshop\Application\Model\User; -use OxidEsales\Eshop\Core\Registry; -class AdminUser extends AdminController +class AdminUser extends AdminSendController { protected $_sThisTemplate = 'd3adminuser.tpl'; @@ -34,66 +30,29 @@ class AdminUser extends AdminController */ protected $sms; - /** - * @var User - */ - protected $user; + /** @var User */ + protected $item; + + /** @var UserRecipients */ + protected $itemRecipients; public function __construct() { - $this->user = $user = oxNew(User::class); - $user->load($this->getEditObjectId()); - - $this->addTplParam('recipient', $this->getRecipientFromCurrentUser()); - + $this->item = $this->d3GetMockableOxNewObject(User::class); + $this->itemRecipients = $this->d3GetMockableOxNewObject(UserRecipients::class, $this->item); parent::__construct(); } /** - * @return Recipient|false + * @return string + * @throws noRecipientFoundException */ - public function getRecipientFromCurrentUser() + protected function sendMessage(): string { - try { - return oxNew(UserRecipients::class, $this->user)->getSmsRecipient(); - } catch (noRecipientFoundException $e) { - /** @var string $message */ - $message = Registry::getLang()->translateString($e->getMessage()); - Registry::getUtilsView()->addErrorToDisplay($message); - } - return false; - } - - /** - * @return void - * @throws Exception - */ - public function send(): void - { - /** @var string $messageBody */ - $messageBody = Registry::getRequest()->getRequestEscapedParameter('messagebody'); - - if (strlen($messageBody) <= 1) { - /** @var string $message */ - $message = Registry::getLang()->translateString('D3LM_EXC_MESSAGE_NO_LENGTH'); - Registry::getUtilsView()->addErrorToDisplay($message); - return; - } - - $user = oxNew(User::class); - $user->load($this->getEditObjectId()); - - $sms = oxNew(Sms::class, $messageBody); - if ($sms->sendUserAccountMessage($user)) { - /** @var string $format */ - $format = Registry::getLang()->translateString('D3LM_EXC_SMS_SUCC_SENT'); - $smsCount = $sms->getResponse() ? $sms->getResponse()->getSmsCount() : 0; - Registry::getUtilsView()->addErrorToDisplay(sprintf($format, $smsCount)); - } else { - $errorMsg = $sms->getResponse() instanceof ResponseInterface ? $sms->getResponse()->getErrorMessage() : 'no response'; - /** @var string $format */ - $format = Registry::getLang()->translateString('D3LM_EXC_MESSAGE_UNEXPECTED_ERR_SEND'); - Registry::getUtilsView()->addErrorToDisplay(sprintf($format, $errorMsg)); - } + /** @var Sms $sms */ + $sms = $this->d3GetMockableOxNewObject(Sms::class, $this->getMessageBody()); + return $sms->sendUserAccountMessage($this->item) ? + (string) $this->getSuccessSentMessage($sms) : + $this->getUnsuccessfullySentMessage($sms); } } diff --git a/src/tests/unit/Application/Controller/Admin/AdminOrderTest.php b/src/tests/unit/Application/Controller/Admin/AdminOrderTest.php index c1e7843..1f699fa 100644 --- a/src/tests/unit/Application/Controller/Admin/AdminOrderTest.php +++ b/src/tests/unit/Application/Controller/Admin/AdminOrderTest.php @@ -16,340 +16,39 @@ 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\SMS\Response; use D3\LinkmobilityClient\ValueObject\Recipient; use D3\TestingTools\Development\CanAccessRestricted; -use InvalidArgumentException; use OxidEsales\Eshop\Application\Model\Order; -use OxidEsales\Eshop\Core\Registry; -use OxidEsales\Eshop\Core\Request; -use OxidEsales\Eshop\Core\UtilsView; -use OxidEsales\TestingLibrary\UnitTestCase; use PHPUnit\Framework\MockObject\MockObject; use ReflectionException; -class AdminOrderTest extends UnitTestCase +class AdminOrderTest extends AdminSend { use CanAccessRestricted; - /** - * @test - * @return void - * @throws ReflectionException - * @covers \D3\Linkmobility4OXID\Application\Controller\Admin\AdminOrder::__construct - */ - public function canConstruct() - { - /** @var Order|MockObject $orderMock */ - $orderMock = $this->getMockBuilder(Order::class) - ->onlyMethods(['load']) - ->getMock(); - $orderMock->method('load')->willReturn(true); - - /** @var Recipient|MockObject $recipientMock */ - $recipientMock = $this->getMockBuilder(Recipient::class) - ->disableOriginalConstructor() - ->getMock(); - - /** @var AdminOrder|MockObject $sut */ - $sut = $this->getMockBuilder(AdminOrder::class) - ->disableOriginalConstructor() - ->onlyMethods(['d3GetMockableOxNewObject', 'getEditObjectId', 'getRecipientFromCurrentOrder']) - ->getMock(); - $sut->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($orderMock) { - $args = func_get_args(); - switch ($args[0]) { - case Order::class: - return $orderMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); - $sut->method('getEditObjectId')->willReturn('editObjId'); - $sut->method('getRecipientFromCurrentOrder')->willReturn($recipientMock); - - $this->callMethod( - $sut, - '__construct' - ); - - $this->assertSame( - $orderMock, - $this->getValue( - $sut, - 'order' - ) - ); - - $this->assertSame( - $recipientMock, - $this->callMethod( - $sut, - 'getViewDataElement', - ['recipient'] - ) - ); - } + protected $subjectUnderTestClass = AdminOrder::class; + protected $itemClass = Order::class; + protected $itemRecipientClass = OrderRecipients::class; /** * @test - * @return void - * @throws ReflectionException - * @covers \D3\Linkmobility4OXID\Application\Controller\Admin\AdminOrder::getRecipientFromCurrentOrder - */ - public function canGetRecipientFromCurrentOrderPassed() - { - /** @var Recipient|MockObject $recipientMock */ - $recipientMock = $this->getMockBuilder(Recipient::class) - ->disableOriginalConstructor() - ->getMock(); - - /** @var OrderRecipients|MockObject $orderRecipientsMock */ - $orderRecipientsMock = $this->getMockBuilder(OrderRecipients::class) - ->disableOriginalConstructor() - ->onlyMethods(['getSmsRecipient']) - ->getMock(); - $orderRecipientsMock->method('getSmsRecipient')->willReturn($recipientMock); - - /** @var AdminOrder|MockObject $sut */ - $sut = $this->getMockBuilder(AdminOrder::class) - ->onlyMethods(['d3GetMockableOxNewObject']) - ->disableOriginalConstructor() - ->getMock(); - $sut->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($orderRecipientsMock) { - $args = func_get_args(); - switch ($args[0]) { - case OrderRecipients::class: - return $orderRecipientsMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); - $this->setValue( - $sut, - 'order', - oxNew(Order::class) - ); - - $this->assertSame( - $recipientMock, - $this->callMethod( - $sut, - 'getRecipientFromCurrentOrder' - ) - ); - } - - /** - * @test - * @return void - * @throws ReflectionException - * @covers \D3\Linkmobility4OXID\Application\Controller\Admin\AdminOrder::getRecipientFromCurrentOrder - */ - public function canGetRecipientFromCurrentOrderThrowsException() - { - /** @var UtilsView|MockObject $utilsViewMock */ - $utilsViewMock = $this->getMockBuilder(UtilsView::class) - ->onlyMethods(['addErrorToDisplay']) - ->getMock(); - $utilsViewMock->expects($this->once())->method('addErrorToDisplay'); - - /** @var OrderRecipients|MockObject $orderRecipientsMock */ - $orderRecipientsMock = $this->getMockBuilder(OrderRecipients::class) - ->disableOriginalConstructor() - ->onlyMethods(['getSmsRecipient']) - ->getMock(); - $orderRecipientsMock->method('getSmsRecipient')->willThrowException( - oxNew(noRecipientFoundException::class) - ); - - /** @var AdminOrder|MockObject $sut */ - $sut = $this->getMockBuilder(AdminOrder::class) - ->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject']) - ->disableOriginalConstructor() - ->getMock(); - $sut->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($orderRecipientsMock) { - $args = func_get_args(); - switch ($args[0]) { - case OrderRecipients::class: - return $orderRecipientsMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($utilsViewMock) { - $args = func_get_args(); - switch ($args[0]) { - case UtilsView::class: - return $utilsViewMock; - default: - return Registry::get($args[0]); - } - } - ); - $this->setValue( - $sut, - 'order', - oxNew(Order::class) - ); - - $this->assertFalse( - $this->callMethod( - $sut, - 'getRecipientFromCurrentOrder' - ) - ); - } - - /** - * @test - * @param $throwsException - * @return void - * @throws ReflectionException - * @covers \D3\Linkmobility4OXID\Application\Controller\Admin\AdminOrder::send - * @dataProvider canSendDataProvider - */ - public function canSend($throwsException) - { - /** @var UtilsView|MockObject $utilsViewMock */ - $utilsViewMock = $this->getMockBuilder(UtilsView::class) - ->onlyMethods(['addErrorToDisplay']) - ->getMock(); - $utilsViewMock->expects($this->once())->method('addErrorToDisplay'); - - /** @var AdminOrder|MockObject $sut */ - $sut = $this->getMockBuilder(AdminOrder::class) - ->onlyMethods(['d3GetMockableRegistryObject', 'sendMessage']) - ->disableOriginalConstructor() - ->getMock(); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($utilsViewMock) { - $args = func_get_args(); - switch ($args[0]) { - case UtilsView::class: - return $utilsViewMock; - default: - return Registry::get($args[0]); - } - } - ); - $sut->method('sendMessage')->will( - $throwsException ? - $this->throwException(oxNew(noRecipientFoundException::class)) : - $this->returnValue('successfully sent message') - ); - - $this->callMethod( - $sut, - 'send' - ); - } - - /** - * @return array - */ - public function canSendDataProvider(): array - { - return [ - 'can send message' => [false], - 'can not send message' => [true], - ]; - } - - /** - * @param $message - * @param $expectException - * @param $expected - * @test - * @return void - * @throws ReflectionException - * @covers \D3\Linkmobility4OXID\Application\Controller\Admin\AdminOrder::getMessageBody - * @dataProvider canGetMessageBodyDataProvider - */ - public function canGetMessageBody($message, $expectException, $expected) - { - /** @var Request|MockObject $requestMock */ - $requestMock = $this->getMockBuilder(Request::class) - ->onlyMethods(['getRequestEscapedParameter']) - ->getMock(); - $requestMock->method('getRequestEscapedParameter')->willReturn($message); - - /** @var Request|MockObject $sut */ - $sut = $this->getMockBuilder(AdminOrder::class) - ->onlyMethods(['d3GetMockableRegistryObject']) - ->disableOriginalConstructor() - ->getMock(); - $sut->method('d3GetMockableRegistryObject')->willReturnCallback( - function () use ($requestMock) { - $args = func_get_args(); - switch ($args[0]) { - case Request::class: - return $requestMock; - default: - return Registry::get($args[0]); - } - } - ); - - if ($expectException) { - $this->expectException(InvalidArgumentException::class); - } - - $this->assertSame( - $expected, - $this->callMethod( - $sut, - 'getMessageBody' - ) - ); - } - - /** - * @return array[] - */ - public function canGetMessageBodyDataProvider(): array - { - return [ - 'message not string' => [[], true, ''], - 'message empty string' => ['', true, ''], - 'message whitespace string' => [' ', true, ''], - 'message right string' => ['messagefixture', false, 'messagefixture'], - ]; - } - - /** - * @test - * @param $canSendOrderMessage + * @param $canSendItemMessage * @return void * @throws ReflectionException * @covers \D3\Linkmobility4OXID\Application\Controller\Admin\AdminOrder::sendMessage * @dataProvider canSendMessageDataProvider */ - public function canSendMessage($canSendOrderMessage) + public function canSendMessage($canSendItemMessage) { - /** @var Order|MockObject $orderMock */ - $orderMock = $this->getMockBuilder(Order::class) - ->onlyMethods(['load']) - ->getMock(); - $orderMock->method('load')->willReturn(true); - /** @var Sms|MockObject $smsMock */ $smsMock = $this->getMockBuilder(Sms::class) ->disableOriginalConstructor() ->onlyMethods(['sendOrderMessage']) ->getMock(); - $smsMock->expects($this->once())->method('sendOrderMessage')->willReturn($canSendOrderMessage); + $smsMock->expects($this->once())->method('sendOrderMessage')->willReturn($canSendItemMessage); /** @var AdminOrder|MockObject $sut */ $sut = $this->getMockBuilder(AdminOrder::class) @@ -357,11 +56,9 @@ class AdminOrderTest extends UnitTestCase ->onlyMethods(['d3GetMockableOxNewObject', 'getMessageBody', 'getSuccessSentMessage', 'getUnsuccessfullySentMessage']) ->getMock(); $sut->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($orderMock, $smsMock) { + function () use ($smsMock) { $args = func_get_args(); switch ($args[0]) { - case Order::class: - return $orderMock; case Sms::class: return $smsMock; default: @@ -370,11 +67,17 @@ class AdminOrderTest extends UnitTestCase } ); $sut->method('getMessageBody')->willReturn('messageBodyFixture'); - $sut->expects($this->exactly((int) $canSendOrderMessage))->method('getSuccessSentMessage') + $sut->expects($this->exactly((int) $canSendItemMessage))->method('getSuccessSentMessage') ->willReturn(oxNew(successfullySentException::class, 'expectedReturn')); - $sut->expects($this->exactly((int) !$canSendOrderMessage))->method('getUnsuccessfullySentMessage') + $sut->expects($this->exactly((int) !$canSendItemMessage))->method('getUnsuccessfullySentMessage') ->willReturn('expectedReturn'); + $this->setValue( + $sut, + 'item', + oxNew($this->itemClass) + ); + $this->assertIsString( $this->callMethod( $sut, @@ -382,122 +85,4 @@ class AdminOrderTest extends UnitTestCase ) ); } - - /** - * @return array - */ - public function canSendMessageDataProvider(): array - { - return [ - 'send order message' => [true], - 'dont send order message' => [false] - ]; - } - - /** - * @test - * @param $hasResponse - * @return void - * @throws ReflectionException - * @covers \D3\Linkmobility4OXID\Application\Controller\Admin\AdminOrder::getSuccessSentMessage - * @dataProvider canGetSuccessSentMessageDataProvider - */ - public function canGetSuccessSentMessage($hasResponse) - { - /** @var successfullySentException|MockObject $successfullySendExceptionMock */ - $successfullySendExceptionMock = $this->getMockBuilder(successfullySentException::class) - ->disableOriginalConstructor() - ->getMock(); - - /** @var Response|MockObject $resonseMock */ - $resonseMock = $this->getMockBuilder(Response::class) - ->onlyMethods(['getSmsCount']) - ->disableOriginalConstructor() - ->getMock(); - $resonseMock->expects($hasResponse ? $this->once() : $this->never())->method('getSmsCount') - ->willReturn(20); - - /** @var Sms|MockObject $smsMock */ - $smsMock = $this->getMockBuilder(Sms::class) - ->disableOriginalConstructor() - ->onlyMethods(['getResponse']) - ->getMock(); - $smsMock->method('getResponse')->willReturn($hasResponse ? $resonseMock : null); - - /** @var AdminOrder|MockObject $sut */ - $sut = $this->getMockBuilder(AdminOrder::class) - ->disableOriginalConstructor() - ->onlyMethods(['d3GetMockableOxNewObject']) - ->getMock(); - $sut->method('d3GetMockableOxNewObject')->willReturnCallback( - function () use ($successfullySendExceptionMock) { - $args = func_get_args(); - switch ($args[0]) { - case successfullySentException::class: - return $successfullySendExceptionMock; - default: - return call_user_func_array("oxNew", $args); - } - } - ); - - $this->assertSame( - $successfullySendExceptionMock, - $this->callMethod( - $sut, - 'getSuccessSentMessage', - [$smsMock] - ) - ); - } - - /** - * @return array - */ - public function canGetSuccessSentMessageDataProvider(): array - { - return [ - 'has response' => [true], - 'has no response' => [false], - ]; - } - - /** - * @test - * @param $hasResponse - * @return void - * @throws ReflectionException - * @covers \D3\Linkmobility4OXID\Application\Controller\Admin\AdminOrder::getUnsuccessfullySentMessage - * @dataProvider canGetSuccessSentMessageDataProvider - */ - public function canGetUnsuccessfullySentMessage($hasResponse) - { - /** @var Response|MockObject $resonseMock */ - $resonseMock = $this->getMockBuilder(Response::class) - ->onlyMethods(['getErrorMessage']) - ->disableOriginalConstructor() - ->getMock(); - $resonseMock->expects($hasResponse ? $this->once() : $this->never())->method('getErrorMessage') - ->willReturn('errorMessage'); - - /** @var Sms|MockObject $smsMock */ - $smsMock = $this->getMockBuilder(Sms::class) - ->disableOriginalConstructor() - ->onlyMethods(['getResponse']) - ->getMock(); - $smsMock->method('getResponse')->willReturn($hasResponse ? $resonseMock : null); - - /** @var AdminOrder|MockObject $sut */ - $sut = $this->getMockBuilder(AdminOrder::class) - ->disableOriginalConstructor() - ->getMock(); - - $this->assertIsString( - $this->callMethod( - $sut, - 'getUnsuccessfullySentMessage', - [$smsMock] - ) - ); - } } \ No newline at end of file diff --git a/src/tests/unit/Application/Controller/Admin/AdminSend.php b/src/tests/unit/Application/Controller/Admin/AdminSend.php new file mode 100644 index 0000000..9bc7005 --- /dev/null +++ b/src/tests/unit/Application/Controller/Admin/AdminSend.php @@ -0,0 +1,471 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\Linkmobility4OXID\tests\unit\Application\Controller\Admin; + +use D3\Linkmobility4OXID\Application\Controller\Admin\AdminOrder; +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\OrderRecipients; +use D3\Linkmobility4OXID\Application\Model\UserRecipients; +use D3\LinkmobilityClient\SMS\Response; +use D3\LinkmobilityClient\ValueObject\Recipient; +use D3\TestingTools\Development\CanAccessRestricted; +use InvalidArgumentException; +use OxidEsales\Eshop\Application\Model\Order; +use OxidEsales\Eshop\Application\Model\User; +use OxidEsales\Eshop\Core\Registry; +use OxidEsales\Eshop\Core\Request; +use OxidEsales\Eshop\Core\UtilsView; +use OxidEsales\TestingLibrary\UnitTestCase; +use PHPUnit\Framework\MockObject\MockObject; +use ReflectionException; + +class AdminSend extends UnitTestCase +{ + use CanAccessRestricted; + + protected $subjectUnderTestClass; + protected $itemClass; + protected $itemRecipientClass; + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\Linkmobility4OXID\Application\Controller\Admin\AdminOrder::__construct + * @covers \D3\Linkmobility4OXID\Application\Controller\Admin\AdminUser::__construct + */ + public function canConstruct() + { + /** @var Order|MockObject $itemMock */ + $itemMock = $this->getMockBuilder($this->itemClass) + ->onlyMethods(['load']) + ->getMock(); + $itemMock->method('load')->willReturn(true); + + /** @var Recipient|MockObject $recipientMock */ + $recipientMock = $this->getMockBuilder(Recipient::class) + ->disableOriginalConstructor() + ->getMock(); + + /** @var AdminOrder|MockObject $sut */ + $sut = $this->getMockBuilder($this->subjectUnderTestClass) + ->disableOriginalConstructor() + ->onlyMethods(['d3GetMockableOxNewObject', 'getEditObjectId', 'getRecipientFromCurrentItem']) + ->getMock(); + $sut->method('d3GetMockableOxNewObject')->willReturnCallback( + function () use ($itemMock) { + $args = func_get_args(); + switch ($args[0]) { + case Order::class: + return $itemMock; + default: + return call_user_func_array("oxNew", $args); + } + } + ); + $sut->method('getEditObjectId')->willReturn('editObjId'); + $sut->method('getRecipientFromCurrentItem')->willReturn($recipientMock); + + $this->callMethod( + $sut, + '__construct' + ); + + $this->assertSame( + $itemMock, + $this->getValue( + $sut, + 'item' + ) + ); + + $this->assertInstanceOf( + $this->itemRecipientClass, + $this->getValue( + $sut, + 'itemRecipients' + ) + ); + + $this->assertSame( + $recipientMock, + $this->callMethod( + $sut, + 'getViewDataElement', + ['recipient'] + ) + ); + } + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\Linkmobility4OXID\Application\Controller\Admin\AdminOrder::getRecipientFromCurrentItem + * @covers \D3\Linkmobility4OXID\Application\Controller\Admin\AdminUser::getRecipientFromCurrentItem + */ + public function canGetRecipientFromCurrentItemPassed() + { + /** @var Recipient|MockObject $recipientMock */ + $recipientMock = $this->getMockBuilder(Recipient::class) + ->disableOriginalConstructor() + ->getMock(); + + /** @var OrderRecipients|UserRecipients|MockObject $itemsRecipientsMock */ + $itemsRecipientsMock = $this->getMockBuilder($this->itemRecipientClass) + ->disableOriginalConstructor() + ->onlyMethods(['getSmsRecipient']) + ->getMock(); + $itemsRecipientsMock->method('getSmsRecipient')->willReturn($recipientMock); + + /** @var AdminOrder|AdminUser|MockObject $sut */ + $sut = $this->getMockBuilder($this->subjectUnderTestClass) + ->onlyMethods(['send']) + ->disableOriginalConstructor() + ->getMock(); + + $this->setValue( + $sut, + 'item', + oxNew($this->itemClass) + ); + + $this->setValue( + $sut, + 'itemRecipients', + $itemsRecipientsMock + ); + + $this->assertSame( + $recipientMock, + $this->callMethod( + $sut, + 'getRecipientFromCurrentItem' + ) + ); + } + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\Linkmobility4OXID\Application\Controller\Admin\AdminOrder::getRecipientFromCurrentItem + * @covers \D3\Linkmobility4OXID\Application\Controller\Admin\AdminUser::getRecipientFromCurrentItem + */ + public function canGetRecipientFromCurrentItemThrowsException() + { + /** @var UtilsView|MockObject $utilsViewMock */ + $utilsViewMock = $this->getMockBuilder(UtilsView::class) + ->onlyMethods(['addErrorToDisplay']) + ->getMock(); + $utilsViewMock->expects($this->once())->method('addErrorToDisplay'); + /** @var OrderRecipients|MockObject $itemRecipientsMock */ + $itemRecipientsMock = $this->getMockBuilder($this->itemRecipientClass) + ->disableOriginalConstructor() + ->onlyMethods(['getSmsRecipient']) + ->getMock(); + $itemRecipientsMock->method('getSmsRecipient')->willThrowException( + oxNew(noRecipientFoundException::class) + ); + + /** @var AdminOrder|AdminUser|MockObject $sut */ + $sut = $this->getMockBuilder($this->subjectUnderTestClass) + ->onlyMethods(['d3GetMockableOxNewObject', 'd3GetMockableRegistryObject']) + ->disableOriginalConstructor() + ->getMock(); + $sut->method('d3GetMockableOxNewObject')->willReturnCallback( + function () use ($itemRecipientsMock) { + $args = func_get_args(); + switch ($args[0]) { + case OrderRecipients::class: + case UserRecipients::class: + return $itemRecipientsMock; + default: + return call_user_func_array("oxNew", $args); + } + } + ); + $sut->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($utilsViewMock) { + $args = func_get_args(); + switch ($args[0]) { + case UtilsView::class: + return $utilsViewMock; + default: + return Registry::get($args[0]); + } + } + ); + $this->setValue( + $sut, + 'item', + oxNew($this->itemClass) + ); + $this->setValue( + $sut, + 'itemRecipients', + oxNew($this->itemRecipientClass, oxNew($this->itemClass)) + ); + + $this->assertFalse( + $this->callMethod( + $sut, + 'getRecipientFromCurrentItem' + ) + ); + } + + /** + * @test + * @param $throwsException + * @return void + * @throws ReflectionException + * @covers \D3\Linkmobility4OXID\Application\Controller\Admin\AdminOrder::send + * @covers \D3\Linkmobility4OXID\Application\Controller\Admin\AdminUser::send + * @dataProvider canSendDataProvider + */ + public function canSend($throwsException) + { + /** @var UtilsView|MockObject $utilsViewMock */ + $utilsViewMock = $this->getMockBuilder(UtilsView::class) + ->onlyMethods(['addErrorToDisplay']) + ->getMock(); + $utilsViewMock->expects($this->once())->method('addErrorToDisplay'); + + /** @var AdminOrder|AdminUser|MockObject $sut */ + $sut = $this->getMockBuilder($this->subjectUnderTestClass) + ->onlyMethods(['d3GetMockableRegistryObject', 'sendMessage']) + ->disableOriginalConstructor() + ->getMock(); + $sut->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($utilsViewMock) { + $args = func_get_args(); + switch ($args[0]) { + case UtilsView::class: + return $utilsViewMock; + default: + return Registry::get($args[0]); + } + } + ); + $sut->method('sendMessage')->will( + $throwsException ? + $this->throwException(oxNew(noRecipientFoundException::class)) : + $this->returnValue('successfully sent message') + ); + + $this->callMethod( + $sut, + 'send' + ); + } + + /** + * @return array + */ + public function canSendDataProvider(): array + { + return [ + 'can send message' => [false], + 'can not send message' => [true], + ]; + } + + /** + * @param $message + * @param $expectException + * @param $expected + * @test + * @return void + * @throws ReflectionException + * @covers \D3\Linkmobility4OXID\Application\Controller\Admin\AdminOrder::getMessageBody + * @covers \D3\Linkmobility4OXID\Application\Controller\Admin\AdminUser::getMessageBody + * @dataProvider canGetMessageBodyDataProvider + */ + public function canGetMessageBody($message, $expectException, $expected) + { + /** @var Request|MockObject $requestMock */ + $requestMock = $this->getMockBuilder(Request::class) + ->onlyMethods(['getRequestEscapedParameter']) + ->getMock(); + $requestMock->method('getRequestEscapedParameter')->willReturn($message); + + /** @var AdminOrder|AdminUser|MockObject $sut */ + $sut = $this->getMockBuilder($this->subjectUnderTestClass) + ->onlyMethods(['d3GetMockableRegistryObject']) + ->disableOriginalConstructor() + ->getMock(); + $sut->method('d3GetMockableRegistryObject')->willReturnCallback( + function () use ($requestMock) { + $args = func_get_args(); + switch ($args[0]) { + case Request::class: + return $requestMock; + default: + return Registry::get($args[0]); + } + } + ); + + if ($expectException) { + $this->expectException(InvalidArgumentException::class); + } + + $this->assertSame( + $expected, + $this->callMethod( + $sut, + 'getMessageBody' + ) + ); + } + + /** + * @return array[] + */ + public function canGetMessageBodyDataProvider(): array + { + return [ + 'message not string' => [[], true, ''], + 'message empty string' => ['', true, ''], + 'message whitespace string' => [' ', true, ''], + 'message right string' => ['messagefixture', false, 'messagefixture'], + ]; + } + + /** + * @return array + */ + public function canSendMessageDataProvider(): array + { + return [ + 'send item message' => [true], + 'dont send item message' => [false] + ]; + } + + /** + * @test + * @param $hasResponse + * @return void + * @throws ReflectionException + * @covers \D3\Linkmobility4OXID\Application\Controller\Admin\AdminOrder::getSuccessSentMessage + * @covers \D3\Linkmobility4OXID\Application\Controller\Admin\AdminUser::getSuccessSentMessage + * @dataProvider canGetSuccessSentMessageDataProvider + */ + public function canGetSuccessSentMessage($hasResponse) + { + /** @var successfullySentException|MockObject $successfullySendExceptionMock */ + $successfullySendExceptionMock = $this->getMockBuilder(successfullySentException::class) + ->disableOriginalConstructor() + ->getMock(); + + /** @var Response|MockObject $resonseMock */ + $resonseMock = $this->getMockBuilder(Response::class) + ->onlyMethods(['getSmsCount']) + ->disableOriginalConstructor() + ->getMock(); + $resonseMock->expects($hasResponse ? $this->once() : $this->never())->method('getSmsCount') + ->willReturn(20); + + /** @var Sms|MockObject $smsMock */ + $smsMock = $this->getMockBuilder(Sms::class) + ->disableOriginalConstructor() + ->onlyMethods(['getResponse']) + ->getMock(); + $smsMock->method('getResponse')->willReturn($hasResponse ? $resonseMock : null); + + /** @var AdminOrder|AdminUser|MockObject $sut */ + $sut = $this->getMockBuilder($this->subjectUnderTestClass) + ->disableOriginalConstructor() + ->onlyMethods(['d3GetMockableOxNewObject']) + ->getMock(); + $sut->method('d3GetMockableOxNewObject')->willReturnCallback( + function () use ($successfullySendExceptionMock) { + $args = func_get_args(); + switch ($args[0]) { + case successfullySentException::class: + return $successfullySendExceptionMock; + default: + return call_user_func_array("oxNew", $args); + } + } + ); + + $this->assertSame( + $successfullySendExceptionMock, + $this->callMethod( + $sut, + 'getSuccessSentMessage', + [$smsMock] + ) + ); + } + + /** + * @return array + */ + public function canGetSuccessSentMessageDataProvider(): array + { + return [ + 'has response' => [true], + 'has no response' => [false], + ]; + } + + /** + * @test + * @param $hasResponse + * @return void + * @throws ReflectionException + * @covers \D3\Linkmobility4OXID\Application\Controller\Admin\AdminOrder::getUnsuccessfullySentMessage + * @covers \D3\Linkmobility4OXID\Application\Controller\Admin\AdminUser::getUnsuccessfullySentMessage + * @dataProvider canGetSuccessSentMessageDataProvider + */ + public function canGetUnsuccessfullySentMessage($hasResponse) + { + /** @var Response|MockObject $resonseMock */ + $resonseMock = $this->getMockBuilder(Response::class) + ->onlyMethods(['getErrorMessage']) + ->disableOriginalConstructor() + ->getMock(); + $resonseMock->expects($hasResponse ? $this->once() : $this->never())->method('getErrorMessage') + ->willReturn('errorMessage'); + + /** @var Sms|MockObject $smsMock */ + $smsMock = $this->getMockBuilder(Sms::class) + ->disableOriginalConstructor() + ->onlyMethods(['getResponse']) + ->getMock(); + $smsMock->method('getResponse')->willReturn($hasResponse ? $resonseMock : null); + + /** @var AdminOrder|AdminUser|MockObject $sut */ + $sut = $this->getMockBuilder($this->subjectUnderTestClass) + ->disableOriginalConstructor() + ->getMock(); + + $this->assertIsString( + $this->callMethod( + $sut, + 'getUnsuccessfullySentMessage', + [$smsMock] + ) + ); + } +} \ No newline at end of file diff --git a/src/tests/unit/Application/Controller/Admin/AdminUserTest.php b/src/tests/unit/Application/Controller/Admin/AdminUserTest.php new file mode 100644 index 0000000..dcf6275 --- /dev/null +++ b/src/tests/unit/Application/Controller/Admin/AdminUserTest.php @@ -0,0 +1,87 @@ + + * @link https://www.oxidmodule.com + */ + +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\successfullySentException; +use D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms; +use D3\Linkmobility4OXID\Application\Model\UserRecipients; +use D3\TestingTools\Development\CanAccessRestricted; +use OxidEsales\Eshop\Application\Model\User; +use PHPUnit\Framework\MockObject\MockObject; +use ReflectionException; + +class AdminUserTest extends AdminSend +{ + use CanAccessRestricted; + + protected $subjectUnderTestClass = AdminUser::class; + protected $itemClass = User::class; + protected $itemRecipientClass = UserRecipients::class; + + /** + * @test + * @param $canSendItemMessage + * @return void + * @throws ReflectionException + * @covers \D3\Linkmobility4OXID\Application\Controller\Admin\AdminOrder::sendMessage + * @dataProvider canSendMessageDataProvider + */ + public function canSendMessage($canSendItemMessage) + { + /** @var Sms|MockObject $smsMock */ + $smsMock = $this->getMockBuilder(Sms::class) + ->disableOriginalConstructor() + ->onlyMethods(['sendUserAccountMessage']) + ->getMock(); + $smsMock->expects($this->once())->method('sendUserAccountMessage')->willReturn($canSendItemMessage); + + /** @var AdminUser|MockObject $sut */ + $sut = $this->getMockBuilder(AdminUser::class) + ->disableOriginalConstructor() + ->onlyMethods(['d3GetMockableOxNewObject', 'getMessageBody', 'getSuccessSentMessage', 'getUnsuccessfullySentMessage']) + ->getMock(); + $sut->method('d3GetMockableOxNewObject')->willReturnCallback( + function () use ($smsMock) { + $args = func_get_args(); + switch ($args[0]) { + case Sms::class: + return $smsMock; + default: + return call_user_func_array("oxNew", $args); + } + } + ); + $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') + ->willReturn('expectedReturn'); + + $this->setValue( + $sut, + 'item', + oxNew($this->itemClass) + ); + + $this->assertIsString( + $this->callMethod( + $sut, + 'sendMessage' + ) + ); + } +} \ No newline at end of file