Completed
Pull Request — master (#17)
by Eric
08:11
created

testLabelPrefixWithAllowDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 21
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 21
loc 21
rs 9.3142
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Lug package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lug\Bundle\ResourceBundle\Tests\Form\Extension;
13
14
use Lug\Bundle\ResourceBundle\Form\Extension\CollectionExtension;
15
use Lug\Bundle\ResourceBundle\Form\Extension\LabelFormExtension;
16
use Lug\Bundle\ResourceBundle\Tests\Form\EventSubscriber\Mock\CollectionSubscriberMock;
17
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
18
use Symfony\Component\Form\Extension\Core\Type\FormType;
19
use Symfony\Component\Form\FormFactoryInterface;
20
use Symfony\Component\Form\Forms;
21
22
/**
23
 * @author GeLo <[email protected]>
24
 */
25
class LabelFormExtensionTest extends \PHPUnit_Framework_TestCase
26
{
27
    /**
28
     * @var FormFactoryInterface
29
     */
30
    private $factory;
31
32
    /**
33
     * @var LabelFormExtension
34
     */
35
    private $extension;
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    protected function setUp()
41
    {
42
        $this->extension = new LabelFormExtension();
43
44
        $this->factory = Forms::createFormFactoryBuilder()
45
            ->addTypeExtension($this->extension)
46
            ->getFormFactory();
47
    }
48
49
    public function testInheritance()
50
    {
51
        $this->assertSame(FormType::class, $this->extension->getExtendedType());
52
    }
53
54 View Code Duplication
    public function testDefault()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56
        $form = $this->factory
57
            ->createBuilder(FormType::class)
58
            ->add($fieldName = 'field', FormType::class)
59
            ->getForm();
60
61
        $view = $form->createView();
62
        $buttonView = $view->children[$fieldName];
63
64
        $this->assertArrayHasKey('label', $buttonView->vars);
65
        $this->assertNull($buttonView->vars['label']);
66
67
        $this->assertArrayHasKey('label_translation_arguments', $buttonView->vars);
68
        $this->assertEmpty($buttonView->vars['label_translation_arguments']);
69
    }
70
71 View Code Duplication
    public function testLabelPrefix()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
    {
73
        $form = $this->factory
74
            ->createBuilder(FormType::class, null, ['label_prefix' => $labelPrefix = 'prefix'])
75
            ->add($fieldName = 'field')
76
            ->getForm();
77
78
        $view = $form->createView();
79
        $fieldView = $view->children[$fieldName];
80
81
        $this->assertArrayHasKey('label', $fieldView->vars);
82
        $this->assertSame($labelPrefix.'.'.$fieldName, $fieldView->vars['label']);
83
    }
84
85
    public function testEmbedLabelPrefix()
86
    {
87
        $embedForm = $this->factory
88
            ->createNamedBuilder(
89
                $embedFormName = 'embed_form',
90
                FormType::class,
91
                null,
92
                ['label_prefix' => $embedLabelPrefix = 'embed_prefix']
93
            )
94
            ->add($embedFieldName = 'embed_field');
95
96
        $form = $this->factory
97
            ->createBuilder(FormType::class, null, ['label_prefix' => $labelPrefix = 'prefix'])
98
            ->add($embedForm)
99
            ->add($fieldName = 'field')
100
            ->getForm();
101
102
        $view = $form->createView();
103
104
        $fieldView = $view->children[$fieldName];
105
        $embedFormView = $view->children[$embedFormName];
106
        $embedFieldView = $embedFormView->children[$embedFieldName];
107
108
        $this->assertArrayHasKey('label', $fieldView->vars);
109
        $this->assertSame($labelPrefix.'.'.$fieldName, $fieldView->vars['label']);
110
111
        $this->assertArrayHasKey('label', $embedFormView->vars);
112
        $this->assertSame($labelPrefix.'.'.$embedFormName, $embedFormView->vars['label']);
113
114
        $this->assertArrayHasKey('label', $embedFieldView->vars);
115
        $this->assertSame($embedLabelPrefix.'.'.$embedFieldName, $embedFieldView->vars['label']);
116
    }
117
118
    public function testLabelPrefixWithExplicitLabel()
119
    {
120
        $form = $this->factory
121
            ->createBuilder(FormType::class, null, ['label_prefix' => 'prefix'])
122
            ->add($fieldName = 'field', null, ['label' => $explicitLabel = 'explicit'])
123
            ->getForm();
124
125
        $view = $form->createView();
126
        $fieldView = $view->children[$fieldName];
127
128
        $this->assertArrayHasKey('label', $fieldView->vars);
129
        $this->assertSame($explicitLabel, $fieldView->vars['label']);
130
    }
131
132 View Code Duplication
    public function testLabelPrefixWithAllowAdd()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
    {
134
        $this->factory = Forms::createFormFactoryBuilder()
135
            ->addTypeExtension($this->extension)
136
            ->addTypeExtension(new CollectionExtension(new CollectionSubscriberMock()))
137
            ->getFormFactory();
138
139
        $form = $this->factory
140
            ->createBuilder(FormType::class, null, ['label_prefix' => $labelPrefix = 'prefix'])
141
            ->add($fieldName = 'field', CollectionType::class, ['allow_add' => true])
142
            ->getForm();
143
144
        $view = $form->createView();
145
        $fieldView = $view->children[$fieldName];
146
147
        $this->assertArrayHasKey('label', $fieldView->vars);
148
        $this->assertSame($labelPrefix.'.'.$fieldName.'.label', $fieldView->vars['label']);
149
150
        $this->assertArrayHasKey('label_add', $fieldView->vars);
151
        $this->assertSame($labelPrefix.'.'.$fieldName.'.add', $fieldView->vars['label_add']);
152
    }
153
154 View Code Duplication
    public function testLabelPrefixWithAllowDelete()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
    {
156
        $this->factory = Forms::createFormFactoryBuilder()
157
            ->addTypeExtension($this->extension)
158
            ->addTypeExtension(new CollectionExtension(new CollectionSubscriberMock()))
159
            ->getFormFactory();
160
161
        $form = $this->factory
162
            ->createBuilder(FormType::class, null, ['label_prefix' => $labelPrefix = 'prefix'])
163
            ->add($fieldName = 'field', CollectionType::class, ['allow_delete' => true])
164
            ->getForm();
165
166
        $view = $form->createView();
167
        $fieldView = $view->children[$fieldName];
168
169
        $this->assertArrayHasKey('label', $fieldView->vars);
170
        $this->assertSame($labelPrefix.'.'.$fieldName.'.label', $fieldView->vars['label']);
171
172
        $this->assertArrayHasKey('label_delete', $fieldView->vars);
173
        $this->assertSame($labelPrefix.'.'.$fieldName.'.delete', $fieldView->vars['label_delete']);
174
    }
175
176
    public function testLabelTranslationArguments()
177
    {
178
        $form = $this->factory->create(FormType::class, null, [
179
            'label_translation_arguments' => $arguments = ['foo' => 'bar'],
180
        ]);
181
182
        $view = $form->createView();
183
184
        $this->assertArrayHasKey('label_translation_arguments', $view->vars);
185
        $this->assertSame($arguments, $view->vars['label_translation_arguments']);
186
    }
187
}
188