PaymentTest.php 7.5 KB

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