Completed
Push — master ( 0729b3...dbaae6 )
by Kamil
18:26
created

ProductSpec::it_adds_association()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
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\Association\Model\AssociableInterface;
17
use Sylius\Component\Resource\Model\ToggleableInterface;
18
use Sylius\Component\Product\Model\ProductAssociationInterface;
19
use Sylius\Component\Product\Model\ArchetypeInterface;
20
use Sylius\Component\Product\Model\AttributeValueInterface;
21
use Sylius\Component\Product\Model\OptionInterface;
22
use Sylius\Component\Product\Model\ProductInterface;
23
use Sylius\Component\Product\Model\VariantInterface;
24
25
/**
26
 * @author Paweł Jędrzejewski <[email protected]>
27
 * @author Gonzalo Vilaseca <[email protected]>
28
 */
29
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('Sylius\Component\Product\Model\Product');
40
    }
41
42
    function it_implements_Sylius_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_is_associatable()
53
    {
54
        $this->shouldImplement(AssociableInterface::class);
55
    }
56
57
    function it_has_no_id_by_default()
58
    {
59
        $this->getId()->shouldReturn(null);
60
    }
61
62
    function it_does_not_belong_to_any_archetype_by_default()
63
    {
64
        $this->getArchetype()->shouldReturn(null);
65
    }
66
67
    function it_can_belong_to_a_product_archetype(ArchetypeInterface $archetype)
68
    {
69
        $this->setArchetype($archetype);
70
        $this->getArchetype()->shouldReturn($archetype);
71
    }
72
73
    function it_has_no_name_by_default()
74
    {
75
        $this->getName()->shouldReturn(null);
76
    }
77
78
    function its_name_is_mutable()
79
    {
80
        $this->setName('Super product');
81
        $this->getName()->shouldReturn('Super product');
82
    }
83
84
    function it_has_no_slug_by_default()
85
    {
86
        $this->getSlug()->shouldReturn(null);
87
    }
88
89
    function its_slug_is_mutable()
90
    {
91
        $this->setSlug('super-product');
92
        $this->getSlug()->shouldReturn('super-product');
93
    }
94
95
    function it_has_no_description_by_default()
96
    {
97
        $this->getDescription()->shouldReturn(null);
98
    }
99
100
    function its_description_is_mutable()
101
    {
102
        $this->setDescription('This product is super cool because...');
103
        $this->getDescription()->shouldReturn('This product is super cool because...');
104
    }
105
106
    function it_initializes_availability_date_by_default()
107
    {
108
        $this->getAvailableOn()->shouldHaveType(\DateTime::class);
109
    }
110
111
    function it_is_available_by_default()
112
    {
113
        $this->shouldBeAvailable();
114
    }
115
116
    function its_availability_date_is_mutable()
117
    {
118
        $availableOn = new \DateTime('yesterday');
119
120
        $this->setAvailableOn($availableOn);
121
        $this->getAvailableOn()->shouldReturn($availableOn);
122
    }
123
124
    function it_is_available_only_if_availability_date_is_in_past()
125
    {
126
        $availableOn = new \DateTime('yesterday');
127
128
        $this->setAvailableOn($availableOn);
129
        $this->shouldBeAvailable();
130
131
        $availableOn = new \DateTime('tomorrow');
132
133
        $this->setAvailableOn($availableOn);
134
        $this->shouldNotBeAvailable();
135
    }
136
137
    function it_initializes_attribute_collection_by_default()
138
    {
139
        $this->getAttributes()->shouldHaveType('Doctrine\Common\Collections\Collection');
140
    }
141
142
    function it_adds_attribute(AttributeValueInterface $attribute)
143
    {
144
        $attribute->setProduct($this)->shouldBeCalled();
145
146
        $this->addAttribute($attribute);
147
        $this->hasAttribute($attribute)->shouldReturn(true);
148
    }
149
150
    function it_removes_attribute(AttributeValueInterface $attribute)
151
    {
152
        $attribute->setProduct($this)->shouldBeCalled();
153
154
        $this->addAttribute($attribute);
155
        $this->hasAttribute($attribute)->shouldReturn(true);
156
157
        $attribute->setProduct(null)->shouldBeCalled();
158
159
        $this->removeAttribute($attribute);
160
        $this->hasAttribute($attribute)->shouldReturn(false);
161
    }
162
163
    function it_should_not_have_master_variant_by_default()
164
    {
165
        $this->getMasterVariant()->shouldReturn(null);
166
    }
167
168
    function its_master_variant_should_be_mutable_and_define_given_variant_as_master(VariantInterface $variant)
169
    {
170
        $variant->setProduct($this)->shouldBeCalled();
171
        $variant->setMaster(true)->shouldBeCalled();
172
173
        $this->setMasterVariant($variant);
174
    }
175
176
    function it_should_not_add_master_variant_twice_to_collection(VariantInterface $variant)
