1
|
|
|
<?php |
2
|
|
|
namespace codeonyii\yii2validators\tests; |
3
|
|
|
|
4
|
|
|
use codeonyii\yii2validators\AtLeastValidator; |
5
|
|
|
use codeonyii\yii2validators\tests\FakeModel; |
6
|
|
|
use yii\base\InvalidConfigException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* This is a temporary trait just created to avoid code duplication |
10
|
|
|
* when testing using PHP 5.6 and 7+ versions in PHPUnit since each |
11
|
|
|
* PHPUnit version will use a different Testcase class and it forces |
12
|
|
|
* us to have two tests classes, one for each version. |
13
|
|
|
* |
14
|
|
|
* PHP 5.6 is now in security updates only, till Dec, 2018. After that |
15
|
|
|
* we can remove tests for 5.6 versions because it will not be more |
16
|
|
|
* supported by PHP team. |
17
|
|
|
* |
18
|
|
|
* @author: Sidney Lins - [email protected] |
19
|
|
|
* @since: 1.2.5 |
20
|
|
|
*/ |
21
|
|
|
trait TemporaryTraitsForTests { |
22
|
|
|
public function testWrongConfigs() |
23
|
|
|
{ |
24
|
|
|
try { |
25
|
|
|
new AtLeastValidator(); |
26
|
|
|
} catch (\Exception $e) { |
27
|
|
|
$this->assertInstanceOf('yii\base\InvalidConfigException', $e, 'Parameter `in` is required.'); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
try { |
31
|
|
|
new AtLeastValidator(['in' => 'attr1']); |
32
|
|
|
} catch (\Exception $e) { |
33
|
|
|
$this->assertInstanceOf('yii\base\InvalidConfigException', $e, 'Parameter `in` should have at least 2 attributes.'); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testWrongData() |
38
|
|
|
{ |
39
|
|
|
$v = new AtLeastValidator(['in' => 'attr1, attr2, attr3']); |
40
|
|
|
$m = new FakeModel(); |
41
|
|
|
|
42
|
|
|
$v->validateAttribute($m, 'attr1'); |
43
|
|
|
$this->assertCount(1, $m->getErrors()); |
|
|
|
|
44
|
|
|
$this->assertTrue($m->hasErrors('attr1')); |
|
|
|
|
45
|
|
|
$this->assertFalse($m->hasErrors('attr2')); |
|
|
|
|
46
|
|
|
$this->assertFalse($m->hasErrors('attr3')); |
47
|
|
|
|
48
|
|
|
$v->validateAttribute($m, 'attr1, attr2'); |
49
|
|
|
$this->assertCount(2, $m->getErrors()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testProperData() |
53
|
|
|
{ |
54
|
|
|
$v = new AtLeastValidator(['in' => 'attr1, attr2, attr3']); |
55
|
|
|
$m = new FakeModel(); |
56
|
|
|
|
57
|
|
|
$m->attr1 = 'something'; |
58
|
|
|
$v->validateAttribute($m, 'attr1'); |
59
|
|
|
$this->assertCount(0, $m->getErrors(), 'Should not exist errors in attr1'); |
60
|
|
|
|
61
|
|
|
$m->resetData(); |
62
|
|
|
$m->attr2 = 'something'; |
63
|
|
|
$v->validateAttribute($m, 'attr1'); |
64
|
|
|
$this->assertCount(0, $m->getErrors(), 'Should not exist errors in attr1'); |
65
|
|
|
|
66
|
|
|
$m->resetData(); |
67
|
|
|
$m->attr3 = 'something'; |
68
|
|
|
$v->validateAttribute($m, 'attr1'); |
69
|
|
|
$this->assertCount(0, $m->getErrors(), 'Should not exist errors in attr1'); |
70
|
|
|
|
71
|
|
|
$m->resetData(); |
72
|
|
|
$m->attr1 = 'something'; |
73
|
|
|
$m->attr2 = 'something'; |
74
|
|
|
$v->validateAttribute($m, 'attr1'); |
75
|
|
|
$this->assertCount(0, $m->getErrors(), 'Should not exist errors in attr1'); |
76
|
|
|
|
77
|
|
|
$m->resetData(); |
78
|
|
|
$m->attr2 = 'something'; |
79
|
|
|
$m->attr3 = 'something'; |
80
|
|
|
$v->validateAttribute($m, 'attr1'); |
81
|
|
|
$this->assertCount(0, $m->getErrors(), 'Should not exist errors in attr1'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testErrorMessages() |
85
|
|
|
{ |
86
|
|
|
$v = new AtLeastValidator(['in' => 'attr1, attr2, attr3']); |
87
|
|
|
$m = new FakeModel(); |
88
|
|
|
|
89
|
|
|
$v->validateAttribute($m, 'attr1'); |
90
|
|
|
$this->assertEquals('You must fill at least 1 of the attributes "Attr1", "Attr2", "Attr3".', $m->getFirstError('attr1')); |
|
|
|
|
91
|
|
|
|
92
|
|
|
$m->clearErrors(); |
93
|
|
|
$v->min = 2; |
94
|
|
|
$v->validateAttribute($m, 'attr1'); |
95
|
|
|
$this->assertEquals('You must fill at least 2 of the attributes "Attr1", "Attr2", "Attr3".', $m->getFirstError('attr1')); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testMinParam() |
99
|
|
|
{ |
100
|
|
|
$v = new AtLeastValidator(['in' => 'attr1, attr2, attr3']); |
101
|
|
|
$m = new FakeModel(); |
102
|
|
|
|
103
|
|
|
// min = 1; |
104
|
|
|
$v->validateAttribute($m, 'attr1'); |
105
|
|
|
$this->assertCount(1, $m->getErrors()); |
106
|
|
|
|
107
|
|
|
$m->clearErrors(); |
108
|
|
|
$v->min = 2; |
109
|
|
|
$m->attr1 = 'something'; |
110
|
|
|
$v->validateAttribute($m, 'attr1'); |
111
|
|
|
$this->assertCount(1, $m->getErrors(), 'Should have an error since min = 2'); |
112
|
|
|
} |
113
|
|
|
} |