Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

131 lines
4.8KB

  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. * @requires PHP 5.4
  37. * @dataProvider localeDataProvider
  38. */
  39. public function testEmailIsValid($locale)
  40. {
  41. $this->loadLocalProviders($locale);
  42. $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';
  43. $emailAddress = $this->faker->email();
  44. $this->assertRegExp($pattern, $emailAddress);
  45. }
  46. /**
  47. * @requires PHP 5.4
  48. * @dataProvider localeDataProvider
  49. */
  50. public function testUsernameIsValid($locale)
  51. {
  52. $this->loadLocalProviders($locale);
  53. $pattern = '/^[A-Za-z0-9._]+$/';
  54. $username = $this->faker->username();
  55. $this->assertRegExp($pattern, $username);
  56. }
  57. public function loadLocalProviders($locale)
  58. {
  59. $providerPath = realpath(__DIR__ . '/../../../src/Faker/Provider');
  60. if (file_exists($providerPath.'/'.$locale.'/Internet.php')) {
  61. $internet = "\\Faker\\Provider\\$locale\\Internet";
  62. $this->faker->addProvider(new $internet($this->faker));
  63. }
  64. if (file_exists($providerPath.'/'.$locale.'/Person.php')) {
  65. $person = "\\Faker\\Provider\\$locale\\Person";
  66. $this->faker->addProvider(new $person($this->faker));
  67. }
  68. if (file_exists($providerPath.'/'.$locale.'/Company.php')) {
  69. $company = "\\Faker\\Provider\\$locale\\Company";
  70. $this->faker->addProvider(new $company($this->faker));
  71. }
  72. }
  73. public function testPasswordIsValid()
  74. {
  75. $this->assertRegexp('/^.{6}$/', $this->faker->password(6, 6));
  76. }
  77. public function testSlugIsValid()
  78. {
  79. $pattern = '/^[a-z0-9-]+$/';
  80. $slug = $this->faker->slug();
  81. $this->assertSame(preg_match($pattern, $slug), 1);
  82. }
  83. public function testUrlIsValid()
  84. {
  85. $url = $this->faker->url();
  86. $this->assertNotFalse(filter_var($url, FILTER_VALIDATE_URL));
  87. }
  88. public function testLocalIpv4()
  89. {
  90. $this->assertNotFalse(filter_var(Internet::localIpv4(), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4));
  91. }
  92. public function testIpv4()
  93. {
  94. $this->assertNotFalse(filter_var($this->faker->ipv4(), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4));
  95. }
  96. public function testIpv4NotLocalNetwork()
  97. {
  98. $this->assertNotRegExp('/\A1\./', $this->faker->ipv4());
  99. }
  100. public function testIpv4NotBroadcast()
  101. {
  102. $this->assertNotEquals('255.255.255.255', $this->faker->ipv4());
  103. }
  104. public function testIpv6()
  105. {
  106. $this->assertNotFalse(filter_var($this->faker->ipv6(), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6));
  107. }
  108. public function testMacAddress()
  109. {
  110. $this->assertRegExp('/^([0-9A-F]{2}[:]){5}([0-9A-F]{2})$/i', Internet::macAddress());
  111. }
  112. }