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 spec\Sylius\Component\Product\Model; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Collections\Collection; |
17
|
|
|
use PhpSpec\ObjectBehavior; |
18
|
|
|
use Sylius\Component\Attribute\Model\AttributeValueInterface; |
19
|
|
|
use Sylius\Component\Product\Model\ProductAssociationInterface; |
20
|
|
|
use Sylius\Component\Product\Model\ProductAttributeValueInterface; |
21
|
|
|
use Sylius\Component\Product\Model\ProductInterface; |
22
|
|
|
use Sylius\Component\Product\Model\ProductOptionInterface; |
23
|
|
|
use Sylius\Component\Product\Model\ProductVariantInterface; |
24
|
|
|
use Sylius\Component\Resource\Model\ToggleableInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @author Paweł Jędrzejewski <[email protected]> |
28
|
|
|
* @author Gonzalo Vilaseca <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
final class ProductSpec extends ObjectBehavior |
31
|
|
|
{ |
32
|
|
|
function let() |
33
|
|
|
{ |
34
|
|
|
$this->setCurrentLocale('en_US'); |
35
|
|
|
$this->setFallbackLocale('en_US'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
function it_implements_product_interface(): void |
39
|
|
|
{ |
40
|
|
|
$this->shouldImplement(ProductInterface::class); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
function it_implements_toggleable_interface(): void |
44
|
|
|
{ |
45
|
|
|
$this->shouldImplement(ToggleableInterface::class); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
function it_has_no_id_by_default(): void |
49
|
|
|
{ |
50
|
|
|
$this->getId()->shouldReturn(null); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
function it_has_no_name_by_default(): void |
54
|
|
|
{ |
55
|
|
|
$this->getName()->shouldReturn(null); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
function its_name_is_mutable(): void |
59
|
|
|
{ |
60
|
|
|
$this->setName('Super product'); |
61
|
|
|
$this->getName()->shouldReturn('Super product'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
function it_has_no_slug_by_default(): void |
65
|
|
|
{ |
66
|
|
|
$this->getSlug()->shouldReturn(null); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
function its_slug_is_mutable(): void |
70
|
|
|
{ |
71
|
|
|
$this->setSlug('super-product'); |
72
|
|
|
$this->getSlug()->shouldReturn('super-product'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
function it_has_no_description_by_default(): void |
76
|
|
|
{ |
77
|
|
|
$this->getDescription()->shouldReturn(null); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
function its_description_is_mutable(): void |
81
|
|
|
{ |
82
|
|
|
$this->setDescription('This product is super cool because...'); |
83
|
|
|
$this->getDescription()->shouldReturn('This product is super cool because...'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
function it_initializes_attribute_collection_by_default(): void |
87
|
|
|
{ |
88
|
|
|
$this->getAttributes()->shouldHaveType(Collection::class); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
function it_adds_attribute(ProductAttributeValueInterface $attribute): void |
92
|
|
|
{ |
93
|
|
|
$attribute->setProduct($this)->shouldBeCalled(); |
94
|
|
|
|
95
|
|
|
$this->addAttribute($attribute); |
96
|
|
|
$this->hasAttribute($attribute)->shouldReturn(true); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
function it_removes_attribute(ProductAttributeValueInterface $attribute): void |
100
|
|
|
{ |
101
|
|
|
$attribute->setProduct($this)->shouldBeCalled(); |
102
|
|
|
|
103
|
|
|
$this->addAttribute($attribute); |
104
|
|
|
$this->hasAttribute($attribute)->shouldReturn(true); |
105
|
|
|
|
106
|
|
|
$attribute->setProduct(null)->shouldBeCalled(); |
107
|
|
|
|
108
|
|
|
$this->removeAttribute($attribute); |
109
|
|
|
$this->hasAttribute($attribute)->shouldReturn(false); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
function it_refuses_to_add_non_product_attribute(AttributeValueInterface $attribute): void |
113
|
|
|
{ |
114
|
|
|
$this->shouldThrow('\InvalidArgumentException')->duringAddAttribute($attribute); |
115
|
|
|
$this->hasAttribute($attribute)->shouldReturn(false); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
function it_refuses_to_remove_non_product_attribute(AttributeValueInterface $attribute): void |
119
|
|
|
{ |
120
|
|
|
$this->shouldThrow('\InvalidArgumentException')->duringRemoveAttribute($attribute); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
function it_has_no_variants_by_default(): void |
124
|
|
|
{ |
125
|
|
|
$this->hasVariants()->shouldReturn(false); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
function its_says_it_has_variants_only_if_multiple_variants_are_defined( |
129
|
|
|
ProductVariantInterface $firstVariant, |
130
|
|
|
ProductVariantInterface $secondVariant |
131
|
|
|
): void { |
132
|
|
|
$firstVariant->setProduct($this)->shouldBeCalled(); |
133
|
|
|
$secondVariant->setProduct($this)->shouldBeCalled(); |
134
|
|
|
|
135
|
|
|
$this->addVariant($firstVariant); |
136
|
|
|
$this->addVariant($secondVariant); |
137
|
|
|
$this->hasVariants()->shouldReturn(true); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
function it_initializes_variants_collection_by_default(): void |
141
|
|
|
{ |
142
|
|
|
$this->getVariants()->shouldHaveType(Collection::class); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
function it_does_not_include_unavailable_variants_in_available_variants(ProductVariantInterface $variant): void |
146
|
|
|
{ |
147
|
|
|
$variant->setProduct($this)->shouldBeCalled(); |
148
|
|
|
|
149
|
|
|
$this->addVariant($variant); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
function it_returns_available_variants( |
153
|
|
|
ProductVariantInterface $unavailableVariant, |
154
|
|
|
ProductVariantInterface $variant |
155
|
|
|
): void { |
156
|
|
|
$unavailableVariant->setProduct($this)->shouldBeCalled(); |
157
|
|
|
$variant->setProduct($this)->shouldBeCalled(); |
158
|
|
|
|
159
|
|
|
$this->addVariant($unavailableVariant); |
160
|
|
|
$this->addVariant($variant); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
function it_initializes_options_collection_by_default(): void |
164
|
|
|
{ |
165
|
|
|
$this->getOptions()->shouldHaveType(Collection::class); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
function it_has_no_options_by_default(): void |
169
|
|
|
{ |
170
|
|
|
$this->hasOptions()->shouldReturn(false); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
function its_says_it_has_options_only_if_any_option_defined(ProductOptionInterface $option): void |
174
|
|
|
{ |
175
|
|
|
$this->addOption($option); |
176
|
|
|
$this->hasOptions()->shouldReturn(true); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
function it_adds_option_properly(ProductOptionInterface $option): void |
180
|
|
|
{ |
181
|
|
|
$this->addOption($option); |
182
|
|
|
$this->hasOption($option)->shouldReturn(true); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
function it_removes_option_properly(ProductOptionInterface $option): void |
186
|
|
|
{ |
187
|
|
|
$this->addOption($option); |
188
|
|
|
$this->hasOption($option)->shouldReturn(true); |
189
|
|
|
|
190
|
|
|
$this->removeOption($option); |
191
|
|
|
$this->hasOption($option)->shouldReturn(false); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
function it_initializes_creation_date_by_default(): void |
195
|
|
|
{ |
196
|
|
|
$this->getCreatedAt()->shouldHaveType(\DateTimeInterface::class); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
function its_creation_date_is_mutable(\DateTime $creationDate): void |
200
|
|
|
{ |
201
|
|
|
$this->setCreatedAt($creationDate); |
202
|
|
|
$this->getCreatedAt()->shouldReturn($creationDate); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
function it_has_no_last_update_date_by_default(): void |
206
|
|
|
{ |
207
|
|
|
$this->getUpdatedAt()->shouldReturn(null); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
function its_last_update_date_is_mutable(\DateTime $updateDate): void |
211
|
|
|
{ |
212
|
|
|
$this->setUpdatedAt($updateDate); |
213
|
|
|
$this->getUpdatedAt()->shouldReturn($updateDate); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
function it_is_enabled_by_default(): void |
217
|
|
|
{ |
218
|
|
|
$this->shouldBeEnabled(); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
function it_is_toggleable(): void |
222
|
|
|
{ |
223
|
|
|
$this->disable(); |
224
|
|
|
$this->shouldNotBeEnabled(); |
225
|
|
|
|
226
|
|
|
$this->enable(); |
227
|
|
|
$this->shouldBeEnabled(); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
function it_adds_association(ProductAssociationInterface $association): void |
231
|
|
|
{ |
232
|
|
|
$association->setOwner($this)->shouldBeCalled(); |
233
|
|
|
$this->addAssociation($association); |
234
|
|
|
|
235
|
|
|
$this->hasAssociation($association)->shouldReturn(true); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
function it_allows_to_remove_association(ProductAssociationInterface $association): void |
239
|
|
|
{ |
240
|
|
|
$association->setOwner($this)->shouldBeCalled(); |
241
|
|
|
$association->setOwner(null)->shouldBeCalled(); |
242
|
|
|
|
243
|
|
|
$this->addAssociation($association); |
244
|
|
|
$this->removeAssociation($association); |
245
|
|
|
|
246
|
|
|
$this->hasAssociation($association)->shouldReturn(false); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
function it_is_simple_if_it_has_one_variant_and_no_options(ProductVariantInterface $variant): void |
250
|
|
|
{ |
251
|
|
|
$variant->setProduct($this)->shouldBeCalled(); |
252
|
|
|
$this->addVariant($variant); |
253
|
|
|
|
254
|
|
|
$this->isSimple()->shouldReturn(true); |
255
|
|
|
$this->isConfigurable()->shouldReturn(false); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
function it_is_configurable_if_it_has_at_least_two_variants( |
259
|
|
|
ProductVariantInterface $firstVariant, |
260
|
|
|
ProductVariantInterface $secondVariant |
261
|
|
|
): void { |
262
|
|
|
$firstVariant->setProduct($this)->shouldBeCalled(); |
263
|
|
|
$this->addVariant($firstVariant); |
264
|
|
|
$secondVariant->setProduct($this)->shouldBeCalled(); |
265
|
|
|
$this->addVariant($secondVariant); |
266
|
|
|
|
267
|
|
|
$this->isConfigurable()->shouldReturn(true); |
268
|
|
|
$this->isSimple()->shouldReturn(false); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
function it_is_configurable_if_it_has_one_variant_and_at_least_one_option( |
272
|
|
|
ProductOptionInterface $option, |
273
|
|
|
ProductVariantInterface $variant |
274
|
|
|
): void { |
275
|
|
|
$variant->setProduct($this)->shouldBeCalled(); |
276
|
|
|
$this->addVariant($variant); |
277
|
|
|
$this->addOption($option); |
278
|
|
|
|
279
|
|
|
$this->isConfigurable()->shouldReturn(true); |
280
|
|
|
$this->isSimple()->shouldReturn(false); |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|