菜谱项目

PersonTest.php 925B

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace Faker\Test\Provider\es_ES;
  3. use Faker\Generator;
  4. use Faker\Provider\es_ES\Person;
  5. class PersonTest extends \PHPUnit_Framework_TestCase
  6. {
  7. public function setUp()
  8. {
  9. $faker = new Generator();
  10. $faker->seed(1);
  11. $faker->addProvider(new Person($faker));
  12. $this->faker = $faker;
  13. }
  14. public function testDNI()
  15. {
  16. $dni = $this->faker->dni;
  17. $this->assertTrue($this->isValidDNI($dni));
  18. }
  19. // validation taken from http://kiwwito.com/php-function-for-spanish-dni-nie-validation/
  20. public function isValidDNI($string)
  21. {
  22. if (strlen($string) != 9 ||
  23. preg_match('/^[XYZ]?([0-9]{7,8})([A-Z])$/i', $string, $matches) !== 1) {
  24. return false;
  25. }
  26. $map = 'TRWAGMYFPDXBNJZSQVHLCKE';
  27. list(, $number, $letter) = $matches;
  28. return strtoupper($letter) === $map[((int) $number) % 23];
  29. }
  30. }