ConfigTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Payment\Test\Unit\Model;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  8. use Magento\Payment\Model\Config;
  9. use Magento\Payment\Model\MethodInterface;
  10. use Magento\Store\Model\ScopeInterface;
  11. /**
  12. * Class ConfigTest
  13. */
  14. class ConfigTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /** @var \Magento\Payment\Model\Config */
  17. protected $config;
  18. /** @var ObjectManagerHelper */
  19. protected $objectManagerHelper;
  20. /** @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject */
  21. protected $scopeConfig;
  22. /** @var \Magento\Payment\Model\Method\Factory|\PHPUnit_Framework_MockObject_MockObject */
  23. protected $paymentMethodFactory;
  24. /** @var \Magento\Framework\Locale\ResolverInterface|\PHPUnit_Framework_MockObject_MockObject */
  25. protected $localeResolver;
  26. /** @var \Magento\Framework\Config\DataInterface|\PHPUnit_Framework_MockObject_MockObject */
  27. protected $dataStorage;
  28. /**
  29. * @var \Magento\Framework\Stdlib\DateTime\DateTime|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $date;
  32. /**
  33. * Test payments list
  34. *
  35. * @var array
  36. */
  37. private $paymentMethodsList = [
  38. 'not_active_method' => ['active' => 0],
  39. 'active_method_no_model' => ['active' => 1],
  40. 'active_method' => ['active' => 1, 'model' => 'model_name'],
  41. ];
  42. /**
  43. * List of test month
  44. *
  45. * @var array
  46. */
  47. protected $monthList = [
  48. 1 => 'January',
  49. 'February',
  50. 'March',
  51. 'April',
  52. 'May',
  53. 'June',
  54. 'July',
  55. 'August',
  56. 'September',
  57. 'October',
  58. 'November',
  59. 'December',
  60. ];
  61. /**
  62. * Expected months list
  63. *
  64. * @var array
  65. */
  66. protected $expectedMonthList = [
  67. 1 => '01 - January',
  68. '02 - February',
  69. '03 - March',
  70. '04 - April',
  71. '05 - May',
  72. '06 - June',
  73. '07 - July',
  74. '08 - August',
  75. '09 - September',
  76. '10 - October',
  77. '11 - November',
  78. '12 - December',
  79. ];
  80. /**
  81. * Current year value in ISO
  82. */
  83. const CURRENT_YEAR = '2250';
  84. protected function setUp()
  85. {
  86. $this->scopeConfig = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  87. $this->paymentMethodFactory = $this->createMock(\Magento\Payment\Model\Method\Factory::class);
  88. $this->localeResolver = $this->createMock(\Magento\Framework\Locale\ResolverInterface::class);
  89. $this->dataStorage = $this->createMock(\Magento\Framework\Config\DataInterface::class);
  90. $this->date = $this->createMock(\Magento\Framework\Stdlib\DateTime\DateTime::class);
  91. $this->objectManagerHelper = new ObjectManagerHelper($this);
  92. $this->config = $this->objectManagerHelper->getObject(
  93. \Magento\Payment\Model\Config::class,
  94. [
  95. 'scopeConfig' => $this->scopeConfig,
  96. 'paymentMethodFactory' => $this->paymentMethodFactory,
  97. 'localeResolver' => $this->localeResolver,
  98. 'dataStorage' => $this->dataStorage,
  99. 'date' => $this->date
  100. ]
  101. );
  102. }
  103. /**
  104. * @covers \Magento\Payment\Model\Config::getActiveMethods
  105. * @param bool $isActive
  106. * @dataProvider getActiveMethodsDataProvider
  107. */
  108. public function testGetActiveMethods($isActive)
  109. {
  110. $adapter = $this->createMock(MethodInterface::class);
  111. $this->scopeConfig->expects(static::once())
  112. ->method('getValue')
  113. ->with('payment', ScopeInterface::SCOPE_STORE, null)
  114. ->willReturn($this->paymentMethodsList);
  115. $this->paymentMethodFactory->expects(static::once())
  116. ->method('create')
  117. ->with($this->paymentMethodsList['active_method']['model'])
  118. ->willReturn($adapter);
  119. $adapter->expects(static::once())
  120. ->method('setStore')
  121. ->with(null);
  122. $adapter->expects(static::once())
  123. ->method('getConfigData')
  124. ->with('active', static::isNull())
  125. ->willReturn($isActive);
  126. static::assertEquals($isActive ? ['active_method' => $adapter] : [], $this->config->getActiveMethods());
  127. }
  128. /**
  129. * @return array
  130. */
  131. public function getActiveMethodsDataProvider()
  132. {
  133. return [[true], [false]];
  134. }
  135. public function testGetCcTypes()
  136. {
  137. $expected = [];
  138. $this->dataStorage->expects($this->once())->method('get')->with('credit_cards')->will(
  139. $this->returnValue($expected)
  140. );
  141. $this->assertEquals($expected, $this->config->getCcTypes());
  142. }
  143. public function testGetMethodsInfo()
  144. {
  145. $expected = [];
  146. $this->dataStorage->expects($this->once())->method('get')->with('methods')->will(
  147. $this->returnValue($expected)
  148. );
  149. $this->assertEquals($expected, $this->config->getMethodsInfo());
  150. }
  151. public function testGetGroups()
  152. {
  153. $expected = [];
  154. $this->dataStorage->expects($this->once())->method('get')->with('groups')->will(
  155. $this->returnValue($expected)
  156. );
  157. $this->assertEquals($expected, $this->config->getGroups());
  158. }
  159. public function testGetMonths()
  160. {
  161. $this->localeResolver->expects($this->once())->method('getLocale')->willReturn('en_US');
  162. $this->assertEquals($this->expectedMonthList, $this->config->getMonths());
  163. }
  164. public function testGetYears()
  165. {
  166. $this->date->expects($this->once())->method('date')->with('Y')->will($this->returnValue(self::CURRENT_YEAR));
  167. $this->assertEquals($this->_getPreparedYearsList(), $this->config->getYears());
  168. }
  169. /**
  170. * Generates expected years list
  171. *
  172. * @return array
  173. */
  174. private function _getPreparedYearsList()
  175. {
  176. $expectedYearsList = [];
  177. for ($index = 0; $index <= Config::YEARS_RANGE; $index++) {
  178. $year = (int)self::CURRENT_YEAR + $index;
  179. $expectedYearsList[$year] = $year;
  180. }
  181. return $expectedYearsList;
  182. }
  183. }