CouponTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SalesRule\Test\Unit\Helper;
  7. class CouponTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\SalesRule\Helper\Coupon
  11. */
  12. protected $helper;
  13. /**
  14. * @var \Magento\Framework\App\Config
  15. */
  16. protected $scopeConfig;
  17. /**
  18. * @var \Magento\Framework\App\Helper\Context
  19. */
  20. protected $context;
  21. /**
  22. * @var array
  23. */
  24. protected $couponParameters;
  25. /**
  26. * @var string
  27. */
  28. protected $separator = '|';
  29. protected function setUp()
  30. {
  31. $this->couponParameters = [
  32. 'separator' => $this->separator,
  33. 'charset' => [
  34. 'format' => 'abc',
  35. ],
  36. ];
  37. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  38. $className = \Magento\SalesRule\Helper\Coupon::class;
  39. $arguments = $objectManager->getConstructArguments(
  40. $className,
  41. ['couponParameters' => $this->couponParameters]
  42. );
  43. /** @var \Magento\Framework\App\Helper\Context $context */
  44. $context = $arguments['context'];
  45. $this->scopeConfig = $context->getScopeConfig();
  46. $this->helper = $objectManager->getObject(\Magento\SalesRule\Helper\Coupon::class, $arguments);
  47. }
  48. public function testGetFormatsList()
  49. {
  50. $helper = $this->helper;
  51. $this->assertArrayHasKey(
  52. $helper::COUPON_FORMAT_ALPHABETICAL,
  53. $helper->getFormatsList(),
  54. 'The returned list should contain COUPON_FORMAT_ALPHABETICAL constant value as a key'
  55. );
  56. $this->assertArrayHasKey(
  57. $helper::COUPON_FORMAT_ALPHANUMERIC,
  58. $helper->getFormatsList(),
  59. 'The returned list should contain COUPON_FORMAT_ALPHANUMERIC constant value as a key'
  60. );
  61. $this->assertArrayHasKey(
  62. $helper::COUPON_FORMAT_NUMERIC,
  63. $helper->getFormatsList(),
  64. 'The returned list should contain COUPON_FORMAT_NUMERIC constant value as a key'
  65. );
  66. }
  67. public function testGetDefaultLength()
  68. {
  69. $helper = $this->helper;
  70. $defaultLength = 100;
  71. $this->scopeConfig->expects($this->once())
  72. ->method('getValue')
  73. ->with($helper::XML_PATH_SALES_RULE_COUPON_LENGTH, \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
  74. ->will($this->returnValue($defaultLength));
  75. $this->assertEquals($defaultLength, $helper->getDefaultLength());
  76. }
  77. public function testGetDefaultFormat()
  78. {
  79. $helper = $this->helper;
  80. $defaultFormat = 'format';
  81. $this->scopeConfig->expects($this->once())
  82. ->method('getValue')
  83. ->with($helper::XML_PATH_SALES_RULE_COUPON_FORMAT, \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
  84. ->will($this->returnValue($defaultFormat));
  85. $this->assertEquals($defaultFormat, $helper->getDefaultFormat());
  86. }
  87. public function testGetDefaultPrefix()
  88. {
  89. $helper = $this->helper;
  90. $defaultPrefix = 'prefix';
  91. $this->scopeConfig->expects($this->once())
  92. ->method('getValue')
  93. ->with($helper::XML_PATH_SALES_RULE_COUPON_PREFIX, \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
  94. ->will($this->returnValue($defaultPrefix));
  95. $this->assertEquals($defaultPrefix, $helper->getDefaultPrefix());
  96. }
  97. public function testGetDefaultSuffix()
  98. {
  99. $helper = $this->helper;
  100. $defaultSuffix = 'suffix';
  101. $this->scopeConfig->expects($this->once())
  102. ->method('getValue')
  103. ->with($helper::XML_PATH_SALES_RULE_COUPON_SUFFIX, \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
  104. ->will($this->returnValue($defaultSuffix));
  105. $this->assertEquals($defaultSuffix, $helper->getDefaultSuffix());
  106. }
  107. public function testGetDefaultDashInterval()
  108. {
  109. $helper = $this->helper;
  110. $defaultDashInterval = 4;
  111. $this->scopeConfig->expects($this->once())
  112. ->method('getValue')
  113. ->with($helper::XML_PATH_SALES_RULE_COUPON_DASH_INTERVAL, \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
  114. ->will($this->returnValue($defaultDashInterval));
  115. $this->assertEquals($defaultDashInterval, $helper->getDefaultDashInterval());
  116. }
  117. public function testGetCharset()
  118. {
  119. $format = 'format';
  120. $expected = ['a', 'b', 'c'];
  121. $this->assertEquals($expected, $this->helper->getCharset($format));
  122. }
  123. public function testGetSeparator()
  124. {
  125. $this->assertEquals($this->separator, $this->helper->getCodeSeparator());
  126. }
  127. }