FormGroupTest::testSetValueSetsInputValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 13
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Form\Container;
6
7
use AbterPhp\Framework\Constant\Html5;
8
use AbterPhp\Framework\Form\Element\IElement;
9
use AbterPhp\Framework\Form\Element\Input;
10
use AbterPhp\Framework\Form\Extra\Help;
11
use AbterPhp\Framework\Form\Label\Label;
12
use AbterPhp\Framework\Html\Attribute;
13
use AbterPhp\Framework\Html\INode;
14
use AbterPhp\Framework\TestDouble\I18n\MockTranslatorFactory;
15
use PHPUnit\Framework\MockObject\MockObject;
16
use PHPUnit\Framework\TestCase;
17
18
class FormGroupTest extends TestCase
19
{
20
    /**
21
     * @return array[]
22
     */
23
    public function renderProvider(): array
24
    {
25
        return [
26
            'simple' => ['<foo>', '<bar>', '<baz>', null, null, null, '<div><bar><foo><baz></div>'],
27
        ];
28
    }
29
30
    /**
31
     * @dataProvider renderProvider
32
     *
33
     * @param string                       $inputOutput
34
     * @param string                       $labelOutput
35
     * @param string                       $helpOutput
36
     * @param array<string,Attribute>|null $attributes
37
     * @param array|null                   $translations
38
     * @param string|null                  $tag
39
     * @param string                       $expectedResult
40
     */
41
    public function testRender(
42
        string $inputOutput,
43
        string $labelOutput,
44
        string $helpOutput,
45
        ?array $attributes,
46
        ?array $translations,
47
        ?string $tag,
48
        string $expectedResult
49
    ): void {
50
        $sut = $this->createFormGroup($inputOutput, $labelOutput, $helpOutput, $attributes, $translations, $tag);
51
52
        $actualResult   = (string)$sut;
53
        $repeatedResult = (string)$sut;
54
55
        $this->assertSame($actualResult, $repeatedResult);
56
        $this->assertSame($expectedResult, $actualResult);
57
    }
58
59
    public function testGetExtendedNodesIncludesInputLabelAndHelp(): void
60
    {
61
        $input = new Input('foo', 'foo');
62
        $label = new Label('foo', 'Foo');
63
        $help  = new Help('help');
64
65
        $sut = new FormGroup($input, $label, $help);
66
67
        $actualResult = $sut->getExtendedNodes();
68
69
        $this->assertContains($input, $actualResult);
70
        $this->assertContains($label, $actualResult);
71
        $this->assertContains($help, $actualResult);
72
    }
73
74
    public function testGetElementsReturnsInput(): void
75
    {
76
        $input = new Input('foo', 'foo');
77
        $label = new Label('foo', 'Foo');
78
79
        $sut = new FormGroup($input, $label);
80
81
        $actualResult = $sut->getElements();
82
83
        $this->assertSame([$input], $actualResult);
84
    }
85
86
    public function testSetValueSetsInputValue(): void
87
    {
88
        $expectedResult = 'bar';
89
90
        $input = new Input('foo', 'foo');
91
        $label = new Label('foo', 'Foo');
92
        $help  = new Help('help');
93
94
        $sut = new FormGroup($input, $label, $help);
95
96
        $sut->setValue($expectedResult);
97
98
        $this->assertEquals($expectedResult, $input->getAttribute(Html5::ATTR_VALUE)->getValue());
99
    }
100
101
    public function testSetTemplateChangesRender(): void
102
    {
103
        $expectedResult = '==||==';
104
105
        $input = new Input('foo', 'foo');
106
        $label = new Label('foo', 'Foo');
107
        $help  = new Help('help');
108
109
        $sut = new FormGroup($input, $label, $help);
110
111
        $sut->setTemplate($expectedResult);
112
113
        $actualResult = (string)$sut;
114
115
        $this->assertStringContainsString($expectedResult, $actualResult);
116
    }
117
118
    public function testGetInput(): void
119
    {
120
        /** @var Input|MockObject $input */
121
        $input = $this->createMock(Input::class);
122
123
        /** @var Label|MockObject $label */
124
        $label = $this->createMock(Label::class);
125
126
        $sut = new FormGroup($input, $label);
127
128
        $actualResult = $sut->getInput();
129
130
        $this->assertSame($input, $actualResult);
131
    }
132
133
    public function testGetLabel(): void
134
    {
135
        /** @var Input|MockObject $input */
136
        $input = $this->createMock(Input::class);
137
138
        /** @var Label|MockObject $label */
139
        $label = $this->createMock(Label::class);
140
141
        $sut = new FormGroup($input, $label);
142
143
        $actualResult = $sut->getLabel();
144
145
        $this->assertSame($label, $actualResult);
146
    }
147
148
    public function testGetHelp(): void
149
    {
150
        /** @var Input|MockObject $input */
151
        $input = $this->createMock(Input::class);
152
153
        /** @var Label|MockObject $label */
154
        $label = $this->createMock(Label::class);
155
156
        /** @var INode|MockObject $help */
157
        $help = $this->createMock(INode::class);
158
159
        $sut = new FormGroup($input, $label, $help);
160
161
        $actualResult = $sut->getHelp();
162
163
        $this->assertSame($help, $actualResult);
164
    }
165
166
    /**
167
     * @param string          $inputOutput
168
     * @param string          $labelOutput
169
     * @param string          $helpOutput
170
     * @param array<string,Attribute>|null $attributes
171
     * @param array|null      $translations
172
     *
173
     * @param string|null     $tag
174
     *
175
     * @return FormGroup
176
     */
177
    private function createFormGroup(
178
        string $inputOutput,
179
        string $labelOutput,
180
        string $helpOutput,
181
        ?array $attributes,
182
        ?array $translations,
183
        ?string $tag
184
    ): FormGroup {
185
        /** @var IElement|MockObject $inputMock */
186
        $inputMock = $this->createMock(Input::class);
187
188
        /** @var Label|MockObject $labelMock */
189
        $labelMock = $this->createMock(Label::class);
190
191
        /** @var Help|MockObject $helpMock */
192
        $helpMock = $this->createMock(Help::class);
193
194
        $inputMock->expects($this->any())->method('__toString')->willReturn($inputOutput);
195
        $labelMock->expects($this->any())->method('__toString')->willReturn($labelOutput);
196
        $helpMock->expects($this->any())->method('__toString')->willReturn($helpOutput);
197
198
        $translatorMock = MockTranslatorFactory::createSimpleTranslator($this, $translations);
199
200
        $formGroup = new FormGroup($inputMock, $labelMock, $helpMock, [], $attributes, $tag);
201
202
        $formGroup->setTranslator($translatorMock);
203
204
        return $formGroup;
205
    }
206
}
207