Passed
Pull Request — master (#7)
by Nimna
12:44
created

PhoneNumberTest::test_can_get_IDD_format()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace APiChef\SlPhoneNumber;
6
7
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use PHPUnit\Framework\Attributes\DataProvider;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\Attributes\DataProvider was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
class PhoneNumberTest extends TestCase
11
{
12
    #[DataProvider('phoneNumbersWithValidity')]
13
    public function test_getDetails_throws_an_exception_when_the_starting_digits_or_length_is_invalid(string $number, bool $validity, string $formated = null)
14
    {
15
        $slPhoneNumber = new PhoneNumber($number);
16
17
        if (! $validity) {
18
            $this->expectException(\InvalidArgumentException::class);
19
        }
20
21
        /** @var PhoneNumberDetails $result */
22
        $result = $slPhoneNumber->getDetails();
23
24
        $this->assertEquals($formated, $result->getNumber());
25
    }
26
27
    #[DataProvider('phoneNumbersWithValidity')]
28
    public function test_it_validates_phone_number_by_starting_digits_and_length(string $number, bool $validity)
29
    {
30
        $slPhoneNumber = new PhoneNumber($number);
31
32
        $this->assertEquals($validity, $slPhoneNumber->isValid());
33
    }
34
35
    public static function phoneNumbersWithValidity()
36
    {
37
        return [
38
            ['0770000000', true, '0770000000'],
39
            ['0112000000', true, '0112000000'],
40
            ['1000000000', false, null],
41
            ['000000000', false, null],
42
            ['00000000000', false, null],
43
44
            ['+94770000000', true, '0770000000'],
45
            ['+94112000000', true, '0112000000'],
46
            ['-94000000000', false, null],
47
            ['+9400000000', false, null],
48
            ['+940000000000', false, null],
49
50
            ['0094770000000', true, '0770000000'],
51
            ['0094112000000', true, '0112000000'],
52
            ['0096000000000', false, null],
53
            ['009400000000', false, null],
54
            ['00940000000000', false, null],
55
        ];
56
    }
57
58
    public function test_get_details_returns_phone_number_details_object()
59
    {
60
        $details = (new PhoneNumber('0710000000'))->getDetails();
61
62
        $this->assertInstanceOf(PhoneNumberDetails::class, $details);
63
        $this->assertEquals('Mobitel', $details->getOperator());
64
65
        $fixedDetails = (new PhoneNumber('0112000000'))->getDetails();
66
67
        $this->assertInstanceOf(PhoneNumberDetails::class, $fixedDetails);
68
        $this->assertEquals('Sri Lanka Telecom', $fixedDetails->getOperator());
69
        $this->assertEquals('Western', $fixedDetails->getProvince());
70
        $this->assertEquals('Colombo', $fixedDetails->getDistrict());
71
        $this->assertEquals('Colombo', $fixedDetails->getArea());
72
    }
73
74
    public function test_it_throws_a_logic_exception_when_trying_to_get_province_from_mobile_number()
75
    {
76
        $details = (new PhoneNumber('0710000000'))->getDetails();
77
        $this->expectException(\LogicException::class);
78
        $details->getProvince();
79
    }
80
81
    public function test_it_throws_a_logic_exception_when_trying_to_get_district_from_mobile_number()
82
    {
83
        $details = (new PhoneNumber('0710000000'))->getDetails();
84
        $this->expectException(\LogicException::class);
85
        $details->getDistrict();
86
    }
87
88
    public function test_it_throws_a_logic_exception_when_trying_to_get_area_from_mobile_number()
89
    {
90
        $details = (new PhoneNumber('0710000000'))->getDetails();
91
        $this->expectException(\LogicException::class);
92
        $details->getArea();
93
    }
94
95
    #[DataProvider('validityPhoneNumbers')]
96
    public function test_can_get_local_format(string $number)
97
    {
98
        $localFormat = (new PhoneNumber($number))->toLocalFormat();
99
        $this->assertEquals('0710000000', $localFormat);
100
    }
101
102
    #[DataProvider('validityPhoneNumbers')]
103
    public function test_can_get_IDD_format(string $number)
104
    {
105
        $localFormat = (new PhoneNumber($number))->toIDDFormat();
106
        $this->assertEquals('+94710000000', $localFormat);
107
    }
108
109
    public static function validityPhoneNumbers(): array
110
    {
111
        return [
112
            ['0710000000'],
113
            ['+94710000000'],
114
            ['0094710000000'],
115
        ];
116
    }
117
}
118