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

FormTest::testGetElementByName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 6
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
        $form->getUid() => 1,
63
        'other_custom_data' => 123,
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->handle(new FormData('post', []));
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->handle(new FormData('post', [
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->handle(new FormData('post', [$form->getUid() => 1]));
141
      $this->assertEquals(true, $form->isSubmitted());
142
    }
143
144
145
    /**
146
     *
147
     */
148
    public function testValidateWithoutSubmit() {
149
      $form = new Form();
150
      $form->setName('test-form');
151
      $this->assertFalse($form->isValid());
152
      $this->assertFalse($form->isSubmitted());
153
    }
154
155
156
    /**
157
     *
158
     */
159
    public function testHandleRequestContext() {
160
      $exception = null;
0 ignored issues
show
Unused Code introduced by
$exception is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
161
162
      $form = new Form();
163
      $form->setName('test-form');
164
      $form->handle(new FormData('post', [
165
        $form->getUid() => 1,
166
        'other_custom_data' => 123,
167
        'email' => 'test@test',
168
      ]));
169
170
      $this->assertTrue($form->isSubmitted());
171
      $this->assertTrue($form->isValid());
172
173
      $form->handle(new FormData('post', []));
174
      $this->assertFalse($form->isSubmitted());
175
    }
176
177
178
    /**
179
     * @expectedException \Exception
180
     */
181
    public function testAddElementsWithSameNames() {
182
      $form = new Form();
183
      $form->addElement((new Input())->setName('test'));
184
185
186
      $this->assertCount(1, $form->getElements());
187
188
      $form->addElement((new Input())->setName('test'));
189
    }
190
191
192
    /**
193
     *
194
     */
195
    public function testSetElementsWithSameNames() {
196
      $form = new Form();
197
      $form->setElement((new Input())->setName('test')->setValue('first'));
198
199
200
      $elements = $form->getElements();
201
      $this->assertCount(1, $elements);
202
      $this->assertEquals('first', $elements['test']->getValue());
203
204
      $form->setElement((new Input())->setName('test')->setValue('second'));
205
206
      $elements = $form->getElements();
207
      $this->assertCount(1, $elements);
208
      $this->assertEquals('second', $elements['test']->getValue());
209
210
    }
211
212
213
    public function testFormValidationCache() {
214
      $form = new Form();
215
      $form->setName('user_registration');
216
217
218
      $element = (new Input())->setName('test')->setValue('first');
219
220
      $checkedItemsNum = 0;
221
222
      $element->addValidator(new CallBackValidator(function ($value) use (&$checkedItemsNum) {
223
        $checkedItemsNum++;
224
225
        return !empty($value);
226
      }));
227
228
      $form->setElement($element);
229
      # emulate form submit
230
      $form->handle(new FormData('post', [
231
        $form->getUid() => '1',
232
        'test' => '123',
233
      ]));
234
235
      $this->assertTrue($form->isValid());
236
      $this->assertEquals(1, $checkedItemsNum);
237
      $this->assertTrue($form->isValid());
238
      $this->assertTrue($form->isValid());
239
240
      $this->assertEquals(1, $checkedItemsNum);
241
242
    }
243
244
245
    public function testRenderStartEnd() {
246
      $form = new Form();
247
      $form->hidden('test', '123');
248
      $start = $form->renderStart();
249
      $this->assertContains('<input type="hidden" name="test" value="123"', $start);
250
251
      $this->assertEquals('</form>', $form->renderEnd());
252
    }
253
254
255
    public function testRenderElements() {
256
      $form = new Form();
257
      $form->textarea('text', '123');
258
      $this->assertContains('<dl><dt>123</dt><dd><textarea name="text" ></textarea></dd></dl>', $form->render());
259
    }
260
261
262
    /**
263
     * @expectedException \InvalidArgumentException
264
     */
265
    public function testGetElementByInvalidName() {
266
      $form = new Form();
267
      $form->getElement('email');
268
    }
269
270
271
    public function testGetElementByName() {
272
      $form = new Form();
273
      $form->input('email');
274
      $form->textarea('desc');
275
276
      $this->assertInstanceOf(Input::class, $form->getElement('email'));
277
      $this->assertInstanceOf(TextArea::class, $form->getElement('desc'));
278
    }
279
280
281
    public function testFormValidationErrors() {
282
      $form = new Form();
283
      $form->input('name')->addValidator((new Required())->setError('name input error'));
284
      $form->input('email')->addValidator((new Required())->setError('email input error'));
285
286
      $form->handle(new FormData('post', [$form->getUid() => 1]));
287
      $this->assertFalse($form->isValid());
288
      $this->assertEquals(['name input error', 'email input error'], $form->getErrors());
289
290
      $form->handle(new FormData('post', [
291
        $form->getUid() => 1,
292
        'email' => '[email protected]'
293
      ]));
294
      $this->assertFalse($form->isValid());
295
      $this->assertEquals(['name input error'], $form->getErrors());
296
297
      $form->handle(new FormData('post', [
298
        $form->getUid() => 1,
299
        'name' => 'testName',
300
        'email' => '[email protected]'
301
      ]));
302
      $this->assertTrue($form->isValid());
303
      $this->assertEquals([], $form->getErrors());
304
    }
305
306
307
    public function testReSetData() {
308
      $form = new Form();
309
      $form->input('name');
310
      $form->input('email');
311
      $form->input('age');
312
313
      $form->handle(new FormData('post', [
314
        'email' => '[email protected]',
315
        'name' => 'petro'
316
      ]));
317
      $this->assertEquals('[email protected]', $form->getElements()['email']->getValue());
318
      $this->assertEquals(['email' => '[email protected]', 'name' => 'petro'], $form->getData());
319
320
      $form->handle(new FormData('post', [
321
        'name' => 'stepan'
322
      ]));
323
      $this->assertEquals(null, $form->getElement('email')->getValue());
324
      $this->assertEquals('stepan', $form->getElement('name')->getValue());
325
    }
326
327
328
    public function testHandleRequest() {
329
      $form = new Form();
330
      $form->addElement((new Input())->setName('email'));
331
      $form->addElement((new TextArea())->setName('description'));
332
      $form->setMethod('post');
333
334
      $form->handle(new FormData('post', [
335
        $form->getUid() => 1,
336
        'email' => '[email protected]',
337
        'description' => 'some description text'
338
      ]));
339
      $this->assertTrue($form->isSubmitted());
340
      $this->assertEquals('[email protected]', $form->getElement('email')->getValue());
341
      $this->assertEquals('some description text', $form->getElement('description')->getValue());
342
343
      $form->handle(new FormData('post', [
344
        $form->getUid() => 1,
345
        'email' => '[email protected]',
346
      ]));
347
      $this->assertTrue($form->isSubmitted());
348
      $this->assertEquals('[email protected]', $form->getElement('email')->getValue());
349
      $this->assertNull($form->getElement('description')->getValue());
350
351
      $form->handle(new FormData('post', []));
352
      $this->assertFalse($form->isSubmitted());
353
      $this->assertNull($form->getElement('email')->getValue());
354
      $this->assertNull($form->getElement('description')->getValue());
355
356
    }
357
358
  }