Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MassgeneratorTest.php
Go to the documentation of this file.
1 <?php
7 
11 class MassgeneratorTest extends \PHPUnit\Framework\TestCase
12 {
16  protected $objectManager;
17 
21  protected $charset;
22 
23  protected function setUp()
24  {
25  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
26  $this->charset = str_split(md5((string)time()));
27  }
28 
37  public function testGenerateCode(array $data, $length)
38  {
39  $salesRuleCouponMock = $this->createPartialMock(\Magento\SalesRule\Helper\Coupon::class, ['getCharset']);
40 
42  $massgenerator = $this->objectManager->getObject(
43  \Magento\SalesRule\Model\Coupon\Massgenerator::class,
44  ['salesRuleCoupon' => $salesRuleCouponMock, 'data' => $data]
45  );
46 
47  $salesRuleCouponMock->expects($this->once())
48  ->method('getCharset')
49  ->with($data['format'])
50  ->will($this->returnValue($this->charset));
51  $code = $massgenerator->generateCode();
52 
53  $this->assertTrue(strlen($code) === $length);
54  $this->assertNotEmpty($code);
55  if (isset($data['data'])) {
56  $this->assertCount($data['length'] / $data['dash'], explode($data['delimiter'], $code));
57  }
58  }
59 
67  public function testGetDelimiter(array $data)
68  {
69  $salesRuleCouponMock = $this->createPartialMock(\Magento\SalesRule\Helper\Coupon::class, ['getCodeSeparator']);
71  $massgenerator = $this->objectManager->getObject(
72  \Magento\SalesRule\Model\Coupon\Massgenerator::class,
73  ['salesRuleCoupon' => $salesRuleCouponMock, 'data' => $data]
74  );
75 
76  if (empty($data['delimiter'])) {
77  $salesRuleCouponMock->expects($this->once())
78  ->method('getCodeSeparator')
79  ->will($this->returnValue('test-separator'));
80  $this->assertEquals('test-separator', $massgenerator->getDelimiter());
81  } else {
82  $this->assertEquals($data['delimiter'], $massgenerator->getDelimiter());
83  }
84  }
85 
89  public function testGeneratePool()
90  {
91  $qty = 10;
92  $data = [
93  'qty' => $qty,
94  'length' => 15,
95  'format' => 'test-format',
96  ];
97 
98  $salesRuleCouponMock = $this->createPartialMock(\Magento\SalesRule\Helper\Coupon::class, ['getCharset']);
99  $resourceMock = $this->createPartialMock(
100  \Magento\SalesRule\Model\ResourceModel\Coupon::class,
101  ['exists', '__wakeup', 'getIdFieldName']
102  );
103  $dateMock = $this->createPartialMock(\Magento\Framework\Stdlib\DateTime\DateTime::class, ['gmtTimestamp']);
104  $dateTimeMock = $this->createPartialMock(\Magento\Framework\Stdlib\DateTime::class, ['formatDate']);
105  $couponFactoryMock = $this->createPartialMock(\Magento\SalesRule\Model\CouponFactory::class, ['create']);
106  $couponMock = $this->createPartialMock(\Magento\SalesRule\Model\Coupon::class, [
107  '__wakeup',
108  'setId',
109  'setRuleId',
110  'setUsageLimit',
111  'setUsagePerCustomer',
112  'setExpirationDate',
113  'setCreatedAt',
114  'setType',
115  'setCode',
116  'save'
117  ]);
118 
119  $couponMock->expects($this->any())->method('setId')->will($this->returnSelf());
120  $couponMock->expects($this->any())->method('setRuleId')->will($this->returnSelf());
121  $couponMock->expects($this->any())->method('setUsageLimit')->will($this->returnSelf());
122  $couponMock->expects($this->any())->method('setUsagePerCustomer')->will($this->returnSelf());
123  $couponMock->expects($this->any())->method('setExpirationDate')->will($this->returnSelf());
124  $couponMock->expects($this->any())->method('setCreatedAt')->will($this->returnSelf());
125  $couponMock->expects($this->any())->method('setType')->will($this->returnSelf());
126  $couponMock->expects($this->any())->method('setCode')->will($this->returnSelf());
127  $couponMock->expects($this->any())->method('save')->will($this->returnSelf());
128  $couponFactoryMock->expects($this->once())
129  ->method('create')
130  ->will($this->returnValue($couponMock));
131  $salesRuleCouponMock->expects($this->any())
132  ->method('getCharset')
133  ->with($data['format'])
134  ->will($this->returnValue($this->charset));
136  $massgenerator = $this->objectManager->getObject(
137  \Magento\SalesRule\Model\Coupon\Massgenerator::class,
138  [
139  'couponFactory' => $couponFactoryMock,
140  'dateTime' => $dateTimeMock,
141  'date' => $dateMock,
142  'resource' => $resourceMock,
143  'data' => $data,
144  'salesRuleCoupon' => $salesRuleCouponMock
145  ]
146  );
147 
148  $this->assertEquals($massgenerator, $massgenerator->generatePool());
149  $this->assertEquals($qty, $massgenerator->getGeneratedCount());
150  $codes = $massgenerator->getGeneratedCodes();
151  ($qty > 0) ? $this->assertNotEmpty($codes) : $this->assertEmpty($codes);
152  }
153 
159  public function testGeneratePoolException()
160  {
161  $data = [
162  'qty' => 3,
163  'length' => 15,
164  'format' => 'test-format',
165  'max_attempts' => 0,
166  ];
167 
168  $salesRuleCouponMock = $this->createPartialMock(\Magento\SalesRule\Helper\Coupon::class, ['getCharset']);
169  $resourceMock = $this->createPartialMock(
170  \Magento\SalesRule\Model\ResourceModel\Coupon::class,
171  ['exists', '__wakeup', 'getIdFieldName']
172  );
173  $dateMock = $this->createPartialMock(\Magento\Framework\Stdlib\DateTime\DateTime::class, ['gmtTimestamp']);
174  $dateTimeMock = $this->createPartialMock(\Magento\Framework\Stdlib\DateTime::class, ['formatDate']);
175  $couponFactoryMock = $this->createPartialMock(\Magento\SalesRule\Model\CouponFactory::class, ['create']);
176  $couponMock = $this->createPartialMock(\Magento\SalesRule\Model\Coupon::class, ['__wakeup']);
177 
178  $couponFactoryMock->expects($this->once())
179  ->method('create')
180  ->will($this->returnValue($couponMock));
181  $salesRuleCouponMock->expects($this->any())
182  ->method('getCharset')
183  ->with($data['format'])
184  ->will($this->returnValue($this->charset));
185  $resourceMock->expects($this->any())
186  ->method('exists')
187  ->will($this->returnValue(true));
188 
190  $massgenerator = $this->objectManager->getObject(
191  \Magento\SalesRule\Model\Coupon\Massgenerator::class,
192  [
193  'couponFactory' => $couponFactoryMock,
194  'dateTime' => $dateTimeMock,
195  'date' => $dateMock,
196  'resource' => $resourceMock,
197  'data' => $data,
198  'salesRuleCoupon' => $salesRuleCouponMock
199  ]
200  );
201 
202  $this->assertEquals($massgenerator, $massgenerator->generatePool());
203  }
204 
213  public function testValidateData(array $data, $result)
214  {
216  $massgenerator = $this->objectManager->getObject(\Magento\SalesRule\Model\Coupon\Massgenerator::class);
217 
218  $this->assertEquals($result, $massgenerator->validateData($data));
219  }
220 
224  public function testGetGeneratedCount()
225  {
227  $massgenerator = $this->objectManager->getObject(\Magento\SalesRule\Model\Coupon\Massgenerator::class);
228 
229  $this->assertEquals(0, $massgenerator->getGeneratedCount());
230  }
231 
237  public function validateDataProvider()
238  {
239  return [
240  [
241  'data' => [
242  'qty' => 20,
243  'rule_id' => 1,
244  'length' => 15,
245  'format' => 'test-format',
246  ],
247  'result' => true,
248  ],
249  [
250  'data' => [
251  'qty' => 0,
252  'rule_id' => 1,
253  'length' => 15,
254  'format' => 'test-format',
255  ],
256  'result' => false
257  ],
258  [
259  'data' => [
260  'qty' => 0,
261  'rule_id' => 1,
262  'length' => 15,
263  'format' => '',
264  ],
265  'result' => false
266  ],
267  [
268  'data' => [
269  'qty' => 2,
270  'length' => 15,
271  ],
272  'result' => false
273  ]
274  ];
275  }
276 
282  public function delimiterDataProvider()
283  {
284  return [
285  [
286  'data' => [
287  'delimiter' => 'delimiter-value',
288  ],
289  ],
290  [
291  'data' => []
292  ]
293  ];
294  }
295 
301  public function generatorDataProvider()
302  {
303  return [
304  [
305  'data' => [
306  'format' => 'test-format',
307  'length' => 10,
308  ],
309  'length' => 10,
310  ],
311  [
312  'data' => [
313  'format' => 'test-format',
314  'length' => 18,
315  'dash' => 6,
316  'delimiter' => '-',
317  ],
318  'length' => 20
319  ]
320  ];
321  }
322 }
return false
Definition: gallery.phtml:36
$code
Definition: info.phtml:12