Completed
Pull Request — master (#23)
by Vitaliy
04:08
created

AdvancedFormValidationTest::testFormValidation()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 24
rs 8.9713
cc 1
eloc 19
nc 1
nop 0
1
<?php
2
3
  namespace Tests\Form;
4
5
  use Fiv\Form\Form;
6
7
  /**
8
   *
9
   */
10
  class ExampleMessageForm extends Form {
11
12
    /**
13
     * @return bool
14
     */
15
    public function isValid() {
16
      if (!parent::isValid()) {
17
        return false;
18
      }
19
20
      if (
21
        $this->getElements()['emailFrom']->getValue() == '[email protected]'
22
        and $this->getElements()['emailTo']->getValue() == '[email protected]'
23
        and $this->getElements()['message']->getValue() == 'copy message text'
24
      ) {
25
        $this->validationResult = false;
26
        $this->addError('message duplicate error');
27
      }
28
29
      return $this->validationResult;
30
    }
31
32
  }
33
34
  /**
35
   *
36
   */
37
  class AdvancedFormValidationTest extends \PHPUnit_Framework_TestCase {
38
39
40
    /**
41
     *
42
     */
43
    public function testFormValidation() {
44
      $form = new ExampleMessageForm();
45
      $form->input('emailFrom');
46
      $form->input('emailTo');
47
      $form->input('message');
48
49
      $form->setData([
50
        $form->getUid() => 1,
51
        'emailFrom' => '[email protected]',
52
        'emailTo' => '[email protected]',
53
        'message' => 'new message text',
54
      ]);
55
      $this->assertTrue($form->isValid());
56
      $this->assertEquals([], $form->getErrors());
57
58
      $form->setData([
59
        $form->getUid() => 1,
60
        'emailFrom' => '[email protected]',
61
        'emailTo' => '[email protected]',
62
        'message' => 'copy message text',
63
      ]);
64
      $this->assertFalse($form->isValid());
65
      $this->assertEquals(['message duplicate error'], $form->getErrors());
66
    }
67
68
  }