Completed
Pull Request — master (#32)
by Vitaliy
02:58
created

FormTest::testHandleRequest()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 33
rs 8.8571
cc 1
eloc 25
nc 1
nop 0
1
<?php
2
3
  namespace Tests\Fiv\Form;
4
5
  use Fiv\Form\Element\Input;
6
  use Fiv\Form\Element\TextArea;
7
  use Fiv\Form\Form;
8
  use Fiv\Form\FormData;
9
  use Fiv\Form\Validator\CallBackValidator;
10
  use Fiv\Form\Validator\Required;
11
12
  /**
13
   * @package Tests\Form\Form
14
   */
15
  class FormTest extends \PHPUnit_Framework_TestCase {
16
17
    /**
18
     *
19
     */
20
    public function testIsValidFlush() {
21
      $form = new Form();
22
      $form->input('email');
23
24
      $form->handle(new FormData('post', [
25
        $form->getUid() => 1,
26
        'email' => 'test@test',
27
      ]));
28
      $this->assertTrue($form->isValid());
29
30
      $form->handle(new FormData('post', []));
31
      $this->assertFalse($form->isValid());
32
    }
33
34
35
    /**
36
     *
37
     */
38
    public function testUid() {
39
      $form = new Form();
40
41
      $this->assertEquals(32, strlen($form->getUid()));
42
43
      $form->setName('test');
44
      $this->assertEquals('test', $form->getUid());
45
    }
46
47
48
    /**
49
     *
50
     */
51
    public function testData() {
52
      $form = new Form();
53
      $form->input('email');
54
55
      $form->handle(new FormData('post', [
56
        $form->getUid() => 1,
57
        'other_custom_data' => 123,
58
        'email' => 'test@test',
59
      ]));
60
61
      $this->assertEquals([
62
        'email' => 'test@test',
63
      ], $form->getData());
64
65
    }
66
67
68
    /**
69
     *
70
     */
71
    public function testGetElements() {
72
      $form = new Form();
73
      $this->assertEmpty($form->getElements());
74
    }
75
76
77
    /**
78
     *
79
     */
80
    public function testFormMethods() {
81
      $form = new Form();
82
      $this->assertEquals('post', $form->getMethod());
83
84
      $form->setMethod('POST');
85
      $this->assertEquals('post', $form->getMethod());
86
87
      $form->setMethod('get');
88
      $this->assertEquals('get', $form->getMethod());
89
90
      $form->setMethod('put');
91
      $this->assertEquals('put', $form->getMethod());
92
93
      $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...
94
      $this->assertEquals(null, $form->getMethod());
95
96
      $form->setAttribute('method', 'test');
97
      $this->assertEquals('test', $form->getMethod());
98
99
    }
100
101
102
    /**
103
     *
104
     */
105
    public function testFormRender() {
106
      $form = new Form();
107
      $this->assertContains('method="post"', $form->render());
108
109
      $form = new Form();
110
      $form->setMethod('get');
111
      $this->assertContains('method="get"', $form->render());
112
    }
113
114
115
    /**
116
     *
117
     */
118
    public function testIsSubmittedFalse() {
119
      $form = new Form();
120
      $form->handle(new FormData('post', []));
121
      $this->assertEquals(false, $form->isSubmitted());
122
    }
123
124
125
    /**
126
     *
127
     */
128
    public function testIsSubmittedTrue() {
129
      $form = new Form();
130
      $form->setName('test-form');
131
      $form->handle(new FormData('post', [
132
        'test-form' => 1,
133
      ]));
134
      $this->assertEquals(true, $form->isSubmitted());
135
136
      $form = new Form();
137
      $form->submit('test-submit', 'test-value');
138
      $form->handle(new FormData('post', [$form->getUid() => 1]));
139
      $this->assertEquals(true, $form->isSubmitted());
140
    }
141
142
143
    /**
144
     *
145
     */
146
    public function testValidateWithoutSubmit() {
147
      $form = new Form();
148
      $form->setName('test-form');
149
      $this->assertFalse($form->isValid());
150
      $this->assertFalse($form->isSubmitted());
151
    }
152
153
154
    /**
155
     *
156
     */
157
    public function testHandleRequestContext() {
158
      $form = new Form();
159
      $form->setName('test-form');
160
      $form->handle(new FormData('post', [
161
        $form->getUid() => 1,
162
        'other_custom_data' => 123,
163
        'email' => 'test@test',
164
      ]));
165
166
      $this->assertTrue($form->isSubmitted());
167
      $this->assertTrue($form->isValid());
168
169
      $form->handle(new FormData('post', []));
170
      $this->assertFalse($form->isSubmitted());
171
    }
172
173
174
    /**
175
     * @expectedException \Exception
176
     */
177
    public function testAddElementsWithSameNames() {
178
      $form = new Form();
179
      $form->addElement((new Input())->setName('test'));
180
181
182
      $this->assertCount(1, $form->getElements());
183
184
      $form->addElement((new Input())->setName('test'));
185
    }
186
187
188
    /**
189
     *
190
     */
191
    public function testSetElementsWithSameNames() {
192
      $form = new Form();
193
      $form->setElement((new Input())->setName('test')->setValue('first'));
194
195
196
      $elements = $form->getElements();
197
      $this->assertCount(1, $elements);
198
      $this->assertEquals('first', $elements['test']->getValue());
199
200
      $form->setElement((new Input())->setName('test')->setValue('second'));
201
202
      $elements = $form->getElements();
203
      $this->assertCount(1, $elements);
204
      $this->assertEquals('second', $elements['test']->getValue());
205
206
    }
207
208
209
    public function testFormValidationCache() {
210
      $form = new Form();
211
      $form->setName('user_registration');
212
213
214
      $element = (new Input())->setName('test')->setValue('first');
215
216
      $checkedItemsNum = 0;
217
218
      $element->addValidator(new CallBackValidator(function ($value) use (&$checkedItemsNum) {
219
        $checkedItemsNum++;
220
221
        return !empty($value);
222
      }));
223
224
      $form->setElement($element);
225
      # emulate form submit
226
      $form->handle(new FormData('post', [
227
        $form->getUid() => '1',
228
        'test' => '123',
229
      ]));
230
231
      $this->assertTrue($form->isValid());
232
      $this->assertEquals(1, $checkedItemsNum);
233
      $this->assertTrue($form->isValid());
234
      $this->assertTrue($form->isValid());
235
236
      $this->assertEquals(1, $checkedItemsNum);
237
238
    }
239
240
241
    public function testRenderStartEnd() {
242
      $form = new Form();
243
      $form->hidden('test', '123');
244
      $start = $form->renderStart();
245
      $this->assertContains('<input type="hidden" name="test" value="123"', $start);
246
247
      $this->assertEquals('</form>', $form->renderEnd());
248
    }
249
250
251
    public function testRenderElements() {
252
      $form = new Form();
253
      $form->textarea('text', '123');
254
      $this->assertContains('<dl><dt>123</dt><dd><textarea name="text" ></textarea></dd></dl>', $form->render());
255
    }
256
257
258
    /**
259
     * @expectedException \InvalidArgumentException
260
     */
261
    public function testGetElementByInvalidName() {
262
      $form = new Form();
263
      $form->getElement('email');
264
    }
265
266
267
    public function testGetElementByName() {
268
      $form = new Form();
269
      $form->input('email');
270
      $form->textarea('desc');
271
272
      $this->assertInstanceOf(Input::class, $form->getElement('email'));
273
      $this->assertInstanceOf(TextArea::class, $form->getElement('desc'));
274
    }
275
276
277
    public function testFormValidationErrors() {
278
      $form = new Form();
279
      $form->input('name')->addValidator((new Required())->setError('name input error'));
280
      $form->input('email')->addValidator((new Required())->setError('email input error'));
281
282
      $form->handle(new FormData('post', [$form->getUid() => 1]));
283
      $this->assertFalse($form->isValid());
284
      $this->assertEquals(['name input error', 'email input error'], $form->getErrors());
285
286
      $form->handle(new FormData('post', [
287
        $form->getUid() => 1,
288
        'email' => '[email protected]',
289
      ]));
290
      $this->assertFalse($form->isValid());
291
      $this->assertEquals(['name input error'], $form->getErrors());
292
293
      $form->handle(new FormData('post', [
294
        $form->getUid() => 1,
295
        'name' => 'testName',
296
        'email' => '[email protected]',
297
      ]));
298
      $this->assertTrue($form->isValid());
299
      $this->assertEquals([], $form->getErrors());
300
    }
301
302
303
    public function testReSetData() {
304
      $form = new Form();
305
      $form->input('name');
306
      $form->input('email');
307
      $form->input('age');
308
309
      $form->handle(new FormData('post', [
310
        $form->getUid() => '1',
311
        'name' => 'petro',
312
        'email' => '[email protected]',
313
      ]));
314
      $this->assertEquals('[email protected]', $form->getElements()['email']->getValue());
315
      $this->assertEquals(
316
        [
317
          'name' => 'petro',
318
          'email' => '[email protected]',
319
          'age' => ''
320
        ],
321
        $form->getData()
322
      );
323
324
      $form->handle(new FormData('post', [
325
        $form->getUid() => '1',
326
        'name' => 'stepan',
327
      ]));
328
      $this->assertEquals(null, $form->getElement('email')->getValue());
329
      $this->assertEquals('stepan', $form->getElement('name')->getValue());
330
    }
331
332
333
    public function testHandleRequest() {
334
      $form = new Form();
335
      $form->addElement((new Input())->setName('email'));
336
      $form->addElement((new TextArea())->setName('description'));
337
      $form->setMethod('post');
338
339
      $form->handle(new FormData('post', [
340
        $form->getUid() => 1,
341
        'email' => '[email protected]',
342
        'description' => 'some description text',
343
      ]));
344
      $this->assertTrue($form->isSubmitted());
345
      $this->assertEquals('[email protected]', $form->getElement('email')->getValue());
346
      $this->assertEquals('some description text', $form->getElement('description')->getValue());
347
348
      $form->handle(new FormData('post', [
349
        $form->getUid() => 1,
350
        'email' => '[email protected]',
351
      ]));
352
      $this->assertTrue($form->isSubmitted());
353
      $this->assertEquals('[email protected]', $form->getElement('email')->getValue());
354
      $this->assertNull($form->getElement('description')->getValue());
355
356
      $form->handle(new FormData('post', [
357
        $form->getUid() => '1',
358
      ]));
359
      $this->assertTrue($form->isSubmitted());
360
      $this->assertNull($form->getElement('email')->getValue());
361
      $this->assertNull($form->getElement('description')->getValue());
362
363
      $form->handle(new FormData('post', []));
364
      $this->assertFalse($form->isSubmitted());
365
    }
366
367
  }