菜谱项目

PaymentTest.php 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace Faker\Test\Provider\es_ES;
  3. use Faker\Generator;
  4. use Faker\Provider\es_ES\Payment;
  5. class PaymentTest extends \PHPUnit_Framework_TestCase
  6. {
  7. /**
  8. * @var Generator
  9. */
  10. private $faker;
  11. public function setUp()
  12. {
  13. $faker = new Generator();
  14. $faker->addProvider(new Payment($faker));
  15. $this->faker = $faker;
  16. }
  17. public function testVAT()
  18. {
  19. $vat = $this->faker->vat();
  20. $this->assertTrue($this->isValidCIF($vat));
  21. }
  22. /**
  23. * Validation taken from https://github.com/amnesty/drupal-nif-nie-cif-validator/
  24. * @link https://github.com/amnesty/drupal-nif-nie-cif-validator/blob/master/includes/nif-nie-cif.php
  25. */
  26. function isValidCIF($docNumber)
  27. {
  28. $fixedDocNumber = strtoupper($docNumber);
  29. return $this->isValidCIFFormat($fixedDocNumber);
  30. }
  31. function isValidCIFFormat($docNumber)
  32. {
  33. return $this->respectsDocPattern($docNumber, '/^[PQSNWR][0-9][0-9][0-9][0-9][0-9][0-9][0-9][A-Z0-9]/')
  34. ||
  35. $this->respectsDocPattern($docNumber, '/^[ABCDEFGHJUV][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]/');
  36. }
  37. function respectsDocPattern($givenString, $pattern)
  38. {
  39. $isValid = FALSE;
  40. $fixedString = strtoupper($givenString);
  41. if (is_int(substr($fixedString, 0, 1))) {
  42. $fixedString = substr("000000000" . $givenString, -9);
  43. }
  44. if (preg_match($pattern, $fixedString)) {
  45. $isValid = TRUE;
  46. }
  47. return $isValid;
  48. }
  49. }