add tests for module classes

This commit is contained in:
Daniel Seifert 2023-01-09 22:17:30 +01:00
parent 5ebe4009dd
commit 890c5dfc10
Signed by: DanielS
GPG Key ID: 6A513E13AEE66170
6 changed files with 466 additions and 7 deletions

View File

@ -53,6 +53,13 @@ services:
arguments:
- D3\Linkmobility4OXID\Application\Model\MessageClient
D3\Linkmobility4OXID\Application\Model\MessageSender:
class: D3\Linkmobility4OXID\Application\Model\MessageSender
factory: 'oxNew'
shared: true
arguments:
- D3\Linkmobility4OXID\Application\Model\MessageSender
D3\Linkmobility4OXID\Application\Model\RequestFactory:
class: D3\Linkmobility4OXID\Application\Model\RequestFactory
factory: 'oxNew'

View File

@ -40,6 +40,14 @@ services:
- 'getUtilsView'
shared: true
# Email
d3ox.linkmobility.OxidEsales\Eshop\Core\Email:
class: 'OxidEsales\Eshop\Core\Email'
factory: 'oxNew'
arguments:
- 'OxidEsales\Eshop\Core\Email'
shared: false
# Order
d3ox.linkmobility.OxidEsales\Eshop\Application\Model\Order:
class: 'OxidEsales\Eshop\Application\Model\Order'

View File

