Completed
Push — 1.1 ( 013821...c05031 )
by Kamil
91:21 queued 68:26
created

AddUserFormSubscriberSpec   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 1
dl 0
loc 115
c 0
b 0
f 0
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 4 1
A it_is_event_subscriber_instance() 0 4 1
A it_adds_user_form_type_and_create_user_check() 0 10 1
A it_removes_user_form_type_by_default() 0 16 1
A it_does_not_remove_user_form_type_if_users_data_is_submitted_and_user_data_is_created() 0 15 1
A it_remove_user_form_type_if_users_data_is_not_submitted_and_user_is_not_created() 0 15 1
A it_does_not_remove_user_form_type_if_users_data_is_submitted_and_user_is_not_created() 0 14 1
A it_does_not_remove_user_form_type_if_users_data_is_not_submitted_and_user_is_created() 0 15 1
A it_throws_invalid_argument_exception_when_forms_normalized_data_does_not_implement_user_aware_interface() 0 11 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\AdminApiBundle\Form\EventSubscriber;
15
16
use PhpSpec\ObjectBehavior;
17
use Prophecy\Argument;
18
use Sylius\Component\User\Model\UserAwareInterface;
19
use Sylius\Component\User\Model\UserInterface;
20
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
21
use Symfony\Component\Form\Form;
22
use Symfony\Component\Form\FormEvent;
23
24
final class AddUserFormSubscriberSpec extends ObjectBehavior
25
{
26
    function let(): void
27
    {
28
        $this->beConstructedWith('\Fully\Qualified\ClassName');
29
    }
30
31
    function it_is_event_subscriber_instance(): void
32
    {
33
        $this->shouldImplement(EventSubscriberInterface::class);
34
    }
35
36
    function it_adds_user_form_type_and_create_user_check(
37
        FormEvent $event,
38
        Form $form
39
    ): void {
40
        $event->getForm()->willReturn($form);
41
42
        $form->add('user', '\Fully\Qualified\ClassName', Argument::type('array'))->shouldBeCalled();
43
44
        $this->preSetData($event);
45
    }
46
47
    function it_removes_user_form_type_by_default(
48
        FormEvent $event,
49
        Form $form,
50
        UserAwareInterface $customer
51
    ): void {
52
        $event->getData()->willReturn([], ['user' => ['plainPassword' => '']]);
53
        $event->getForm()->willReturn($form);
54
        $form->getNormData()->willReturn($customer);
55
        $customer->getUser()->willReturn(null);
56
57
        $event->setData([])->shouldBeCalledTimes(1);
58
        $form->remove('user')->shouldBeCalledTimes(2);
59
60
        $this->preSubmit($event);
61
        $this->preSubmit($event);
62
    }
63
64
    function it_does_not_remove_user_form_type_if_users_data_is_submitted_and_user_data_is_created(
65
        FormEvent $event,
66
        Form $form,
67
        UserAwareInterface $customer,
68
        UserInterface $user
69
    ): void {
70
        $event->getData()->willReturn(['user' => ['plainPassword' => 'test']]);
71
        $event->getForm()->willReturn($form);
72
        $form->getNormData()->willReturn($customer);
73
        $customer->getUser()->willReturn($user);
74
75
        $form->remove('user')->shouldNotBeCalled();
76
77
        $this->preSubmit($event);
78
    }
79
80
    function it_remove_user_form_type_if_users_data_is_not_submitted_and_user_is_not_created(
81
        FormEvent $event,
82
        Form $form,
83
        UserAwareInterface $customer
84
    ): void {
85
        $event->getData()->willReturn(['user' => ['plainPassword' => '']]);
86
        $event->getForm()->willReturn($form);
87
        $form->getNormData()->willReturn($customer);
88
        $customer->getUser()->willReturn(null);
89
90
        $event->setData([])->shouldBeCalled();
91
        $form->remove('user')->shouldBeCalled();
92
93
        $this->preSubmit($event);
94
    }
95
96
    function it_does_not_remove_user_form_type_if_users_data_is_submitted_and_user_is_not_created(
97
        FormEvent $event,
98
        Form $form,
99
        UserAwareInterface $customer
100
    ): void {
101
        $event->getData()->willReturn(['user' => ['plainPassword' => 'test']]);
102
        $event->getForm()->willReturn($form);
103
        $form->getNormData()->willReturn($customer);
104
        $customer->getUser()->willReturn(null);
105
106
        $form->remove('user')->shouldNotBeCalled();
107
108
        $this->preSubmit($event);
109
    }
110
111
    function it_does_not_remove_user_form_type_if_users_data_is_not_submitted_and_user_is_created(
112
        FormEvent $event,
113
        Form $form,
114
        UserAwareInterface $customer,
115
        UserInterface $user
116
    ): void {
117
        $event->getData()->willReturn(['user' => ['plainPassword' => '']]);
118
        $event->getForm()->willReturn($form);
119
        $form->getNormData()->willReturn($customer);
120
        $customer->getUser()->willReturn($user);
121
122
        $form->remove('user')->shouldNotBeCalled();
123
124
        $this->preSubmit($event);
125
    }
126
127
    function it_throws_invalid_argument_exception_when_forms_normalized_data_does_not_implement_user_aware_interface(
128
        FormEvent $event,
129
        Form $form,
130
        UserInterface $user
131
    ): void {
132
        $event->getData()->willReturn(['user' => ['plainPassword' => '']]);
133
        $event->getForm()->willReturn($form);
134
        $form->getNormData()->willReturn($user);
135
136
        $this->shouldThrow(\InvalidArgumentException::class)->during('preSubmit', [$event]);
137
    }
138
}
139