From 1b01348ef3dd8bba647ae4e7d16b398638df3a45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Fri, 2 Aug 2024 15:20:23 +0200 Subject: [PATCH] MAG2-298 - Added unit tests --- Block/Amazon/ButtonV2.php | 9 +- Model/Methods/AmazonPayV2.php | 4 +- Test/Unit/Block/Amazon/ButtonTest.php | 10 + Test/Unit/Block/Amazon/ButtonV2Test.php | 265 ++++++++++++++++++ Test/Unit/Controller/Amazon/ReturnedTest.php | 160 +++++++++++ .../Controller/Onepage/PlaceOrderTest.php | 68 ++++- Test/Unit/Controller/Onepage/ReviewTest.php | 20 +- .../CreateCheckoutSessionPayloadTest.php | 86 ++++++ .../Genericpayment/GetCheckoutSessionTest.php | 86 ++++++ .../UpdateCheckoutSessionTest.php | 86 ++++++ Test/Unit/Model/ConfigProviderTest.php | 17 +- Test/Unit/Model/Methods/AmazonPayV2Test.php | 247 ++++++++++++++++ Test/Unit/Model/Methods/BNPL/DebitTest.php | 37 ++- .../Model/Methods/Klarna/InstallmentTest.php | 2 +- Test/Unit/Model/Methods/PayoneMethodTest.php | 8 + Test/Unit/Service/V1/AmazonPayTest.php | 57 +++- .../Service/V1/Data/AmazonPayResponseTest.php | 16 ++ view/frontend/templates/amazon/buttonv2.phtml | 4 - 18 files changed, 1157 insertions(+), 25 deletions(-) create mode 100644 Test/Unit/Block/Amazon/ButtonV2Test.php create mode 100644 Test/Unit/Controller/Amazon/ReturnedTest.php create mode 100644 Test/Unit/Model/Api/Request/Genericpayment/CreateCheckoutSessionPayloadTest.php create mode 100644 Test/Unit/Model/Api/Request/Genericpayment/GetCheckoutSessionTest.php create mode 100644 Test/Unit/Model/Api/Request/Genericpayment/UpdateCheckoutSessionTest.php create mode 100644 Test/Unit/Model/Methods/AmazonPayV2Test.php diff --git a/Block/Amazon/ButtonV2.php b/Block/Amazon/ButtonV2.php index 5157950b..3016c41b 100644 --- a/Block/Amazon/ButtonV2.php +++ b/Block/Amazon/ButtonV2.php @@ -42,6 +42,11 @@ class ButtonV2 extends Template implements \Magento\Catalog\Block\ShortcutInterf */ protected $alias = 'payone.block.amazon.buttonv2'; + /** + * @var string + */ + protected $name; + /** * @var \Payone\Core\Helper\Api */ @@ -129,9 +134,9 @@ public function getStoreCode() public function getButtonId() { $buttonId = "AmazonPayButton"; - if (strpos($this->getName(), "checkout.cart.shortcut.buttons") !== false) { + if (strpos($this->getName() ?? '', "checkout.cart.shortcut.buttons") !== false) { $buttonId = "AmazonPayButtonBasket"; - } elseif (strpos($this->getName(), "shortcutbuttons") !== false) { + } elseif (strpos($this->getName() ?? '', "shortcutbuttons") !== false) { $buttonId = "AmazonPayButtonMiniBasket"; } return $buttonId; diff --git a/Model/Methods/AmazonPayV2.php b/Model/Methods/AmazonPayV2.php index 73a3164d..ea1ac998 100644 --- a/Model/Methods/AmazonPayV2.php +++ b/Model/Methods/AmazonPayV2.php @@ -76,7 +76,7 @@ class AmazonPayV2 extends PayoneMethod /** * @return bool */ - protected function isAPBPayment() + public function isAPBPayment() { if ($this->isAmazonPayExpress() === true) { return false; @@ -87,7 +87,7 @@ protected function isAPBPayment() /** * @return bool */ - protected function isAmazonPayExpress() + public function isAmazonPayExpress() { if ($this->checkoutSession->getPayoneIsAmazonPayExpressPayment() === true) { return true; diff --git a/Test/Unit/Block/Amazon/ButtonTest.php b/Test/Unit/Block/Amazon/ButtonTest.php index 031d1a69..073da527 100644 --- a/Test/Unit/Block/Amazon/ButtonTest.php +++ b/Test/Unit/Block/Amazon/ButtonTest.php @@ -163,4 +163,14 @@ public function testGetCounterOther() $result = $this->classToTest->getCounter(); $this->assertEquals(3, $result); } + + public function testSetName() + { + $expected = "testName"; + + $this->classToTest->setName($expected); + $result = $this->classToTest->getName(); + + $this->assertEquals($expected, $result); + } } diff --git a/Test/Unit/Block/Amazon/ButtonV2Test.php b/Test/Unit/Block/Amazon/ButtonV2Test.php new file mode 100644 index 00000000..f7204932 --- /dev/null +++ b/Test/Unit/Block/Amazon/ButtonV2Test.php @@ -0,0 +1,265 @@ +. + * + * PHP version 5 + * + * @category Payone + * @package Payone_Magento2_Plugin + * @author FATCHIP GmbH + * @copyright 2003 - 2024 Payone GmbH + * @license GNU Lesser General Public License + * @link http://www.payone.de + */ + +namespace Payone\Core\Test\Unit\Block\Amazon; + +use Payone\Core\Block\Amazon\ButtonV2 as ClassToTest; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; +use Payone\Core\Helper\Api; +use Payone\Core\Model\Methods\AmazonPayV2; +use Magento\Checkout\Model\Session; +use Magento\Quote\Model\Quote; +use Magento\Store\Api\Data\StoreInterface; +use Payone\Core\Test\Unit\BaseTestCase; +use Payone\Core\Test\Unit\PayoneObjectManager; +use Payone\Core\Helper\Base; +use Magento\Framework\View\Element\Template\Context; +use Magento\Framework\UrlInterface; + +class ButtonV2Test extends BaseTestCase +{ + /** + * @var ClassToTest + */ + private $classToTest; + + /** + * @var ObjectManager|PayoneObjectManager + */ + private $objectManager; + + /** + * @var Base + */ + private $baseHelper; + + /** + * @var UrlInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $urlBuilder; + + /** + * @var Session|\PHPUnit\Framework\MockObject\MockObject + */ + private $checkoutSession; + + protected function setUp(): void + { + $this->objectManager = $this->getObjectManager(); + + $this->urlBuilder = $this->getMockBuilder(UrlInterface::class)->disableOriginalConstructor()->getMock(); + + $context = $this->getMockBuilder(Context::class)->disableOriginalConstructor()->getMock(); + $context->method('getUrlBuilder')->willReturn($this->urlBuilder); + + $this->checkoutSession = $this->getMockBuilder(Session::class) + ->disableOriginalConstructor() + ->setMethods(['getQuote']) + ->getMock(); + + $amazonPayment = $this->getMockBuilder(AmazonPayV2::class)->disableOriginalConstructor()->getMock(); + $amazonPayment->method('getMerchantId')->willReturn('test'); + $amazonPayment->method('useSandbox')->willReturn(false); + $amazonPayment->method('getButtonColor')->willReturn('test'); + $amazonPayment->method('getButtonLanguage')->willReturn('test'); + + $apiHelper = $this->getMockBuilder(Api::class)->disableOriginalConstructor()->getMock(); + $apiHelper->method('getCurrencyFromQuote')->willReturn('EUR'); + $apiHelper->method('getQuoteAmount')->willReturn('1000'); + + $this->baseHelper = $this->getMockBuilder(Base::class)->disableOriginalConstructor()->getMock(); + $this->classToTest = $this->objectManager->getObject(ClassToTest::class, [ + 'context' => $context, + 'baseHelper' => $this->baseHelper, + 'checkoutSession' => $this->checkoutSession, + 'amazonPayment' => $amazonPayment, + 'apiHelper' => $apiHelper, + ]); + } + + public function testGetAlias() + { + $expected = 'payone.block.amazon.buttonv2'; + + $result = $this->classToTest->getAlias(); + $this->assertEquals($expected, $result); + } + + public function testSetName() + { + $expected = "testName"; + + $this->classToTest->setName($expected); + $result = $this->classToTest->getName(); + + $this->assertEquals($expected, $result); + } + + public function testGetQuoteId() + { + $expected = 'test'; + + $oQuote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock(); + $oQuote->method('getId')->willReturn($expected); + + $this->checkoutSession->method('getQuote')->willReturn($oQuote); + + $result = $this->classToTest->getQuoteId(); + + $this->assertEquals($expected, $result); + } + + public function testGetStoreCode() + { + $expected = 'test'; + + $store = $this->getMockBuilder(StoreInterface::class)->disableOriginalConstructor()->getMock(); + $store->method('getCode')->willReturn($expected); + + $quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock(); + $quote->method('getStore')->willReturn($store); + + $this->checkoutSession->method('getQuote')->willReturn($quote); + + $result = $this->classToTest->getStoreCode(); + + $this->assertEquals($expected, $result); + } + + public function testGetButtonId() + { + $this->classToTest->setName('fallback'); + $result = $this->classToTest->getButtonId(); + $this->assertEquals('AmazonPayButton', $result); + + $this->classToTest->setName('checkout.cart.shortcut.buttons'); + $result = $this->classToTest->getButtonId(); + $this->assertEquals('AmazonPayButtonBasket', $result); + + $this->classToTest->setName('shortcutbuttons'); + $result = $this->classToTest->getButtonId(); + $this->assertEquals('AmazonPayButtonMiniBasket', $result); + } + + public function testGetQuote() + { + $oQuote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock(); + + $this->checkoutSession->method('getQuote')->willReturn($oQuote); + + $result = $this->classToTest->getQuote(); + + $this->assertInstanceOf(Quote::class, $result); + } + + public function testGetPublicKeyId() + { + $result = $this->classToTest->getPublicKeyId(); + $this->assertEquals(AmazonPayV2::BUTTON_PUBLIC_KEY, $result); + } + + public function testGetMerchantId() + { + $result = $this->classToTest->getMerchantId(); + $this->assertEquals('test', $result); + } + + public function testIsTestMode() + { + $result = $this->classToTest->isTestMode(); + $this->assertFalse($result); + } + + public function testGetCurrency() + { + $oQuote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock(); + + $this->checkoutSession->method('getQuote')->willReturn($oQuote); + + $result = $this->classToTest->getCurrency(); + $this->assertEquals('EUR', $result); + } + + public function testGetAmount() + { + $oQuote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock(); + + $this->checkoutSession->method('getQuote')->willReturn($oQuote); + + $result = $this->classToTest->getAmount(); + $this->assertEquals('1000', $result); + } + + public function testGetProductType() + { + $oQuote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock(); + $oQuote->method('isVirtual')->willReturn(false); + $this->checkoutSession->method('getQuote')->willReturn($oQuote); + + $result = $this->classToTest->getProductType(); + $this->assertEquals("PayAndShip", $result); + } + + public function testGetProductTypeVirtual() + { + $oQuote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock(); + $oQuote->method('isVirtual')->willReturn(true); + $this->checkoutSession->method('getQuote')->willReturn($oQuote); + + $result = $this->classToTest->getProductType(); + $this->assertEquals("PayOnly", $result); + } + + public function testGetPlacement() + { + $result = $this->classToTest->getPlacement(); + $this->assertEquals("Home", $result); + + $this->classToTest->setName("checkout.cart.shortcut.buttons"); + $result = $this->classToTest->getPlacement(); + $this->assertEquals("Cart", $result); + } + + public function testGetButtonColor() + { + $expected = 'test'; + + $this->baseHelper->method('getConfigParam')->willReturn($expected); + + $result = $this->classToTest->getButtonColor(); + $this->assertEquals($expected, $result); + } + + public function testGetButtonLanguage() + { + $expected = 'test'; + + $this->baseHelper->method('getConfigParam')->willReturn($expected); + + $result = $this->classToTest->getButtonLanguage(); + $this->assertEquals($expected, $result); + } +} diff --git a/Test/Unit/Controller/Amazon/ReturnedTest.php b/Test/Unit/Controller/Amazon/ReturnedTest.php new file mode 100644 index 00000000..de766bff --- /dev/null +++ b/Test/Unit/Controller/Amazon/ReturnedTest.php @@ -0,0 +1,160 @@ +objectManager = $this->getObjectManager(); + + $resultRedirect = $this->getMockBuilder(Redirect::class)->disableOriginalConstructor()->getMock(); + $resultRedirect->method('setPath')->willReturn($resultRedirect); + + $resultFactory = $this->getMockBuilder(ResultFactory::class)->disableOriginalConstructor()->getMock(); + $resultFactory->method('create')->willReturn($resultRedirect); + + $messageManager = $this->getMockBuilder(ManagerInterface::class)->disableOriginalConstructor()->getMock(); + + $context = $this->getMockBuilder(Context::class)->disableOriginalConstructor()->getMock(); + $context->method('getResultFactory')->willReturn($resultFactory); + $context->method('getMessageManager')->willReturn($messageManager); + + $this->getCheckoutSession = $this->getMockBuilder(GetCheckoutSession::class)->disableOriginalConstructor()->getMock(); + + $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock(); + $address->method('getEmail')->willReturn('testing@payone.de'); + + $payment = $this->getMockBuilder(Payment::class)->disableOriginalConstructor()->getMock(); + + $quote = $this->getMockBuilder(Quote::class) + ->disableOriginalConstructor() + ->setMethods([ + 'getBillingAddress', + 'setCustomerId', + 'setCustomerEmail', + 'setCustomerIsGuest', + 'setCustomerGroupId', + 'getPayment', + 'setPayment', + 'setInventoryProcessed', + 'collectTotals', + 'save', + ]) + ->getMock(); + $quote->method('getBillingAddress')->willReturn($address); + $quote->method('setCustomerId')->willReturn($quote); + $quote->method('setCustomerEmail')->willReturn($quote); + $quote->method('setCustomerIsGuest')->willReturn($quote); + $quote->method('getPayment')->willReturn($payment); + $quote->method('collectTotals')->willReturn($quote); + + $orderHelper = $this->getMockBuilder(Order::class)->disableOriginalConstructor()->getMock(); + $orderHelper->method('updateAddresses')->willReturn($quote); + + $checkoutHelper = $this->getMockBuilder(Checkout::class)->disableOriginalConstructor()->getMock(); + $checkoutHelper->method('getCurrentCheckoutMethod')->willReturn(Onepage::METHOD_GUEST); + + $this->checkoutSession = $this->getMockBuilder(Session::class) + ->disableOriginalConstructor() + ->setMethods([ + 'getPayoneWorkorderId', + 'setIsPayonePayPalExpress', + 'getPayonePayPalExpressRetry', + 'unsPayonePayPalExpressRetry', + 'getQuote', + ]) + ->getMock(); + $this->checkoutSession->method('getQuote')->willReturn($quote); + + $this->returnHandler = $this->getMockBuilder(ReturnHandler::class)->disableOriginalConstructor()->getMock(); + + $this->classToTest = $this->objectManager->getObject(ClassToTest::class, [ + 'context' => $context, + 'checkoutSession' => $this->checkoutSession, + 'returnHandler' => $this->returnHandler, + 'getCheckoutSession' => $this->getCheckoutSession, + 'orderHelper' => $orderHelper, + 'checkoutHelper' => $checkoutHelper, + ]); + } + + public function testExecute() + { + $this->checkoutSession->method('getPayoneWorkorderId')->willReturn('12345'); + + $response = ['status' => 'OK']; + + $this->getCheckoutSession->method('sendRequest')->willReturn($response); + + $result = $this->classToTest->execute(); + $this->assertInstanceOf(Redirect::class, $result); + } + + public function testExecuteException() + { + $this->checkoutSession->method('getPayoneWorkorderId')->willReturn('12345'); + + $response = ['status' => 'ERROR']; + + $this->getCheckoutSession->method('sendRequest')->willReturn($response); + + #$exception = new \Exception; + #$this->returnHandler->expects($this->classToTest->method('handleReturn')->willThrowException($exception)); + + $result = $this->classToTest->execute(); + $this->assertInstanceOf(Redirect::class, $result); + } + + public function testExecutePayPal() + { + $this->checkoutSession->method('getPayoneWorkorderId')->willReturn(null); + + $result = $this->classToTest->execute(); + $this->assertInstanceOf(Redirect::class, $result); + } +} diff --git a/Test/Unit/Controller/Onepage/PlaceOrderTest.php b/Test/Unit/Controller/Onepage/PlaceOrderTest.php index a8eb8918..0884a5c7 100644 --- a/Test/Unit/Controller/Onepage/PlaceOrderTest.php +++ b/Test/Unit/Controller/Onepage/PlaceOrderTest.php @@ -39,6 +39,7 @@ use Magento\Framework\App\Request\Http; use Magento\Framework\Message\ManagerInterface; use Magento\Quote\Model\Quote\Address; +use Payone\Core\Model\PayoneConfig; use Payone\Core\Test\Unit\BaseTestCase; use Payone\Core\Test\Unit\PayoneObjectManager; use Magento\Quote\Model\Quote\Payment; @@ -81,6 +82,11 @@ class PlaceOrderTest extends BaseTestCase */ private $checkoutHelper; + /** + * @var Quote|\PHPUnit_Framework_MockObject_MockObject + */ + private $quote; + protected function setUp(): void { $this->objectManager = $this->getObjectManager(); @@ -120,9 +126,9 @@ protected function setUp(): void $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock(); $payment = $this->getMockBuilder(Payment::class)->disableOriginalConstructor()->getMock(); - $payment->method('getMethod')->willReturn('payone_paypal'); + $payment->method('getMethod')->willReturn(PayoneConfig::METHOD_PAYPAL); - $quote = $this->getMockBuilder(Quote::class) + $this->quote = $this->getMockBuilder(Quote::class) ->disableOriginalConstructor() ->setMethods([ 'getBillingAddress', @@ -135,13 +141,13 @@ protected function setUp(): void 'save' ]) ->getMock(); - $quote->method('getBillingAddress')->willReturn($address); - $quote->method('getShippingAddress')->willReturn($address); - $quote->method('getIsVirtual')->willReturn(false); - $quote->method('getId')->willReturn('12345'); - $quote->method('setIsActive')->willReturn($quote); - $quote->method('getSubtotal')->willReturn(100); - $quote->method('getPayment')->willReturn($payment); + $this->quote->method('getBillingAddress')->willReturn($address); + $this->quote->method('getShippingAddress')->willReturn($address); + $this->quote->method('getIsVirtual')->willReturn(false); + $this->quote->method('getId')->willReturn('12345'); + $this->quote->method('setIsActive')->willReturn($this->quote); + $this->quote->method('getSubtotal')->willReturn(100); + $this->quote->method('getPayment')->willReturn($payment); $this->checkoutSession = $this->getMockBuilder(Session::class) ->disableOriginalConstructor() @@ -161,9 +167,9 @@ protected function setUp(): void 'getPayoneRedirectUrl', 'setPayonePayPalExpressRetry', 'setPayoneCustomerIsRedirected', + 'setIsPayoneAmazonPayAuth', ]) ->getMock(); - $this->checkoutSession->method('getQuote')->willReturn($quote); $this->checkoutSession->method('setLastQuoteId')->willReturn($this->checkoutSession); $this->checkoutSession->method('setLastSuccessQuoteId')->willReturn($this->checkoutSession); $this->checkoutSession->method('unsPayoneWorkorderId')->willReturn($this->checkoutSession); @@ -190,6 +196,7 @@ protected function setUp(): void public function testExecute() { + $this->checkoutSession->method('getQuote')->willReturn($this->quote); $this->checkoutHelper->method('getQuoteComparisonString')->willReturn("QuoteString"); $this->checkoutSession->method('getPayoneQuoteComparisonString')->willReturn("QuoteString"); $this->request->method('getBeforeForwardInfo')->willReturn(false); @@ -199,6 +206,7 @@ public function testExecute() public function testExecuteValidation() { + $this->checkoutSession->method('getQuote')->willReturn($this->quote); $this->request->method('getBeforeForwardInfo')->willReturn([]); $result = $this->classToTest->execute(); $this->assertNull($result); @@ -206,6 +214,7 @@ public function testExecuteValidation() public function testExecuteException() { + $this->checkoutSession->method('getQuote')->willReturn($this->quote); $this->checkoutHelper->method('getQuoteComparisonString')->willReturn("QuoteString"); $this->checkoutSession->method('getPayoneQuoteComparisonString')->willReturn("QuoteString"); @@ -219,6 +228,7 @@ public function testExecuteException() public function testExecuteSubtotalMismatch() { + $this->checkoutSession->method('getQuote')->willReturn($this->quote); $this->checkoutHelper->method('getQuoteComparisonString')->willReturn("QuoteString"); $this->checkoutSession->method('getPayoneQuoteComparisonString')->willReturn("QuoteFalse"); $this->request->method('getBeforeForwardInfo')->willReturn(false); @@ -228,6 +238,7 @@ public function testExecuteSubtotalMismatch() public function testExecutePayPal() { + $this->checkoutSession->method('getQuote')->willReturn($this->quote); $this->checkoutHelper->method('getQuoteComparisonString')->willReturn("QuoteString"); $this->checkoutSession->method('getPayoneQuoteComparisonString')->willReturn("QuoteString"); $this->checkoutSession->method('getPayoneRedirectUrl')->willReturn("http://someurl.test"); @@ -235,4 +246,41 @@ public function testExecutePayPal() $result = $this->classToTest->execute(); $this->assertNull($result); } + + public function testExecuteAmazonPay() + { + $payment = $this->getMockBuilder(Payment::class)->disableOriginalConstructor()->getMock(); + $payment->method('getMethod')->willReturn(PayoneConfig::METHOD_AMAZONPAYV2); + + $address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock(); + + $quote = $this->getMockBuilder(Quote::class) + ->disableOriginalConstructor() + ->setMethods([ + 'getBillingAddress', + 'getShippingAddress', + 'getIsVirtual', + 'getId', + 'setIsActive', + 'getSubtotal', + 'getPayment', + 'save' + ]) + ->getMock(); + $quote->method('getBillingAddress')->willReturn($address); + $quote->method('getShippingAddress')->willReturn($address); + $quote->method('getIsVirtual')->willReturn(false); + $quote->method('getId')->willReturn('12345'); + $quote->method('setIsActive')->willReturn($quote); + $quote->method('getSubtotal')->willReturn(100); + $quote->method('getPayment')->willReturn($payment); + + $this->checkoutHelper->method('getQuoteComparisonString')->willReturn("QuoteString"); + $this->checkoutSession->method('getPayoneQuoteComparisonString')->willReturn("QuoteString"); + $this->checkoutSession->method('getPayoneRedirectUrl')->willReturn("http://someurl.test"); + $this->checkoutSession->method('getQuote')->willReturn($quote); + $this->request->method('getBeforeForwardInfo')->willReturn(false); + $result = $this->classToTest->execute(); + $this->assertNull($result); + } } diff --git a/Test/Unit/Controller/Onepage/ReviewTest.php b/Test/Unit/Controller/Onepage/ReviewTest.php index e5cd0c7c..cc67e339 100644 --- a/Test/Unit/Controller/Onepage/ReviewTest.php +++ b/Test/Unit/Controller/Onepage/ReviewTest.php @@ -40,10 +40,12 @@ use Magento\Quote\Api\Data\CartExtension; use Magento\Quote\Model\ShippingAssignment; use Magento\Quote\Model\Shipping; +use Payone\Core\Model\Methods\AmazonPayV2; use Payone\Core\Model\PayoneConfig; use Payone\Core\Test\Unit\BaseTestCase; use Payone\Core\Test\Unit\PayoneObjectManager; use Magento\Quote\Model\Quote\Payment; +use Payone\Core\Model\Api\Request\Genericpayment\UpdateCheckoutSession; class ReviewTest extends BaseTestCase { @@ -127,10 +129,13 @@ protected function setUp(): void $pageFactory = $this->getMockBuilder(PageFactory::class)->disableOriginalConstructor()->getMock(); $pageFactory->method('create')->willReturn($page); + $updateCheckoutSession = $this->getMockBuilder(UpdateCheckoutSession::class)->disableOriginalConstructor()->getMock(); + $this->classToTest = $this->objectManager->getObject(ClassToTest::class, [ 'context' => $context, 'checkoutSession' => $this->checkoutSession, - 'pageFactory' => $pageFactory + 'pageFactory' => $pageFactory, + 'updateCheckoutSession' => $updateCheckoutSession, ]); } @@ -144,6 +149,19 @@ public function testExecute() $this->assertInstanceOf(Page::class, $result); } + public function testExecuteAmazonPay() + { + $amazonPay = $this->getMockBuilder(AmazonPayV2::class)->disableOriginalConstructor()->getMock(); + + $this->checkoutSession->method('getPayoneWorkorderId')->willReturn('12345'); + $this->payment->method('getMethod')->willReturn('payone_paypal'); + $this->payment->method('getMethodInstance')->willReturn($amazonPay); + + $this->request->method('getBeforeForwardInfo')->willReturn(false); + $result = $this->classToTest->execute(); + $this->assertInstanceOf(Page::class, $result); + } + public function testExecuteRedirect() { $this->payment->method('getMethod')->willReturn(null); diff --git a/Test/Unit/Model/Api/Request/Genericpayment/CreateCheckoutSessionPayloadTest.php b/Test/Unit/Model/Api/Request/Genericpayment/CreateCheckoutSessionPayloadTest.php new file mode 100644 index 00000000..e82b2bc2 --- /dev/null +++ b/Test/Unit/Model/Api/Request/Genericpayment/CreateCheckoutSessionPayloadTest.php @@ -0,0 +1,86 @@ +. + * + * PHP version 5 + * + * @category Payone + * @package Payone_Magento2_Plugin + * @author FATCHIP GmbH + * @copyright 2003 - 2024 Payone GmbH + * @license GNU Lesser General Public License + * @link http://www.payone.de + */ + +namespace Payone\Core\Test\Unit\Model\Api\Request\Genericpayment; + +use Payone\Core\Model\Api\Request\Genericpayment\CreateCheckoutSessionPayload as ClassToTest; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; +use Payone\Core\Helper\Api; +use Payone\Core\Helper\Shop; +use Payone\Core\Model\Methods\AmazonPayV2; +use Magento\Quote\Model\Quote; +use Magento\Quote\Model\Quote\Address; +use Payone\Core\Test\Unit\BaseTestCase; +use Payone\Core\Test\Unit\PayoneObjectManager; + +class CreateCheckoutSessionPayloadTest extends BaseTestCase +{ + /** + * @var ClassToTest + */ + private $classToTest; + + /** + * @var Api|\PHPUnit_Framework_MockObject_MockObject + */ + private $apiHelper; + + /** + * @var Shop|\PHPUnit_Framework_MockObject_MockObject + */ + private $shopHelper; + + protected function setUp(): void + { + $objectManager = $this->getObjectManager(); + + $this->apiHelper = $this->getMockBuilder(Api::class)->disableOriginalConstructor()->getMock(); + $this->shopHelper = $this->getMockBuilder(Shop::class)->disableOriginalConstructor()->getMock(); + + $this->classToTest = $objectManager->getObject(ClassToTest::class, [ + 'shopHelper' => $this->shopHelper, + 'apiHelper' => $this->apiHelper + ]); + } + + public function testSendRequest() + { + $payment = $this->getMockBuilder(AmazonPayV2::class)->disableOriginalConstructor()->getMock(); + $payment->method('getOperationMode')->willReturn('test'); + + $quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock(); + + $this->shopHelper->method('getConfigParam')->willReturn('12345'); + + $response = ['status' => 'APPROVED']; + $this->apiHelper->method('sendApiRequest')->willReturn($response); + $this->apiHelper->method('getCurrencyFromQuote')->willReturn('EUR'); + $this->apiHelper->method('getQuoteAmount')->willReturn('10000'); + + $result = $this->classToTest->sendRequest($payment, $quote); + $this->assertEquals($response, $result); + } +} diff --git a/Test/Unit/Model/Api/Request/Genericpayment/GetCheckoutSessionTest.php b/Test/Unit/Model/Api/Request/Genericpayment/GetCheckoutSessionTest.php new file mode 100644 index 00000000..8fad977c --- /dev/null +++ b/Test/Unit/Model/Api/Request/Genericpayment/GetCheckoutSessionTest.php @@ -0,0 +1,86 @@ +. + * + * PHP version 5 + * + * @category Payone + * @package Payone_Magento2_Plugin + * @author FATCHIP GmbH + * @copyright 2003 - 2024 Payone GmbH + * @license GNU Lesser General Public License + * @link http://www.payone.de + */ + +namespace Payone\Core\Test\Unit\Model\Api\Request\Genericpayment; + +use Payone\Core\Model\Api\Request\Genericpayment\GetCheckoutSession as ClassToTest; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; +use Payone\Core\Helper\Api; +use Payone\Core\Helper\Shop; +use Payone\Core\Model\Methods\AmazonPayV2; +use Magento\Quote\Model\Quote; +use Magento\Quote\Model\Quote\Address; +use Payone\Core\Test\Unit\BaseTestCase; +use Payone\Core\Test\Unit\PayoneObjectManager; + +class GetCheckoutSessionTest extends BaseTestCase +{ + /** + * @var ClassToTest + */ + private $classToTest; + + /** + * @var Api|\PHPUnit_Framework_MockObject_MockObject + */ + private $apiHelper; + + /** + * @var Shop|\PHPUnit_Framework_MockObject_MockObject + */ + private $shopHelper; + + protected function setUp(): void + { + $objectManager = $this->getObjectManager(); + + $this->apiHelper = $this->getMockBuilder(Api::class)->disableOriginalConstructor()->getMock(); + $this->shopHelper = $this->getMockBuilder(Shop::class)->disableOriginalConstructor()->getMock(); + + $this->classToTest = $objectManager->getObject(ClassToTest::class, [ + 'shopHelper' => $this->shopHelper, + 'apiHelper' => $this->apiHelper + ]); + } + + public function testSendRequest() + { + $payment = $this->getMockBuilder(AmazonPayV2::class)->disableOriginalConstructor()->getMock(); + $payment->method('getOperationMode')->willReturn('test'); + + $quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock(); + + $this->shopHelper->method('getConfigParam')->willReturn('12345'); + + $response = ['status' => 'APPROVED']; + $this->apiHelper->method('sendApiRequest')->willReturn($response); + $this->apiHelper->method('getCurrencyFromQuote')->willReturn('EUR'); + $this->apiHelper->method('getQuoteAmount')->willReturn('10000'); + + $result = $this->classToTest->sendRequest($payment, $quote, '1234567890'); + $this->assertEquals($response, $result); + } +} diff --git a/Test/Unit/Model/Api/Request/Genericpayment/UpdateCheckoutSessionTest.php b/Test/Unit/Model/Api/Request/Genericpayment/UpdateCheckoutSessionTest.php new file mode 100644 index 00000000..0859d20c --- /dev/null +++ b/Test/Unit/Model/Api/Request/Genericpayment/UpdateCheckoutSessionTest.php @@ -0,0 +1,86 @@ +. + * + * PHP version 5 + * + * @category Payone + * @package Payone_Magento2_Plugin + * @author FATCHIP GmbH + * @copyright 2003 - 2024 Payone GmbH + * @license GNU Lesser General Public License + * @link http://www.payone.de + */ + +namespace Payone\Core\Test\Unit\Model\Api\Request\Genericpayment; + +use Payone\Core\Model\Api\Request\Genericpayment\UpdateCheckoutSession as ClassToTest; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; +use Payone\Core\Helper\Api; +use Payone\Core\Helper\Shop; +use Payone\Core\Model\Methods\AmazonPayV2; +use Magento\Quote\Model\Quote; +use Magento\Quote\Model\Quote\Address; +use Payone\Core\Test\Unit\BaseTestCase; +use Payone\Core\Test\Unit\PayoneObjectManager; + +class UpdateCheckoutSessionTest extends BaseTestCase +{ + /** + * @var ClassToTest + */ + private $classToTest; + + /** + * @var Api|\PHPUnit_Framework_MockObject_MockObject + */ + private $apiHelper; + + /** + * @var Shop|\PHPUnit_Framework_MockObject_MockObject + */ + private $shopHelper; + + protected function setUp(): void + { + $objectManager = $this->getObjectManager(); + + $this->apiHelper = $this->getMockBuilder(Api::class)->disableOriginalConstructor()->getMock(); + $this->shopHelper = $this->getMockBuilder(Shop::class)->disableOriginalConstructor()->getMock(); + + $this->classToTest = $objectManager->getObject(ClassToTest::class, [ + 'shopHelper' => $this->shopHelper, + 'apiHelper' => $this->apiHelper + ]); + } + + public function testSendRequest() + { + $payment = $this->getMockBuilder(AmazonPayV2::class)->disableOriginalConstructor()->getMock(); + $payment->method('getOperationMode')->willReturn('test'); + + $quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock(); + + $this->shopHelper->method('getConfigParam')->willReturn('12345'); + + $response = ['status' => 'APPROVED']; + $this->apiHelper->method('sendApiRequest')->willReturn($response); + $this->apiHelper->method('getCurrencyFromQuote')->willReturn('EUR'); + $this->apiHelper->method('getQuoteAmount')->willReturn('10000'); + + $result = $this->classToTest->sendRequest($payment, $quote, '1234567890'); + $this->assertEquals($response, $result); + } +} diff --git a/Test/Unit/Model/ConfigProviderTest.php b/Test/Unit/Model/ConfigProviderTest.php index 9ae63bb1..08079355 100644 --- a/Test/Unit/Model/ConfigProviderTest.php +++ b/Test/Unit/Model/ConfigProviderTest.php @@ -38,6 +38,7 @@ use Payone\Core\Helper\Request; use Magento\Framework\Escaper; use Payone\Core\Helper\Consumerscore; +use Payone\Core\Model\Methods\PayoneMethod; use Payone\Core\Model\PayoneConfig; use Magento\Payment\Model\Method\AbstractMethod; use Payone\Core\Test\Unit\BaseTestCase; @@ -46,6 +47,8 @@ use Magento\Customer\Model\Session as CustomerSession; use Payone\Core\Model\ResourceModel\SavedPaymentData; use Magento\Customer\Model\Customer as CustomerModel; +use Payone\Core\Helper\Ratepay; +use Payone\Core\Model\Methods\Ratepay\Installment; class ConfigProviderTest extends BaseTestCase { @@ -132,6 +135,12 @@ protected function setUp(): void $shopHelper = $this->getMockBuilder(Shop::class)->disableOriginalConstructor()->getMock(); $shopHelper->method('getMagentoVersion')->willReturn("2.4.4"); + $ratepayHelper = $this->getMockBuilder(Ratepay::class)->disableOriginalConstructor()->getMock(); + $ratepayHelper->method('getRatepayConfig')->willReturn([PayoneConfig::METHOD_RATEPAY_INSTALLMENT => []]); + + $ratepayInstallment = $this->getMockBuilder(Installment::class)->disableOriginalConstructor()->getMock(); + $ratepayInstallment->method('getAllowedMonths')->willReturn(['test']); + $this->classToTest = $this->objectManager->getObject(ClassToTest::class, [ 'dataHelper' => $this->dataHelper, 'countryHelper' => $countryHelper, @@ -144,17 +153,19 @@ protected function setUp(): void 'checkoutSession' => $this->checkoutSession, 'savedPaymentData' => $savedPaymentData, 'customerSession' => $this->customerSession, - 'shopHelper' => $shopHelper + 'shopHelper' => $shopHelper, + 'ratepayHelper' => $ratepayHelper, ]); } public function testGetConfig() { - $method = $this->getMockBuilder(AbstractMethod::class) + $method = $this->getMockBuilder(PayoneMethod::class) ->disableOriginalConstructor() - ->setMethods(['getInstructions']) + ->setMethods(['getInstructions', 'isAvailable']) ->getMock(); $method->method('getInstructions')->willReturn('Instruction'); + $method->method('isAvailable')->willReturn(true); $this->dataHelper->method('getMethodInstance')->willReturn($method); $this->customerSession->method('isLoggedIn')->willReturn(false); diff --git a/Test/Unit/Model/Methods/AmazonPayV2Test.php b/Test/Unit/Model/Methods/AmazonPayV2Test.php new file mode 100644 index 00000000..9c6af426 --- /dev/null +++ b/Test/Unit/Model/Methods/AmazonPayV2Test.php @@ -0,0 +1,247 @@ +. + * + * PHP version 5 + * + * @category Payone + * @package Payone_Magento2_Plugin + * @author FATCHIP GmbH + * @copyright 2003 - 2024 Payone GmbH + * @license GNU Lesser General Public License + * @link http://www.payone.de + */ + +namespace Payone\Core\Test\Unit\Model\Methods; + +use Magento\Store\Model\Store; +use Payone\Core\Model\Methods\AmazonPayV2 as ClassToTest; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; +use Magento\Sales\Model\Order; +use Payone\Core\Test\Unit\BaseTestCase; +use Payone\Core\Test\Unit\PayoneObjectManager; +use Payone\Core\Helper\Shop; +use Magento\Checkout\Model\Session; +use Magento\Payment\Model\Info; +use Payone\Core\Model\Api\Request\Authorization; +use Magento\Framework\Exception\LocalizedException; +use Magento\Sales\Model\Order\Payment; +use Magento\Framework\Url; +use Magento\Framework\DataObject; +use Payone\Core\Helper\Toolkit; + +class AmazonPayV2Test extends BaseTestCase +{ + /** + * @var ClassToTest + */ + private $classToTest; + + /** + * @var ObjectManager|PayoneObjectManager + */ + private $objectManager; + + /** + * @var Shop + */ + private $shopHelper; + + /** + * @var Session + */ + private $checkoutSession; + + /** + * @var Authorization + */ + private $authorizationRequest; + + protected function setUp(): void + { + $this->objectManager = $this->getObjectManager(); + + $store = $this->getMockBuilder(Store::class)->disableOriginalConstructor()->getMock(); + $store->method('getCode')->willReturn('test'); + + $order = $this->getMockBuilder(Order::class)->disableOriginalConstructor()->getMock(); + $order->method('getStore')->willReturn($store); + + $info = $this->getMockBuilder(Info::class) + ->disableOriginalConstructor() + ->setMethods(['getAdditionalInformation', 'getOrder']) + ->getMock(); + $info->method('getAdditionalInformation')->willReturn('19010101'); + $info->method('getOrder')->willReturn($order); + + $toolkitHelper = $this->getMockBuilder(Toolkit::class)->disableOriginalConstructor()->getMock(); + $toolkitHelper->method('getAdditionalDataEntry')->willReturn('12'); + + $this->shopHelper = $this->getMockBuilder(Shop::class)->disableOriginalConstructor()->getMock(); + + $this->checkoutSession = $this->getMockBuilder(Session::class) + ->disableOriginalConstructor() + ->setMethods([ + 'unsPayoneRedirectUrl', + 'unsPayoneRedirectedPaymentMethod', + 'unsPayoneCanceledPaymentMethod', + 'unsPayoneIsError', + 'unsShowAmazonPendingNotice', + 'unsAmazonRetryAsync', + 'getPayoneIsAmazonPayExpressPayment', + 'getPayoneWorkorderId', + 'setPayoneAmazonPaySignature', + 'setPayoneAmazonPayPayload', + 'setPayoneRedirectUrl', + 'setPayoneRedirectedPaymentMethod', + ]) + ->getMock(); + + $this->authorizationRequest = $this->getMockBuilder(Authorization::class)->disableOriginalConstructor()->getMock(); + + $url = $this->getMockBuilder(Url::class)->disableOriginalConstructor()->getMock(); + $url->method('getUrl')->willReturn('payoneTest'); + + $this->classToTest = $this->objectManager->getObject(ClassToTest::class, [ + 'shopHelper' => $this->shopHelper, + 'toolkitHelper' => $toolkitHelper, + 'checkoutSession' => $this->checkoutSession, + 'authorizationRequest' => $this->authorizationRequest, + 'url' => $url, + ]); + $this->classToTest->setInfoInstance($info); + } + + public function testIsAPBPayment() + { + $this->checkoutSession->method('getPayoneIsAmazonPayExpressPayment')->willReturn(false); + + $this->assertTrue($this->classToTest->isAPBPayment()); + } + + public function testIsAPBPaymentFalse() + { + $this->checkoutSession->method('getPayoneIsAmazonPayExpressPayment')->willReturn(true); + + $this->assertFalse($this->classToTest->isAPBPayment()); + } + + public function testGetMerchantId() + { + $expected = "testMerchant"; + + $this->shopHelper->method('getConfigParam')->willReturn($expected); + + $result = $this->classToTest->getMerchantId(); + $this->assertEquals($expected, $result); + } + + public function testGetButtonColor() + { + $expected = "buttonColor"; + + $this->shopHelper->method('getConfigParam')->willReturn($expected); + + $result = $this->classToTest->getButtonColor(); + $this->assertEquals($expected, $result); + } + + public function testGetButtonLanguage() + { + $this->shopHelper->method('getConfigParam')->willReturn('de-DE'); + + $result = $this->classToTest->getButtonLanguage(); + $this->assertEquals('de_DE', $result); + } + + public function testUseSandbox() + { + $this->shopHelper->method('getConfigParam')->willReturn('test'); + + $result = $this->classToTest->useSandbox(); + $this->assertTrue($result); + } + + public function testUseSandboxFalse() + { + $this->shopHelper->method('getConfigParam')->willReturn('live'); + + $result = $this->classToTest->useSandbox(); + $this->assertFalse($result); + } + + public function testGetSuccessUrl() + { + $result = $this->classToTest->getSuccessUrl(); + $this->assertStringContainsString("payone", $result); + + $this->classToTest->setNeedsReturnedUrl(true); + + $result = $this->classToTest->getSuccessUrl(); + $this->assertStringContainsString("payone", $result); + } + + public function testAssignData() + { + $data = $this->getMockBuilder(DataObject::class)->disableOriginalConstructor()->getMock(); + + $result = $this->classToTest->assignData($data); + $this->assertInstanceOf(ClassToTest::class, $result); + } + + public function testGetPaymentSpecificParameters() + { + $order = $this->getMockBuilder(Order::class)->disableOriginalConstructor()->getMock(); + $order->method('getIsVirtual')->willReturn(false); + + $this->checkoutSession->method('getPayoneWorkorderId')->willReturn('12345'); + $this->checkoutSession->method('getPayoneIsAmazonPayExpressPayment')->willReturn(false); + + $result = $this->classToTest->getPaymentSpecificParameters($order); + $this->assertCount(5, $result); + } + + public function testGetFrontendConfig() + { + $this->shopHelper->method('getConfigParam')->willReturn('de-DE'); + + $result = $this->classToTest->getFrontendConfig(); + $this->assertCount(5, $result); + } + + public function testAuthorize() + { + $this->checkoutSession->method('getPayoneIsAmazonPayExpressPayment')->willReturn(false); + + $store = $this->getMockBuilder(Store::class)->disableOriginalConstructor()->getMock(); + $store->method('getCode')->willReturn('test'); + + $payment = $this->getMockBuilder(Payment::class)->disableOriginalConstructor()->getMock(); + $payment->method('getAdditionalInformation')->willReturn([]); + + $order = $this->getMockBuilder(Order::class)->disableOriginalConstructor()->getMock(); + $order->method('getStore')->willReturn($store); + $order->method('getPayment')->willReturn($payment); + + $paymentInfo = $this->getMockBuilder(Info::class)->disableOriginalConstructor()->setMethods(['getOrder'])->getMock(); + $paymentInfo->method('getOrder')->willReturn($order); + + $aResponse = ['status' => 'REDIRECT', 'txid' => '12345', 'redirecturl' => 'http://testdomain.com', 'add_paydata[signature]' => 'test', 'add_paydata[payload]' => 'test']; + $this->authorizationRequest->method('sendRequest')->willReturn($aResponse); + + $result = $this->classToTest->authorize($paymentInfo, 100); + $this->assertInstanceOf(ClassToTest::class, $result); + } +} diff --git a/Test/Unit/Model/Methods/BNPL/DebitTest.php b/Test/Unit/Model/Methods/BNPL/DebitTest.php index d7dbe5c4..16320c3a 100644 --- a/Test/Unit/Model/Methods/BNPL/DebitTest.php +++ b/Test/Unit/Model/Methods/BNPL/DebitTest.php @@ -26,9 +26,11 @@ namespace Payone\Core\Test\Unit\Model\Methods\BNPL; +use Payone\Core\Helper\Api; use Payone\Core\Model\Methods\BNPL\Debit as ClassToTest; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; use Magento\Sales\Model\Order; +use Magento\Quote\Model\Quote; use Magento\Quote\Model\Quote\Address; use Magento\Payment\Model\InfoInterface; use Magento\Payment\Model\Info; @@ -36,6 +38,8 @@ use Magento\Store\Model\Store; use Payone\Core\Test\Unit\BaseTestCase; use Payone\Core\Test\Unit\PayoneObjectManager; +use Magento\Framework\App\Config\ScopeConfigInterface; +use Magento\Quote\Api\Data\CartInterface; class DebitTest extends BaseTestCase { @@ -49,14 +53,27 @@ class DebitTest extends BaseTestCase */ private $objectManager; + /** + * @var ScopeConfigInterface + */ + private $scopeConfig; + protected function setUp(): void { $this->objectManager = $this->getObjectManager(); + $this->scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class)->disableOriginalConstructor()->getMock(); + $info = $this->getMockBuilder(Info::class)->disableOriginalConstructor()->setMethods(['getAdditionalInformation', 'getOrder'])->getMock(); $info->method('getAdditionalInformation')->willReturn('test'); - $this->classToTest = $this->objectManager->getObject(ClassToTest::class); + $apiHelper = $this->getMockBuilder(Api::class)->disableOriginalConstructor()->getMock(); + $apiHelper->method('getCurrencyFromOrder')->willReturn('GBP'); + + $this->classToTest = $this->objectManager->getObject(ClassToTest::class, [ + 'scopeConfig' => $this->scopeConfig, + 'apiHelper' => $apiHelper, + ]); $this->classToTest->setInfoInstance($info); } @@ -72,4 +89,22 @@ public function testGetSubTypeSpecificParameters() $result = $this->classToTest->getSubTypeSpecificParameters($order); $this->assertCount(2, $result); } + + public function testIsAvailable() + { + $this->scopeConfig->method('getValue')->willReturn(1); + + $result = $this->classToTest->isAvailable(); + $this->assertTrue($result); + } + + public function testIsAvailableNotEuro() + { + $this->scopeConfig->method('getValue')->willReturn(0); + + $quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock(); + + $result = $this->classToTest->isAvailable($quote); + $this->assertFalse($result); + } } diff --git a/Test/Unit/Model/Methods/Klarna/InstallmentTest.php b/Test/Unit/Model/Methods/Klarna/InstallmentTest.php index a7ee1537..1a2199a5 100644 --- a/Test/Unit/Model/Methods/Klarna/InstallmentTest.php +++ b/Test/Unit/Model/Methods/Klarna/InstallmentTest.php @@ -110,7 +110,7 @@ public function testIsAvailableB2BSessionQuote() $quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock(); $quote->method('getBillingAddress')->willReturn($address); - $result = $this->classToTest->isAvailable(); + $result = $this->classToTest->isAvailable($quote); $this->assertFalse($result); } diff --git a/Test/Unit/Model/Methods/PayoneMethodTest.php b/Test/Unit/Model/Methods/PayoneMethodTest.php index 768ffc0d..72f846f1 100644 --- a/Test/Unit/Model/Methods/PayoneMethodTest.php +++ b/Test/Unit/Model/Methods/PayoneMethodTest.php @@ -154,4 +154,12 @@ public function testGetSuccessUrl() $result = $this->classToTest->getSuccessUrl(); $this->assertEquals($expected, $result); } + + public function testGetFrontendConfig() + { + $expected = []; + + $result = $this->classToTest->getFrontendConfig(); + $this->assertEquals($expected, $result); + } } diff --git a/Test/Unit/Service/V1/AmazonPayTest.php b/Test/Unit/Service/V1/AmazonPayTest.php index d1600bc9..caf7897d 100644 --- a/Test/Unit/Service/V1/AmazonPayTest.php +++ b/Test/Unit/Service/V1/AmazonPayTest.php @@ -43,6 +43,7 @@ use Payone\Core\Model\Api\Request\Genericpayment\GetConfiguration; use Payone\Core\Model\Api\Request\Genericpayment\GetOrderReferenceDetails; use Payone\Core\Model\Api\Request\Genericpayment\SetOrderReferenceDetails; +use Payone\Core\Model\Api\Request\Genericpayment\CreateCheckoutSessionPayload; use Payone\Core\Helper\Order; use Magento\Quote\Model\Quote\Payment; use Magento\Checkout\Model\Type\Onepage; @@ -63,6 +64,11 @@ class AmazonPayTest extends BaseTestCase */ private $response; + /** + * @var CreateCheckoutSessionPayload|\PHPUnit\Framework\MockObject\MockObject + */ + private $createCheckoutSessionPayload; + protected function setUp(): void { $objectManager = $this->getObjectManager(); @@ -112,10 +118,23 @@ protected function setUp(): void $checkoutSession = $this->getMockBuilder(Session::class) ->disableOriginalConstructor() - ->setMethods(['getAmazonWorkorderId', 'getQuote', 'setAmazonWorkorderId', 'setAmazonAddressToken', 'setAmazonReferenceId']) + ->setMethods([ + 'getAmazonWorkorderId', + 'getQuote', + 'setAmazonWorkorderId', + 'setAmazonAddressToken', + 'setAmazonReferenceId', + 'setPayoneIsAmazonPayExpressPayment', + 'setPayoneWorkorderId', + 'setPayoneQuoteComparisonString', + 'getPayoneAmazonPayPayload', + 'getPayoneAmazonPaySignature', + ]) ->getMock(); $checkoutSession->method('getAmazonWorkorderId')->willReturn(null); $checkoutSession->method('getQuote')->willReturn($quote); + $checkoutSession->method('getPayoneAmazonPayPayload')->willReturn('test'); + $checkoutSession->method('getPayoneAmazonPaySignature')->willReturn('test'); $aGetConfigurationResponse = [ 'status' => 'OK', @@ -138,6 +157,8 @@ protected function setUp(): void $setOrderReferenceDetails = $this->getMockBuilder(SetOrderReferenceDetails::class)->disableOriginalConstructor()->getMock(); $setOrderReferenceDetails->method('sendRequest')->willReturn($aSetOrderReferenceDetailsResponse); + $this->createCheckoutSessionPayload = $this->getMockBuilder(CreateCheckoutSessionPayload::class)->disableOriginalConstructor()->getMock(); + $checkoutHelper = $this->getMockBuilder(Checkout::class)->disableOriginalConstructor()->getMock(); $checkoutHelper->method('getCurrentCheckoutMethod')->willReturn(Onepage::METHOD_GUEST); @@ -157,6 +178,7 @@ protected function setUp(): void 'getConfiguration' => $getConfiguration, 'getOrderReferenceDetails' => $getOrderReferenceDetails, 'setOrderReferenceDetails' => $setOrderReferenceDetails, + 'createCheckoutSessionPayload' => $this->createCheckoutSessionPayload, 'orderHelper' => $orderHelper, 'checkoutHelper' => $checkoutHelper, 'view' => $view @@ -168,4 +190,37 @@ public function testGetWorkorderId() $result = $this->classToTest->getWorkorderId('123', '123'); $this->assertInstanceOf(AmazonPayResponse::class, $result); } + + public function testGetAmazonPayApbSession() + { + $result = $this->classToTest->getAmazonPayApbSession(4711); + $this->assertInstanceOf(AmazonPayResponse::class, $result); + $this->assertTrue($result->getSuccess()); + } + + public function testGetCheckoutSessionPayload() + { + $this->createCheckoutSessionPayload->method('sendRequest')->willReturn([ + 'status' => 'OK', + 'add_paydata[signature]' => 'test', + 'add_paydata[payload]' => 'test', + 'workorderid' => 'test', + ]); + + $result = $this->classToTest->getCheckoutSessionPayload(4711); + $this->assertInstanceOf(AmazonPayResponse::class, $result); + $this->assertTrue($result->getSuccess()); + } + + public function testGetCheckoutSessionPayloadError() + { + $this->createCheckoutSessionPayload->method('sendRequest')->willReturn([ + 'status' => 'ERROR', + 'customermessage' => 'ERROR', + ]); + + $result = $this->classToTest->getCheckoutSessionPayload(4711); + $this->assertInstanceOf(AmazonPayResponse::class, $result); + $this->assertFalse($result->getSuccess()); + } } diff --git a/Test/Unit/Service/V1/Data/AmazonPayResponseTest.php b/Test/Unit/Service/V1/Data/AmazonPayResponseTest.php index 51f2a73f..45231773 100644 --- a/Test/Unit/Service/V1/Data/AmazonPayResponseTest.php +++ b/Test/Unit/Service/V1/Data/AmazonPayResponseTest.php @@ -47,6 +47,8 @@ protected function setUp(): void 'success' => true, 'redirectUrl' => 'http://www.test.com', 'amazonReviewHtml' => 'test', + 'payload' => 'testPayload', + 'signature' => 'testSignature', ]; $this->classToTest = $objectManager->getObject(ClassToTest::class, [ @@ -80,4 +82,18 @@ public function testGetAmazonReviewHtml() $expected = 'test'; $this->assertEquals($expected, $result); } + + public function testGetPayload() + { + $result = $this->classToTest->getPayload(); + $expected = 'testPayload'; + $this->assertEquals($expected, $result); + } + + public function testGetSignature() + { + $result = $this->classToTest->getSignature(); + $expected = 'testSignature'; + $this->assertEquals($expected, $result); + } } diff --git a/view/frontend/templates/amazon/buttonv2.phtml b/view/frontend/templates/amazon/buttonv2.phtml index 9a2958d9..8efced88 100644 --- a/view/frontend/templates/amazon/buttonv2.phtml +++ b/view/frontend/templates/amazon/buttonv2.phtml @@ -33,7 +33,6 @@ }