You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 line
7.4KB

  1. <?php
  2. namespace Faker\Test\Provider;
  3. use Faker\Calculator\Iban;
  4. use Faker\Calculator\Luhn;
  5. use Faker\Generator;
  6. use Faker\Provider\Base as BaseProvider;
  7. use Faker\Provider\DateTime as DateTimeProvider;
  8. use Faker\Provider\Payment as PaymentProvider;
  9. use Faker\Provider\Person as PersonProvider;
  10. class PaymentTest extends \PHPUnit_Framework_TestCase
  11. {
  12. private $faker;
  13. public function setUp()
  14. {
  15. $faker = new Generator();
  16. $faker->addProvider(new BaseProvider($faker));
  17. $faker->addProvider(new DateTimeProvider($faker));
  18. $faker->addProvider(new PersonProvider($faker));
  19. $faker->addProvider(new PaymentProvider($faker));
  20. $this->faker = $faker;
  21. }
  22. public function localeDataProvider()
  23. {
  24. $providerPath = realpath(__DIR__ . '/../../../src/Faker/Provider');
  25. $localePaths = array_filter(glob($providerPath . '/*', GLOB_ONLYDIR));
  26. foreach ($localePaths as $path) {
  27. $parts = explode('/', $path);
  28. $locales[] = array($parts[count($parts) - 1]);
  29. }
  30. return $locales;
  31. }
  32. public function loadLocalProviders($locale)
  33. {
  34. $providerPath = realpath(__DIR__ . '/../../../src/Faker/Provider');
  35. if (file_exists($providerPath.'/'.$locale.'/Payment.php')) {
  36. $payment = "\\Faker\\Provider\\$locale\\Payment";
  37. $this->faker->addProvider(new $payment($this->faker));
  38. }
  39. }
  40. public function testCreditCardTypeReturnsValidVendorName()
  41. {
  42. $this->assertTrue(in_array($this->faker->creditCardType, array('Visa', 'MasterCard', 'American Express', 'Discover Card')));
  43. }
  44. public function creditCardNumberProvider()
  45. {
  46. return array(
  47. array('Discover Card', '/^6011\d{12}$/'),
  48. array('Visa', '/^4\d{12,15}$/'),
  49. array('MasterCard', '/^5[1-5]\d{14}$/')
  50. );
  51. }
  52. /**
  53. * @dataProvider creditCardNumberProvider
  54. */
  55. public function testCreditCardNumberReturnsValidCreditCardNumber($type, $regexp)
  56. {
  57. $cardNumber = $this->faker->creditCardNumber($type);
  58. $this->assertRegExp($regexp, $cardNumber);
  59. $this->assertTrue(Luhn::isValid($cardNumber));
  60. }
  61. public function testCreditCardNumberCanFormatOutput()
  62. {
  63. $this->assertRegExp('/^6011-\d{4}-\d{4}-\d{4}$/', $this->faker->creditCardNumber('Discover Card', true));
  64. }
  65. public function testCreditCardExpirationDateReturnsValidDateByDefault()
  66. {
  67. $expirationDate = $this->faker->creditCardExpirationDate;
  68. $this->assertTrue(intval($expirationDate->format('U')) > strtotime('now'));
  69. $this->assertTrue(intval($expirationDate->format('U')) < strtotime('+36 months'));
  70. }
  71. public function testRandomCard()
  72. {
  73. $cardDetails = $this->faker->creditCardDetails;
  74. $this->assertEquals(count($cardDetails), 4);
  75. $this->assertEquals(array('type', 'number', 'name', 'expirationDate'), array_keys($cardDetails));
  76. }
  77. protected $ibanFormats = array(
  78. 'AD' => '/^AD\d{2}\d{4}\d{4}[A-Z0-9]{12}$/',
  79. 'AE' => '/^AE\d{2}\d{3}\d{16}$/',
  80. 'AL' => '/^AL\d{2}\d{8}[A-Z0-9]{16}$/',
  81. 'AT' => '/^AT\d{2}\d{5}\d{11}$/',
  82. 'AZ' => '/^AZ\d{2}[A-Z]{4}[A-Z0-9]{20}$/',
  83. 'BA' => '/^BA\d{2}\d{3}\d{3}\d{8}\d{2}$/',
  84. 'BE' => '/^BE\d{2}\d{3}\d{7}\d{2}$/',
  85. 'BG' => '/^BG\d{2}[A-Z]{4}\d{4}\d{2}[A-Z0-9]{8}$/',
  86. 'BH' => '/^BH\d{2}[A-Z]{4}[A-Z0-9]{14}$/',
  87. 'BR' => '/^BR\d{2}\d{8}\d{5}\d{10}[A-Z]{1}[A-Z0-9]{1}$/',
  88. 'CH' => '/^CH\d{2}\d{5}[A-Z0-9]{12}$/',
  89. 'CR' => '/^CR\d{2}\d{3}\d{14}$/',
  90. 'CY' => '/^CY\d{2}\d{3}\d{5}[A-Z0-9]{16}$/',
  91. 'CZ' => '/^CZ\d{2}\d{4}\d{6}\d{10}$/',
  92. 'DE' => '/^DE\d{2}\d{8}\d{10}$/',
  93. 'DK' => '/^DK\d{2}\d{4}\d{9}\d{1}$/',
  94. 'DO' => '/^DO\d{2}[A-Z0-9]{4}\d{20}$/',
  95. 'EE' => '/^EE\d{2}\d{2}\d{2}\d{11}\d{1}$/',
  96. 'ES' => '/^ES\d{2}\d{4}\d{4}\d{1}\d{1}\d{10}$/',
  97. 'FR' => '/^FR\d{2}\d{5}\d{5}[A-Z0-9]{11}\d{2}$/',
  98. 'GB' => '/^GB\d{2}[A-Z]{4}\d{6}\d{8}$/',
  99. 'GE' => '/^GE\d{2}[A-Z]{2}\d{16}$/',
  100. 'GI' => '/^GI\d{2}[A-Z]{4}[A-Z0-9]{15}$/',
  101. 'GR' => '/^GR\d{2}\d{3}\d{4}[A-Z0-9]{16}$/',
  102. 'GT' => '/^GT\d{2}[A-Z0-9]{4}[A-Z0-9]{20}$/',
  103. 'HR' => '/^HR\d{2}\d{7}\d{10}$/',
  104. 'HU' => '/^HU\d{2}\d{3}\d{4}\d{1}\d{15}\d{1}$/',
  105. 'IE' => '/^IE\d{2}[A-Z]{4}\d{6}\d{8}$/',
  106. 'IL' => '/^IL\d{2}\d{3}\d{3}\d{13}$/',
  107. 'IS' => '/^IS\d{2}\d{4}\d{2}\d{6}\d{10}$/',
  108. 'IT' => '/^IT\d{2}[A-Z]{1}\d{5}\d{5}[A-Z0-9]{12}$/',
  109. 'KW' => '/^KW\d{2}[A-Z]{4}\d{22}$/',
  110. 'KZ' => '/^KZ\d{2}\d{3}[A-Z0-9]{13}$/',
  111. 'LB' => '/^LB\d{2}\d{4}[A-Z0-9]{20}$/',
  112. 'LI' => '/^LI\d{2}\d{5}[A-Z0-9]{12}$/',
  113. 'LT' => '/^LT\d{2}\d{5}\d{11}$/',
  114. 'LU' => '/^LU\d{2}\d{3}[A-Z0-9]{13}$/',
  115. 'LV' => '/^LV\d{2}[A-Z]{4}[A-Z0-9]{13}$/',
  116. 'MC' => '/^MC\d{2}\d{5}\d{5}[A-Z0-9]{11}\d{2}$/',
  117. 'MD' => '/^MD\d{2}[A-Z0-9]{2}[A-Z0-9]{18}$/',
  118. 'ME' => '/^ME\d{2}\d{3}\d{13}\d{2}$/',
  119. 'MK' => '/^MK\d{2}\d{3}[A-Z0-9]{10}\d{2}$/',
  120. 'MR' => '/^MR\d{2}\d{5}\d{5}\d{11}\d{2}$/',
  121. 'MT' => '/^MT\d{2}[A-Z]{4}\d{5}[A-Z0-9]{18}$/',
  122. 'MU' => '/^MU\d{2}[A-Z]{4}\d{2}\d{2}\d{12}\d{3}[A-Z]{3}$/',
  123. 'NL' => '/^NL\d{2}[A-Z]{4}\d{10}$/',
  124. 'NO' => '/^NO\d{2}\d{4}\d{6}\d{1}$/',
  125. 'PK' => '/^PK\d{2}[A-Z]{4}[A-Z0-9]{16}$/',
  126. 'PL' => '/^PL\d{2}\d{8}\d{16}$/',
  127. 'PS' => '/^PS\d{2}[A-Z]{4}[A-Z0-9]{21}$/',
  128. 'PT' => '/^PT\d{2}\d{4}\d{4}\d{11}\d{2}$/',
  129. 'RO' => '/^RO\d{2}[A-Z]{4}[A-Z0-9]{16}$/',
  130. 'RS' => '/^RS\d{2}\d{3}\d{13}\d{2}$/',
  131. 'SA' => '/^SA\d{2}\d{2}[A-Z0-9]{18}$/',
  132. 'SE' => '/^SE\d{2}\d{3}\d{16}\d{1}$/',
  133. 'SI' => '/^SI\d{2}\d{5}\d{8}\d{2}$/',
  134. 'SK' => '/^SK\d{2}\d{4}\d{6}\d{10}$/',
  135. 'SM' => '/^SM\d{2}[A-Z]{1}\d{5}\d{5}[A-Z0-9]{12}$/',
  136. 'TN' => '/^TN\d{2}\d{2}\d{3}\d{13}\d{2}$/',
  137. 'TR' => '/^TR\d{2}\d{5}\d{1}[A-Z0-9]{16}$/',
  138. 'VG' => '/^VG\d{2}[A-Z]{4}\d{16}$/',
  139. );
  140. /**
  141. * @dataProvider localeDataProvider
  142. */
  143. public function testBankAccountNumber($locale)
  144. {
  145. $parts = explode('_', $locale);
  146. $countryCode = array_pop($parts);
  147. if (!isset($this->ibanFormats[$countryCode])) {
  148. // No IBAN format available
  149. return;
  150. }
  151. $this->loadLocalProviders($locale);
  152. try {
  153. $iban = $this->faker->bankAccountNumber;
  154. } catch (\InvalidArgumentException $e) {
  155. // Not implemented, nothing to test
  156. $this->markTestSkipped("bankAccountNumber not implemented for $locale");
  157. return;
  158. }
  159. // Test format
  160. $this->assertRegExp($this->ibanFormats[$countryCode], $iban);
  161. // Test checksum
  162. $this->assertTrue(Iban::isValid($iban), "Checksum for $iban is invalid");
  163. }
  164. public function ibanFormatProvider()
  165. {
  166. $return = array();
  167. foreach ($this->ibanFormats as $countryCode => $regex) {
  168. $return[] = array($countryCode, $regex);
  169. }
  170. return $return;
  171. }
  172. /**
  173. * @dataProvider ibanFormatProvider
  174. */
  175. public function testIban($countryCode, $regex)
  176. {
  177. $iban = $this->faker->iban($countryCode);
  178. // Test format
  179. $this->assertRegExp($regex, $iban);
  180. // Test checksum
  181. $this->assertTrue(Iban::isValid($iban), "Checksum for $iban is invalid");
  182. }
  183. }