Passed
Branch develop (84afed)
by Paulius
04:34 queued 02:36
created

PhoneValidatorTest::testEmptyStringIsValid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Dokobit\Gateway\Tests\Validator\Constraints;
4
5
use Dokobit\Gateway\Validator\Constraints\Phone;
6
use Dokobit\Gateway\Validator\Constraints\PhoneValidator;
7
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
8
use Symfony\Component\Validator\Validation;
9
10
class PhoneValidatorTest extends ConstraintValidatorTestCase
11
{
12
    protected function getApiVersion()
13
    {
14
        return Validation::API_VERSION_2_5;
0 ignored issues
show
Bug introduced by
The constant Symfony\Component\Valida...dation::API_VERSION_2_5 was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
15
    }
16
17
    protected function createValidator()
18
    {
19
        return new PhoneValidator(false);
0 ignored issues
show
Unused Code introduced by
The call to Dokobit\Gateway\Validato...alidator::__construct() has too many arguments starting with false. ( Ignorable by Annotation )

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

19
        return /** @scrutinizer ignore-call */ new PhoneValidator(false);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
20
    }
21
22
    public function testNullIsValid()
23
    {
24
        $this->validator->validate(null, new Phone());
25
26
        $this->assertNoViolation();
27
    }
28
29
    public function testEmptyStringIsValid()
30
    {
31
        $this->validator->validate('', new Phone());
32
33
        $this->assertNoViolation();
34
    }
35
36
    public function testExpectsStringCompatibleType()
37
    {
38
        $this->expectException(\Symfony\Component\Validator\Exception\UnexpectedTypeException::class);
39
        $this->validator->validate(new \stdClass(), new Phone());
40
    }
41
42
    /**
43
     * @dataProvider getValidPhones
44
     */
45
    public function testValidPhones($phone)
46
    {
47
        $this->validator->validate($phone, new Phone());
48
49
        $this->assertNoViolation();
50
    }
51
52
    public function getValidPhones()
53
    {
54
        return array(
55
            // Lithuania
56
            array('+37060000000'),
57
            // Estonia
58
            array('+3720000000'),
59
            array('+37200000000'),
60
            // // Finland
61
            array('+358409999'),
62
            array('+3584099999'),
63
            array('+35840999999'),
64
            array('+358409999999'),
65
            array('+3584099999999'),
66
            array('+3584579999'),
67
            array('+358509999'),
68
        );
69
    }
70
71
    /**
72
     * @dataProvider getInvalidPhones
73
     */
74
    public function testInvalidPhones($phone)
75
    {
76
        $constraint = new Phone(array(
77
            'message' => 'myMessage',
78
        ));
79
80
        $this->validator->validate($phone, $constraint);
81
82
        $this->buildViolation('myMessage')
83
            ->setParameter('{{ value }}', '"'.$phone.'"')
84
            ->setCode(defined('Dokobit\Gateway\Validator\Constraints\Phone::REGEX_FAILED_ERROR')?Phone::REGEX_FAILED_ERROR:null)
0 ignored issues
show
Bug introduced by
It seems like defined('Dokobit\Gateway...GEX_FAILED_ERROR : null can also be of type null; however, parameter $code of Symfony\Component\Valida...ionAssertion::setCode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

84
            ->setCode(/** @scrutinizer ignore-type */ defined('Dokobit\Gateway\Validator\Constraints\Phone::REGEX_FAILED_ERROR')?Phone::REGEX_FAILED_ERROR:null)
Loading history...
85
            ->assertRaised();
86
    }
87
88
    public function getInvalidPhones()
89
    {
90
        return array(
91
            array('1'), // too short
92
            array('personal_cde'), // random crap
93
            array('+00000000000'), // invalid
94
            // Lithuania
95
            array('+37000000000'), // invalid
96
            array('+3706000000000'), // invalid
97
            // Estonia
98
            array('+372000000'),
99
            array('+372000000000'),
100
            // // Finland
101
            array('+35840999'),
102
            array('+35840999999999'),
103
        );
104
    }
105
}
106