PhoneNumberTest::getDefaultObj()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace LeKoala\PhoneNumber\Test;
4
5
use LeKoala\PhoneNumber\DBPhone;
6
use SilverStripe\Dev\SapphireTest;
7
use LeKoala\PhoneNumber\PhoneHelper;
8
9
class PhoneNumberTest extends SapphireTest
10
{
11
    /**
12
     * Defines the fixture file to use for this test class
13
     * @var string
14
     */
15
    protected static $fixture_file = 'PhoneNumberTest.yml';
16
17
    protected static $extra_dataobjects = array(
18
        Test_PhoneNumberModel::class,
19
    );
20
21
    /**
22
     * @return Test_PhoneNumberModel
23
     */
24
    public function getDefaultObj()
25
    {
26
        return $this->objFromFixture(Test_PhoneNumberModel::class, 'default');
27
    }
28
29
    public function testPhoneField()
30
    {
31
        $model = $this->getDefaultObj();
32
33
        $field = new DBPhone('Phone');
34
35
        $nationalNumber = '0473 12 34 56';
36
        $internationalNumber = '+32 473 12 34 56';
37
        $internationalNumberNoSpace = str_replace(' ', '', $internationalNumber);
38
39
        // When provided with a national number, region must be provided through the model (field CountryCode is assumed)
40
        $field->setValue($nationalNumber, $model);
41
        $this->assertEquals($internationalNumber, $field->International());
42
        $this->assertEquals($internationalNumberNoSpace, $field->E164());
43
        $this->assertEquals($nationalNumber, $field->National());
44
45
        // When provided with international number only, region can be computed
46
        $field->setValue($internationalNumber);
47
        $this->assertEquals($internationalNumber, $field->International());
48
        $this->assertEquals($internationalNumberNoSpace, $field->E164());
49
        $this->assertEquals($nationalNumber, $field->National());
50
    }
51
52
    public function testHelper()
53
    {
54
        $list = PhoneHelper::getCountriesList();
55
        $this->assertContains('BE', array_keys($list));
56
57
        $list = PhoneHelper::listCountryPrefixes();
58
        $this->assertContains('BE', array_keys($list));
59
60
        $validNumbers = [
61
            '+32473123456' => 'BE',
62
            '0473123456' => 'BE',
63
        ];
64
        foreach ($validNumbers as $num => $region) {
65
            $this->assertTrue(PhoneHelper::validatePhoneNumber($num, $region), "Could not validate $num for $region");
66
            if (strpos($num, '+') === 0) {
67
                $this->assertTrue(PhoneHelper::validatePhoneNumber($num), "Could not validate $num without $region set");
68
            }
69
        }
70
        $invalidNumbers = [
71
            '+3247312345' => 'BE',
72
            '047312345' => 'BE',
73
        ];
74
        foreach ($invalidNumbers as $num => $region) {
75
            $this->assertFalse(PhoneHelper::validatePhoneNumber($num, $region), "Could not invalidate $num for $region");
76
        }
77
    }
78
}
79