PersonTest.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace Faker\Test\Provider\cs_CZ;
  3. use Faker\Generator;
  4. use Faker\Provider\cs_CZ\Person;
  5. use Faker\Provider\Miscellaneous;
  6. use PHPUnit\Framework\TestCase;
  7. class PersonTest extends TestCase
  8. {
  9. public function testBirthNumber()
  10. {
  11. $faker = new Generator();
  12. $faker->addProvider(new Person($faker));
  13. $faker->addProvider(new Miscellaneous($faker));
  14. for ($i = 0; $i < 1000; $i++) {
  15. $birthNumber = $faker->birthNumber();
  16. $birthNumber = str_replace('/', '', $birthNumber);
  17. // check date
  18. $year = intval(substr($birthNumber, 0, 2), 10);
  19. $month = intval(substr($birthNumber, 2, 2), 10);
  20. $day = intval(substr($birthNumber, 4, 2), 10);
  21. // make 4 digit year from 2 digit representation
  22. $year += $year < 54 ? 2000 : 1900;
  23. // adjust special cases for month
  24. if ($month > 50) $month -= 50;
  25. if ($year >= 2004 && $month > 20) $month -= 20;
  26. $this->assertTrue(checkdate($month, $day, $year), "Birth number $birthNumber: date $year/$month/$day is invalid.");
  27. // check CRC if presented
  28. if (strlen($birthNumber) == 10) {
  29. $crc = intval(substr($birthNumber, -1), 10);
  30. $refCrc = intval(substr($birthNumber, 0, -1), 10) % 11;
  31. if ($refCrc == 10) {
  32. $refCrc = 0;
  33. }
  34. $this->assertEquals($crc, $refCrc, "Birth number $birthNumber: checksum $crc doesn't match expected $refCrc.");
  35. }
  36. }
  37. }
  38. }