1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DmCommonTest\Form; |
4
|
|
|
|
5
|
|
|
use DmCommon\Form\BaseForm; |
6
|
|
|
|
7
|
|
|
class BaseFormTest extends \PHPUnit_Framework_TestCase |
8
|
|
|
{ |
9
|
|
|
/** @var BaseForm|\PHPUnit_Framework_MockObject_MockObject */ |
10
|
|
|
protected $sut; |
11
|
|
|
|
12
|
|
|
public function setUp() |
13
|
|
|
{ |
14
|
|
|
$sut = $this->getMockForAbstractClass('DmCommon\Form\BaseForm'); |
15
|
|
|
|
16
|
|
|
$sut->setData([]); |
17
|
|
|
|
18
|
|
|
$inputFilter = $this->getMock('Zend\InputFilter\InputFilter', ['setData', 'setValidationGroup', 'isValid']); |
19
|
|
|
$inputFilter->expects($this->any())->method('isValid')->will($this->returnValue(true)); |
20
|
|
|
|
21
|
|
|
$sut->setInputFilter($inputFilter); |
22
|
|
|
|
23
|
|
|
$this->sut = $sut; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @covers DmCommon\Form\BaseForm |
28
|
|
|
*/ |
29
|
|
|
public function testGetValidationClassReturnsGivenClassIfNotYetValidated() |
30
|
|
|
{ |
31
|
|
|
$givenClass = 'foo'; |
32
|
|
|
|
33
|
|
|
$elementMock = $this->getMock('Zend\Form\Element', ['getMessages']); |
34
|
|
|
|
35
|
|
|
$elementMock->expects($this->never())->method('getMessages'); |
36
|
|
|
|
37
|
|
|
$actualResult = $this->sut->getValidationClass($elementMock, $givenClass); |
38
|
|
|
|
39
|
|
|
$this->assertEquals($givenClass, $actualResult); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @covers DmCommon\Form\BaseForm |
44
|
|
|
*/ |
45
|
|
|
public function testGetValidationClassAddsErrorClassToGivenClassIfThereAreMessages() |
46
|
|
|
{ |
47
|
|
|
$givenClass = 'foo'; |
48
|
|
|
|
49
|
|
|
$elementMock = $this->getMock('Zend\Form\Element', ['getMessages']); |
50
|
|
|
|
51
|
|
|
$elementMock->expects($this->once())->method('getMessages')->will($this->returnValue(['bar' => []])); |
52
|
|
|
|
53
|
|
|
if (!$this->sut->isValid()) { |
54
|
|
|
$this->fail(print_r($this->sut->getMessages(), true)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$actualResult = $this->sut->getValidationClass($elementMock, $givenClass); |
58
|
|
|
|
59
|
|
|
$this->assertEquals($givenClass . ' has-error', $actualResult); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @covers DmCommon\Form\BaseForm |
64
|
|
|
*/ |
65
|
|
|
public function testGetValidationClassAddsSuccessClassToGivenClassIfThereAreNoMessages() |
66
|
|
|
{ |
67
|
|
|
$givenClass = 'foo'; |
68
|
|
|
|
69
|
|
|
$elementMock = $this->getMock('Zend\Form\Element', ['getMessages']); |
70
|
|
|
|
71
|
|
|
$elementMock->expects($this->once())->method('getMessages')->will($this->returnValue([])); |
72
|
|
|
|
73
|
|
|
if (!$this->sut->isValid()) { |
74
|
|
|
$this->fail(print_r($this->sut->getMessages(), true)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$actualResult = $this->sut->getValidationClass($elementMock, $givenClass); |
78
|
|
|
|
79
|
|
|
$this->assertEquals($givenClass . ' has-success', $actualResult); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|