|
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
|
|
|
namespace spec\Sylius\Behat\Context\Domain; |
|
13
|
|
|
|
|
14
|
|
|
use Behat\Behat\Context\Context; |
|
15
|
|
|
use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException; |
|
16
|
|
|
use PhpSpec\Exception\Example\FailureException; |
|
17
|
|
|
use PhpSpec\Exception\Example\NotEqualException; |
|
18
|
|
|
use PhpSpec\ObjectBehavior; |
|
19
|
|
|
use Prophecy\Argument; |
|
20
|
|
|
use Sylius\Behat\Context\Domain\PromotionContext; |
|
21
|
|
|
use Sylius\Component\Core\Repository\PromotionRepositoryInterface; |
|
22
|
|
|
use Sylius\Component\Core\Test\Services\SharedStorageInterface; |
|
23
|
|
|
use Sylius\Component\Promotion\Model\CouponInterface; |
|
24
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
|
25
|
|
|
use Sylius\Component\Promotion\Model\PromotionInterface; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @mixin PromotionContext |
|
29
|
|
|
* |
|
30
|
|
|
* @author Jan Góralski <[email protected]> |
|
31
|
|
|
*/ |
|
32
|
|
|
class PromotionContextSpec extends ObjectBehavior |
|
33
|
|
|
{ |
|
34
|
|
|
function let( |
|
35
|
|
|
SharedStorageInterface $sharedStorage, |
|
36
|
|
|
PromotionRepositoryInterface $promotionRepository, |
|
37
|
|
|
RepositoryInterface $couponRepository |
|
38
|
|
|
) { |
|
39
|
|
|
$this->beConstructedWith($sharedStorage, $promotionRepository, $couponRepository); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
function it_is_initializable() |
|
43
|
|
|
{ |
|
44
|
|
|
$this->shouldHaveType('Sylius\Behat\Context\Domain\PromotionContext'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
function it_implements_context_interface() |
|
48
|
|
|
{ |
|
49
|
|
|
$this->shouldImplement(Context::class); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
function it_removes_a_coupon( |
|
53
|
|
|
SharedStorageInterface $sharedStorage, |
|
54
|
|
|
RepositoryInterface $couponRepository, |
|
55
|
|
|
CouponInterface $coupon |
|
56
|
|
|
) { |
|
57
|
|
|
$coupon->getId()->willReturn(5); |
|
58
|
|
|
$sharedStorage->set('coupon_id', 5)->shouldBeCalled(); |
|
59
|
|
|
$couponRepository->remove($coupon)->shouldBeCalled(); |
|
60
|
|
|
|
|
61
|
|
|
$this->iDeleteCoupon($coupon); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
function it_throws_exception_when_there_was_a_problem_with_removing_a_coupon( |
|
65
|
|
|
SharedStorageInterface $sharedStorage, |
|
66
|
|
|
RepositoryInterface $couponRepository, |
|
67
|
|
|
CouponInterface $coupon |
|
68
|
|
|
) { |
|
69
|
|
|
$coupon->getId()->willReturn(5); |
|
70
|
|
|
$sharedStorage->set('coupon_id', 5)->shouldBeCalled(); |
|
71
|
|
|
$couponRepository->remove($coupon)->willThrow(ForeignKeyConstraintViolationException::class); |
|
72
|
|
|
|
|
73
|
|
|
$this->shouldThrow(ForeignKeyConstraintViolationException::class)->during('iDeleteCoupon', [$coupon]); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
function it_tries_to_remove_a_coupon_and_catches_exception_in_case_of_failure( |
|
77
|
|
|
SharedStorageInterface $sharedStorage, |
|
78
|
|
|
RepositoryInterface $couponRepository, |
|
79
|
|
|
CouponInterface $coupon |
|
80
|
|
|
) { |
|
81
|
|
|
$coupon->getId()->willReturn(5); |
|
82
|
|
|
$couponRepository->remove($coupon)->willThrow(ForeignKeyConstraintViolationException::class); |
|
83
|
|
|
$sharedStorage->set('last_exception', Argument::type(ForeignKeyConstraintViolationException::class))->shouldBeCalled(); |
|
84
|
|
|
|
|
85
|
|
|
$this->iTryToDeleteCoupon($coupon); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
function it_throws_exception_while_trying_to_remove_a_coupon_and_actually_removing_it( |
|
89
|
|
|
SharedStorageInterface $sharedStorage, |
|
90
|
|
|
RepositoryInterface $couponRepository, |
|
91
|
|
|
CouponInterface $coupon |
|
92
|
|
|
) { |
|
93
|
|
|
$coupon->getId()->willReturn(5); |
|
94
|
|
|
$couponRepository->remove($coupon)->shouldBeCalled(); |
|
95
|
|
|
$sharedStorage->set('last_exception', Argument::any())->shouldNotBeCalled(); |
|
96
|
|
|
|
|
97
|
|
|
$this->shouldThrow(\Exception::class)->during('iTryToDeleteCoupon', [$coupon]); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
function it_checks_whether_a_coupon_is_not_in_the_registry( |
|
101
|
|
|
RepositoryInterface $couponRepository |
|
102
|
|
|
) { |
|
103
|
|
|
$couponRepository->find(5)->willReturn(null); |
|
104
|
|
|
|
|
105
|
|
|
$this->couponShouldNotExistInTheRegistry(5); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
function it_throws_exception_when_a_coupon_is_found_but_it_should_not_exist( |
|
109
|
|
|
SharedStorageInterface $sharedStorage, |
|
110
|
|
|
RepositoryInterface $couponRepository, |
|
111
|
|
|
CouponInterface $coupon |
|
112
|
|
|
) { |
|
113
|
|
|
$coupon->getId()->willReturn(5); |
|
114
|
|
|
$sharedStorage->get('coupon_id')->willReturn(5); |
|
115
|
|
|
$couponRepository->find(5)->willReturn($coupon); |
|
116
|
|
|
|
|
117
|
|
|
$this |
|
118
|
|
|
->shouldThrow(NotEqualException::class) |
|
119
|
|
|
->during('couponShouldNotExistInTheRegistry', [5]) |
|
120
|
|
|
; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
function it_checks_whether_a_coupon_is_in_the_registry( |
|
124
|
|
|
RepositoryInterface $couponRepository, |
|
125
|
|
|
CouponInterface $coupon |
|
126
|
|
|
) { |
|
127
|
|
|
$coupon->getId()->willReturn(5); |
|
128
|
|
|
$couponRepository->find(5)->willReturn($coupon); |
|
129
|
|
|
|
|
130
|
|
|
$this->couponShouldStillExistInTheRegistry($coupon); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
function it_throws_exception_when_a_coupon_is_not_found_but_it_should_exist( |
|
134
|
|
|
RepositoryInterface $couponRepository, |
|
135
|
|
|
CouponInterface $coupon |
|
136
|
|
|
) { |
|
137
|
|
|
$coupon->getId()->willReturn(5); |
|
138
|
|
|
$couponRepository->find(5)->willReturn(null); |
|
139
|
|
|
|
|
140
|
|
|
$this |
|
141
|
|
|
->shouldThrow(FailureException::class) |
|
142
|
|
|
->during('couponShouldStillExistInTheRegistry', [$coupon]) |
|
143
|
|
|
; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
function it_removes_a_promotion( |
|
147
|
|
|
PromotionRepositoryInterface $promotionRepository, |
|
148
|
|
|
SharedStorageInterface $sharedStorage, |
|
149
|
|
|
PromotionInterface $promotion |
|
150
|
|
|
) { |
|
151
|
|
|
$promotion->getId()->willReturn(5); |
|
152
|
|
|
$sharedStorage->set('promotion_id', 5)->shouldBeCalled(); |
|
153
|
|
|
$promotionRepository->remove($promotion)->shouldBeCalled(); |
|
154
|
|
|
|
|
155
|
|
|
$this->iDeletePromotion($promotion); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
function it_checks_whether_a_promotion_does_not_exist_in_the_registry( |
|
159
|
|
|
PromotionRepositoryInterface $promotionRepository |
|
160
|
|
|
) { |
|
161
|
|
|
$promotionRepository->find(5)->willReturn(null); |
|
162
|
|
|
|
|
163
|
|
|
$this->promotionShouldNotExistInTheRegistry(5); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
function it_throws_exception_when_a_promotion_is_found_but_it_should_not_exist( |
|
167
|
|
|
PromotionRepositoryInterface $promotionRepository, |
|
168
|
|
|
PromotionInterface $promotion |
|
169
|
|
|
) { |
|
170
|
|
|
$promotion->getId()->willReturn(5); |
|
171
|
|
|
$promotionRepository->find(5)->willReturn($promotion); |
|
172
|
|
|
|
|
173
|
|
|
$this |
|
174
|
|
|
->shouldThrow(NotEqualException::class) |
|
175
|
|
|
->during('promotionShouldNotExistInTheRegistry', [5]) |
|
176
|
|
|
; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
function it_checks_whether_a_promotion_exists_in_the_registry( |
|
180
|
|
|
PromotionRepositoryInterface $promotionRepository, |
|
181
|
|
|
PromotionInterface $promotion |
|
182
|
|
|
) { |
|
183
|
|
|
$promotion->getId()->willReturn(5); |
|
184
|
|
|
$promotionRepository->find(5)->willReturn($promotion); |
|
185
|
|
|
|
|
186
|
|
|
$this->promotionShouldStillExistInTheRegistry($promotion); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
function it_throws_exception_when_a_promotion_is_not_found_but_it_should_exist( |
|
190
|
|
|
PromotionRepositoryInterface $promotionRepository, |
|
191
|
|
|
PromotionInterface $promotion |
|
192
|
|
|
) { |
|
193
|
|
|
$promotion->getId()->willReturn(5); |
|
194
|
|
|
$promotionRepository->find(5)->willReturn(null); |
|
195
|
|
|
|
|
196
|
|
|
$this |
|
197
|
|
|
->shouldThrow(FailureException::class) |
|
198
|
|
|
->during('promotionShouldStillExistInTheRegistry', [$promotion]) |
|
199
|
|
|
; |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|