From 890c5dfc1058f32592c53633d71221ee3a05a912 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Mon, 9 Jan 2023 22:17:30 +0100 Subject: [PATCH] add tests for module classes --- src/Config/linkmobility4oxid.yaml | 7 + src/Config/oxid.yaml | 8 + src/Modules/Application/Model/OrderModel.php | 8 +- src/Modules/Core/EmailCore.php | 15 +- .../Application/Model/OrderModelTest.php | 83 +++++ src/tests/unit/Modules/Core/EmailCoreTest.php | 352 ++++++++++++++++++ 6 files changed, 466 insertions(+), 7 deletions(-) create mode 100644 src/tests/unit/Modules/Application/Model/OrderModelTest.php create mode 100644 src/tests/unit/Modules/Core/EmailCoreTest.php diff --git a/src/Config/linkmobility4oxid.yaml b/src/Config/linkmobility4oxid.yaml index 5554cda..6c401eb 100644 --- a/src/Config/linkmobility4oxid.yaml +++ b/src/Config/linkmobility4oxid.yaml @@ -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' diff --git a/src/Config/oxid.yaml b/src/Config/oxid.yaml index 82b06b0..66996d0 100644 --- a/src/Config/oxid.yaml +++ b/src/Config/oxid.yaml @@ -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' diff --git a/src/Modules/Application/Model/OrderModel.php b/src/Modules/Application/Model/OrderModel.php index 3e5fc61..1f35a7f 100644 --- a/src/Modules/Application/Model/OrderModel.php +++ b/src/Modules/Application/Model/OrderModel.php @@ -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); } } diff --git a/src/Modules/Core/EmailCore.php b/src/Modules/Core/EmailCore.php index 58182b7..a205cce 100644 --- a/src/Modules/Core/EmailCore.php +++ b/src/Modules/Core/EmailCore.php @@ -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)); } diff --git a/src/tests/unit/Modules/Application/Model/OrderModelTest.php b/src/tests/unit/Modules/Application/Model/OrderModelTest.php new file mode 100644 index 0000000..a517cce --- /dev/null +++ b/src/tests/unit/Modules/Application/Model/OrderModelTest.php @@ -0,0 +1,83 @@ + + * @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()], + ]; + } +} \ No newline at end of file diff --git a/src/tests/unit/Modules/Core/EmailCoreTest.php b/src/tests/unit/Modules/Core/EmailCoreTest.php new file mode 100644 index 0000000..8fcbe5e --- /dev/null +++ b/src/tests/unit/Modules/Core/EmailCoreTest.php @@ -0,0 +1,352 @@ + + * @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' + ) + ); + } +} \ No newline at end of file