BackendTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Test\Unit\Helper;
  7. use Magento\Config\Model\Config;
  8. use Magento\Config\Model\Config\ScopeDefiner;
  9. use Magento\Directory\Helper\Data;
  10. use Magento\Framework\App\Helper\Context;
  11. use Magento\Paypal\Helper\Backend;
  12. use Magento\Paypal\Model\Config\StructurePlugin;
  13. class BackendTest extends \PHPUnit\Framework\TestCase
  14. {
  15. const SCOPE = 'website';
  16. const SCOPE_ID = 1;
  17. /**
  18. * @var Context|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. private $context;
  21. /**
  22. * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. private $request;
  25. /**
  26. * @var Data|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. private $directoryHelperMock;
  29. /**
  30. * @var Config|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. private $backendConfig;
  33. /**
  34. * @var ScopeDefiner|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. private $scopeDefiner;
  37. /**
  38. * @var Backend
  39. */
  40. private $helper;
  41. protected function setUp()
  42. {
  43. $this->context = $this->getMockBuilder(\Magento\Framework\App\Helper\Context::class)
  44. ->disableOriginalConstructor()
  45. ->getMock();
  46. $this->request = $this->createMock(\Magento\Framework\App\RequestInterface::class);
  47. $this->context->expects(static::once())
  48. ->method('getRequest')
  49. ->willReturn($this->request);
  50. $this->directoryHelperMock = $this->getMockBuilder(\Magento\Directory\Helper\Data::class)
  51. ->disableOriginalConstructor()
  52. ->getMock();
  53. $this->backendConfig = $this->getMockBuilder(\Magento\Config\Model\Config::class)
  54. ->disableOriginalConstructor()
  55. ->getMock();
  56. $this->scopeDefiner = $this->getMockBuilder(\Magento\Config\Model\Config\ScopeDefiner::class)
  57. ->disableOriginalConstructor()
  58. ->getMock();
  59. $this->helper = new Backend(
  60. $this->context,
  61. $this->directoryHelperMock,
  62. $this->backendConfig,
  63. $this->scopeDefiner
  64. );
  65. }
  66. public function testGetConfigurationCountryCodeFromRequest()
  67. {
  68. $this->configurationCountryCodePrepareRequest('US');
  69. $this->configurationCountryCodeAssertResult('US');
  70. }
  71. /**
  72. * @param string|null $request
  73. * @dataProvider getConfigurationCountryCodeFromConfigDataProvider
  74. */
  75. public function testGetConfigurationCountryCodeFromConfig($request)
  76. {
  77. $this->configurationCountryCodePrepareRequest($request);
  78. $this->configurationCountryCodePrepareConfig('GB');
  79. $this->configurationCountryCodeAssertResult('GB');
  80. }
  81. /**
  82. * @return array
  83. */
  84. public function getConfigurationCountryCodeFromConfigDataProvider()
  85. {
  86. return [
  87. [null],
  88. ['not country code'],
  89. ];
  90. }
  91. /**
  92. * @param string|null $request
  93. * @param string|null|false $config
  94. * @param string|null $default
  95. * @dataProvider getConfigurationCountryCodeFromDefaultDataProvider
  96. */
  97. public function testGetConfigurationCountryCodeFromDefault($request, $config, $default)
  98. {
  99. $this->configurationCountryCodePrepareRequest($request);
  100. $this->configurationCountryCodePrepareConfig($config);
  101. $this->directoryHelperMock->expects($this->once())
  102. ->method('getDefaultCountry')
  103. ->will($this->returnValue($default));
  104. $this->configurationCountryCodeAssertResult($default);
  105. }
  106. /**
  107. * @return array
  108. */
  109. public function getConfigurationCountryCodeFromDefaultDataProvider()
  110. {
  111. return [
  112. [null, false, 'DE'],
  113. ['not country code', false, 'DE'],
  114. ['not country code', '', 'any final result']
  115. ];
  116. }
  117. /**
  118. * Prepare request for test
  119. *
  120. * @param string|null $request
  121. */
  122. private function configurationCountryCodePrepareRequest($request)
  123. {
  124. $this->request->expects($this->atLeastOnce())
  125. ->method('getParam')
  126. ->willReturnMap(
  127. [
  128. [StructurePlugin::REQUEST_PARAM_COUNTRY, null, $request],
  129. [self::SCOPE, null, self::SCOPE_ID]
  130. ]
  131. );
  132. }
  133. /**
  134. * Prepare backend config for test
  135. *
  136. * @param string|null|false $config
  137. */
  138. private function configurationCountryCodePrepareConfig($config)
  139. {
  140. $this->scopeDefiner->expects($this->once())
  141. ->method('getScope')
  142. ->willReturn(self::SCOPE);
  143. $this->backendConfig->expects($this->once())
  144. ->method('setData')
  145. ->with(self::SCOPE, self::SCOPE_ID);
  146. $this->backendConfig->expects($this->once())
  147. ->method('getConfigDataValue')
  148. ->with(\Magento\Paypal\Block\Adminhtml\System\Config\Field\Country::FIELD_CONFIG_PATH)
  149. ->willReturn($config);
  150. }
  151. /**
  152. * Assert result of getConfigurationCountryCode method
  153. *
  154. * @param string $expected
  155. */
  156. private function configurationCountryCodeAssertResult($expected)
  157. {
  158. $this->assertEquals($expected, $this->helper->getConfigurationCountryCode());
  159. }
  160. }