177
    {
178
        $variant->isMaster()->willReturn(true);
179
        $variant->isDeleted()->willReturn(false);
180
181
        $variant->setProduct($this)->shouldBeCalled();
182
        $variant->setMaster(true)->shouldBeCalled();
183
184
        $this->setMasterVariant($variant);
185
        $this->setMasterVariant($variant);
186
187
        $this->hasVariants()->shouldReturn(false);
188
    }
189
190
    function its_hasVariants_should_return_false_if_no_variants_defined()
191
    {
192
        $this->hasVariants()->shouldReturn(false);
193
    }
194
195
    function its_hasVariants_should_return_true_only_if_any_variants_defined(VariantInterface $variant)
196
    {
197
        $variant->isMaster()->willReturn(false);
198
        $variant->isDeleted()->willReturn(false);
199
200
        $variant->setProduct($this)->shouldBeCalled();
201
202
        $this->addVariant($variant);
203
        $this->hasVariants()->shouldReturn(true);
204
    }
205
206
    function it_should_initialize_variants_collection_by_default()
207
    {
208
        $this->getVariants()->shouldHaveType('Doctrine\Common\Collections\Collection');
209
    }
210
211
    function it_should_initialize_option_collection_by_default()
212
    {
213
        $this->getOptions()->shouldHaveType('Doctrine\Common\Collections\Collection');
214
    }
215
216
    function its_hasOptions_should_return_false_if_no_options_defined()
217
    {
218
        $this->hasOptions()->shouldReturn(false);
219
    }
220
221
    function its_hasOptions_should_return_true_only_if_any_options_defined(OptionInterface $option)
222
    {
223
        $this->addOption($option);
224
        $this->hasOptions()->shouldReturn(true);
225
    }
226
227
    function its_options_collection_should_be_mutable(Collection $options)
228
    {
229
        $this->setOptions($options);
230
        $this->getOptions()->shouldReturn($options);
231
    }
232
233
    function it_should_add_option_properly(OptionInterface $option)
234
    {
235
        $this->addOption($option);
236
        $this->hasOption($option)->shouldReturn(true);
237
    }
238
239
    function it_should_remove_option_properly(OptionInterface $option)
240
    {
241
        $this->addOption($option);
242
        $this->hasOption($option)->shouldReturn(true);
243
244
        $this->removeOption($option);
245
        $this->hasOption($option)->shouldReturn(false);
246
    }
247
248
    function it_initializes_creation_date_by_default()
249
    {
250
        $this->getCreatedAt()->shouldHaveType(\DateTime::class);
251
    }
252
253
    function its_creation_date_is_mutable()
254
    {
255
        $date = new \DateTime('last year');
256
257
        $this->setCreatedAt($date);
258
        $this->getCreatedAt()->shouldReturn($date);
259
    }
260
261
    function it_has_no_last_update_date_by_default()
262
    {
263
        $this->getUpdatedAt()->shouldReturn(null);
264
    }
265
266
    function its_last_update_date_is_mutable()
267
    {
268
        $date = new \DateTime('last year');
269
270
        $this->setUpdatedAt($date);
271
        $this->getUpdatedAt()->shouldReturn($date);
272
    }
273
274
    function it_has_no_deletion_date_by_default()
275
    {
276
        $this->getDeletedAt()->shouldReturn(null);
277
    }
278
279
    function it_is_not_be_deleted_by_default()
280
    {
281
        $this->shouldNotBeDeleted();
282
    }
283
284
    function its_deletion_date_is_mutable()
285
    {
286
        $deletedAt = new \DateTime();
287
288
        $this->setDeletedAt($deletedAt);
289
        $this->getDeletedAt()->shouldReturn($deletedAt);
290
    }
291
292
    function it_is_deleted_only_if_deletion_date_is_in_past()
293
    {
294
        $deletedAt = new \DateTime('yesterday');
295
296
        $this->setDeletedAt($deletedAt);
297
        $this->shouldBeDeleted();
298
299
        $deletedAt = new \DateTime('tomorrow');
300
301
        $this->setDeletedAt($deletedAt);
302
        $this->shouldNotBeDeleted();
303
    }
304
305
    function it_is_enabled_by_default()
306
    {
307
        $this->shouldBeEnabled();
308
    }
309
310
    function it_is_toggleable()
311
    {
312
        $this->disable();
313
        $this->shouldNotBeEnabled();
314
315
        $this->enable();
316
        $this->shouldBeEnabled();
317
    }
318
319
    function it_adds_association(ProductAssociationInterface $association)
320
    {
321
        $association->setOwner($this)->shouldBeCalled();
322
        $this->addAssociation($association);
323
324
        $this->hasAssociation($association)->shouldReturn(true);
325
    }
326
327
    function it_allows_to_remove_association(ProductAssociationInterface $association)
328
    {
329
        $association->setOwner($this)->shouldBeCalled();
330
        $association->setOwner(null)->shouldBeCalled();
331
332
        $this->addAssociation($association);
333
        $this->removeAssociation($association);
334
335
        $this->hasAssociation($association)->shouldReturn(false);
336
    }
337
}
338