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

Commit

Permalink
Merge pull request #406 from bessl/master
Browse files Browse the repository at this point in the history
Replace regex with PHP filter in InternetTest
  • Loading branch information
fzaninotto committed Aug 29, 2014
2 parents e49b998 + abb6bb3 commit fc3eafb
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions test/Faker/Provider/InternetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,10 @@ public function setUp()
$this->faker = $faker;
}

/**
* @link http://stackoverflow.com/questions/12026842/how-to-validate-an-email-address-in-php
*/
public function testEmailIsValid()
{
$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';
$emailaddress = $this->faker->email();
$this->assertSame(preg_match($pattern, $emailaddress), 1);
$this->assertNotFalse(filter_var($emailaddress, FILTER_VALIDATE_EMAIL));
}

public function testUsernameIsValid()
Expand All @@ -39,7 +35,7 @@ public function testUsernameIsValid()

public function testPasswordIsValid()
{
$this->assertRegexp('/^.{6}$/', $this->faker->password(6,6));
$this->assertRegexp('/^.{6}$/', $this->faker->password(6, 6));
}

public function testSlugIsValid()
Expand All @@ -49,21 +45,20 @@ public function testSlugIsValid()
$this->assertSame(preg_match($pattern, $slug), 1);
}

/**
* @link http://code.tutsplus.com/tutorials/8-regular-expressions-you-should-know--net-6149
*/
public function testUrlIsValid()
{
$pattern = '/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/';
$url = $this->faker->url();
$this->assertSame(preg_match($pattern, $url), 1);
$this->assertNotFalse(filter_var($url, FILTER_VALIDATE_URL));
}

public function testLocalIpv4()
{
$range1 = '(10)(\.(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|[1][0-9][0-9]|[1-9][0-9]|[0-9])){3}';
$range2 = '(192)\.(168)(\.(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|[1][0-9][0-9]|[1-9][0-9]|[0-9])){2}';
$this->assertRegExp('/^'.$range1.'|'.$range2.'$/', Internet::localIpv4());
$this->assertNotFalse(filter_var(Internet::localIpv4(), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4));
}

public function testIpv6()
{
$this->assertNotFalse(filter_var($this->faker->ipv6(), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6));
}

public function testMacAddress()
Expand Down

0 comments on commit fc3eafb

Please sign in to comment.