Passed
Push — master ( 0b0ee1...9503f0 )
by Petr
06:38 queued 03:11
created

BasicTest::testShittySettingClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
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\Interfaces;
11
use kalanis\kw_rules\Interfaces\IRules;
12
use kalanis\kw_rules\Validate;
13
use kalanis\kw_templates\Interfaces\IHtmlElement;
14
use kalanis\kw_templates\HtmlElement\TAttributes;
15
use kalanis\kw_templates\HtmlElement\THtmlElement;
16
17
18
class BasicTest extends CommonTestClass
19
{
20
    /**
21
     * @param string $type
22
     * @param string $instance
23
     * @throws FormsException
24
     * @dataProvider factoryProvider
25
     */
26
    public function testFactory(string $type, string $instance): void
27
    {
28
        $factory = new Controls\Factory();
29
        $control = $factory->getControl($type);
30
        $this->assertInstanceOf($instance, $control);
31
    }
32
33
    public function factoryProvider(): array
34
    {
35
        return [
36
            ['input', Controls\Input::class],
37
            ['text', Controls\Text::class],
38
            ['textarea', Controls\Textarea::class],
39
            ['email', Controls\Email::class],
40
            ['pass', Controls\Password::class],
41
            ['password', Controls\Password::class],
42
            ['phone', Controls\Telephone::class],
43
            ['telephone', Controls\Telephone::class],
44
            ['chk', Controls\Checkbox::class],
45
            ['check', Controls\Checkbox::class],
46
            ['checkbox', Controls\Checkbox::class],
47
            ['checkboxswitch', Controls\CheckboxSwitch::class],
48
            ['select', Controls\Select::class],
49
            ['selectbox', Controls\Select::class],
50
            ['radio', Controls\Radio::class],
51
            ['radioset', Controls\RadioSet::class],
52
            ['radiobutton', Controls\Radio::class],
53
            ['hidden', Controls\Hidden::class],
54
            ['date', Controls\DatePicker::class],
55
            ['datetime', Controls\DateTimePicker::class],
56
            ['daterange', Controls\DateRange::class],
57
            ['description', Controls\Description::class],
58
            ['desc', Controls\Description::class],
59
            ['html', Controls\Html::class],
60
            ['file', Controls\File::class],
61
            ['button', Controls\Button::class],
62
            ['accept', Controls\Submit::class],
63
            ['submit', Controls\Submit::class],
64
            ['cancel', Controls\Reset::class],
65
            ['reset', Controls\Reset::class],
66
            ['captchadis', Controls\Security\Captcha\Disabled::class],
67
            ['captchatext', Controls\Security\Captcha\Text::class],
68
            ['captchaplus', Controls\Security\Captcha\Numerical::class],
69
            ['nocaptcha', Controls\Security\Captcha\NoCaptcha::class],
70
            ['csrf', Controls\Security\Csrf::class],
71
            ['multisend', Controls\Security\MultiSend::class],
72
        ];
73
    }
74
75
    /**
76
     * @param mixed $type
77
     * @throws FormsException
78
     * @dataProvider factoryDieProvider
79
     */
80
    public function testFactoryDie($type): void
81
    {
82
        $factory = new Controls\Factory();
83
        $this->expectException(FormsException::class);
84
        $factory->getControl(strval($type));
85
    }
86
87
    public function factoryDieProvider(): array
88
    {
89
        return [
90
            [Controls\Input::class],
91
            ['something'],
92
            [123456],
93
        ];
94
    }
95
96
    /**
97
     * @throws FormsException
98
     */
99
    public function testShittySettingInstance(): void
100
    {
101
        $factory = new XFactory();
102
        $this->expectException(FormsException::class);
103
        $factory->getControl('not_instance');
104
    }
105
106
    /**
107
     * @throws FormsException
108
     */
109
    public function testShittySettingClass(): void
110
    {
111
        $factory = new XFactory();
112
        $this->expectException(FormsException::class);
113
        $factory->getControl('not_class');
114
    }
115
116
    public function testKey(): void
117
    {
118
        $key = new Key();
119
        $this->assertEmpty($key->getKey());
120
        $key->setKey('sdfghj');
121
        $this->assertNotEmpty($key->getKey());
122
        $this->assertEquals('sdfghj', $key->getKey());
123
    }
124
125
    public function testValue(): void
126
    {
127
        $value = new Value();
128
        $this->assertEmpty($value->getValue());
129
        $value->setValue('sdfghj');
130
        $this->assertNotEmpty($value->getValue());
131
        $this->assertEquals('sdfghj', $value->getValue());
132
    }
133
134
    public function testLabel(): void
135
    {
136
        $label = new Label();
137
        $this->assertEmpty($label->getLabel());
138
        $label->setLabel('yxcvbnm');
139
        $this->assertNotEmpty($label->getLabel());
140
        $this->assertEquals('yxcvbnm', $label->getLabel());
141
    }
142
143
    public function testTmplError(): void
144
    {
145
        $error = new TemplateError();
146
        $error->setTemplateError('');
147
        $this->assertEmpty($error->getTemplateError());
148
        $error->setTemplateError('lkjhgfdsa');
149
        $this->assertNotEmpty($error->getTemplateError());
150
        $this->assertEquals('lkjhgfdsa', $error->getTemplateError());
151
    }
152
153
    public function testChecked(): void
154
    {
155
        $checked = new Checked();
156
        $this->assertEmpty($checked->getValue());
157
        $checked->setValue('yxcvbnm');
158
        $this->assertEquals('rjgvnsg', $checked->getValue());
159
        $checked->setValue('none');
160
        $this->assertEmpty($checked->getValue());
161
    }
162
163
    public function testSelected(): void
164
    {
165
        $selected = new Selected();
166
        $this->assertEmpty($selected->getValue());
167
        $selected->setValue('rjgvnsg');
168
        $this->assertEquals('rjgvnsg', $selected->getValue());
169
        $selected->setValue('yxcvbnm');
170
        $this->assertEmpty($selected->getValue());
171
        $selected->setValue('none');
172
        $this->assertEmpty($selected->getValue());
173
    }
174
175
    public function testMultiple(): void
176
    {
177
        $multiple = new Multiple();
178
        $this->assertFalse($multiple->getMultiple());
179
        $multiple->setMultiple('yxcvbnm');
180
        $this->assertTrue($multiple->getMultiple());
181
        $multiple->setMultiple('none');
182
        $this->assertFalse($multiple->getMultiple());
183
    }
184
185
    /**
186
     * @throws RenderException
187
     */
188
    public function testControl(): void
189
    {
190
        $validate = new Validate();
191
192
        $input = new Control();
193
        $input->addRule(IRules::IS_NOT_EMPTY, 'still empty!'); // factory, check for errors
194
195
        $this->assertEmpty($input->getLabel());
196
        $this->assertEmpty($input->renderLabel());
197
        $this->assertEquals(0, $input->count());
198
199
        $input->setLabel('not to look');
200
        $this->assertEquals('<label for="">not to look</label>', $input->renderLabel());
201
        $input->setAttribute('id', 'poiu');
202
        $this->assertEquals('<label for="poiu">not to look</label>', $input->renderLabel());
203
204
        $validate->validate($input); // check after init
205
206
        $this->assertEquals('still empty!', $input->renderErrors($validate->getErrors()[$input->getKey()])); // got errors
207
208
        $input->setValue('jhgfd');
209
210
        $validate->validate($input); // check after fill
211
212
        $this->assertEmpty($input->renderErrors($validate->getErrors())); // no errors
213
    }
214
215
    /**
216
     * @throws RenderException
217
     */
218
    public function testWrapperInherit(): void
219
    {
220
        $wrappers = new Control();
221
        $wrappers->resetWrappers();
222
        $this->assertEmpty($wrappers->wrappers());
223
        $this->assertEmpty($wrappers->wrappersLabel());
224
        $this->assertEmpty($wrappers->wrappersInput());
225
        $this->assertEmpty($wrappers->wrappersChild());
226
        $this->assertEmpty($wrappers->wrappersChildren());
227
        $this->assertEmpty($wrappers->wrappersError());
228
        $this->assertEmpty($wrappers->wrappersErrors());
229
230
        $wrappers->addWrapper('span', ['style' => 'width:100em']);
231
        $wrappers->addWrapperLabel('div');
232
        $wrappers->addWrapperInput('div');
233
        $wrappers->addWrapperChild('span');
234
        $wrappers->addWrapperChildren(new Html(), ['class' => 'wat']);
235
        $wrappers->addWrapperError(['span', 'span']);
236
        $wrappers->addWrapperErrors('div');
237
238
        $sub = new Wrappers();
239
        $wrappers->inherit($sub);
240
241
        $sub->wrapping('div', $sub->wrappersInput());
242
    }
243
244
    /**
245
     * @throws RenderException
246
     */
247
    public function testWrapperObject(): void
248
    {
249
        $wrappers = new Wrappers();
250
        $wrappers->resetWrappers();
251
        $this->assertEmpty($wrappers->wrappersLabel());
252
        $wrappers->addWrapperLabel('div');
253
        $wrappers->wrapping('div', new Html());
254
    }
255
256
    /**
257
     * @throws RenderException
258
     */
259
    public function testWrapperDie(): void
260
    {
261
        $wrappers = new Wrappers();
262
        $wrappers->resetWrappers();
263
        $this->assertEmpty($wrappers->wrappersLabel());
264
        $wrappers->addWrapperLabel('div');
265
        $this->expectException(RenderException::class);
266
        $wrappers->wrapping('div', 123456);
267
    }
268
}
269
270
271
class Key
272
{
273
    use Controls\TKey;
274
}
275
276
277
class Value
278
{
279
    use Controls\TValue;
280
}
281
282
283
class Label
284
{
285
    use Controls\TLabel;
286
}
287
288
289
class TemplateError
290
{
291
    use Controls\TTemplateError;
292
}
293
294
295
class Checked
296
{
297
    use Controls\TChecked;
298
    use TAttributes;
299
300
    protected $originalValue = 'rjgvnsg';
301
}
302
303
304
class Selected
305
{
306
    use Controls\TSelected;
307
    use TAttributes;
308
309
    protected $originalValue = 'rjgvnsg';
310
}
311
312
313
class Multiple
314
{
315
    use Controls\TMultiple;
316
    use TAttributes;
317
318
    protected $originalValue = 'rjgvnsg';
319
    protected $children = [];
320
}
321
322
323
class Wrappers implements Interfaces\IWrapper, IHtmlElement
324
{
325
    use THtmlElement;
326
    use Controls\TWrappers;
327
328
    /**
329
     * @param string $string
330
     * @param mixed $wrappers
331
     * @throws RenderException
332
     * @return string
333
     */
334
    public function wrapping(string $string, $wrappers): string
335
    {
336
        return $this->wrapIt($string, $wrappers);
337
    }
338
339
    public function count(): int
340
    {
341
        return 0;
342
    }
343
}
344
345
346
class Html implements IHtmlElement
347
{
348
    use THtmlElement;
349
350
    public function count(): int
351
    {
352
        return 0;
353
    }
354
}
355
356
357
class Control extends Controls\AControl
358
{
359
}
360
361
362
class XFactory extends Controls\Factory
363
{
364
    /** @var array<string, string> */
365
    protected static $map = [
366
        'text' => Controls\Text::class,
367
        'not_instance' => \stdClass::class,
368
        'not_class'    => 'this_is_not_a_class',
369
    ];
370
}
371