InternetTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. use PHPUnit\Framework\TestCase;
  9. class InternetTest extends TestCase
  10. {
  11. /**
  12. * @var Generator
  13. */
  14. private $faker;
  15. public function setUp()
  16. {
  17. $faker = new Generator();
  18. $faker->addProvider(new Lorem($faker));
  19. $faker->addProvider(new Person($faker));
  20. $faker->addProvider(new Internet($faker));
  21. $faker->addProvider(new Company($faker));
  22. $this->faker = $faker;
  23. }
  24. public function localeDataProvider()
  25. {
  26. $providerPath = realpath(__DIR__ . '/../../../src/Faker/Provider');
  27. $localePaths = array_filter(glob($providerPath . '/*', GLOB_ONLYDIR));
  28. foreach ($localePaths as $path) {
  29. $parts = explode('/', $path);
  30. $locales[] = array($parts[count($parts) - 1]);
  31. }
  32. return $locales;
  33. }
  34. /**
  35. * @link http://stackoverflow.com/questions/12026842/how-to-validate-an-email-address-in-php
  36. *
  37. * @dataProvider localeDataProvider
  38. */
  39. public function testEmailIsValid($locale)
  40. {
  41. if ($locale !== 'en_US' && !class_exists('Transliterator')) {
  42. $this->markTestSkipped('Transliterator class not available (intl extension)');
  43. }
  44. $this->loadLocalProviders($locale);
  45. $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';
  46. $emailAddress = $this->faker->email();
  47. $this->assertRegExp($pattern, $emailAddress);
  48. }
  49. /**
  50. * @dataProvider localeDataProvider
  51. */
  52. public function testUsernameIsValid($locale)
  53. {
  54. if ($locale !== 'en_US' && !class_exists('Transliterator')) {
  55. $this->markTestSkipped('Transliterator class not available (intl extension)');
  56. }
  57. $this->loadLocalProviders($locale);
  58. $pattern = '/^[A-Za-z0-9]+([._][A-Za-z0-9]+)*$/';
  59. $username = $this->faker->username();
  60. $this->assertRegExp($pattern, $username);
  61. }
  62. /**
  63. * @dataProvider localeDataProvider
  64. */
  65. public function testDomainnameIsValid($locale)
  66. {
  67. if ($locale !== 'en_US' && !class_exists('Transliterator')) {
  68. $this->markTestSkipped('Transliterator class not available (intl extension)');
  69. }
  70. $this->loadLocalProviders($locale);
  71. $pattern = '/^[a-z]+(\.[a-z]+)+$/';
  72. $domainName = $this->faker->domainName();
  73. $this->assertRegExp($pattern, $domainName);
  74. }
  75. /**
  76. * @dataProvider localeDataProvider
  77. */
  78. public function testDomainwordIsValid($locale)
  79. {
  80. if ($locale !== 'en_US' && !class_exists('Transliterator')) {
  81. $this->markTestSkipped('Transliterator class not available (intl extension)');
  82. }
  83. $this->loadLocalProviders($locale);
  84. $pattern = '/^[a-z]+$/';
  85. $domainWord = $this->faker->domainWord();
  86. $this->assertRegExp($pattern, $domainWord);
  87. }
  88. public function loadLocalProviders($locale)
  89. {
  90. $providerPath = realpath(__DIR__ . '/../../../src/Faker/Provider');
  91. if (file_exists($providerPath.'/'.$locale.'/Internet.php')) {
  92. $internet = "\\Faker\\Provider\\$locale\\Internet";
  93. $this->faker->addProvider(new $internet($this->faker));
  94. }
  95. if (file_exists($providerPath.'/'.$locale.'/Person.php')) {
  96. $person = "\\Faker\\Provider\\$locale\\Person";
  97. $this->faker->addProvider(new $person($this->faker));
  98. }
  99. if (file_exists($providerPath.'/'.$locale.'/Company.php')) {
  100. $company = "\\Faker\\Provider\\$locale\\Company";
  101. $this->faker->addProvider(new $company($this->faker));
  102. }
  103. }
  104. public function testPasswordIsValid()
  105. {
  106. $this->assertRegexp('/^.{6}$/', $this->faker->password(6, 6));
  107. }
  108. public function testSlugIsValid()
  109. {
  110. $pattern = '/^[a-z0-9-]+$/';
  111. $slug = $this->faker->slug();
  112. $this->assertSame(preg_match($pattern, $slug), 1);
  113. }
  114. public function testUrlIsValid()
  115. {
  116. $url = $this->faker->url();
  117. $this->assertNotFalse(filter_var($url, FILTER_VALIDATE_URL));
  118. }
  119. public function testLocalIpv4()
  120. {
  121. $this->assertNotFalse(filter_var(Internet::localIpv4(), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4));
  122. }
  123. public function testIpv4()
  124. {
  125. $this->assertNotFalse(filter_var($this->faker->ipv4(), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4));
  126. }
  127. public function testIpv4NotLocalNetwork()
  128. {
  129. $this->assertNotRegExp('/\A0\./', $this->faker->ipv4());
  130. }
  131. public function testIpv4NotBroadcast()
  132. {
  133. $this->assertNotEquals('255.255.255.255', $this->faker->ipv4());
  134. }
  135. public function testIpv6()
  136. {
  137. $this->assertNotFalse(filter_var($this->faker->ipv6(), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6));
  138. }
  139. public function testMacAddress()
  140. {
  141. $this->assertRegExp('/^([0-9A-F]{2}[:]){5}([0-9A-F]{2})$/i', Internet::macAddress());
  142. }
  143. }