Completed
Push — master ( 031900...586888 )
by Kamil
20:21
created

it_removes_empty_translations()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 28
rs 8.8571
cc 1
eloc 23
nc 1
nop 7
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\Bundle\ResourceBundle\Form\EventSubscriber;
13
14
use Doctrine\Common\Collections\Collection;
15
use PhpSpec\ObjectBehavior;
16
use Prophecy\Argument;
17
use Sylius\Component\Resource\Model\TranslatableInterface;
18
use Sylius\Component\Resource\Model\TranslationInterface;
19
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
20
use Symfony\Component\Form\FormConfigInterface;
21
use Symfony\Component\Form\FormEvent;
22
use Symfony\Component\Form\FormEvents;
23
use Symfony\Component\Form\FormInterface;
24
25
/**
26
 * @author Anna Walasek <[email protected]>
27
 */
28
class ResourceTranslationsSubscriberSpec extends ObjectBehavior
29
{
30
    function let()
31
    {
32
        $this->beConstructedWith(['en_US' => true, 'pl_PL' => false]);
33
    }
34
35
    function it_is_initializable()
36
    {
37
        $this->shouldHaveType('Sylius\Bundle\ResourceBundle\Form\EventSubscriber\ResourceTranslationsSubscriber');
38
    }
39
40
    function it_implements_event_subscriber_interface()
41
    {
42
        $this->shouldImplement(EventSubscriberInterface::class);
43
    }
44
45
    function it_subscribes_to_event()
46
    {
47
        $this
48
            ->getSubscribedEvents()
49
            ->shouldReturn([FormEvents::PRE_SET_DATA => 'preSetData', FormEvents::SUBMIT => 'submit'])
50
        ;
51
    }
52
53
    function it_sets_empty_translations_for_available_locales(
54
        FormEvent $event,
55
        FormInterface $form,
56
        FormConfigInterface $formConfig
57
    ) {
58
        $event->getForm()->willReturn($form);
59
        $form->getConfig()->willReturn($formConfig);
60
        $form->has('en_US')->willReturn(false);
61
        $form->has('pl_PL')->willReturn(false);
62
        $formConfig->getOption('type')->willReturn('object_translation_type');
63
64
        $form->add('en_US', 'object_translation_type', ['required' => true])->shouldBeCalled();
65
        $form->add('pl_PL', 'object_translation_type', ['required' => false])->shouldBeCalled();
66
67
        $this->preSetData($event);
68
    }
69
70
    function it_sets_locale_and_translatable_resource_for_translations(
71
        FormEvent $event,
72
        FormInterface $form,
73
        Collection $data,
74
        FormInterface $parent,
75
        TranslatableInterface $translatable,
76
        TranslationInterface $englishTranslation,
77
        TranslationInterface $polishTranslation
78
    ) {
79
        $event->getData()->willReturn($data);
80
        $event->getForm()->willReturn($form);
81
        $form->getParent()->willReturn($parent);
82
        $parent->getData()->willReturn($translatable);
83
        $data->getIterator()->willReturn(new \ArrayIterator(['en_US' => $englishTranslation->getWrappedObject(), 'pl_PL' => $polishTranslation->getWrappedObject()]));
84
85
        $englishTranslation->setLocale('en_US')->shouldBeCalled();
86
        $englishTranslation->setTranslatable($translatable)->shouldBeCalled();
87
        $polishTranslation->setLocale('pl_PL')->shouldBeCalled();
88
        $polishTranslation->setTranslatable($translatable)->shouldBeCalled();
89
        $event->setData($data)->shouldBeCalled();
90
91
        $this->submit($event);
92
93
    }
94
95
    function it_removes_empty_translations(
96
        FormEvent $event,
97
        FormInterface $form,
98
        Collection $data,
99
        FormInterface $parent,
100
        TranslatableInterface $translatable,
101
        TranslationInterface $englishTranslation,
102
        \Iterator $iterator
103
    ) {
104
        $event->getData()->willReturn($data);
105
        $event->getForm()->willReturn($form);
106
        $form->getParent()->willReturn($parent);
107
        $parent->getData()->willReturn($translatable);
108
109
        $data->getIterator()->willReturn($iterator);
110
        $iterator->rewind()->shouldBeCalled();
111
        $iterator->valid()->willReturn(true, true, false)->shouldBeCalled();
112
        $iterator->current()->willReturn($englishTranslation, null);
113
        $iterator->key()->willReturn('en_US', 'pl_PL');
114
115
        $englishTranslation->setLocale('en_US')->shouldBeCalled();
116
        $englishTranslation->setTranslatable($translatable)->shouldBeCalled();
117
        $iterator->next()->shouldBeCalled();
118
        $data->offsetUnset('pl_PL')->shouldBeCalled();
119
        $event->setData($data)->shouldBeCalled();
120
121
        $this->submit($event);
122
    }
123
}
124