This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
/
StepTest.php
207 lines (185 loc) · 5.45 KB
/
StepTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Validator
*/
namespace ZendTest\Validator;
use Zend\Validator;
/**
* @category Zend
* @package Zend_Validator
* @subpackage UnitTests
* @group Zend_Validator
*/
class StepTest extends \PHPUnit_Framework_TestCase
{
/**
* Step object
*
* @var \Zend\Validator\Step
*/
protected $_validator;
/**
* Creates a new Zend\Validator\Step object for each test method
*
* @return void
*/
public function setUp()
{
$this->_validator = new Validator\Step();
}
/**
* Ensures that the validator follows expected behavior
*
* @return void
*/
public function testBasic()
{
// By default, baseValue == 0 and step == 1
$valuesExpected = array(
array(1.00, true),
array(0.00, true),
array(2, true),
array(3, true),
array(2.1, false),
array('2', true),
array('1', true),
array('1.2', false),
array(1.01, false),
array('not a scalar', false)
);
foreach ($valuesExpected as $element) {
$this->assertEquals($element[1], $this->_validator->isValid($element[0]),
'Test failed with ' . var_export($element, 1));
}
}
public function testDecimalBaseValue()
{
$valuesExpected = array(
array(1.1, false),
array(0.1, true),
array(2.1, true),
array(3.1, false),
array('2.1', true),
array('1.1', false),
array(1.11, false),
array('not a scalar', false)
);
$validator = new Validator\Step(array(
'baseValue' => 0.1,
'step' => 2
));
foreach ($valuesExpected as $element) {
$this->assertEquals($element[1], $validator->isValid($element[0]),
'Test failed with ' . var_export($element, 1));
}
}
public function testDecimalStep()
{
$valuesExpected = array(
array(1.1, false),
array(0.1, false),
array(2.1, true),
array(3.1, false),
array(4.2, true),
array(6.3, true),
array(8.4, true),
array(10.5, true),
array(12.6, true),
array(14.7, true),
array(16.8, true),
array(18.9, true),
array(21.0, true),
array('2.1', true),
array('1.1', false),
array(1.11, false),
array('not a scalar', false)
);
$validator = new Validator\Step(array(
'baseValue' => 0,
'step' => 2.1
));
foreach ($valuesExpected as $element) {
$this->assertEquals($element[1], $validator->isValid($element[0]),
'Test failed with ' . var_export($element, 1));
}
}
public function testDecimalStep2()
{
$valuesExpected = array(
array(0.01, true),
array(0.02, true),
array(0.03, true),
array(0.04, true),
array(0.05, true),
array(0.06, true),
array(0.07, true),
array(0.08, true),
array(0.09, true),
array(0.001, false),
array(0.002, false),
array(0.003, false),
array(0.004, false),
array(0.005, false),
array(0.006, false),
array(0.007, false),
array(0.008, false),
array(0.009, false)
);
$validator = new Validator\Step(array(
'baseValue' => 0,
'step' => 0.01
));
foreach ($valuesExpected as $element) {
$this->assertEquals($element[1], $validator->isValid($element[0]),
'Test failed with ' . var_export($element, 1));
}
}
/**
* Ensures that getMessages() returns expected default value
*
* @return void
*/
public function testGetMessages()
{
$this->assertEquals(array(), $this->_validator->getMessages());
}
/**
* Ensures that set/getBaseValue() works
*/
public function testCanSetBaseValue()
{
$this->_validator->setBaseValue(2);
$this->assertEquals('2', $this->_validator->getBaseValue());
}
/**
* Ensures that set/getStep() works
*/
public function testCanSetStepValue()
{
$this->_validator->setStep(2);
$this->assertEquals('2', $this->_validator->getStep());
}
public function testEqualsMessageTemplates()
{
$validator = new Validator\Step();
$this->assertAttributeEquals($validator->getOption('messageTemplates'),
'messageTemplates', $validator);
}
public function testSetStepFloat()
{
$step = 0.01;
$this->_validator->setStep($step);
$this->assertAttributeSame($step, 'step', $this->_validator);
}
public function testSetStepString()
{
$step = '0.01';
$this->_validator->setStep($step);
$this->assertAttributeSame((float) $step, 'step', $this->_validator);
}
}