Completed
Push — master ( 250c3f...5d0c73 )
by Shcherbak
15:55
created

FormTest::testIsValidFlush()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
  namespace Tests\Form\Form;
4
5
  use Fiv\Form\Element\Input;
6
  use Fiv\Form\Form;
7
  use Fiv\Form\Validator\CallBackValidator;
8
9
  /**
10
   * @package Tests\Form\Form
11
   */
12
  class FormTest extends \Tests\Form\MainTestCase {
13
14
    /**
15
     *
16
     */
17
    public function testIsValidFlush() {
18
      $form = new Form();
19
      $form->input('email');
20
21
      $form->setData([
22
        $form->getUid() => 1,
23
        'email' => 'test@test',
24
      ]);
25
26
      $this->assertTrue($form->isValid());
27
28
      $form->setData([]);
29
30
      $this->assertFalse($form->isValid());
31
    }
32
33
34
    /**
35
     *
36
     */
37
    public function testUid() {
38
      $form = new Form();
39
40
      $this->assertEquals(32, strlen($form->getUid()));
41
42
      $form->setName('test');
43
      $this->assertEquals('test', $form->getUid());
44
    }
45
46
47
    /**
48
     *
49
     */
50
    public function testGetElements() {
51
      $form = new Form();
52
      $this->assertEmpty($form->getElements());
53
    }
54
55
56
    /**
57
     *
58
     */
59
    public function testFormMethods() {
60
      $form = new Form();
61
      $this->assertEquals('post', $form->getMethod());
62
63
      $form->setMethod('POST');
64
      $this->assertEquals('post', $form->getMethod());
65
66
      $form->setMethod('get');
67
      $this->assertEquals('get', $form->getMethod());
68
69
      $form->setMethod('put');
70
      $this->assertEquals('put', $form->getMethod());
71
72
      $form->setMethod(false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
73
      $this->assertEquals(null, $form->getMethod());
74
75
      $form->setAttribute('method', 'test');
76
      $this->assertEquals('test', $form->getMethod());
77
78
    }
79
80
81
    /**
82
     *
83
     */
84
    public function testFormRender() {
85
      $form = new Form();
86
      $this->assertContains('method="post"', $form->render());
87
88
      $form = new Form();
89
      $form->setMethod('get');
90
      $this->assertContains('method="get"', $form->render());
91
    }
92
93
94
    /**
95
     *
96
     */
97
    public function testIsSubmittedFalse() {
98
      $form = new Form();
99
      $form->setData([]);
100
      $this->assertEquals(false, $form->isSubmitted());
101
    }
102
103
104
    /**
105
     *
106
     */
107
    public function testIsSubmittedTrue() {
108
      $form = new Form();
109
      $form->setName('test-form');
110
      $form->setData([
111
        'test-form' => 1,
112
      ]);
113
      $this->assertEquals(true, $form->isSubmitted());
114
115
      $form = new Form();
116
      $form->submit('test-submit', 'test-value');
117
      $form->setData([
118
        $form->getUid() => 1,
119
      ]);
120
      $this->assertEquals(true, $form->isSubmitted());
121
    }
122
123
124
    /**
125
     * @expectedException \Exception
126
     */
127
    public function testThrowExceptionWithoutData() {
128
      $form = new Form();
129
      $form->setName('test-form');
130
      $form->isValid();
131
    }
132
133
134
    /**
135
     *
136
     */
137
    public function testFormSetData() {
138
      $exception = null;
139
140
      try {
141
        $form = new Form();
142
        $form->setName('test-form');
143
        $form->setData(null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array|object<Iterator>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
144
      } catch (\Exception $e) {
145
        $exception = $e;
146
      }
147
148
      $this->assertNotEmpty($exception, 'Should throw exception if data not array or Iterator!');
149
    }
150
151
152
    /**
153
     * @expectedException \Exception
154
     */
155
    public function testAddElementsWithSameNames() {
156
      $form = new Form();
157
      $form->addElement((new Input())->setName('test'));
158
159
160
      $this->assertCount(1, $form->getElements());
161
162
      $form->addElement((new Input())->setName('test'));
163
    }
164
165
166
    /**
167
     *
168
     */
169
    public function testSetElementsWithSameNames() {
170
      $form = new Form();
171
      $form->setElement((new Input())->setName('test')->setValue('first'));
172
173
174
      $elements = $form->getElements();
175
      $this->assertCount(1, $elements);
176
      $this->assertEquals('first', $elements['test']->getValue());
177
178
      $form->setElement((new Input())->setName('test')->setValue('second'));
179
180
      $elements = $form->getElements();
181
      $this->assertCount(1, $elements);
182
      $this->assertEquals('second', $elements['test']->getValue());
183
184
    }
185
186
187
    public function testFormValidationCache() {
188
      $form = new Form();
189
      $form->setName('user_registration');
190
191
192
      $element = (new Input())->setName('test')->setValue('first');
193
194
      $checkedItemsNum = 0;
195
196
      $element->addValidator(new CallBackValidator(function ($value) use (&$checkedItemsNum) {
197
        $checkedItemsNum++;
198
199
        return !empty($value);
200
      }));
201
202
      $form->setElement($element);
203
      # emulate form submit
204
      $form->setData([
205
        $form->getUid() => '1',
206
        'test' => '123',
207
      ]);
208
209
      $this->assertTrue($form->isValid());
210
      $this->assertEquals(1, $checkedItemsNum);
211
      $this->assertTrue($form->isValid());
212
      $this->assertTrue($form->isValid());
213
214
      $this->assertEquals(1, $checkedItemsNum);
215
216
    }
217
218
219
    public function testRenderStartEnd() {
220
      $form = new Form();
221
      $form->hidden('test', '123');
222
      $start = $form->renderStart();
223
      $this->assertContains('<input type="hidden" name="test" value="123"', $start);
224
225
      $this->assertEquals('</form>', $form->renderEnd());
226
    }
227
228
229
    public function testRenderElements() {
230
      $form = new Form();
231
      $form->textarea('text', '123');
232
      $this->assertContains('<dl><dt>123</dt><dd><textarea name="text" ></textarea></dd></dl>', $form->render());
233
    }
234
235
236
  }