25 $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
26 $this->charset = str_split(md5((
string)
time()));
37 public function testGenerateCode(array
$data, $length)
39 $salesRuleCouponMock = $this->createPartialMock(\
Magento\SalesRule\Helper\Coupon::class, [
'getCharset']);
42 $massgenerator = $this->objectManager->getObject(
43 \
Magento\SalesRule\Model\Coupon\Massgenerator::class,
44 [
'salesRuleCoupon' => $salesRuleCouponMock,
'data' =>
$data]
47 $salesRuleCouponMock->expects($this->once())
48 ->method(
'getCharset')
49 ->with(
$data[
'format'])
50 ->will($this->returnValue($this->charset));
51 $code = $massgenerator->generateCode();
53 $this->assertTrue(strlen(
$code) === $length);
54 $this->assertNotEmpty(
$code);
55 if (isset(
$data[
'data'])) {
67 public function testGetDelimiter(array
$data)
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]
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());
82 $this->assertEquals(
$data[
'delimiter'], $massgenerator->getDelimiter());
89 public function testGeneratePool()
95 'format' =>
'test-format',
98 $salesRuleCouponMock = $this->createPartialMock(\
Magento\SalesRule\Helper\Coupon::class, [
'getCharset']);
99 $resourceMock = $this->createPartialMock(
101 [
'exists',
'__wakeup',
'getIdFieldName']
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, [
111 'setUsagePerCustomer',
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())
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,
139 'couponFactory' => $couponFactoryMock,
140 'dateTime' => $dateTimeMock,
142 'resource' => $resourceMock,
144 'salesRuleCoupon' => $salesRuleCouponMock
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);
159 public function testGeneratePoolException()
164 'format' =>
'test-format',
168 $salesRuleCouponMock = $this->createPartialMock(\
Magento\SalesRule\Helper\Coupon::class, [
'getCharset']);
169 $resourceMock = $this->createPartialMock(
171 [
'exists',
'__wakeup',
'getIdFieldName']
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']);
178 $couponFactoryMock->expects($this->once())
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())
187 ->will($this->returnValue(
true));
190 $massgenerator = $this->objectManager->getObject(
191 \
Magento\SalesRule\Model\Coupon\Massgenerator::class,
193 'couponFactory' => $couponFactoryMock,
194 'dateTime' => $dateTimeMock,
196 'resource' => $resourceMock,
198 'salesRuleCoupon' => $salesRuleCouponMock
202 $this->assertEquals($massgenerator, $massgenerator->generatePool());
216 $massgenerator = $this->objectManager->getObject(\
Magento\SalesRule\Model\Coupon\Massgenerator::class);
218 $this->assertEquals(
$result, $massgenerator->validateData(
$data));
224 public function testGetGeneratedCount()
227 $massgenerator = $this->objectManager->getObject(\
Magento\SalesRule\Model\Coupon\Massgenerator::class);
229 $this->assertEquals(0, $massgenerator->getGeneratedCount());
245 'format' =>
'test-format',
254 'format' =>
'test-format',
287 'delimiter' =>
'delimiter-value',
306 'format' =>
'test-format',
313 'format' =>
'test-format',