Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BackendTest.php
Go to the documentation of this file.
1 <?php
7 
14 
15 class BackendTest extends \PHPUnit\Framework\TestCase
16 {
17  const SCOPE = 'website';
18 
19  const SCOPE_ID = 1;
20 
24  private $context;
25 
29  private $request;
30 
34  private $directoryHelperMock;
35 
39  private $backendConfig;
40 
44  private $scopeDefiner;
45 
49  private $helper;
50 
51  protected function setUp()
52  {
53  $this->context = $this->getMockBuilder(\Magento\Framework\App\Helper\Context::class)
54  ->disableOriginalConstructor()
55  ->getMock();
56  $this->request = $this->createMock(\Magento\Framework\App\RequestInterface::class);
57  $this->context->expects(static::once())
58  ->method('getRequest')
59  ->willReturn($this->request);
60  $this->directoryHelperMock = $this->getMockBuilder(\Magento\Directory\Helper\Data::class)
61  ->disableOriginalConstructor()
62  ->getMock();
63  $this->backendConfig = $this->getMockBuilder(\Magento\Config\Model\Config::class)
64  ->disableOriginalConstructor()
65  ->getMock();
66  $this->scopeDefiner = $this->getMockBuilder(\Magento\Config\Model\Config\ScopeDefiner::class)
67  ->disableOriginalConstructor()
68  ->getMock();
69 
70  $this->helper = new Backend(
71  $this->context,
72  $this->directoryHelperMock,
73  $this->backendConfig,
74  $this->scopeDefiner
75  );
76  }
77 
79  {
80  $this->configurationCountryCodePrepareRequest('US');
81  $this->configurationCountryCodeAssertResult('US');
82  }
83 
89  {
90  $this->configurationCountryCodePrepareRequest($request);
91  $this->configurationCountryCodePrepareConfig('GB');
92  $this->configurationCountryCodeAssertResult('GB');
93  }
94 
99  {
100  return [
101  [null],
102  ['not country code'],
103  ];
104  }
105 
112  public function testGetConfigurationCountryCodeFromDefault($request, $config, $default)
113  {
114  $this->configurationCountryCodePrepareRequest($request);
115  $this->configurationCountryCodePrepareConfig($config);
116  $this->directoryHelperMock->expects($this->once())
117  ->method('getDefaultCountry')
118  ->will($this->returnValue($default));
119  $this->configurationCountryCodeAssertResult($default);
120  }
121 
126  {
127  return [
128  [null, false, 'DE'],
129  ['not country code', false, 'DE'],
130  ['not country code', '', 'any final result']
131  ];
132  }
133 
139  private function configurationCountryCodePrepareRequest($request)
140  {
141  $this->request->expects($this->atLeastOnce())
142  ->method('getParam')
143  ->willReturnMap(
144  [
145  [StructurePlugin::REQUEST_PARAM_COUNTRY, null, $request],
146  [self::SCOPE, null, self::SCOPE_ID]
147  ]
148  );
149  }
150 
156  private function configurationCountryCodePrepareConfig($config)
157  {
158 
159  $this->scopeDefiner->expects($this->once())
160  ->method('getScope')
161  ->willReturn(self::SCOPE);
162 
163  $this->backendConfig->expects($this->once())
164  ->method('setData')
165  ->with(self::SCOPE, self::SCOPE_ID);
166 
167  $this->backendConfig->expects($this->once())
168  ->method('getConfigDataValue')
169  ->with(\Magento\Paypal\Block\Adminhtml\System\Config\Field\Country::FIELD_CONFIG_PATH)
170  ->willReturn($config);
171  }
172 
178  private function configurationCountryCodeAssertResult($expected)
179  {
180  $this->assertEquals($expected, $this->helper->getConfigurationCountryCode());
181  }
182 }
$config
Definition: fraud_order.php:17
testGetConfigurationCountryCodeFromDefault($request, $config, $default)