Completed
Push — symfony3-wololo-packages ( fecf70...6bf04d )
by Kamil
28:46 queued 11:35
created

ProductVariantSpec::getMatchers()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 9
nc 1
nop 0
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\Core\Model;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Doctrine\Common\Collections\Collection;
16
use PhpSpec\ObjectBehavior;
17
use Sylius\Component\Core\Model\ChannelInterface;
18
use Sylius\Component\Core\Model\ChannelPricingInterface;
19
use Sylius\Component\Core\Model\ProductInterface;
20
use Sylius\Component\Core\Model\ProductVariant;
21
use Sylius\Component\Core\Model\ProductVariantInterface;
22
use Sylius\Component\Product\Model\ProductVariant as BaseProductVariant;
23
use Sylius\Component\Shipping\Model\ShippableInterface;
24
use Sylius\Component\Shipping\Model\ShippingCategoryInterface;
25
use Sylius\Component\Taxation\Model\TaxableInterface;
26
use Sylius\Component\Taxation\Model\TaxCategoryInterface;
27
28
/**
29
 * @author Paweł Jędrzejewski <[email protected]>
30
 */
31
final class ProductVariantSpec extends ObjectBehavior
32
{
33
    function it_is_initializable()
34
    {
35
        $this->shouldHaveType(ProductVariant::class);
36
    }
37
38
    function it_implements_a_product_variant_interface()
39
    {
40
        $this->shouldImplement(ProductVariantInterface::class);
41
    }
42
43
    function it_implements_a_taxable_interface()
44
    {
45
        $this->shouldImplement(TaxableInterface::class);
46
    }
47
48
    function it_extends_a_product_variant_model()
49
    {
50
        $this->shouldHaveType(BaseProductVariant::class);
51
    }
52
53
    function it_implements_a_shippable_interface()
54
    {
55
        $this->shouldImplement(ShippableInterface::class);
56
    }
57
58
    function it_has_no_weight_by_default()
59
    {
60
        $this->getWeight()->shouldReturn(null);
61
    }
62
63
    function its_weight_is_mutable()
64
    {
65
        $this->setWeight(120);
66
        $this->getWeight()->shouldReturn(120);
67
    }
68
69
    function it_has_no_width_by_default()
70
    {
71
        $this->getWidth()->shouldReturn(null);
72
    }
73
74
    function its_width_is_mutable()
75
    {
76
        $this->setWidth(15);
77
        $this->getWidth()->shouldReturn(15);
78
    }
79
80
    function it_has_no_height_by_default()
81
    {
82
        $this->getHeight()->shouldReturn(null);
83
    }
84
85
    function its_height_is_mutable()
86
    {
87
        $this->setHeight(40);
88
        $this->getHeight()->shouldReturn(40);
89
    }
90
91
    function it_returns_correct_shipping_weight()
92
    {
93
        $this->setWeight(140);
94
        $this->getShippingWeight()->shouldReturn(140);
95
    }
96
97
    function it_returns_correct_shipping_volume()
98
    {
99
        $this->setWidth(10);
100
        $this->setHeight(20);
101
        $this->setDepth(10);
102
        $this->getShippingVolume()->shouldReturn(2000);
103
    }
104
105
    function it_returns_correct_shipping_width()
106
    {
107
        $this->setWidth(100);
108
        $this->getShippingWidth()->shouldReturn(100);
109
    }
110
111
    function it_returns_correct_shipping_height()
112
    {
113
        $this->setHeight(110);
114
        $this->getShippingHeight()->shouldReturn(110);
115
    }
116
117
    function it_has_no_code_by_default()
118
    {
119
        $this->getCode()->shouldReturn(null);
120
    }
121
122
    function its_code_is_mutable()
123
    {
124
        $this->setCode('dummy-sku123');
125
        $this->getCode()->shouldReturn('dummy-sku123');
126
    }
127
128
    function it_does_not_have_tax_category_by_default()
129
    {
130
        $this->getTaxCategory()->shouldReturn(null);
131
    }
132
133
    function it_allows_setting_the_tax_category(TaxCategoryInterface $taxCategory)
134
    {
135
        $this->setTaxCategory($taxCategory);
136
        $this->getTaxCategory()->shouldReturn($taxCategory);
137
    }
138
139
    function it_allows_resetting_the_tax_category(TaxCategoryInterface $taxCategory)
140
    {
141
        $this->setTaxCategory($taxCategory);
142
        $this->getTaxCategory()->shouldReturn($taxCategory);
143
144
        $this->setTaxCategory(null);
145
        $this->getTaxCategory()->shouldReturn(null);
146
    }
147
148
    function it_has_no_shipping_category_by_default()
149
    {
150
        $this->getShippingCategory()->shouldReturn(null);
151
    }
152
153
    function its_shipping_category_is_mutable(ShippingCategoryInterface $shippingCategory)
154
    {
155
        $this->setShippingCategory($shippingCategory);
156
        $this->getShippingCategory()->shouldReturn($shippingCategory);
157
    }
158
159
    function it_adds_and_removes_channel_pricings(ChannelPricingInterface $channelPricing)
160
    {
161
        $channelPricing->setProductVariant($this)->shouldBeCalled();
162
        $this->addChannelPricing($channelPricing);
163
        $this->hasChannelPricing($channelPricing)->shouldReturn(true);
164
165
        $channelPricing->setProductVariant(null)->shouldBeCalled();
166
        $this->removeChannelPricing($channelPricing);
167
        $this->hasChannelPricing($channelPricing)->shouldReturn(false);
168
    }
169
170
    function it_has_channel_pricings_collection(
171
        ChannelPricingInterface $firstChannelPricing,
172
        ChannelPricingInterface $secondChannelPricing
173
    ) {
174
        $firstChannelPricing->setProductVariant($this)->shouldBeCalled();
175
        $secondChannelPricing->setProductVariant($this)->shouldBeCalled();
176
        $this->addChannelPricing($firstChannelPricing);
177
        $this->addChannelPricing($secondChannelPricing);
178
179
        $this->getChannelPricings()->shouldBeSameAs(new ArrayCollection([$firstChannelPricing, $secondChannelPricing]));
180
    }
181
182
    function it_checks_if_contains_channel_pricing_for_given_channel(
183
        ChannelInterface $firstChannel,
184
        ChannelInterface $secondChannel,
185
        ChannelPricingInterface $firstChannelPricing
186
    ) {
187
        $firstChannelPricing->setProductVariant($this)->shouldBeCalled();
188
        $this->addChannelPricing($firstChannelPricing);
189
190
        $firstChannelPricing->getChannel()->willReturn($firstChannel);
191
192
        $this->hasChannelPricingForChannel($firstChannel)->shouldReturn(true);
193
        $this->hasChannelPricingForChannel($secondChannel)->shouldReturn(false);
194
    }
195
196
    function it_returns_channel_pricing_for_given_channel(
197
        ChannelInterface $channel,
198
        ChannelPricingInterface $channelPricing
199
    ) {
200
        $channelPricing->setProductVariant($this)->shouldBeCalled();
201
        $this->addChannelPricing($channelPricing);
202
203
        $channelPricing->getChannel()->willReturn($channel);
204
205
        $this->getChannelPricingForChannel($channel)->shouldReturn($channelPricing);
206
    }
207
208
    public function getMatchers()
209
    {
210
        return [
211
            'beSameAs' => function ($subject, $key) {
212
                if (!$subject instanceof Collection || !$key instanceof Collection) {
213
                    return false;
214
                }
215
                for ($i = 0; $i < $subject->count(); $i++) {
216
                    if ($subject->get($i) !== $key->get($i)->getWrappedObject()) {
217
                        return false;
218
                    }
219
                }
220
                return true;
221
            },
222
        ];
223
    }
224
}
225