|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sylius package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Paweł Jędrzejewski |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Sylius\Bundle\PromotionBundle\Tests\Command; |
|
15
|
|
|
|
|
16
|
|
|
use InvalidArgumentException; |
|
17
|
|
|
use Sylius\Bundle\PromotionBundle\Command\GenerateCouponsCommand; |
|
18
|
|
|
use Sylius\Component\Core\Model\PromotionInterface; |
|
19
|
|
|
use Sylius\Component\Core\Repository\PromotionRepositoryInterface; |
|
20
|
|
|
use Sylius\Component\Promotion\Generator\PromotionCouponGeneratorInstruction; |
|
21
|
|
|
use Sylius\Component\Promotion\Generator\PromotionCouponGeneratorInterface; |
|
22
|
|
|
use Symfony\Bundle\FrameworkBundle\Console\Application; |
|
23
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
|
24
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
|
25
|
|
|
|
|
26
|
|
|
class GenerateCouponsCommandTest extends KernelTestCase |
|
27
|
|
|
{ |
|
28
|
|
|
/** @var GenerateCouponsCommand */ |
|
29
|
|
|
private $command; |
|
30
|
|
|
|
|
31
|
|
|
/** @var CommandTester */ |
|
32
|
|
|
private $commandTester; |
|
33
|
|
|
|
|
34
|
|
|
/** @var PromotionRepositoryInterface */ |
|
35
|
|
|
private $promotionRepository; |
|
36
|
|
|
|
|
37
|
|
|
/** @var PromotionCouponGeneratorInterface */ |
|
38
|
|
|
private $couponGenerator; |
|
39
|
|
|
|
|
40
|
|
|
public function setup(): void |
|
41
|
|
|
{ |
|
42
|
|
|
$kernel = static::createKernel(); |
|
43
|
|
|
$kernel->boot(); |
|
44
|
|
|
$application = new Application($kernel); |
|
45
|
|
|
|
|
46
|
|
|
$this->promotionRepository = $this->createMock(PromotionRepositoryInterface::class); |
|
|
|
|
|
|
47
|
|
|
$kernel->getContainer()->set('sylius.repository.promotion', $this->promotionRepository); |
|
48
|
|
|
|
|
49
|
|
|
$this->couponGenerator = $this->createMock(PromotionCouponGeneratorInterface::class); |
|
|
|
|
|
|
50
|
|
|
$kernel->getContainer()->set('sylius.promotion_coupon_generator', $this->couponGenerator); |
|
51
|
|
|
|
|
52
|
|
|
$this->command = $application->find('sylius:promotion:generate-coupons'); |
|
53
|
|
|
$this->commandTester = new CommandTester($this->command); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @test |
|
58
|
|
|
*/ |
|
59
|
|
|
public function it_returns_an_error_if_there_is_no_promotion_for_code(): void |
|
60
|
|
|
{ |
|
61
|
|
|
$this->promotionRepository |
|
|
|
|
|
|
62
|
|
|
->method('findOneBy') |
|
63
|
|
|
->with($this->equalTo(['code' => 'UNKNOWN_PROMOTION'])) |
|
64
|
|
|
->willReturn(null) |
|
65
|
|
|
; |
|
66
|
|
|
|
|
67
|
|
|
$this->commandTester->execute([ |
|
68
|
|
|
'command' => $this->command->getName(), |
|
69
|
|
|
'promotion-code' => 'UNKNOWN_PROMOTION', |
|
70
|
|
|
'count' => 10, |
|
71
|
|
|
]); |
|
72
|
|
|
|
|
73
|
|
|
// the output of the command in the console |
|
74
|
|
|
$output = $this->commandTester->getDisplay(); |
|
75
|
|
|
$this->assertNotEquals($this->commandTester->getStatusCode(), 0); |
|
76
|
|
|
$this->assertContains('No promotion found with this code', $output); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @test |
|
81
|
|
|
*/ |
|
82
|
|
|
public function it_returns_an_error_if_the_promotion_does_not_allow_coupons(): void |
|
83
|
|
|
{ |
|
84
|
|
|
$promotion = $this->createMock(PromotionInterface::class); |
|
85
|
|
|
$promotion->method('isCouponBased')->willReturn(false); |
|
86
|
|
|
|
|
87
|
|
|
$this->promotionRepository->method('findOneBy') |
|
|
|
|
|
|
88
|
|
|
->with($this->equalTo(['code' => 'INVALID_PROMOTION'])) |
|
89
|
|
|
->willReturn($promotion) |
|
90
|
|
|
; |
|
91
|
|
|
|
|
92
|
|
|
$this->commandTester->execute([ |
|
93
|
|
|
'command' => $this->command->getName(), |
|
94
|
|
|
'promotion-code' => 'INVALID_PROMOTION', |
|
95
|
|
|
'count' => 10, |
|
96
|
|
|
]); |
|
97
|
|
|
|
|
98
|
|
|
// the output of the command in the console |
|
99
|
|
|
$output = $this->commandTester->getDisplay(); |
|
100
|
|
|
$this->assertNotEquals($this->commandTester->getStatusCode(), 0); |
|
101
|
|
|
$this->assertContains('This promotion is not coupon based', $output); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @test |
|
106
|
|
|
*/ |
|
107
|
|
|
public function it_handles_generator_exceptions_gracefully(): void |
|
108
|
|
|
{ |
|
109
|
|
|
$promotion = $this->createMock(PromotionInterface::class); |
|
110
|
|
|
$promotion->method('isCouponBased')->willReturn(true); |
|
111
|
|
|
|
|
112
|
|
|
$this->promotionRepository |
|
|
|
|
|
|
113
|
|
|
->method('findOneBy') |
|
114
|
|
|
->with($this->equalTo(['code' => 'VALID_PROMOTION'])) |
|
115
|
|
|
->willReturn($promotion); |
|
116
|
|
|
|
|
117
|
|
|
$expectedInstructions = new PromotionCouponGeneratorInstruction(); |
|
118
|
|
|
$expectedInstructions->setAmount(10); |
|
119
|
|
|
$expectedInstructions->setCodeLength(10); |
|
120
|
|
|
|
|
121
|
|
|
$this->couponGenerator |
|
|
|
|
|
|
122
|
|
|
->method('generate') |
|
123
|
|
|
->with( |
|
124
|
|
|
$this->equalTo($promotion), |
|
125
|
|
|
$this->equalTo($expectedInstructions) |
|
126
|
|
|
) |
|
127
|
|
|
->willThrowException(new InvalidArgumentException('Could not generate')) |
|
128
|
|
|
; |
|
129
|
|
|
|
|
130
|
|
|
$this->commandTester->execute([ |
|
131
|
|
|
'command' => $this->command->getName(), |
|
132
|
|
|
'promotion-code' => 'VALID_PROMOTION', |
|
133
|
|
|
'count' => 10, |
|
134
|
|
|
]); |
|
135
|
|
|
|
|
136
|
|
|
// the output of the command in the console |
|
137
|
|
|
$output = $this->commandTester->getDisplay(); |
|
138
|
|
|
$this->assertEquals($this->commandTester->getStatusCode(), 1); |
|
139
|
|
|
$this->assertContains('Could not generate', $output); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @test |
|
144
|
|
|
*/ |
|
145
|
|
|
public function it_generates_coupons_with_default_length(): void |
|
146
|
|
|
{ |
|
147
|
|
|
$promotion = $this->createMock(PromotionInterface::class); |
|
148
|
|
|
$promotion->method('isCouponBased')->willReturn(true); |
|
149
|
|
|
|
|
150
|
|
|
$this->promotionRepository |
|
|
|
|
|
|
151
|
|
|
->method('findOneBy') |
|
152
|
|
|
->with($this->equalTo(['code' => 'VALID_PROMOTION'])) |
|
153
|
|
|
->willReturn($promotion); |
|
154
|
|
|
|
|
155
|
|
|
$expectedInstructions = new PromotionCouponGeneratorInstruction(); |
|
156
|
|
|
$expectedInstructions->setAmount(5); |
|
157
|
|
|
$expectedInstructions->setCodeLength(10); |
|
158
|
|
|
|
|
159
|
|
|
$this->couponGenerator |
|
|
|
|
|
|
160
|
|
|
->expects($this->once()) |
|
161
|
|
|
->method('generate') |
|
162
|
|
|
->with( |
|
163
|
|
|
$this->equalTo($promotion), |
|
164
|
|
|
$this->equalTo($expectedInstructions) |
|
165
|
|
|
) |
|
166
|
|
|
; |
|
167
|
|
|
|
|
168
|
|
|
$this->commandTester->execute([ |
|
169
|
|
|
'command' => $this->command->getName(), |
|
170
|
|
|
'promotion-code' => 'VALID_PROMOTION', |
|
171
|
|
|
'count' => 5, |
|
172
|
|
|
]); |
|
173
|
|
|
|
|
174
|
|
|
// the output of the command in the console |
|
175
|
|
|
$output = $this->commandTester->getDisplay(); |
|
176
|
|
|
$this->assertEquals($this->commandTester->getStatusCode(), 0); |
|
177
|
|
|
$this->assertContains('Coupons have been generated', $output); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @test |
|
182
|
|
|
*/ |
|
183
|
|
|
public function it_generates_coupons_with_customized_length(): void |
|
184
|
|
|
{ |
|
185
|
|
|
$promotion = $this->createMock(PromotionInterface::class); |
|
186
|
|
|
$promotion->method('isCouponBased')->willReturn(true); |
|
187
|
|
|
|
|
188
|
|
|
$this->promotionRepository |
|
|
|
|
|
|
189
|
|
|
->method('findOneBy') |
|
190
|
|
|
->with($this->equalTo(['code' => 'VALID_PROMOTION'])) |
|
191
|
|
|
->willReturn($promotion); |
|
192
|
|
|
|
|
193
|
|
|
$expectedInstructions = new PromotionCouponGeneratorInstruction(); |
|
194
|
|
|
$expectedInstructions->setAmount(10); |
|
195
|
|
|
$expectedInstructions->setCodeLength(7); |
|
196
|
|
|
|
|
197
|
|
|
$this->couponGenerator |
|
|
|
|
|
|
198
|
|
|
->expects($this->once()) |
|
199
|
|
|
->method('generate') |
|
200
|
|
|
->with( |
|
201
|
|
|
$this->equalTo($promotion), |
|
202
|
|
|
$this->equalTo($expectedInstructions) |
|
203
|
|
|
) |
|
204
|
|
|
; |
|
205
|
|
|
|
|
206
|
|
|
$this->commandTester->execute( |
|
207
|
|
|
[ |
|
208
|
|
|
'command' => $this->command->getName(), |
|
209
|
|
|
'promotion-code' => 'VALID_PROMOTION', |
|
210
|
|
|
'count' => 10, |
|
211
|
|
|
'--length' => 7, |
|
212
|
|
|
] |
|
213
|
|
|
); |
|
214
|
|
|
|
|
215
|
|
|
// the output of the command in the console |
|
216
|
|
|
$output = $this->commandTester->getDisplay(); |
|
217
|
|
|
$this->assertEquals($this->commandTester->getStatusCode(), 0); |
|
218
|
|
|
$this->assertContains('Coupons have been generated', $output); |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..