菜谱项目

InternetTest.php 6.1KB

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