PersonTest.php 1.6 KB

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