You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
3.2KB

  1. <?php
  2. namespace Faker\Test\Provider;
  3. use Faker\Generator;
  4. use Faker\Provider\Company;
  5. use Faker\Provider\Internet;
  6. use Faker\Provider\Lorem;
  7. use Faker\Provider\Person;
  8. class InternetTest extends \PHPUnit_Framework_TestCase
  9. {
  10. public function setUp()
  11. {
  12. $faker = new Generator();
  13. $faker->addProvider(new Lorem($faker));
  14. $faker->addProvider(new Person($faker));
  15. $faker->addProvider(new Internet($faker));
  16. $faker->addProvider(new Company($faker));
  17. $this->faker = $faker;
  18. }
  19. /**
  20. * @link http://stackoverflow.com/questions/12026842/how-to-validate-an-email-address-in-php
  21. */
  22. public function testEmailIsValid()
  23. {
  24. $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';
  25. $emailaddress = $this->faker->email();
  26. $this->assertSame(preg_match($pattern, $emailaddress), 1);
  27. }
  28. public function testUsernameIsValid()
  29. {
  30. $pattern = '/^[A-Za-z0-9_.]+$/';
  31. $emailaddress = $this->faker->username();
  32. $this->assertSame(preg_match($pattern, $emailaddress), 1);
  33. }
  34. public function testSlugIsValid()
  35. {
  36. $pattern = '/^[a-z0-9-]+$/';
  37. $slug = $this->faker->slug();
  38. $this->assertSame(preg_match($pattern, $slug), 1);
  39. }
  40. /**
  41. * @link http://code.tutsplus.com/tutorials/8-regular-expressions-you-should-know--net-6149
  42. */
  43. public function testUrlIsValid()
  44. {
  45. $pattern = '/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/';
  46. $url = $this->faker->url();
  47. $this->assertSame(preg_match($pattern, $url), 1);
  48. }
  49. public function testLocalIpv4()
  50. {
  51. $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}';
  52. $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}';
  53. $this->assertRegExp('/^'.$range1.'|'.$range2.'$/', Internet::localIpv4());
  54. }
  55. public function testMacAddress()
  56. {
  57. $this->assertRegExp('/^([0-9A-F]{2}[:]){5}([0-9A-F]{2})$/i', Internet::macAddress());
  58. }
  59. }