Completed
Push — 1.0 ( 3bca9c...afd9f8 )
by Kamil
31:40
created

BuildAttributesFormSubscriberSpec   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 9

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 9
dl 0
loc 88
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 4 1
A it_subscribes_to_event() 0 7 1
B it_adds_attribute_values_in_different_locales_to_a_product() 0 30 1
A it_removes_empty_attribute_values_in_different_locales() 0 21 1
A it_throws_an_invalid_argument_exception_if_data_is_not_a_product() 0 9 1
A it_throws_an_invalid_argument_exception_if_data_is_not_a_product_during_submit() 0 9 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
declare(strict_types=1);
13
14
namespace spec\Sylius\Bundle\ProductBundle\Form\EventSubscriber;
15
16
use Doctrine\Common\Collections\ArrayCollection;
17
use PhpSpec\ObjectBehavior;
18
use Sylius\Component\Product\Model\ProductAttributeInterface;
19
use Sylius\Component\Product\Model\ProductAttributeValueInterface;
20
use Sylius\Component\Product\Model\ProductInterface;
21
use Sylius\Component\Resource\Factory\FactoryInterface;
22
use Sylius\Component\Resource\Translation\Provider\TranslationLocaleProviderInterface;
23
use Symfony\Component\Form\FormEvent;
24
use Symfony\Component\Form\FormEvents;
25
26
final class BuildAttributesFormSubscriberSpec extends ObjectBehavior
27
{
28
    function let(FactoryInterface $attributeValueFactory, TranslationLocaleProviderInterface $localeProvider): void
29
    {
30
        $this->beConstructedWith($attributeValueFactory, $localeProvider);
31
    }
32
33
    function it_subscribes_to_event(): void
34
    {
35
        static::getSubscribedEvents()->shouldReturn([
36
            FormEvents::PRE_SET_DATA => 'preSetData',
37
            FormEvents::POST_SUBMIT => 'postSubmit',
38
        ]);
39
    }
40
41
    function it_adds_attribute_values_in_different_locales_to_a_product(
42
        FactoryInterface $attributeValueFactory,
43
        TranslationLocaleProviderInterface $localeProvider,
44
        FormEvent $event,
45
        ProductInterface $product,
46
        ProductAttributeInterface $attribute,
47
        ProductAttributeValueInterface $attributeValue,
48
        ProductAttributeValueInterface $newAttributeValue
49
    ): void {
50
        $event->getData()->willReturn($product);
51
52
        $localeProvider->getDefinedLocalesCodes()->willReturn(['en_US', 'pl_PL']);
53
        $localeProvider->getDefaultLocaleCode()->willReturn('en_US');
54
55
        $attributeValue->getAttribute()->willReturn($attribute);
56
        $attributeValue->getLocaleCode()->willReturn('en_US');
57
        $attributeValue->getCode()->willReturn('mug_material');
58
59
        $attributes = new ArrayCollection([$attributeValue->getWrappedObject()]);
60
        $product->getAttributes()->willReturn($attributes);
61
        $product->hasAttributeByCodeAndLocale('mug_material', 'en_US')->willReturn(true);
62
        $product->hasAttributeByCodeAndLocale('mug_material', 'pl_PL')->willReturn(false);
63
64
        $attributeValueFactory->createNew()->willReturn($newAttributeValue);
65
        $newAttributeValue->setAttribute($attribute)->shouldBeCalled();
66
        $newAttributeValue->setLocaleCode('pl_PL')->shouldBeCalled();
67
        $product->addAttribute($newAttributeValue)->shouldBeCalled();
68
69
        $this->preSetData($event);
70
    }
71
72
    function it_removes_empty_attribute_values_in_different_locales(
73
        FormEvent $event,
74
        ProductInterface $product,
75
        ProductAttributeInterface $attribute,
76
        ProductAttributeValueInterface $attributeValue,
77
        ProductAttributeValueInterface $attributeValue2
78
    ): void {
79
        $event->getData()->willReturn($product);
80
81
        $attributes = new ArrayCollection([$attributeValue->getWrappedObject(), $attributeValue2->getWrappedObject()]);
82
        $product->getAttributes()->willReturn($attributes);
83
84
        $attribute->getStorageType()->willReturn('text');
85
86
        $attributeValue->getValue()->willReturn(null);
87
        $attributeValue2->getValue()->willReturn('yellow');
88
89
        $product->removeAttribute($attributeValue)->shouldBeCalled();
90
91
        $this->postSubmit($event);
92
    }
93
94
    function it_throws_an_invalid_argument_exception_if_data_is_not_a_product(FormEvent $event, \stdClass $stdObject): void
95
    {
96
        $event->getData()->willReturn($stdObject);
97
98
        $this
99
            ->shouldThrow(\InvalidArgumentException::class)
100
            ->during('preSetData', [$event])
101
        ;
102
    }
103
104
    function it_throws_an_invalid_argument_exception_if_data_is_not_a_product_during_submit(FormEvent $event, \stdClass $stdObject): void
105
    {
106
        $event->getData()->willReturn($stdObject);
107
108
        $this
109
            ->shouldThrow(\InvalidArgumentException::class)
110
            ->during('postSubmit', [$event])
111
        ;
112
    }
113
}
114