Completed
Pull Request — master (#28)
by Vitaliy
03:02
created

FormTest::testHandleRequest()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 29
rs 8.8571
cc 1
eloc 22
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\RequestContext;
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->setData([
0 ignored issues
show
Deprecated Code introduced by
The method Fiv\Form\Form::setData() has been deprecated.

This method has been deprecated.

Loading history...
25
        $form->getUid() => 1,
26
        'email' => 'test@test',
27
      ]);
28
29
      $this->assertTrue($form->isValid());
30
31
      $form->setData([]);
0 ignored issues
show
Deprecated Code introduced by
The method Fiv\Form\Form::setData() has been deprecated.

This method has been deprecated.

Loading history...
32
33
      $this->assertFalse($form->isValid());
34
    }
35
36
37
    /**
38
     *
39
     */
40
    public function testUid() {
41
      $form = new Form();
42
43
      $this->assertEquals(32, strlen($form->getUid()));
44
45
      $form->setName('test');
46
      $this->assertEquals('test', $form->getUid());
47
    }
48
49
50
    /**
51
     *
52
     */
53
    public function testData() {
54
      $form = new Form();
55
      $form->input('email');
56
57
      $form->setData([
0 ignored issues
show
Deprecated Code introduced by
The method Fiv\Form\Form::setData() has been deprecated.

This method has been deprecated.

Loading history...
58
        $form->getUid() => 1,
59
        'other_custom_data' => 123,
60
        'email' => 'test@test',
61
      ]);
62
63
      $this->assertEquals([
64
        'email' => 'test@test',
65
      ], $form->getData());
66
67
    }
68
69
70
    /**
71
     *
72
     */
73
    public function testGetElements() {
74
      $form = new Form();
75
      $this->assertEmpty($form->getElements());
76
    }
77
78
79
    /**
80
     *
81
     */
82
    public function testFormMethods() {
83
      $form = new Form();
84
      $this->assertEquals('post', $form->getMethod());
85
86
      $form->setMethod('POST');
87
      $this->assertEquals('post', $form->getMethod());
88
89
      $form->setMethod('get');
90
      $this->assertEquals('get', $form->getMethod());
91
92
      $form->setMethod('put');
93
      $this->assertEquals('put', $form->getMethod());
94
95
      $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...
96
      $this->assertEquals(null, $form->getMethod());
97
98
      $form->setAttribute('method', 'test');
99
      $this->assertEquals('test', $form->getMethod());
100
101
    }
102
103
104
    /**
105
     *
106
     */
107
    public function testFormRender() {
108
      $form = new Form();
109
      $this->assertContains('method="post"', $form->render());
110
111
      $form = new Form();
112
      $form->setMethod('get');
113
      $this->assertContains('method="get"', $form->render());
114
    }
115
116
117
    /**
118
     *
119
     */
120
    public function testIsSubmittedFalse() {
121
      $form = new Form();
122
      $form->setData([]);
0 ignored issues
show
Deprecated Code introduced by
The method Fiv\Form\Form::setData() has been deprecated.

This method has been deprecated.

Loading history...
123
      $this->assertEquals(false, $form->isSubmitted());
124
    }
125
126
127
    /**
128
     *
129
     */
130
    public function testIsSubmittedTrue() {
131
      $form = new Form();
132
      $form->setName('test-form');
133
      $form->setData([
0 ignored issues
show
Deprecated Code introduced by
The method Fiv\Form\Form::setData() has been deprecated.

This method has been deprecated.

Loading history...
134
        'test-form' => 1,
135
      ]);
136
      $this->assertEquals(true, $form->isSubmitted());
137
138
      $form = new Form();
139
      $form->submit('test-submit', 'test-value');
140
      $form->setData([
0 ignored issues
show
Deprecated Code introduced by
The method Fiv\Form\Form::setData() has been deprecated.

This method has been deprecated.

Loading history...
141
        $form->getUid() => 1,
142
      ]);
143
      $this->assertEquals(true, $form->isSubmitted());
144
    }
145
146
147
    /**
148
     *
149
     */
150
    public function testValidateWithoutSubmit() {
151
      $form = new Form();
152
      $form->setName('test-form');
153
      $this->assertFalse($form->isValid());
154
      $this->assertFalse($form->isSubmitted());
155
    }
156
157
158
    /**
159
     *
160
     */
161
    public function testFormSetData() {
162
      $exception = null;
163
164
      try {
165
        $form = new Form();
166
        $form->setName('test-form');
167
        $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...
Deprecated Code introduced by
The method Fiv\Form\Form::setData() has been deprecated.

This method has been deprecated.

Loading history...
168
      } catch (\Exception $e) {
169
        $exception = $e;
170
      }
171
172
      $this->assertNotEmpty($exception, 'Should throw exception if data not array or Iterator!');
173
    }
174
175
176
    /**
177
     * @expectedException \Exception
178
     */
179
    public function testAddElementsWithSameNames() {
180
      $form = new Form();
181
      $form->addElement((new Input())->setName('test'));
182
183
184
      $this->assertCount(1, $form->getElements());
185
186
      $form->addElement((new Input())->setName('test'));
187
    }
188
189
190
    /**
191
     *
192
     */
193
    public function testSetElementsWithSameNames() {
194
      $form = new Form();
195
      $form->setElement((new Input())->setName('test')->setValue('first'));
196
197
198
      $elements = $form->getElements();
199
      $this->assertCount(1, $elements);
200
      $this->assertEquals('first', $elements['test']->getValue());
201
202
      $form->setElement((new Input())->setName('test')->setValue('second'));
203
204
      $elements = $form->getElements();
205
      $this->assertCount(1, $elements);
206
      $this->assertEquals('second', $elements['test']->getValue());
207
208
    }
