Passed
Push — master ( 6a1d3b...e8f7f2 )
by Sidney
02:42
created

TemporaryTraitsForTests::testWrongConfigs()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 8
nc 4
nop 0
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.');
0 ignored issues
show
Bug introduced by
It seems like assertInstanceOf() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

27
            $this->/** @scrutinizer ignore-call */ 
28
                   assertInstanceOf('yii\base\InvalidConfigException', $e, 'Parameter `in` is required.');
Loading history...
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());
0 ignored issues
show
Bug introduced by
It seems like assertCount() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
        $this->/** @scrutinizer ignore-call */ 
44
               assertCount(1, $m->getErrors());
Loading history...
44
        $this->assertTrue($m->hasErrors('attr1'));
0 ignored issues
show
Bug introduced by
It seems like assertTrue() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
        $this->/** @scrutinizer ignore-call */ 
45
               assertTrue($m->hasErrors('attr1'));
Loading history...
45
        $this->assertFalse($m->hasErrors('attr2'));
0 ignored issues
show
Bug introduced by
It seems like assertFalse() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
        $this->/** @scrutinizer ignore-call */ 
46
               assertFalse($m->hasErrors('attr2'));
Loading history...
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'));
0 ignored issues
show
Bug introduced by
It seems like assertEquals() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

90
        $this->/** @scrutinizer ignore-call */ 
91
               assertEquals('You must fill at least 1 of the attributes "Attr1", "Attr2", "Attr3".', $m->getFirstError('attr1'));
Loading history...
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
}