ProviderOverrideTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. $faker = Faker\Factory::create($locale);
  67. $this->assertRegExp(static::TEST_STRING_REGEX, $faker->userName);
  68. $this->assertRegExp(static::TEST_EMAIL_REGEX, $faker->email);
  69. $this->assertRegExp(static::TEST_EMAIL_REGEX, $faker->safeEmail);
  70. $this->assertRegExp(static::TEST_EMAIL_REGEX, $faker->freeEmail);
  71. $this->assertRegExp(static::TEST_EMAIL_REGEX, $faker->companyEmail);
  72. }
  73. /**
  74. * @dataProvider localeDataProvider
  75. * @param string $locale
  76. */
  77. public function testPerson($locale = null)
  78. {
  79. $faker = Faker\Factory::create($locale);
  80. $this->assertRegExp(static::TEST_STRING_REGEX, $faker->name);
  81. $this->assertRegExp(static::TEST_STRING_REGEX, $faker->title);
  82. $this->assertRegExp(static::TEST_STRING_REGEX, $faker->firstName);
  83. $this->assertRegExp(static::TEST_STRING_REGEX, $faker->lastName);
  84. }
  85. /**
  86. * @dataProvider localeDataProvider
  87. * @param string $locale
  88. */
  89. public function testPhoneNumber($locale = null)
  90. {
  91. $faker = Faker\Factory::create($locale);
  92. $this->assertRegExp(static::TEST_STRING_REGEX, $faker->phoneNumber);
  93. }
  94. /**
  95. * @dataProvider localeDataProvider
  96. * @param string $locale
  97. */
  98. public function testUserAgent($locale = null)
  99. {
  100. $faker = Faker\Factory::create($locale);
  101. $this->assertRegExp(static::TEST_STRING_REGEX, $faker->userAgent);
  102. }
  103. /**
  104. * @dataProvider localeDataProvider
  105. *
  106. * @param null $locale
  107. * @param string $locale
  108. */
  109. public function testUuid($locale = null)
  110. {
  111. $faker = Faker\Factory::create($locale);
  112. $this->assertRegExp(static::TEST_STRING_REGEX, $faker->uuid);
  113. }
  114. /**
  115. * @return array
  116. */
  117. public function localeDataProvider()
  118. {
  119. $locales = $this->getAllLocales();
  120. $data = array();
  121. foreach ($locales as $locale) {
  122. $data[] = array(
  123. $locale
  124. );
  125. }
  126. return $data;
  127. }
  128. /**
  129. * Returns all locales as array values
  130. *
  131. * @return array
  132. */
  133. private function getAllLocales()
  134. {
  135. static $locales = array();
  136. if ( ! empty($locales)) {
  137. return $locales;
  138. }
  139. // Finding all PHP files in the xx_XX directories
  140. $providerDir = __DIR__ .'/../../../src/Faker/Provider';
  141. foreach (glob($providerDir .'/*_*/*.php') as $file) {
  142. $localisation = basename(dirname($file));
  143. if (isset($locales[ $localisation ])) {
  144. continue;
  145. }
  146. $locales[ $localisation ] = $localisation;
  147. }
  148. return $locales;
  149. }
  150. }