Completed
Push — scalar-types/resource ( 4814fd )
by Kamil
23:02
created

it_subscribes_to_event()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
nc 1
cc 1
eloc 2
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
declare(strict_types=1);
13
14
namespace spec\Sylius\Bundle\ResourceBundle\Form\EventSubscriber;
15
16
use PhpSpec\ObjectBehavior;
17
use Prophecy\Argument;
18
use Sylius\Bundle\ResourceBundle\Form\EventSubscriber\AddCodeFormSubscriber;
19
use Sylius\Component\Resource\Exception\UnexpectedTypeException;
20
use Sylius\Component\Resource\Model\CodeAwareInterface;
21
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
22
use Symfony\Component\Form\Extension\Core\Type\FormType;
23
use Symfony\Component\Form\Extension\Core\Type\TextType;
24
use Symfony\Component\Form\FormEvent;
25
use Symfony\Component\Form\FormEvents;
26
use Symfony\Component\Form\FormInterface;
27
28
/**
29
 * @author Anna Walasek <[email protected]>
30
 */
31
final class AddCodeFormSubscriberSpec extends ObjectBehavior
32
{
33
    function it_implements_event_subscriber_interface(): void
34
    {
35
        $this->shouldImplement(EventSubscriberInterface::class);
36
    }
37
38
    function it_subscribes_to_event(): void
39
    {
40
        $this::getSubscribedEvents()->shouldReturn([FormEvents::PRE_SET_DATA => 'preSetData']);
41
    }
42
43
    function it_sets_code_as_enabled_when_resource_is_new(FormEvent $event, FormInterface $form, CodeAwareInterface $resource): void
44
    {
45
        $event->getData()->willReturn($resource);
46
        $event->getForm()->willReturn($form);
47
48
        $resource->getCode()->willReturn(null);
49
50
        $form
51
            ->add('code', Argument::type('string'), Argument::withEntry('disabled', false))
52
            ->shouldBeCalled()
53
        ;
54
55
        $this->preSetData($event);
56
    }
57
58
    function it_sets_code_as_disabled_when_resource_is_not_new(
59
        FormEvent $event,
60
        FormInterface $form,
61
        CodeAwareInterface $resource
62
    ): void {
63
        $event->getData()->willReturn($resource);
64
        $event->getForm()->willReturn($form);
65
66
        $resource->getCode()->willReturn('Code12');
67
68
        $form
69
            ->add('code', Argument::type('string'), Argument::withEntry('disabled', true))
70
            ->shouldBeCalled()
71
        ;
72
73
        $this->preSetData($event);
74
    }
75
76
    function it_throws_exception_when_resource_does_not_implement_code_aware_interface(FormEvent $event, $object): void
77
    {
78
        $event->getData()->willReturn($object);
79
        $this->shouldThrow(UnexpectedTypeException::class)->during('preSetData', [$event]);
80
    }
81
82
    function it_sets_code_as_enabled_when_there_is_no_resource(
83
        FormEvent $event,
84
        FormInterface $form
85
    ): void {
86
        $event->getData()->willReturn(null);
87
        $event->getForm()->willReturn($form);
88
89
        $form
90
            ->add('code', TextType::class, Argument::withEntry('disabled', false))
91
            ->shouldBeCalled()
92
        ;
93
94
        $this->preSetData($event);
95
    }
96
97
    function it_adds_code_with_specified_type(FormEvent $event, FormInterface $form, CodeAwareInterface $resource): void
98
    {
99
        $this->beConstructedWith(FormType::class);
100
101
        $event->getData()->willReturn($resource);
102
        $event->getForm()->willReturn($form);
103
104
        $resource->getCode()->willReturn('Code12');
105
106
        $form
107
            ->add('code', FormType::class, Argument::withEntry('disabled', true))
108
            ->shouldBeCalled()
109
        ;
110
111
        $this->preSetData($event);
112
    }
113
114
    function it_adds_code_with_type_text_by_default(FormEvent $event, FormInterface $form, CodeAwareInterface $resource): void
115
    {
116
        $event->getData()->willReturn($resource);
117
        $event->getForm()->willReturn($form);
118
119
        $resource->getCode()->willReturn('Code12');
120
121
        $form
122
            ->add('code', TextType::class, Argument::withEntry('disabled', true))
123
            ->shouldBeCalled()
124
        ;
125
126
        $this->preSetData($event);
127
    }
128
129
    function it_adds_code_with_label_sylius_ui_code_by_default(
130
        FormEvent $event,
131
        FormInterface $form,
132
        CodeAwareInterface $resource
133
    ): void {
134
        $event->getData()->willReturn($resource);
135
        $event->getForm()->willReturn($form);
136
137
        $resource->getCode()->willReturn('banana_resource');
138
139
        $form
140
            ->add('code', TextType::class, Argument::withEntry('label', 'sylius.ui.code'))
141
            ->shouldBeCalled()
142
        ;
143
144
        $this->preSetData($event);
145
    }
146
147
    function it_adds_code_with_specified_type_and_label(
148
        FormEvent $event,
149
        FormInterface $form,
150
        CodeAwareInterface $resource
151
    ): void {
152
        $this->beConstructedWith(FormType::class, ['label' => 'sylius.ui.name']);
153
154
        $event->getData()->willReturn($resource);
155
        $event->getForm()->willReturn($form);
156
157
        $resource->getCode()->willReturn('Code12');
158
159
        $form
160
            ->add('code', FormType::class, Argument::withEntry('label', 'sylius.ui.name'))
161
            ->shouldBeCalled()
162
        ;
163
164
        $this->preSetData($event);
165
    }
166
}
167