Passed
Branch develop (84afed)
by Paulius
02:35
created

CodeValidatorTest::getValidCodes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 15
rs 9.9332
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\Code;
6
use Dokobit\Gateway\Validator\Constraints\CodeValidator;
7
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
8
use Symfony\Component\Validator\Validation;
9
10
class CodeValidatorTest 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 CodeValidator(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 CodeValidator(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 Code());
25
26
        $this->assertNoViolation();
27
    }
28
29
    public function testEmptyStringIsValid()
30
    {
31
        $this->validator->validate('', new Code());
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 Code());
40
    }
41
42
    /**
43
     * @dataProvider getValidCodes
44
     */
45
    public function testValidCodes($code)
46
    {
47
        $this->validator->validate($code, new Code());
48
49
        $this->assertNoViolation();
50
    }
51
52
    public function getValidCodes()
53
    {
54
        return array(
55
            // Test
56
            array('11412090004'),
57
            // Lithuania, Estonia
58
            array('39009090909'),
59
            array('39009090909'),
60
            array('49009090909'),
61
            array('59009090909'),
62
            array('69009090909'),
63
            // Finland
64
            array('060606+1234'),
65
            array('060606-1234'),
66
            array('060606A1234'),
67
        );
68
    }
69
70
    /**
71
     * @dataProvider getInvalidCodes
72
     */
73
    public function testInvalidCodes($code)
74
    {
75
        $constraint = new Code(array(
76
            'message' => 'myMessage',
77
        ));
78
79
        $this->validator->validate($code, $constraint);
80
81
        $this->buildViolation('myMessage')
82
            ->setParameter('{{ value }}', '"'.$code.'"')
83
            ->setCode(defined('Dokobit\Gateway\Validator\Constraints\Code::REGEX_FAILED_ERROR')?Code::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

83
            ->setCode(/** @scrutinizer ignore-type */ defined('Dokobit\Gateway\Validator\Constraints\Code::REGEX_FAILED_ERROR')?Code::REGEX_FAILED_ERROR:null)
Loading history...
84
            ->assertRaised();
85
    }
86
87
    public function getInvalidCodes()
88
    {
89
        return array(
90
            // Test
91
            array('1'), // too short
92
            array('personal_cde'), // random crap
93
            // Lithuania, Estonia
94
            array('30000000000'), // invalid
95
            array('36513040898'), // invalid month 13
96
            array('96512040898'), // invalid month 13
97
            array('900909090909'), // too long
98
            // Finland
99
            array('060606B1234'), // invalid century char
100
            array('061306B1234'), // invalid month
101
            array('060606A12344'), // too long
102
            array('060606A123'), // too short
103
        );
104
    }
105
}
106