123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Payment\Test\Unit\Block\Transparent;
- use Magento\Checkout\Model\Session;
- use Magento\Framework\App\RequestInterface;
- use Magento\Framework\DataObject;
- use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
- use Magento\Framework\UrlInterface;
- use Magento\Payment\Model\Method\TransparentInterface;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class FormTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var FormTesting | \PHPUnit_Framework_MockObject_MockObject
- */
- private $form;
- /**
- * @var RequestInterface | \PHPUnit_Framework_MockObject_MockObject
- */
- private $requestMock;
- /**
- * @var UrlInterface | \PHPUnit_Framework_MockObject_MockObject
- */
- private $urlBuilderMock;
- /**
- * @var TransparentInterface | \PHPUnit_Framework_MockObject_MockObject
- */
- private $methodMock;
- /**
- * @var Session | \PHPUnit_Framework_MockObject_MockObject
- */
- private $checkoutSessionMock;
- protected function setUp()
- {
- $objectManagerHelper = new ObjectManager($this);
- $this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
- ->setMethods(['getParam'])
- ->getMockForAbstractClass();
- $this->urlBuilderMock = $this->getMockBuilder(\Magento\Framework\UrlInterface::class)
- ->setMethods(['getUrl'])
- ->getMockForAbstractClass();
- $context = $objectManagerHelper->getObject(
- \Magento\Framework\View\Element\Template\Context::class,
- [
- 'request' => $this->requestMock,
- 'urlBuilder' => $this->urlBuilderMock
- ]
- );
- $this->methodMock = $this->getMockBuilder(\Magento\Payment\Model\Method\TransparentInterface::class)
- ->getMock();
- $this->checkoutSessionMock = $this->getMockBuilder(\Magento\Checkout\Model\Session::class)
- ->setMethods([])
- ->disableOriginalConstructor()
- ->getMock();
- $paymentConfigMock = $this->getMockBuilder(\Magento\Payment\Model\Config::class)
- ->setMethods([])
- ->disableOriginalConstructor()
- ->getMock();
- $this->form = new FormTesting(
- $context,
- $paymentConfigMock,
- $this->checkoutSessionMock
- );
- }
- public function testIsAjaxRequest()
- {
- $this->requestMock->expects($this->exactly(2))
- ->method('getParam')
- ->with('isAjax')
- ->willReturnOnConsecutiveCalls(true, false);
- $this->assertTrue($this->form->isAjaxRequest());
- $this->assertFalse($this->form->isAjaxRequest());
- }
- /**
- * @param string $fieldName
- * @param mixed $fieldValue
- * @param mixed $expected
- *
- * @dataProvider getMethodConfigDataDataProvider
- */
- public function testGetMethodConfigData($fieldName, $fieldValue, $expected)
- {
- $this->initializeMethodWithConfigMock([[$fieldName, null, $fieldValue]]);
- $this->form->setMethod($this->methodMock);
- $this->assertEquals($expected, $this->form->getMethodConfigData($fieldName));
- }
- /**
- * Initializes method mock with config mock
- *
- * @param array $configMap
- */
- private function initializeMethodWithConfigMock(array $configMap = [])
- {
- $configInterface = $this->getMockBuilder(\Magento\Payment\Model\Method\ConfigInterface::class)
- ->getMock();
- $configInterface->expects($this->any())
- ->method('getValue')
- ->willReturnMap($configMap);
- $this->methodMock->expects($this->any())
- ->method('getConfigInterface')
- ->willReturn($configInterface);
- }
- /**
- * Data provider for testGetMethodConfigData
- *
- * @see testGetMethodConfigData
- *
- * @case #1 Set string value
- * @case #2 Set boolean value
- *
- * @return array
- */
- public function getMethodConfigDataDataProvider()
- {
- return [
- ['gateway_name', 'payment_gateway', 'payment_gateway'],
- ['sandbox_flag', true, true],
- ];
- }
- /**
- * @dataProvider getCgiUrlDataProvider
- *
- * @param $sandboxFlag
- * @param $cgiUrlTestMode
- * @param $cgiUrl
- * @param $expectedUrl
- */
- public function testGetCgiUrl($sandboxFlag, $cgiUrlTestMode, $cgiUrl, $expectedUrl)
- {
- $this->initializeMethodWithConfigMock(
- [
- ['sandbox_flag', null, $sandboxFlag],
- ['cgi_url_test_mode', null, $cgiUrlTestMode],
- ['cgi_url', null, $cgiUrl]
- ]
- );
- $this->form->setMethod($this->methodMock);
- $this->assertEquals($expectedUrl, $this->form->getCgiUrl());
- }
- /**
- * Data provider for testGetCgiUrl
- *
- * @see testGetCgiUrl
- *
- * @case #1 The sandboxFlag is 1 we expected cgi_url_test_mode_value
- * @case #2 The sandboxFlag is 0 we expected cgi_url_value
- *
- * @return array
- */
- public function getCgiUrlDataProvider()
- {
- return [
- [
- 1,
- 'cgi_url_test_mode_value',
- 'cgi_url_value',
- 'cgi_url_test_mode_value'
- ],
- [
- 0,
- 'cgi_url_test_mode_value',
- 'cgi_url_value',
- 'cgi_url_value'
- ],
- ];
- }
- public function testGetOrderUrl()
- {
- $orderUrlPattern = 'order_url';
- $builtOrderUrl = 'built_url';
- $this->initializeMethodWithConfigMock([['place_order_url', null, $orderUrlPattern]]);
- $this->urlBuilderMock->expects($this->once())
- ->method('getUrl')
- ->with($orderUrlPattern)
- ->willReturn($builtOrderUrl);
- $this->form->setMethod($this->methodMock);
- $this->assertEquals($builtOrderUrl, $this->form->getOrderUrl());
- }
- public function testGetDateDelim()
- {
- $dateDelimiter = '/';
- $this->initializeMethodWithConfigMock([['date_delim', null, $dateDelimiter]]);
- $this->form->setMethod($this->methodMock);
- $this->assertEquals($dateDelimiter, $this->form->getDateDelim());
- }
- public function testGetCardFieldsMap()
- {
- $ccfields = 'x_card_code,x_exp_date,x_card_num';
- $this->initializeMethodWithConfigMock([['ccfields', null, $ccfields]]);
- $this->form->setMethod($this->methodMock);
- $expected = json_encode(['cccvv' => 'x_card_code', 'ccexpdate' => 'x_exp_date', 'ccnum' => 'x_card_num']);
- $this->assertEquals($expected, $this->form->getCardFieldsMap());
- }
- public function testToHtmlShouldRender()
- {
- $quoteMock = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
- ->disableOriginalConstructor()
- ->getMock();
- $paymentMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Payment::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->checkoutSessionMock->expects($this->once())
- ->method('getQuote')
- ->willReturn($quoteMock);
- $quoteMock->expects($this->once())
- ->method('getPayment')
- ->willReturn($paymentMock);
- $paymentMock->expects($this->once())
- ->method('getMethodInstance')
- ->willReturn($this->methodMock);
- $this->form->toHtml();
- }
- public function testToHtmlShouldNotRenderEmptyQuote()
- {
- $this->checkoutSessionMock->expects($this->once())
- ->method('getQuote')
- ->willReturn(null);
- $this->assertEmpty($this->form->toHtml());
- }
- public function testToHtmlShouldNotRenderEmptyPayment()
- {
- $quoteMock = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->checkoutSessionMock->expects($this->once())
- ->method('getQuote')
- ->willReturn($quoteMock);
- $quoteMock->expects($this->once())
- ->method('getPayment')
- ->willReturn(null);
- $this->assertEmpty($this->form->toHtml());
- }
- public function testGetMethodSuccess()
- {
- $this->form->setMethod($this->methodMock);
- $this->assertSame($this->methodMock, $this->form->getMethod());
- }
- public function testGetMethodNotTransparentInterface()
- {
- $this->expectException(\Magento\Framework\Exception\LocalizedException::class);
- $this->expectExceptionMessage((string)__('We cannot retrieve the transparent payment method model object.'));
- $methodMock = $this->getMockBuilder(\Magento\Payment\Model\MethodInterface::class)
- ->getMockForAbstractClass();
- $this->form->setMethod($methodMock);
- $this->form->getMethod();
- }
- }
|