菜谱项目

ProviderOverrideTest.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * @author Mark van der Velden <mark@dynom.nl>
  4. */
  5. namespace Faker\Test\Provider;
  6. use Faker;
  7. /**
  8. * Class ProviderOverrideTest
  9. *
  10. * @package Faker\Test\Provider
  11. *
  12. * This class tests a large portion of all locale specific providers. It does not test the entire stack, because each
  13. * locale specific provider (can) has specific implementations. The goal of this test is to test the common denominator
  14. * and to try to catch possible invalid multi-byte sequences.
  15. */
  16. class ProviderOverrideTest extends \PHPUnit_Framework_TestCase
  17. {
  18. /**
  19. * Constants with regular expression patterns for testing the output.
  20. *
  21. * Regular expressions are sensitive for malformed strings (e.g.: strings with incorrect encodings) so by using
  22. * PCRE for the tests, even though they seem fairly pointless, we test for incorrect encodings also.
  23. */
  24. const TEST_STRING_REGEX = '/.+/u';
  25. /**
  26. * Slightly more specific for e-mail, the point isn't to properly validate e-mails.
  27. */
  28. const TEST_EMAIL_REGEX = '/^(.+)@(.+)$/ui';
  29. /**
  30. * @dataProvider localeDataProvider
  31. * @param string $locale
  32. */
  33. public function testAddress($locale = null)
  34. {
  35. $faker = Faker\Factory::create($locale);
  36. $this->assertRegExp(static::TEST_STRING_REGEX, $faker->city);
  37. $this->assertRegExp(static::TEST_STRING_REGEX, $faker->postcode);
  38. $this->assertRegExp(static::TEST_STRING_REGEX, $faker->address);
  39. $this->assertRegExp(static::TEST_STRING_REGEX, $faker->country);
  40. }
  41. /**
  42. * @dataProvider localeDataProvider
  43. * @param string $locale
  44. */
  45. public function testCompany($locale = null)
  46. {
  47. $faker = Faker\Factory::create($locale);
  48. $this->assertRegExp(static::TEST_STRING_REGEX, $faker->company);
  49. }
  50. /**
  51. * @dataProvider localeDataProvider
  52. * @param string $locale
  53. */
  54. public function testDateTime($locale = null)
  55. {
  56. $faker = Faker\Factory::create($locale);
  57. $this->assertRegExp(static::TEST_STRING_REGEX, $faker->century);
  58. $this->assertRegExp(static::TEST_STRING_REGEX, $faker->timezone);
  59. }
  60. /**
  61. * @dataProvider localeDataProvider
  62. * @param string $locale
  63. */
  64. public function testInternet($locale = null)
  65. {
  66. if ($locale && $locale !== 'en_US' && !class_exists('Transliterator')) {
  67. $this->markTestSkipped('Transliterator class not available (intl extension)');
  68. }
  69. $faker = Faker\Factory::create($locale);
  70. $this->assertRegExp(static::TEST_STRING_REGEX, $faker->userName);
  71. $this->assertRegExp(static::TEST_EMAIL_REGEX, $faker->email);
  72. $this->assertRegExp(static::TEST_EMAIL_REGEX, $faker->safeEmail);
  73. $this->assertRegExp(static::TEST_EMAIL_REGEX, $faker->freeEmail);
  74. $this->assertRegExp(static::TEST_EMAIL_REGEX, $faker->companyEmail);
  75. }
  76. /**
  77. * @dataProvider localeDataProvider
  78. * @param string $locale
  79. */
  80. public function testPerson($locale = null)
  81. {
  82. $faker = Faker\Factory::create($locale);
  83. $this->assertRegExp(static::TEST_STRING_REGEX, $faker->name);
  84. $this->assertRegExp(static::TEST_STRING_REGEX, $faker->title);
  85. $this->assertRegExp(static::TEST_STRING_REGEX, $faker->firstName);
  86. $this->assertRegExp(static::TEST_STRING_REGEX, $faker->lastName);
  87. }
  88. /**
  89. * @dataProvider localeDataProvider
  90. * @param string $locale
  91. */
  92. public function testPhoneNumber($locale = null)
  93. {
  94. $faker = Faker\Factory::create($locale);
  95. $this->assertRegExp(static::TEST_STRING_REGEX, $faker->phoneNumber);
  96. }
  97. /**
  98. * @dataProvider localeDataProvider
  99. * @param string $locale
  100. */
  101. public function testUserAgent($locale = null)
  102. {
  103. $faker = Faker\Factory::create($locale);
  104. $this->assertRegExp(static::TEST_STRING_REGEX, $faker->userAgent);
  105. }
  106. /**
  107. * @dataProvider localeDataProvider
  108. *
  109. * @param null $locale
  110. * @param string $locale
  111. */
  112. public function testUuid($locale = null)
  113. {
  114. $faker = Faker\Factory::create($locale);
  115. $this->assertRegExp(static::TEST_STRING_REGEX, $faker->uuid);
  116. }
  117. /**
  118. * @return array
  119. */
  120. public function localeDataProvider()
  121. {
  122. $locales = $this->getAllLocales();
  123. $data = array();
  124. foreach ($locales as $locale) {
  125. $data[] = array(
  126. $locale
  127. );
  128. }
  129. return $data;
  130. }
  131. /**
  132. * Returns all locales as array values
  133. *
  134. * @return array
  135. */
  136. private function getAllLocales()
  137. {
  138. static $locales = array();
  139. if ( ! empty($locales)) {
  140. return $locales;
  141. }
  142. // Finding all PHP files in the xx_XX directories
  143. $providerDir = __DIR__ .'/../../../src/Faker/Provider';
  144. foreach (glob($providerDir .'/*_*/*.php') as $file) {
  145. $localisation = basename(dirname($file));
  146. if (isset($locales[ $localisation ])) {
  147. continue;
  148. }
  149. $locales[ $localisation ] = $localisation;
  150. }
  151. return $locales;
  152. }
  153. }