Skip to content
This repository was archived by the owner on Dec 11, 2020. It is now read-only.

Commit a2dec96

Browse files
authored
Merge pull request #975 from vlakoff/userName
Remove trailing dot in username if any
2 parents 8c5d915 + f6d0c24 commit a2dec96

File tree

3 files changed

+67
-6
lines changed

3 files changed

+67
-6
lines changed

src/Faker/Provider/Internet.php

+24-3
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,18 @@ public function userName()
9393
$format = static::randomElement(static::$userNameFormats);
9494
$username = static::bothify($this->generator->parse($format));
9595

96-
return strtolower(static::transliterate($username));
96+
$username = strtolower(static::transliterate($username));
97+
98+
// check if transliterate() didn't support the language and removed all letters
99+
if (trim($username, '._') === '') {
100+
throw new \Exception('userName failed with the selected locale. Try a different locale or activate the "intl" PHP extension.');
101+
}
102+
103+
// clean possible trailing dots from first/last names
104+
$username = str_replace('..', '.', $username);
105+
$username = rtrim($username, '.');
106+
107+
return $username;
97108
}
98109
/**
99110
* @example 'fY4èHdZv68'
@@ -120,7 +131,17 @@ public function domainWord()
120131
{
121132
$lastName = $this->generator->format('lastName');
122133

123-
return strtolower(static::transliterate($lastName));
134+
$lastName = strtolower(static::transliterate($lastName));
135+
136+
// check if transliterate() didn't support the language and removed all letters
137+
if (trim($lastName, '._') === '') {
138+
throw new \Exception('domainWord failed with the selected locale. Try a different locale or activate the "intl" PHP extension.');
139+
}
140+
141+
// clean possible trailing dot from last name
142+
$lastName = rtrim($lastName, '.');
143+
144+
return $lastName;
124145
}
125146

126147
/**
@@ -214,7 +235,7 @@ protected static function transliterate($string)
214235
}
215236

216237
$transId = 'Any-Latin; Latin-ASCII; NFD; [:Nonspacing Mark:] Remove; NFC;';
217-
if (function_exists('transliterator_transliterate') && $transliterator = \Transliterator::create($transId)) {
238+
if (class_exists('Transliterator') && $transliterator = \Transliterator::create($transId)) {
218239
$transString = $transliterator->transliterate($string);
219240
} else {
220241
$transString = static::toAscii($string);

test/Faker/Provider/InternetTest.php

+39-3
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,65 @@ public function localeDataProvider()
4040
/**
4141
* @link http://stackoverflow.com/questions/12026842/how-to-validate-an-email-address-in-php
4242
*
43-
* @requires PHP 5.4
4443
* @dataProvider localeDataProvider
4544
*/
4645
public function testEmailIsValid($locale)
4746
{
47+
if ($locale !== 'en_US' && !class_exists('Transliterator')) {
48+
$this->markTestSkipped('Transliterator class not available (intl extension)');
49+
}
50+
4851
$this->loadLocalProviders($locale);
4952
$pattern = '/^(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){255,})(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){65,}@)(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22))(?:\\.(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-+[a-z0-9]+)*\\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-+[a-z0-9]+)*)|(?:\\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\\]))$/iD';
5053
$emailAddress = $this->faker->email();
5154
$this->assertRegExp($pattern, $emailAddress);
5255
}
5356

5457
/**
55-
* @requires PHP 5.4
5658
* @dataProvider localeDataProvider
5759
*/
5860
public function testUsernameIsValid($locale)
5961
{
62+
if ($locale !== 'en_US' && !class_exists('Transliterator')) {
63+
$this->markTestSkipped('Transliterator class not available (intl extension)');
64+
}
65+
6066
$this->loadLocalProviders($locale);
61-
$pattern = '/^[A-Za-z0-9._]+$/';
67+
$pattern = '/^[A-Za-z0-9]+([._][A-Za-z0-9]+)*$/';
6268
$username = $this->faker->username();
6369
$this->assertRegExp($pattern, $username);
6470
}
6571

72+
/**
73+
* @dataProvider localeDataProvider
74+
*/
75+
public function testDomainnameIsValid($locale)
76+
{
77+
if ($locale !== 'en_US' && !class_exists('Transliterator')) {
78+
$this->markTestSkipped('Transliterator class not available (intl extension)');
79+
}
80+
81+
$this->loadLocalProviders($locale);
82+
$pattern = '/^[a-z]+(\.[a-z]+)+$/';
83+
$domainName = $this->faker->domainName();
84+
$this->assertRegExp($pattern, $domainName);
85+
}
86+
87+
/**
88+
* @dataProvider localeDataProvider
89+
*/
90+
public function testDomainwordIsValid($locale)
91+
{
92+
if ($locale !== 'en_US' && !class_exists('Transliterator')) {
93+
$this->markTestSkipped('Transliterator class not available (intl extension)');
94+
}
95+
96+
$this->loadLocalProviders($locale);
97+
$pattern = '/^[a-z]+$/';
98+
$domainWord = $this->faker->domainWord();
99+
$this->assertRegExp($pattern, $domainWord);
100+
}
101+
66102
public function loadLocalProviders($locale)
67103
{
68104
$providerPath = realpath(__DIR__ . '/../../../src/Faker/Provider');

test/Faker/Provider/ProviderOverrideTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ public function testDateTime($locale = null)
7777
*/
7878
public function testInternet($locale = null)
7979
{
80+
if ($locale && $locale !== 'en_US' && !class_exists('Transliterator')) {
81+
$this->markTestSkipped('Transliterator class not available (intl extension)');
82+
}
83+
8084
$faker = Faker\Factory::create($locale);
8185

8286
$this->assertRegExp(static::TEST_STRING_REGEX, $faker->userName);

0 commit comments

Comments
 (0)