Passed
Push — master ( 9503f0...488e4f )
by Petr
02:34
created

TreeTest::testChangingControlsDeepNest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 52
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 43
nc 1
nop 0
dl 0
loc 52
rs 9.232
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace ControlTests;
4
5
6
use CommonTestClass;
7
use kalanis\kw_forms\Controls;
8
use kalanis\kw_forms\Exceptions\FormsException;
9
use kalanis\kw_forms\Exceptions\RenderException;
10
use kalanis\kw_forms\Form;
11
use kalanis\kw_rules\Interfaces\IRules;
12
use kalanis\kw_rules\Validate;
13
14
15
class TreeTest extends CommonTestClass
16
{
17
    /**
18
     * @throws RenderException
19
     */
20
    public function testSimpleAny(): void
21
    {
22
        $input = new Controls\AnyControl();
23
        $input->setLabel('rfg');
24
        $this->assertEmpty($input->getLabel());
25
        $input->setValue('edc');
26
        $this->assertEmpty($input->getValue());
27
        $input->setKey('rdx');
28
        $this->assertEquals('rdx', $input->getKey());
29
        $this->assertEmpty($input->render());
30
        $this->assertEmpty($input->renderLabel());
31
        $this->assertEmpty($input->renderInput());
32
        $this->assertEmpty($input->renderErrors([]));
33
    }
34
35
    /**
36
     * @throws FormsException
37
     */
38
    public function testInForm(): void
39
    {
40
        $form = new Form('testing');
41
        $toTest = new Controls\AnyControl();
42
        $toTest->setKey('uhb');
43
        $form->addControlDefaultKey($toTest);
44
        $form->setInputs(new \Adapter());
45
        $this->assertTrue($form->isValid());
46
    }
47
48
    public function testChangingControls(): void
49
    {
50
        $con1 = new Controls\Text();
51
        $con1->set('foo', 'zgv');
52
        $con1->setValue('tfc');
53
        $con1->addRule(IRules::IS_NOT_EMPTY, 'must not be empty');
54
        $con2 = new Controls\Text();
55
        $con2->set('bar');
56
        $con2->addRule(IRules::IS_NOT_EMPTY, 'must not be empty');
57
58
        $input = new Controls\AnyControl();
59
        $input->addControl('foo', $con1);
60
        $input->addControl('bar', $con2);
61
62
        $libVal = new Validate();
63
        $input->needAll(true);
64
        $this->assertFalse($input->validateControls($libVal));
65
66
        $input->needAll(false);
67
        $this->assertTrue($input->validateControls($libVal));
68
    }
69
70
    /**
71
     * @throws FormsException
72
     */
73
    public function testChangingControlsDeepNest(): void
74
    {
75
        $con1 = new Controls\Text(); // filled from adapter
76
        $con1->set('foo', 'zgv');
77
        $con1->addRule(IRules::IS_NOT_EMPTY, 'must not be empty');
78
        $con2 = new Controls\Text(); // filled from adapter
79
        $con2->set('bar');
80
        $con2->addRule(IRules::IS_NOT_EMPTY, 'must not be empty');
81
        $con3 = new Controls\Text(); // never filled
82
        $con3->set('bvt');
83
        $con3->addRule(IRules::IS_NOT_EMPTY, 'must not be empty');
84
        $con4 = new Controls\Text(); // filled from adapter
85
        $con4->set('sgg');
86
        $con4->addRule(IRules::IS_NOT_EMPTY, 'must not be empty');
87
88
        $input3 = new Controls\AnyControl();
89
        $input3->addControl('sgg', $con4);
90
        $input3->addControl('bvt', $con3);
91
        $input2 = new Controls\AnyControl();
92
        $input2->addControl('bar', $con2);
93
        $input2->addControl('mor', $input3);
94
        $input = new Controls\AnyControl();
95
        $input->addControl('ext', $input2);
96
        $input->addControl('foo', $con1);
97
98
        $adapter = new \Adapter();
99
        $form = new Form('testing');
100
        $form->addControlDefaultKey($input);
101
        $form->setInputs($adapter);
102
103
        $input->needAll(true);
104
        $input2->needAll(true);
105
        $input3->needAll(true);
106
        $form->setSentValues();
107
        $this->assertFalse($form->isValid());
108
109
        $input3->needAll(false);
110
        $form->setSentValues();
111
        $this->assertTrue($form->isValid());
112
113
        $input2->needAll(false);
114
        $form->setSentValues();
115
        $this->assertTrue($form->isValid());
116
117
        $input->needAll(false);
118
        $form->setSentValues();
119
        $this->assertTrue($form->isValid());
120
121
        $input->needAll(true);
122
        $input3->needAll(true);
123
        $form->setSentValues();
124
        $this->assertTrue($form->isValid());
125
    }
126
127
    public function testExistsControls(): void
128
    {
129
        $con3 = new Controls\Text();
130
        $con3->set('non');
131
        $con3->addRule(IRules::IS_NOT_EMPTY, 'might be empty');
132
133
        $input = new Controls\AnyControl();
134
        $this->assertEquals(0, $input->count());
135
        $this->assertFalse($input->hasControl('non'));
136
        $this->assertNull($input->getControl('non'));
137
        $input->addControl('non', $con3);
138
        $this->assertEquals(1, $input->count());
139
        $this->assertTrue($input->hasControl('non'));
140
        $this->assertIsObject($input->getControl('non'));
141
        $input->removeControl('non');
142
        $this->assertEquals(0, $input->count());
143
        $this->assertFalse($input->hasControl('non'));
144
        $this->assertNull($input->getControl('non'));
145
    }
146
147
    /**
148
     * @throws FormsException
149
     */
150
    public function testGettingControlsDeepNest(): void
151
    {
152
        $con1 = new Controls\Text(); // filled from adapter
153
        $con1->set('foo', 'zgv');
154
        $con1->addRule(IRules::IS_NOT_EMPTY, 'must not be empty');
155
        $con2 = new Controls\Text(); // filled from adapter
156
        $con2->set('bar');
157
        $con2->addRule(IRules::IS_NOT_EMPTY, 'must not be empty');
158
        $con3 = new Controls\Text(); // never filled
159
        $con3->set('bvt');
160
        $con3->addRule(IRules::IS_NOT_EMPTY, 'must not be empty');
161
        $con4 = new Controls\Text(); // filled from adapter
162
        $con4->set('sgg');
163
        $con4->addRule(IRules::IS_NOT_EMPTY, 'must not be empty');
164
165
        $input2 = new Controls\AnyControl();
166
        $input2->addControl('bar', $con2);
167
        $input2->addControl('bvt', $con3);
168
        $input2->addControl('sgg', $con4);
169
        $input = new Controls\AnyControl();
170
        $input->addControl('foo', $con1);
171
        $input->addControl('mor', $input2);
172
173
        $input->needAll(true);
174
        $input2->needAll(true);
175
176
        $adapter = new \Adapter();
177
        $form = new Form('testing');
178
        $form->addControlDefaultKey($input);
179
        $form->setInputs($adapter);
180
181
        $this->assertNull($input->getControl('non'));
182
        $this->assertIsObject($input->getControl('foo'));
183
        $this->assertIsObject($input->getControl('sgg'));
184
        $this->assertEmpty($form->renderErrorsArray());
185
186
        $this->assertEquals(['foo' => '', 'bar' => '', 'bvt' => '', 'sgg' => '', ], $form->getLabels());
187
        $form->setLabels([
188
            'bvt' => 'rdc',
189
            'ofd' => 'gsa',
190
        ]);
191
        $this->assertEquals(['foo' => '', 'bar' => '', 'bvt' => 'rdc', 'sgg' => '', ], $form->getLabels());
192
193
        $form->setSentValues();
194
        $this->assertFalse($form->isValid());
195
        $this->assertNotEmpty($form->renderErrorsArray());
196
    }
197
}
198