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

FormTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 73
c 2
b 0
f 0
dl 0
loc 126
rs 10
1
<?php
2
3
namespace BasicTests;
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_storage\Storage\Target\Memory;
13
use kalanis\kw_storage\StorageException;
14
15
16
class FormTest extends CommonTestClass
17
{
18
    /**
19
     * @throws FormsException
20
     */
21
    public function testFormInit(): void
22
    {
23
        $files = new \Files();
24
        $files->loadEntries('');
25
        $inputs = new \Adapter();
26
27
        $form = new Form('test');
28
        $form->setInputs($inputs, $files);
29
30
        $this->assertInstanceOf(Controls\Factory::class, $form->getControlFactory());
31
        $form->addControlDefaultKey($form->getControlFactory()->getControl('html')->setEntry('html', 'testing input'));
32
        $this->assertEmpty($form->getControl('baz'));
33
    }
34
35
    /**
36
     * @throws FormsException
37
     * @throws StorageException
38
     */
39
    public function testStorage(): void
40
    {
41
        $files = new \Files();
42
        $files->loadEntries('');
43
44
        $form = new Form('test');
45
        $form->setInputs(new \Adapter(), $files);
46
        $form->setStorage(new Memory());
47
        $form->addText('foo', 'first');
48
        $form->addText('bar', 'second');
49
        $form->addText('baz', 'third');
50
        $form->addFiles('other', 'file');
51
52
        $this->assertEmpty($form->getValue('foo'));
53
        $this->assertEmpty($form->getValue('bar'));
54
        $this->assertEmpty($form->getValue('unknown'));
55
56
        $form->setSentValues();
57
        $form->store();
58
        $form->loadStored();
59
60
        $this->assertEquals('aff', $form->getValue('foo'));
61
        $this->assertEquals('poa', $form->getValue('bar'));
62
63
        $form->setValue('other', reset($files));
64
        $form->setValue('baz', 'here');
65
    }
66
67
    /**
68
     * @throws FormsException
69
     */
70
    public function testLabels(): void
71
    {
72
        $form = new Form('test');
73
        $form->setInputs(new \Adapter());
74
        $form->addText('foo', 'first');
75
        $form->addText('bar', 'second');
76
        $form->addText('baz', 'third');
77
78
        $form->setLabels([
79
            'foo' => 'uno',
80
            'bar' => 'duo',
81
        ]);
82
83
        $labels = $form->getLabels();
84
        $this->assertNotEmpty($labels['foo']);
85
        $this->assertEquals('uno', $labels['foo']);
86
        $this->assertNotEmpty($labels['bar']);
87
        $this->assertEquals('duo', $labels['bar']);
88
89
        $form->setLabel('troi', 'baz');
90
        $this->assertEquals('troi', $form->getLabel('baz'));
91
        $this->assertEmpty($form->getLabel('what'));
92
93
        $form->setLabel('our form');
94
        $this->assertEquals('our form', $form->getLabel());
95
    }
96
97
    /**
98
     * @throws FormsException
99
     * @throws RenderException
100
     */
101
    public function testProcessing(): void
102
    {
103
        $form = new Form('test');
104
        $form->addHidden('dez', 'shade');
105
        $form->addText('foo', 'first')->addRule(IRules::IS_FILLED, 'content filled');
106
        $form->addText('bar', 'second');
107
        $last = $form->addText('baz', 'third');
108
        $this->assertFalse($form->process('baz'));
109
        $this->assertEmpty($form->renderControlErrors('baz'));
110
111
        $form->setInputs(new \Adapter());
112
        $this->assertFalse($form->process());
113
        $this->assertEmpty($form->renderErrors());
114
115
        $form->addSubmit('sgg', 'ijn');
116
        $form->setInputs(new \Adapter());
117
        $this->assertTrue($form->process());
118
        $this->assertTrue($form->process('baz'));
119
120
        $last->addRule(IRules::IS_NUMERIC, 'must be a number');
121
        $this->assertFalse($form->process());
122
        $this->assertEquals('must be a number', $form->renderControlErrors('baz'));
123
124
        $this->assertNotEmpty($form->render());
125
        $this->assertNotEmpty($form->renderStart());
126
        $this->assertNotEmpty($form->renderEnd());
127
    }
128
129
    /**
130
     * @throws FormsException
131
     * @throws RenderException
132
     */
133
    public function testLayout(): void
134
    {
135
        $form = new Form('test');
136
        $form->setInputs(new \Adapter());
137
        $form->addText('foo', 'first');
138
        $form->addText('bar', 'second');
139
        $form->setLayout('table');
140
        $form->setLayout('inlineTable');
141
        $this->assertNotEmpty($form->render());
142
    }
143
}
144