ProviderOverrideTest.php 5.1 KB

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