Completed
Push — master ( 3cc3b2...768def )
by Vitaliy
01:53
created

FormTest::testFormValidationCache()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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