@ -16,12 +16,15 @@ declare(strict_types=1);
namespace D3\Linkmobility4OXID\Modules\Application\Model;
use D3\Linkmobility4OXID\Modules\Core\EmailCore;
use D3\TestingTools\Production\IsMockable;
use OxidEsales\Eshop\Core\Email;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class OrderModel extends OrderModel_parent
{
use IsMockable;
/**
* @return void
* @throws ContainerExceptionInterface
@ -29,11 +32,12 @@ class OrderModel extends OrderModel_parent
*/
public function cancelOrder(): void
{
parent::cancelOrder();
// parent::cancelOrder();
$this->d3CallMockableFunction([OrderModel_parent::class, 'cancelOrder']);
if ((bool) $this->getFieldData('oxstorno') === true) {
/** @var EmailCore $Email */
$Email = oxNew(Email::class);
$Email = d3GetOxidDIC()->get('d3ox.linkmobility.'.Email::class);
$Email->d3SendCancelMessage($this);
}
}

View File

@ -16,6 +16,7 @@ declare(strict_types=1);
namespace D3\Linkmobility4OXID\Modules\Core;
use D3\Linkmobility4OXID\Application\Model\MessageSender;
use D3\TestingTools\Production\IsMockable;
use Exception;
use OxidEsales\Eshop\Application\Model\Order;
use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
@ -26,6 +27,8 @@ use Psr\Container\NotFoundExceptionInterface;
class EmailCore extends EmailCore_parent
{
use IsMockable;
/** @var string */
protected $d3OrderCustSmsTemplate = 'd3sms_ordercust.tpl';
/** @var string */
@ -43,7 +46,8 @@ class EmailCore extends EmailCore_parent
*/
public function sendOrderEmailToUser($order, $subject = null)
{
$ret = parent::sendOrderEmailToUser($order, $subject);
// $ret = parent::sendOrderEmailToUser($order, $subject);
$ret = $this->d3CallMockableFunction([EmailCore_parent::class, 'sendOrderEmailToUser'], [$order, $subject]);
$this->d3SendOrderFinishedMessageToUser($order);
@ -60,7 +64,8 @@ class EmailCore extends EmailCore_parent
*/
public function sendSendedNowMail($order, $subject = null)
{
$ret = parent::sendSendedNowMail($order, $subject);
// $ret = parent::sendSendedNowMail($order, $subject);
$ret = $this->d3CallMockableFunction([EmailCore_parent::class, 'sendSendedNowMail'], [$order, $subject]);
$this->d3SendedNowMessage($order);
@ -77,7 +82,7 @@ class EmailCore extends EmailCore_parent
*/
public function d3SendOrderFinishedMessageToUser(Order $order): void
{
$messageSender = oxNew(MessageSender::class);
$messageSender = d3GetOxidDIC()->get(MessageSender::class);
$messageSender->sendOrderFinishedMessage($order, $this->d3GetOrderFinishedSmsMessageBody($order));
}
@ -106,7 +111,7 @@ class EmailCore extends EmailCore_parent
*/
public function d3SendedNowMessage(Order $order): void
{
$messageSender = oxNew(MessageSender::class);
$messageSender = d3GetOxidDIC()->get(MessageSender::class);
$messageSender->sendSendedNowMessage($order, $this->d3GetSendedNowSmsMessageBody($order));
}
@ -135,7 +140,7 @@ class EmailCore extends EmailCore_parent
*/
public function d3SendCancelMessage(Order $order): void
{
$messageSender = oxNew(MessageSender::class);
$messageSender = d3GetOxidDIC()->get(MessageSender::class);
$messageSender->sendCancelOrderMessage($order, $this->d3GetCancelOrderSmsMessageBody($order));
}

View File

@ -0,0 +1,83 @@
<?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\Modules\Application\Model;
use D3\Linkmobility4OXID\Modules\Application\Model\OrderModel;
use D3\Linkmobility4OXID\Modules\Core\EmailCore;
use D3\Linkmobility4OXID\tests\unit\LMUnitTestCase;
use D3\TestingTools\Development\CanAccessRestricted;
use OxidEsales\Eshop\Application\Model\Order;
use OxidEsales\Eshop\Core\Email;
use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException;
class OrderModelTest extends LMUnitTestCase
{
use CanAccessRestricted;
/**
* @test
* @param $stornoValue
* @param $invocationCount
* @return void
* @throws ReflectionException
* @dataProvider canCancelOrderDataProvider
* @covers \D3\Linkmobility4OXID\Modules\Application\Model\OrderModel::cancelOrder
*/
public function canCancelOrder($stornoValue, $invocationCount)
{
/** @var EmailCore|MockObject $EmailMock */
$EmailMock = $this->getMockBuilder(Email::class)
->onlyMethods(['d3SendCancelMessage'])
->getMock();
$EmailMock->expects($invocationCount)->method('d3SendCancelMessage');
d3GetOxidDIC()->set('d3ox.linkmobility.'.Email::class, $EmailMock);
/** @var OrderModel|MockObject $sut */
$sut = $this->getMockBuilder(Order::class)
->onlyMethods(['getFieldData', 'd3CallMockableFunction'])
->getMock();
$sut->method('getFieldData')->willReturnCallback(
function () use ($stornoValue) {
$aArgs = func_get_args();
switch ($aArgs[0]) {
case 'oxstorno':
return $stornoValue;
}
$this->fail('Unknown variable '.$aArgs[0]);
}
);
$sut->method('d3CallMockableFunction')->willReturn(true);
$this->callMethod(
$sut,
'cancelOrder'
);
}
/**
* @return array[]
*/
public function canCancelOrderDataProvider(): array
{
return [
'is canceled' => [true, $this->once()],
'not canceled' => [false, $this->never()],
];
}
}

View File

@ -0,0 +1,352 @@
<?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\Modules\Core;
use D3\Linkmobility4OXID\Application\Model\MessageSender;
use D3\Linkmobility4OXID\Modules\Application\Model\OrderModel;
use D3\Linkmobility4OXID\Modules\Core\EmailCore;
use D3\Linkmobility4OXID\tests\unit\LMUnitTestCase;
use D3\TestingTools\Development\CanAccessRestricted;
use OxidEsales\Eshop\Application\Model\Order;
use OxidEsales\Eshop\Core\Email;
use OxidEsales\EshopCommunity\Internal\Framework\Templating\TemplateRenderer;
use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException;
class EmailCoreTest extends LMUnitTestCase
{
use CanAccessRestricted;
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\Linkmobility4OXID\Modules\Core\EmailCore::sendOrderEmailToUser
*/
public function canSendOrderEmailToUser()
{
/** @var Order|MockObject $orderMock */
$orderMock = $this->getMockBuilder(Order::class)
->disableOriginalConstructor()
->getMock();
/** @var EmailCore|MockObject $sut */
$sut = $this->getMockBuilder(Email::class)
->onlyMethods(['d3CallMockableFunction', 'd3SendOrderFinishedMessageToUser'])
->getMock();
$sut->method('d3CallMockableFunction')->willReturn('returnFixture');
$sut->expects($this->once())->method('d3SendOrderFinishedMessageToUser')->with(
$this->identicalTo($orderMock)
);
$this->assertSame(
'returnFixture',
$this->callMethod(
$sut,
'sendOrderEmailToUser',
[$orderMock]
)
);
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\Linkmobility4OXID\Modules\Core\EmailCore::sendSendedNowMail
*/
public function canSendSendedNowMail()
{
/** @var Order|MockObject $orderMock */
$orderMock = $this->getMockBuilder(Order::class)
->disableOriginalConstructor()
->getMock();
/** @var EmailCore|MockObject $sut */
$sut = $this->getMockBuilder(Email::class)
->onlyMethods(['d3CallMockableFunction', 'd3SendedNowMessage'])
->getMock();
$sut->method('d3CallMockableFunction')->willReturn('returnFixture');
$sut->expects($this->once())->method('d3SendedNowMessage')->with(
$this->identicalTo($orderMock)
);
$this->assertSame(
'returnFixture',
$this->callMethod(
$sut,
'sendSendedNowMail',
[$orderMock]
)
);
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\Linkmobility4OXID\Modules\Core\EmailCore::d3SendOrderFinishedMessageToUser
*/
public function canSendOrderFinishedMessageToUser()
{
/** @var OrderModel|MockObject $orderMock */
$orderMock = $this->getMockBuilder(Order::class)
->disableOriginalConstructor()
->getMock();
/** @var MessageSender|MockObject $messageSenderMock */
$messageSenderMock = $this->getMockBuilder(MessageSender::class)
->onlyMethods(['sendOrderFinishedMessage'])
->getMock();
$messageSenderMock->expects($this->once())->method('sendOrderFinishedMessage')->with(
$this->identicalTo($orderMock)
);
d3GetOxidDIC()->set(MessageSender::class, $messageSenderMock);
/** @var EmailCore|MockObject $sut */
$sut = $this->getMockBuilder(Email::class)
->onlyMethods(['d3GetOrderFinishedSmsMessageBody'])
->getMock();
$sut->method('d3GetOrderFinishedSmsMessageBody')->with(
$this->identicalTo($orderMock)
)->willReturn('smsMessageBodyFixture');
$this->callMethod(
$sut,
'd3SendOrderFinishedMessageToUser',
[$orderMock]
);
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\Linkmobility4OXID\Modules\Core\EmailCore::d3GetOrderFinishedSmsMessageBody
*/
public function canGetOrderFinishedSmsMessageBody()
{
/** @var Order|MockObject $orderMock */
$orderMock = $this->getMockBuilder(Order::class)
->disableOriginalConstructor()
->getMock();
/** @var TemplateRenderer|MockObject $tplRendererMock */
$tplRendererMock = $this->getMockBuilder(TemplateRenderer::class)
->disableOriginalConstructor()
->onlyMethods(['renderTemplate'])
->getMock();
$tplRendererMock->method('renderTemplate')->willReturn('renderedFixture');
/** @var EmailCore|MockObject $sut */
$sut = $this->getMockBuilder(Email::class)
->onlyMethods(['d3GetTplRenderer', 'setViewData', 'getViewData'])
->getMock();
$sut->method('d3GetTplRenderer')->willReturn($tplRendererMock);
$sut->method('setViewData')->with(
$this->identicalTo('order'),
$this->identicalTo($orderMock)
);
$sut->method('getViewData')->willReturn([]);
$this->assertSame(
'renderedFixture',
$this->callMethod(
$sut,
'd3GetOrderFinishedSmsMessageBody',
[$orderMock]
)
);
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\Linkmobility4OXID\Modules\Core\EmailCore::d3SendedNowMessage
*/
public function canSendedNowMessage()
{
/** @var OrderModel|MockObject $orderMock */
$orderMock = $this->getMockBuilder(Order::class)
->disableOriginalConstructor()
->getMock();
/** @var MessageSender|MockObject $messageSenderMock */
$messageSenderMock = $this->getMockBuilder(MessageSender::class)
->onlyMethods(['sendSendedNowMessage'])
->getMock();
$messageSenderMock->expects($this->once())->method('sendSendedNowMessage')->with(
$this->identicalTo($orderMock)
);
d3GetOxidDIC()->set(MessageSender::class, $messageSenderMock);
/** @var EmailCore|MockObject $sut */
$sut = $this->getMockBuilder(Email::class)
->onlyMethods(['d3GetSendedNowSmsMessageBody'])
->getMock();
$sut->method('d3GetSendedNowSmsMessageBody')->with(
$this->identicalTo($orderMock)
)->willReturn('smsMessageBodyFixture');
$this->callMethod(
$sut,
'd3SendedNowMessage',
[$orderMock]
);
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\Linkmobility4OXID\Modules\Core\EmailCore::d3GetSendedNowSmsMessageBody
*/
public function canGetSendedNowSmsMessageBody()
{
/** @var Order|MockObject $orderMock */
$orderMock = $this->getMockBuilder(Order::class)
->disableOriginalConstructor()
->getMock();
/** @var TemplateRenderer|MockObject $tplRendererMock */
$tplRendererMock = $this->getMockBuilder(TemplateRenderer::class)
->disableOriginalConstructor()
->onlyMethods(['renderTemplate'])
->getMock();
$tplRendererMock->method('renderTemplate')->willReturn('renderedFixture');
/** @var EmailCore|MockObject $sut */
$sut = $this->getMockBuilder(Email::class)
->onlyMethods(['d3GetTplRenderer', 'setViewData', 'getViewData'])
->getMock();
$sut->method('d3GetTplRenderer')->willReturn($tplRendererMock);
$sut->method('setViewData')->with(
$this->identicalTo('order'),
$this->identicalTo($orderMock)
);
$sut->method('getViewData')->willReturn([]);
$this->assertSame(
'renderedFixture',
$this->callMethod(
$sut,
'd3GetSendedNowSmsMessageBody',
[$orderMock]
)
);
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\Linkmobility4OXID\Modules\Core\EmailCore::d3SendCancelMessage
*/
public function canSendCancelMessage()
{
/** @var OrderModel|MockObject $orderMock */
$orderMock = $this->getMockBuilder(Order::class)
->disableOriginalConstructor()
->getMock();
/** @var MessageSender|MockObject $messageSenderMock */
$messageSenderMock = $this->getMockBuilder(MessageSender::class)
->onlyMethods(['sendCancelOrderMessage'])
->getMock();
$messageSenderMock->expects($this->once())->method('sendCancelOrderMessage')->with(
$this->identicalTo($orderMock)
);
d3GetOxidDIC()->set(MessageSender::class, $messageSenderMock);
/** @var EmailCore|MockObject $sut */
$sut = $this->getMockBuilder(Email::class)
->onlyMethods(['d3GetCancelOrderSmsMessageBody'])
->getMock();
$sut->method('d3GetCancelOrderSmsMessageBody')->with(
$this->identicalTo($orderMock)
)->willReturn('smsMessageBodyFixture');
$this->callMethod(
$sut,
'd3SendCancelMessage',
[$orderMock]
);
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\Linkmobility4OXID\Modules\Core\EmailCore::d3GetCancelOrderSmsMessageBody
*/
public function canGetCancelOrderSmsMessageBody()
{
/** @var Order|MockObject $orderMock */
$orderMock = $this->getMockBuilder(Order::class)
->disableOriginalConstructor()
->getMock();
/** @var TemplateRenderer|MockObject $tplRendererMock */
$tplRendererMock = $this->getMockBuilder(TemplateRenderer::class)
->disableOriginalConstructor()
->onlyMethods(['renderTemplate'])
->getMock();
$tplRendererMock->method('renderTemplate')->willReturn('renderedFixture');
/** @var EmailCore|MockObject $sut */
$sut = $this->getMockBuilder(Email::class)
->onlyMethods(['d3GetTplRenderer', 'setViewData', 'getViewData'])
->getMock();
$sut->method('d3GetTplRenderer')->willReturn($tplRendererMock);
$sut->method('setViewData')->with(
$this->identicalTo('order'),
$this->identicalTo($orderMock)
);
$sut->method('getViewData')->willReturn([]);
$this->assertSame(
'renderedFixture',
$this->callMethod(
$sut,
'd3GetCancelOrderSmsMessageBody',
[$orderMock]
)
);
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\Linkmobility4OXID\Modules\Core\EmailCore::d3GetTplRenderer
*/
public function canGetTplRenderer()
{
/** @var EmailCore|MockObject $sut */
$sut = $this->getMockBuilder(Email::class)
->disableOriginalConstructor()
->getMock();
$this->assertInstanceOf(
TemplateRenderer::class,
$this->callMethod(
$sut,
'd3GetTplRenderer'
)
);
}
}