From 966082b46c25144628a81a517bb03ca9ecd26128 Mon Sep 17 00:00:00 2001 From: Bernhard Essl Date: Fri, 29 Aug 2014 09:49:00 +0200 Subject: [PATCH 1/4] replace regex with PHP filter for InternetTest --- test/Faker/Provider/InternetTest.php | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/test/Faker/Provider/InternetTest.php b/test/Faker/Provider/InternetTest.php index b14bc6eac0..d476b2a21b 100644 --- a/test/Faker/Provider/InternetTest.php +++ b/test/Faker/Provider/InternetTest.php @@ -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() @@ -49,21 +45,15 @@ 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 testMacAddress() From 4e44e3320398a76e683b57c44feb2a3d4d8515f9 Mon Sep 17 00:00:00 2001 From: Bernhard Essl Date: Fri, 29 Aug 2014 09:49:56 +0200 Subject: [PATCH 2/4] Add test case for ipv6 --- test/Faker/Provider/InternetTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/Faker/Provider/InternetTest.php b/test/Faker/Provider/InternetTest.php index d476b2a21b..470070df56 100644 --- a/test/Faker/Provider/InternetTest.php +++ b/test/Faker/Provider/InternetTest.php @@ -56,6 +56,11 @@ public function testLocalIpv4() $this->assertNotFalse(filter_var(Internet::localIpv4(), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)); } + public function testIpv6() + { + $this->assertNotFalse(filter_var(Internet::ipv6(), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)); + } + public function testMacAddress() { $this->assertRegExp('/^([0-9A-F]{2}[:]){5}([0-9A-F]{2})$/i', Internet::macAddress()); From 19fa5196baf451ec5a3213b272ac43290756a791 Mon Sep 17 00:00:00 2001 From: Bernhard Essl Date: Fri, 29 Aug 2014 09:53:10 +0200 Subject: [PATCH 3/4] Make code sniffer happy --- test/Faker/Provider/InternetTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Faker/Provider/InternetTest.php b/test/Faker/Provider/InternetTest.php index 470070df56..7252bde951 100644 --- a/test/Faker/Provider/InternetTest.php +++ b/test/Faker/Provider/InternetTest.php @@ -35,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() From abb6bb3f33bd46290e3c0b5acd658f16dbc737e0 Mon Sep 17 00:00:00 2001 From: Bernhard Essl Date: Fri, 29 Aug 2014 10:00:35 +0200 Subject: [PATCH 4/4] bugfix no static call --- test/Faker/Provider/InternetTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Faker/Provider/InternetTest.php b/test/Faker/Provider/InternetTest.php index 7252bde951..1bc4cb5e94 100644 --- a/test/Faker/Provider/InternetTest.php +++ b/test/Faker/Provider/InternetTest.php @@ -58,7 +58,7 @@ public function testLocalIpv4() public function testIpv6() { - $this->assertNotFalse(filter_var(Internet::ipv6(), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)); + $this->assertNotFalse(filter_var($this->faker->ipv6(), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)); } public function testMacAddress()