Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CouponTest.php
Go to the documentation of this file.
1 <?php
8 
9 class CouponTest extends \PHPUnit\Framework\TestCase
10 {
14  protected $helper;
15 
19  protected $scopeConfig;
20 
24  protected $context;
25 
29  protected $couponParameters;
30 
34  protected $separator = '|';
35 
36  protected function setUp()
37  {
38  $this->couponParameters = [
39  'separator' => $this->separator,
40  'charset' => [
41  'format' => 'abc',
42  ],
43  ];
44  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
45  $className = \Magento\SalesRule\Helper\Coupon::class;
46  $arguments = $objectManager->getConstructArguments(
47  $className,
48  ['couponParameters' => $this->couponParameters]
49  );
51  $context = $arguments['context'];
52  $this->scopeConfig = $context->getScopeConfig();
53  $this->helper = $objectManager->getObject(\Magento\SalesRule\Helper\Coupon::class, $arguments);
54  }
55 
56  public function testGetFormatsList()
57  {
59  $this->assertArrayHasKey(
60  $helper::COUPON_FORMAT_ALPHABETICAL,
61  $helper->getFormatsList(),
62  'The returned list should contain COUPON_FORMAT_ALPHABETICAL constant value as a key'
63  );
64  $this->assertArrayHasKey(
65  $helper::COUPON_FORMAT_ALPHANUMERIC,
66  $helper->getFormatsList(),
67  'The returned list should contain COUPON_FORMAT_ALPHANUMERIC constant value as a key'
68  );
69  $this->assertArrayHasKey(
70  $helper::COUPON_FORMAT_NUMERIC,
71  $helper->getFormatsList(),
72  'The returned list should contain COUPON_FORMAT_NUMERIC constant value as a key'
73  );
74  }
75 
76  public function testGetDefaultLength()
77  {
79  $defaultLength = 100;
80  $this->scopeConfig->expects($this->once())
81  ->method('getValue')
82  ->with($helper::XML_PATH_SALES_RULE_COUPON_LENGTH, \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
83  ->will($this->returnValue($defaultLength));
84 
85  $this->assertEquals($defaultLength, $helper->getDefaultLength());
86  }
87 
88  public function testGetDefaultFormat()
89  {
91  $defaultFormat = 'format';
92  $this->scopeConfig->expects($this->once())
93  ->method('getValue')
94  ->with($helper::XML_PATH_SALES_RULE_COUPON_FORMAT, \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
95  ->will($this->returnValue($defaultFormat));
96 
97  $this->assertEquals($defaultFormat, $helper->getDefaultFormat());
98  }
99 
100  public function testGetDefaultPrefix()
101  {
103  $defaultPrefix = 'prefix';
104  $this->scopeConfig->expects($this->once())
105  ->method('getValue')
106  ->with($helper::XML_PATH_SALES_RULE_COUPON_PREFIX, \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
107  ->will($this->returnValue($defaultPrefix));
108 
109  $this->assertEquals($defaultPrefix, $helper->getDefaultPrefix());
110  }
111 
112  public function testGetDefaultSuffix()
113  {
115  $defaultSuffix = 'suffix';
116  $this->scopeConfig->expects($this->once())
117  ->method('getValue')
118  ->with($helper::XML_PATH_SALES_RULE_COUPON_SUFFIX, \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
119  ->will($this->returnValue($defaultSuffix));
120 
121  $this->assertEquals($defaultSuffix, $helper->getDefaultSuffix());
122  }
123 
124  public function testGetDefaultDashInterval()
125  {
127  $defaultDashInterval = 4;
128  $this->scopeConfig->expects($this->once())
129  ->method('getValue')
130  ->with($helper::XML_PATH_SALES_RULE_COUPON_DASH_INTERVAL, \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
131  ->will($this->returnValue($defaultDashInterval));
132 
133  $this->assertEquals($defaultDashInterval, $helper->getDefaultDashInterval());
134  }
135 
136  public function testGetCharset()
137  {
138  $format = 'format';
139  $expected = ['a', 'b', 'c'];
140 
141  $this->assertEquals($expected, $this->helper->getCharset($format));
142  }
143 
144  public function testGetSeparator()
145  {
146  $this->assertEquals($this->separator, $this->helper->getCodeSeparator());
147  }
148 }
$objectManager
Definition: bootstrap.php:17
$format
Definition: list.phtml:12
$arguments
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31