add tests for MessageSender class

This commit is contained in:
Daniel Seifert 2023-01-05 23:33:29 +01:00
parent 2f76fa37b6
commit 339af80a2d
Signed by: DanielS
GPG Key ID: 6A513E13AEE66170
2 changed files with 243 additions and 6 deletions

View File

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

View File

@ -0,0 +1,226 @@
<?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);
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'
)
);
}
}