Failed Conditions
Push — issue#774 ( 057f04...ee9377 )
by Guilherme
05:15
created

testNotPhoneNumber()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\ValidationBundle\Tests\Validator\Constraints;
12
13
use libphonenumber\PhoneNumber;
14
use LoginCidadao\ValidationBundle\Validator\Constraints\MobilePhoneNumber;
15
use LoginCidadao\ValidationBundle\Validator\Constraints\MobilePhoneNumberValidator;
16
use Symfony\Component\Validator\Context\ExecutionContextInterface;
17
18
class MobilePhoneNumberValidatorTest extends \PHPUnit_Framework_TestCase
19
{
20
    public function testNotPhoneNumber()
21
    {
22
        $this->assertFalse(MobilePhoneNumberValidator::isMobile(new \DateTime()));
23
    }
24
25
    public function testNotMobile()
26
    {
27
        $phone = new PhoneNumber();
28
        $phone->setCountryCode(55);
29
        $phone->setNationalNumber('5133333333');
30
31
        $this->assertFalse(MobilePhoneNumberValidator::isMobile($phone));
32
    }
33
34
    public function testMissing9thDigit()
35
    {
36
        $phone = new PhoneNumber();
37
        $phone->setCountryCode(55);
38
        $phone->setNationalNumber('5199999999');
39
40
        $this->assertFalse(MobilePhoneNumberValidator::isMobile($phone));
41
    }
42
43
    public function testValidateSuccessWithString()
44
    {
45
        $validator = new MobilePhoneNumberValidator();
46
        $validator->initialize($this->getContext($this->never()));
47
        $validator->validate('+5551999999999', new MobilePhoneNumber());
48
    }
49
50
    public function testSkipWithInvalidString()
51
    {
52
        $validator = new MobilePhoneNumberValidator();
53
        $validator->initialize($this->getContext($this->never()));
54
        $validator->validate('+5', new MobilePhoneNumber());
55
    }
56
57
    public function testValidationFailure()
58
    {
59
        $phone = (new PhoneNumber())
60
            ->setCountryCode(55)
61
            ->setNationalNumber('5199999999');
62
63
        $validator = new MobilePhoneNumberValidator();
64
        $validator->initialize($this->getContext($this->once()));
65
        $validator->validate($phone, new MobilePhoneNumber());
66
    }
67
68
    public function testValidMobileNumber()
69
    {
70
        $phone = new PhoneNumber();
71
        $phone->setCountryCode(55);
72
        $phone->setNationalNumber('51999999999');
73
74
        $this->assertTrue(MobilePhoneNumberValidator::isMobile($phone));
75
    }
76
77
    /**
78
     * @return ExecutionContextInterface|\PHPUnit_Framework_MockObject_MockObject
79
     */
80
    private function getContext($violations)
81
    {
82
        $context = $this->getMock('Symfony\Component\Validator\Context\ExecutionContextInterface');
83
        $context->expects($violations)->method('addViolation');
84
85
        return $context;
86
    }
87
}
88