菜谱项目

PaymentTest.php 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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]|2[2-7])\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. 'FI' => '/^FI\d{2}\d{6}\d{7}\d{1}$/',
  98. 'FR' => '/^FR\d{2}\d{5}\d{5}[A-Z0-9]{11}\d{2}$/',
  99. 'GB' => '/^GB\d{2}[A-Z]{4}\d{6}\d{8}$/',
  100. 'GE' => '/^GE\d{2}[A-Z]{2}\d{16}$/',
  101. 'GI' => '/^GI\d{2}[A-Z]{4}[A-Z0-9]{15}$/',
  102. 'GR' => '/^GR\d{2}\d{3}\d{4}[A-Z0-9]{16}$/',
  103. 'GT' => '/^GT\d{2}[A-Z0-9]{4}[A-Z0-9]{20}$/',
  104. 'HR' => '/^HR\d{2}\d{7}\d{10}$/',
  105. 'HU' => '/^HU\d{2}\d{3}\d{4}\d{1}\d{15}\d{1}$/',
  106. 'IE' => '/^IE\d{2}[A-Z]{4}\d{6}\d{8}$/',
  107. 'IL' => '/^IL\d{2}\d{3}\d{3}\d{13}$/',
  108. 'IS' => '/^IS\d{2}\d{4}\d{2}\d{6}\d{10}$/',
  109. 'IT' => '/^IT\d{2}[A-Z]{1}\d{5}\d{5}[A-Z0-9]{12}$/',
  110. 'KW' => '/^KW\d{2}[A-Z]{4}\d{22}$/',
  111. 'KZ' => '/^KZ\d{2}\d{3}[A-Z0-9]{13}$/',
  112. 'LB' => '/^LB\d{2}\d{4}[A-Z0-9]{20}$/',
  113. 'LI' => '/^LI\d{2}\d{5}[A-Z0-9]{12}$/',
  114. 'LT' => '/^LT\d{2}\d{5}\d{11}$/',
  115. 'LU' => '/^LU\d{2}\d{3}[A-Z0-9]{13}$/',
  116. 'LV' => '/^LV\d{2}[A-Z]{4}[A-Z0-9]{13}$/',
  117. 'MC' => '/^MC\d{2}\d{5}\d{5}[A-Z0-9]{11}\d{2}$/',
  118. 'MD' => '/^MD\d{2}[A-Z0-9]{2}[A-Z0-9]{18}$/',
  119. 'ME' => '/^ME\d{2}\d{3}\d{13}\d{2}$/',
  120. 'MK' => '/^MK\d{2}\d{3}[A-Z0-9]{10}\d{2}$/',
  121. 'MR' => '/^MR\d{2}\d{5}\d{5}\d{11}\d{2}$/',
  122. 'MT' => '/^MT\d{2}[A-Z]{4}\d{5}[A-Z0-9]{18}$/',
  123. 'MU' => '/^MU\d{2}[A-Z]{4}\d{2}\d{2}\d{12}\d{3}[A-Z]{3}$/',
  124. 'NL' => '/^NL\d{2}[A-Z]{4}\d{10}$/',
  125. 'NO' => '/^NO\d{2}\d{4}\d{6}\d{1}$/',
  126. 'PK' => '/^PK\d{2}[A-Z]{4}[A-Z0-9]{16}$/',
  127. 'PL' => '/^PL\d{2}\d{8}\d{16}$/',
  128. 'PS' => '/^PS\d{2}[A-Z]{4}[A-Z0-9]{21}$/',
  129. 'PT' => '/^PT\d{2}\d{4}\d{4}\d{11}\d{2}$/',
  130. 'RO' => '/^RO\d{2}[A-Z]{4}[A-Z0-9]{16}$/',
  131. 'RS' => '/^RS\d{2}\d{3}\d{13}\d{2}$/',
  132. 'SA' => '/^SA\d{2}\d{2}[A-Z0-9]{18}$/',
  133. 'SE' => '/^SE\d{2}\d{3}\d{16}\d{1}$/',
  134. 'SI' => '/^SI\d{2}\d{5}\d{8}\d{2}$/',
  135. 'SK' => '/^SK\d{2}\d{4}\d{6}\d{10}$/',
  136. 'SM' => '/^SM\d{2}[A-Z]{1}\d{5}\d{5}[A-Z0-9]{12}$/',
  137. 'TN' => '/^TN\d{2}\d{2}\d{3}\d{13}\d{2}$/',
  138. 'TR' => '/^TR\d{2}\d{5}\d{1}[A-Z0-9]{16}$/',
  139. 'VG' => '/^VG\d{2}[A-Z]{4}\d{16}$/',
  140. );
  141. /**
  142. * @dataProvider localeDataProvider
  143. */
  144. public function testBankAccountNumber($locale)
  145. {
  146. $parts = explode('_', $locale);
  147. $countryCode = array_pop($parts);
  148. if (!isset($this->ibanFormats[$countryCode])) {
  149. // No IBAN format available
  150. return;
  151. }
  152. $this->loadLocalProviders($locale);
  153. try {
  154. $iban = $this->faker->bankAccountNumber;
  155. } catch (\InvalidArgumentException $e) {
  156. // Not implemented, nothing to test
  157. $this->markTestSkipped("bankAccountNumber not implemented for $locale");
  158. return;
  159. }
  160. // Test format
  161. $this->assertRegExp($this->ibanFormats[$countryCode], $iban);
  162. // Test checksum
  163. $this->assertTrue(Iban::isValid($iban), "Checksum for $iban is invalid");
  164. }
  165. public function ibanFormatProvider()
  166. {
  167. $return = array();
  168. foreach ($this->ibanFormats as $countryCode => $regex) {
  169. $return[] = array($countryCode, $regex);
  170. }
  171. return $return;
  172. }
  173. /**
  174. * @dataProvider ibanFormatProvider
  175. */
  176. public function testIban($countryCode, $regex)
  177. {
  178. $iban = $this->faker->iban($countryCode);
  179. // Test format
  180. $this->assertRegExp($regex, $iban);
  181. // Test checksum
  182. $this->assertTrue(Iban::isValid($iban), "Checksum for $iban is invalid");
  183. }
  184. }