diff --git a/src/Application/Model/MessageSender.php b/src/Application/Model/MessageSender.php index 6a72eb0..6817052 100644 --- a/src/Application/Model/MessageSender.php +++ b/src/Application/Model/MessageSender.php @@ -30,7 +30,7 @@ class MessageSender */ public function sendOrderFinishedMessage(Order $order, string $messageBody): void { - if ((oxNew(Configuration::class))->sendOrderFinishedMessage()) { + if ($this->getConfiguration()->sendOrderFinishedMessage()) { $this->sendMessageByOrder($order, $messageBody); } } @@ -43,7 +43,7 @@ class MessageSender */ public function sendSendedNowMessage(Order $order, string $messageBody): void { - if ((oxNew(Configuration::class))->sendOrderSendedNowMessage()) { + if ($this->getConfiguration()->sendOrderSendedNowMessage()) { $this->sendMessageByOrder($order, $messageBody); } } @@ -56,7 +56,7 @@ class MessageSender */ public function sendCancelOrderMessage(Order $order, string $messageBody): void { - if ((oxNew(Configuration::class))->sendOrderCanceledMessage()) { + if ($this->getConfiguration()->sendOrderCanceledMessage()) { $this->sendMessageByOrder($order, $messageBody); } } @@ -69,14 +69,25 @@ class MessageSender */ public function sendMessageByOrder(Order $order, string $messageBody): void { - if ((bool) strlen(trim($messageBody)) === false) { + if ((bool) strlen(trim($messageBody)) === false) return; - } try { - $sms = oxNew(Sms::class, $messageBody); + d3GetOxidDIC()->setParameter(Sms::class.'.args.message', $messageBody); + /** @var Sms $sms */ + $sms = d3GetOxidDIC()->get(Sms::class); $sms->sendOrderMessage($order); } catch (noRecipientFoundException $e) { } } + + /** + * @return Configuration + */ + protected function getConfiguration(): Configuration + { + /** @var Configuration $configuration */ + $configuration = d3GetOxidDIC()->get(Configuration::class); + return $configuration; + } } diff --git a/src/tests/unit/Application/Model/MessageSenderTest.php b/src/tests/unit/Application/Model/MessageSenderTest.php new file mode 100644 index 0000000..346e016 --- /dev/null +++ b/src/tests/unit/Application/Model/MessageSenderTest.php @@ -0,0 +1,226 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\Linkmobility4OXID\tests\unit\Application\Model; + +use D3\Linkmobility4OXID\Application\Model\Configuration; +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\TestingTools\Development\CanAccessRestricted; +use OxidEsales\Eshop\Application\Model\Order; +use PHPUnit\Framework\MockObject\MockObject; +use ReflectionException; + +class MessageSenderTest extends LMUnitTestCase +{ + use CanAccessRestricted; + + /** + * @test + * @param $actionConfigured + * @param $invocationCount + * @return void + * @throws ReflectionException + * @dataProvider canSendOrderFinishedMessageDataProvider + * @covers \D3\Linkmobility4OXID\Application\Model\MessageSender::sendOrderFinishedMessage + */ + public function canSendOrderFinishedMessage($actionConfigured, $invocationCount) + { + /** @var Configuration|MockObject $configurationMock */ + $configurationMock = $this->getMockBuilder(Configuration::class) + ->onlyMethods(['sendOrderFinishedMessage']) + ->getMock(); + $configurationMock->method('sendOrderFinishedMessage')->willReturn($actionConfigured); + + /** @var Order|MockObject $orderMock */ + $orderMock = $this->getMockBuilder(Order::class) + ->disableOriginalConstructor() + ->getMock(); + + /** @var MessageSender|MockObject $sut */ + $sut = $this->getMockBuilder(MessageSender::class) + ->onlyMethods(['getConfiguration', 'sendMessageByOrder']) + ->getMock(); + $sut->method('getConfiguration')->willReturn($configurationMock); + $sut->expects($invocationCount)->method('sendMessageByOrder'); + + $this->callMethod( + $sut, + 'sendOrderFinishedMessage', + [$orderMock, 'messageBody'] + ); + } + + /** + * @test + * @param $actionConfigured + * @param $invocationCount + * @return void + * @throws ReflectionException + * @dataProvider canSendOrderFinishedMessageDataProvider + * @covers \D3\Linkmobility4OXID\Application\Model\MessageSender::sendSendedNowMessage + */ + public function canSendSendedNowMessage($actionConfigured, $invocationCount) + { + /** @var Configuration|MockObject $configurationMock */ + $configurationMock = $this->getMockBuilder(Configuration::class) + ->onlyMethods(['sendOrderSendedNowMessage']) + ->getMock(); + $configurationMock->method('sendOrderSendedNowMessage')->willReturn($actionConfigured); + + /** @var Order|MockObject $orderMock */ + $orderMock = $this->getMockBuilder(Order::class) + ->disableOriginalConstructor() + ->getMock(); + + /** @var MessageSender|MockObject $sut */ + $sut = $this->getMockBuilder(MessageSender::class) + ->onlyMethods(['getConfiguration', 'sendMessageByOrder']) + ->getMock(); + $sut->method('getConfiguration')->willReturn($configurationMock); + $sut->expects($invocationCount)->method('sendMessageByOrder'); + + $this->callMethod( + $sut, + 'sendSendedNowMessage', + [$orderMock, 'messageBody'] + ); + } + + /** + * @test + * @param $actionConfigured + * @param $invocationCount + * @return void + * @throws ReflectionException + * @dataProvider canSendOrderFinishedMessageDataProvider + * @covers \D3\Linkmobility4OXID\Application\Model\MessageSender::sendCancelOrderMessage + */ + public function canSendCancelOrderMessage($actionConfigured, $invocationCount) + { + /** @var Configuration|MockObject $configurationMock */ + $configurationMock = $this->getMockBuilder(Configuration::class) + ->onlyMethods(['sendOrderCanceledMessage']) + ->getMock(); + $configurationMock->method('sendOrderCanceledMessage')->willReturn($actionConfigured); + + /** @var Order|MockObject $orderMock */ + $orderMock = $this->getMockBuilder(Order::class) + ->disableOriginalConstructor() + ->getMock(); + + /** @var MessageSender|MockObject $sut */ + $sut = $this->getMockBuilder(MessageSender::class) + ->onlyMethods(['getConfiguration', 'sendMessageByOrder']) + ->getMock(); + $sut->method('getConfiguration')->willReturn($configurationMock); + $sut->expects($invocationCount)->method('sendMessageByOrder'); + + $this->callMethod( + $sut, + 'sendCancelOrderMessage', + [$orderMock, 'messageBody'] + ); + } + + /** + * @return array[] + */ + public function canSendOrderFinishedMessageDataProvider(): array + { + return [ + 'action configured' => [true, $this->once()], + 'action not configured' => [false, $this->never()], + ]; + } + + /** + * @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) + { + /** @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) + ); + d3GetOxidDIC()->set(Sms::class, $smsMock); + + /** @var Order|MockObject $orderMock */ + $orderMock = $this->getMockBuilder(Order::class) + ->disableOriginalConstructor() + ->getMock(); + + /** @var MessageSender|MockObject $sut */ + $sut = $this->getMockBuilder(MessageSender::class) + ->onlyMethods(['getConfiguration']) + ->getMock(); + + $this->callMethod( + $sut, + 'sendMessageByOrder', + [$orderMock, $messageBody] + ); + } + + /** + * @return array + */ + 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], + ]; + } + + /** + * @test + * @return void + * @throws ReflectionException + * @covers \D3\Linkmobility4OXID\Application\Model\MessageSender::getConfiguration + */ + public function canGetConfiguration() + { + /** @var MessageSender|MockObject $sut */ + $sut = $this->getMockBuilder(MessageSender::class) + ->onlyMethods(['sendMessageByOrder']) + ->getMock(); + + $this->assertInstanceOf( + Configuration::class, + $this->callMethod( + $sut, + 'getConfiguration' + ) + ); + } +} \ No newline at end of file