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