1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PluginSimpleValidate\Tests\unit; |
4
|
|
|
|
5
|
|
|
use PluginSimpleValidate\Field; |
6
|
|
|
|
7
|
|
|
class FieldTest extends Base |
8
|
|
|
{ |
9
|
|
|
public function test_construct() |
10
|
|
|
{ |
11
|
|
|
$field = new Field('name', 'value'); |
12
|
|
|
$this->assertEquals('name', $field->getName()); |
13
|
|
|
$this->assertEquals('value', $field->getValue()); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public function test_is_required(){ |
17
|
|
|
$field = (new Field('username', ''))->required(); |
18
|
|
|
$this->assertFalse($field->isValid($this->language)); |
19
|
|
|
$this->assertEquals(['field is required'], $field->getErrors()); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function test_is_less_than(){ |
23
|
|
|
$field = (new Field('score', 90.90))->lessThan(90.87); |
24
|
|
|
$this->assertFalse($field->isValid($this->language)); |
25
|
|
|
$this->assertEquals(['field must be less than 90.87'], $field->getErrors()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function test_equal(){ |
29
|
|
|
$field = (new Field('confirm_password', 'newpassword'))->equal('oldpassword'); |
30
|
|
|
$this->assertFalse($field->isValid($this->language)); |
31
|
|
|
$this->assertEquals(['field must be equal with "oldpassword"'], $field->getErrors()); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function test_between(){ |
35
|
|
|
$field = (new Field('grade_b', 86))->between(79, 86); |
36
|
|
|
$this->assertFalse($field->isValid($this->language)); |
37
|
|
|
$this->assertEquals(['field must be greater than 79 or less than 86'], $field->getErrors()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function test_field_multi_rules(){ |
41
|
|
|
$field = (new Field('email', ''))->required()->validEmail(); |
42
|
|
|
$this->assertFalse($field->isValid($this->language)); |
43
|
|
|
$this->assertEquals(['field is required', 'field must be a valid email address'], $field->getErrors()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function test_is_true(){ |
47
|
|
|
$field = (new Field('value', 5 < 4))->isTrue('comparison error'); |
48
|
|
|
$this->assertFalse($field->isValid($this->language)); |
49
|
|
|
$this->assertEquals(['comparison error'], $field->getErrors()); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|