Skip to content

Commit 59cbcb5

Browse files
committed
Add Regex test cases for pt_PT PhoneNumber #925
- Add tests for e164PhoneNumber, e164MobileNumber and e164LandlineNumber. - Simplify regex for phoneNumber. Note: - Regex for phoneNumber was duplicating some information. It also validated for landline numbers starting at 20 even tho it wasn't present in the provider $formats. - Regex for country code, mobile number and landline number could be placed in a const variable. The problem is: there's some issues with interpolating self/static variables and concatenation would probably be hard to read.
1 parent 3591ca3 commit 59cbcb5

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

src/Provider/pt_PT/PhoneNumber.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
class PhoneNumber extends \Faker\Provider\PhoneNumber
66
{
77
/**
8-
* Returns the pt_PT phone country code.
8+
* Phone country code.
99
*/
1010
public const COUNTRY_CODE = '+351';
1111

1212
/**
13-
* pt_PT Mobile Service Codes
13+
* Mobile Service Codes
1414
*/
1515
public const MOBILE_SERVICE_CODE = [
1616
91,
@@ -20,7 +20,7 @@ class PhoneNumber extends \Faker\Provider\PhoneNumber
2020
];
2121

2222
/**
23-
* pt_PT Geographic Area Codes
23+
* Geographic Area Codes
2424
*/
2525
public const AREA_CODE = [
2626
21,
@@ -35,24 +35,24 @@ class PhoneNumber extends \Faker\Provider\PhoneNumber
3535
];
3636

3737
/**
38-
* pt_PT Geographic Area and Mobile Service Codes
38+
* Geographic Area and Mobile Service Codes
3939
*/
40-
public const AREA_OR_MOBILE_SERVICE_CODE = [
41-
...self::MOBILE_SERVICE_CODE,
40+
public const AREA_AND_MOBILE_SERVICE_CODE = [
4241
...self::AREA_CODE,
42+
...self::MOBILE_SERVICE_CODE,
4343
];
4444

4545
/**
4646
* @see http://en.wikipedia.org/wiki/Telephone_numbers_in_Portugal
4747
*/
4848
protected static $formats = [
49-
'{{countryCode}} {{areaOrMobileServiceCode}}#######',
49+
'{{countryCode}} {{areaAndMobileServiceCode}}#######',
5050
'{{mobileServiceCode}}#######',
5151
'{{areaCode}}#######',
5252
];
5353

5454
protected static $e164Formats = [
55-
'{{countryCode}}{{areaOrMobileServiceCode}}#######',
55+
'{{countryCode}}{{areaAndMobileServiceCode}}#######',
5656
];
5757

5858
protected static $e164MobileFormat = [
@@ -75,23 +75,23 @@ public static function mobileNumber()
7575
return static::numerify(static::randomElement(static::$mobileNumberPrefixes));
7676
}
7777

78-
public static function areaOrMobileServiceCode()
78+
public static function areaAndMobileServiceCode()
7979
{
80-
return self::randomElement(static::AREA_OR_MOBILE_SERVICE_CODE);
80+
return static::randomElement(self::AREA_AND_MOBILE_SERVICE_CODE);
8181
}
8282

8383
public static function areaCode()
8484
{
85-
return self::randomElement(static::AREA_CODE);
85+
return static::randomElement(self::AREA_CODE);
8686
}
8787

8888
public static function mobileServiceCode()
8989
{
90-
return self::randomElement(static::MOBILE_SERVICE_CODE);
90+
return static::randomElement(self::MOBILE_SERVICE_CODE);
9191
}
9292

9393
/**
94-
* Returns the pt_PT phone country code.
94+
* Returns the phone country code.
9595
*
9696
* @return string
9797
*/
@@ -101,8 +101,8 @@ public static function countryCode()
101101
}
102102

103103
/**
104-
* Returns a pt_PT mobile number in E.164 format.
105-
*
104+
* Returns a mobile number in E.164 format.
105+
*
106106
* Example: +35193XXXXXXX
107107
*
108108
* @return string
@@ -113,8 +113,8 @@ public function e164MobileNumber()
113113
}
114114

115115
/**
116-
* Returns a pt_PT landline number in E.164 format.
117-
*
116+
* Returns a landline number in E.164 format.
117+
*
118118
* Example: +35121XXXXXXX
119119
*
120120
* @return string
@@ -123,4 +123,4 @@ public function e164LandlineNumber()
123123
{
124124
return static::numerify($this->generator->parse(static::randomElement(static::$e164LandlineFormat)));
125125
}
126-
}
126+
}

test/Provider/pt_PT/PhoneNumberTest.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,29 @@ final class PhoneNumberTest extends TestCase
1414
{
1515
public function testPhoneNumberReturnsPhoneNumberWithOrWithoutPrefix(): void
1616
{
17-
self::assertMatchesRegularExpression('/^(9[1,2,3,6][0-9]{7})|(2[0-9]{8})|(\+351 [2][0-9]{8})|(\+351 9[1,2,3,6][0-9]{7})/', $this->faker->phoneNumber());
17+
self::assertMatchesRegularExpression('/^(?:\+351 )?(?:9[1,2,3,6][0-9]{7}|2[1-9][0-9]{7})$/', $this->faker->phoneNumber());
1818
}
1919

20-
public function testMobileNumberReturnsMobileNumberWithOrWithoutPrefix(): void
20+
public function testMobileNumberReturnsMobileNumberWithoutPrefix(): void
2121
{
2222
self::assertMatchesRegularExpression('/^(9[1,2,3,6][0-9]{7})/', $this->faker->mobileNumber());
2323
}
2424

25+
public function testE164PhoneNumberReturnsE164MobileOrLandlineNumber(): void
26+
{
27+
self::assertMatchesRegularExpression('/^\+351(?:9[1,2,3,6][0-9]{7}|2[1-9][0-9]{7})$/', $this->faker->e164PhoneNumber());
28+
}
29+
30+
public function testE164MobileNumberReturnsE164MobileNumber(): void
31+
{
32+
self::assertMatchesRegularExpression('/^\+3519[1,2,3,6][0-9]{7}$/', $this->faker->e164MobileNumber());
33+
}
34+
35+
public function testE164LandlineNumberReturnsE164LandlineNumber(): void
36+
{
37+
self::assertMatchesRegularExpression('/^\+3512[1-9][0-9]{7}$/', $this->faker->e164LandlineNumber());
38+
}
39+
2540
protected function getProviders(): iterable
2641
{
2742
yield new PhoneNumber($this->faker);

0 commit comments

Comments
 (0)