209
210
211
    public function testFormValidationCache() {
212
      $form = new Form();
213
      $form->setName('user_registration');
214
215
216
      $element = (new Input())->setName('test')->setValue('first');
217
218
      $checkedItemsNum = 0;
219
220
      $element->addValidator(new CallBackValidator(function ($value) use (&$checkedItemsNum) {
221
        $checkedItemsNum++;
222
223
        return !empty($value);
224
      }));
225
226
      $form->setElement($element);
227
      # emulate form submit
228
      $form->setData([
0 ignored issues
show
Deprecated Code introduced by
The method Fiv\Form\Form::setData() has been deprecated.

This method has been deprecated.

Loading history...
229
        $form->getUid() => '1',
230
        'test' => '123',
231
      ]);
232
233
      $this->assertTrue($form->isValid());
234
      $this->assertEquals(1, $checkedItemsNum);
235
      $this->assertTrue($form->isValid());
236
      $this->assertTrue($form->isValid());
237
238
      $this->assertEquals(1, $checkedItemsNum);
239
240
    }
241
242
243
    public function testRenderStartEnd() {
244
      $form = new Form();
245
      $form->hidden('test', '123');
246
      $start = $form->renderStart();
247
      $this->assertContains('<input type="hidden" name="test" value="123"', $start);
248
249
      $this->assertEquals('</form>', $form->renderEnd());
250
    }
251
252
253
    public function testRenderElements() {
254
      $form = new Form();
255
      $form->textarea('text', '123');
256
      $this->assertContains('<dl><dt>123</dt><dd><textarea name="text" ></textarea></dd></dl>', $form->render());
257
    }
258
259
260
    /**
261
     * @expectedException \InvalidArgumentException
262
     */
263
    public function testGetElementByInvalidName() {
264
      $form = new Form();
265
      $form->getElement('email');
266
    }
267
268
269
    public function testGetElementByName() {
270
      $form = new Form();
271
      $form->input('email');
272
      $form->textarea('desc');
273
274
      $this->assertInstanceOf(Input::class, $form->getElement('email'));
275
      $this->assertInstanceOf(TextArea::class, $form->getElement('desc'));
276
    }
277
278
279
    public function testFormValidationErrors() {
280
      $form = new Form();
281
      $form->input('name')->addValidator((new Required())->setError('name input error'));
282
      $form->input('email')->addValidator((new Required())->setError('email input error'));
283
284
      $form->setData([$form->getUid() => 1]);
0 ignored issues
show
Deprecated Code introduced by
The method Fiv\Form\Form::setData() has been deprecated.

This method has been deprecated.

Loading history...
285
      $this->assertFalse($form->isValid());
286
      $this->assertEquals(['name input error', 'email input error'], $form->getErrors());
287
288
      $form->setData([$form->getUid() => 1, 'email' => '[email protected]']);
0 ignored issues
show
Deprecated Code introduced by
The method Fiv\Form\Form::setData() has been deprecated.

This method has been deprecated.

Loading history...
289
      $this->assertFalse($form->isValid());
290
      $this->assertEquals(['name input error'], $form->getErrors());
291
292
      $form->setData([$form->getUid() => 1, 'name' => 'testName', 'email' => '[email protected]']);
0 ignored issues
show
Deprecated Code introduced by
The method Fiv\Form\Form::setData() has been deprecated.

This method has been deprecated.

Loading history...
293
      $this->assertTrue($form->isValid());
294
      $this->assertEquals([], $form->getErrors());
295
    }
296
297
298
    public function testReSetData() {
299
      $form = new Form();
300
      $form->input('name');
301
      $form->input('email');
302
      $form->input('age');
303
304
      $form->setData(['email' => '[email protected]', 'name' => 'petro']);
0 ignored issues
show
Deprecated Code introduced by
The method Fiv\Form\Form::setData() has been deprecated.

This method has been deprecated.

Loading history...
305
      $this->assertEquals('[email protected]', $form->getElements()['email']->getValue());
306
      $this->assertEquals(['email' => '[email protected]', 'name' => 'petro'], $form->getData());
307
308
      $form->setData(['name' => 'stepan']);
0 ignored issues
show
Deprecated Code introduced by
The method Fiv\Form\Form::setData() has been deprecated.

This method has been deprecated.

Loading history...
309
      $this->assertEquals(null, $form->getElements()['email']->getValue());
310
      $this->assertEquals('stepan', $form->getElements()['name']->getValue());
311
    }
312
313
314
    public function testHandleRequest() {
315
      $form = new Form();
316
      $form->addElement((new Input())->setName('email'));
317
      $form->addElement((new TextArea())->setName('description'));
318
      $form->setMethod('post');
319
320
      $form->handleRequestContext(new RequestContext('post', [
321
        $form->getUid() => 1,
322
        'email' => '[email protected]',
323
        'description' => 'some description text'
324
      ]));
325
      $this->assertTrue($form->isSubmitted());
326
      $this->assertEquals('[email protected]', $form->getElement('email')->getValue());
327
      $this->assertEquals('some description text', $form->getElement('description')->getValue());
328
329
      $form->handleRequestContext(new RequestContext('post', [
330
        $form->getUid() => 1,
331
        'email' => '[email protected]',
332
      ]));
333
      $this->assertTrue($form->isSubmitted());
334
      $this->assertEquals('[email protected]', $form->getElement('email')->getValue());
335
      $this->assertNull($form->getElement('description')->getValue());
336
337
      $form->handleRequestContext(new RequestContext('post', []));
338
      $this->assertFalse($form->isSubmitted());
339
      $this->assertNull($form->getElement('email')->getValue());
340
      $this->assertNull($form->getElement('description')->getValue());
341
342
    }
343
344
  }