PersonTest.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace Faker\Test\Provider\zh_TW;
  3. use Faker\Generator;
  4. use Faker\Provider\zh_TW\Person;
  5. use PHPUnit\Framework\TestCase;
  6. class PersonTest extends TestCase
  7. {
  8. /**
  9. * @var Generator
  10. */
  11. private $faker;
  12. public function setUp()
  13. {
  14. $faker = new Generator();
  15. $faker->addProvider(new Person($faker));
  16. $this->faker = $faker;
  17. }
  18. /**
  19. * @see https://zh.wikipedia.org/wiki/%E4%B8%AD%E8%8F%AF%E6%B0%91%E5%9C%8B%E5%9C%8B%E6%B0%91%E8%BA%AB%E5%88%86%E8%AD%89
  20. */
  21. public function testPersonalIdentityNumber()
  22. {
  23. $id = $this->faker->personalIdentityNumber;
  24. $firstChar = substr($id, 0, 1);
  25. $codesString = Person::$idBirthplaceCode[$firstChar] . substr($id, 1);
  26. // After transfer the first alphabet word into 2 digit number, there should be totally 11 numbers
  27. $this->assertRegExp("/^[0-9]{11}$/", $codesString);
  28. $total = 0;
  29. $codesArray = str_split($codesString);
  30. foreach ($codesArray as $key => $code) {
  31. $total += $code * Person::$idDigitValidator[$key];
  32. }
  33. // Validate
  34. $this->assertEquals(0, ($total % 10));
  35. }
  36. }