Skip to content

Commit

Permalink
Fix kk_KZ\Person::individualIdentificationNumber generation (#411)
Browse files Browse the repository at this point in the history
  • Loading branch information
vadimonus committed Dec 23, 2021
1 parent fafead6 commit a664100
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
18 changes: 11 additions & 7 deletions src/Faker/Provider/kk_KZ/Person.php
Expand Up @@ -176,25 +176,29 @@ class Person extends \Faker\Provider\Person
];

/**
* Note! When calculating individual identification number
* 2000-01-01 - 2000-12-31 counts as 21th century
* 1900-01-01 - 1900-12-31 counts as 20th century
*
* @param int $year
*
* @return int|null
* @return int
*/
private static function getCenturyByYear($year)
{
if ($year >= 2000 && $year <= DateTime::year()) {
if (($year >= 2100) || ($year < 1800)) {
throw new \InvalidArgumentException('Unexpected century');
}

if ($year >= 2000) {
return self::CENTURY_21ST;
}

if ($year >= 1900) {
return self::CENTURY_20TH;
}

if ($year >= 1800) {
return self::CENTURY_19TH;
}

return null;
return self::CENTURY_19TH;
}

/**
Expand Down
39 changes: 34 additions & 5 deletions test/Faker/Provider/kk_KZ/PersonTest.php
Expand Up @@ -11,13 +11,42 @@
*/
final class PersonTest extends TestCase
{
/**
* Note! When calculating individual identification number
* 2000-01-01 - 2000-12-31 counts as 21th century
* 1900-01-01 - 1900-12-31 counts as 20th century
*/
public function testIndividualIdentificationNumberIsValid()
{
$birthDate = DateTime::dateTimeBetween('-30 years', '-10 years');
$individualIdentificationNumber = $this->faker->individualIdentificationNumber($birthDate);
$controlDigit = Person::checkSum($individualIdentificationNumber);

self::assertSame($controlDigit, (int) substr($individualIdentificationNumber, 11, 1));
// 21st century.
$birthDate = DateTime::dateTimeBetween('2000-01-01', '2099-12-31');
$individualIdentificationNumber = $this->faker->individualIdentificationNumber($birthDate, Person::GENDER_MALE);
self::assertSame(Person::MALE_CENTURY_21ST, (int) $individualIdentificationNumber[6]);
self::assertSame(Person::checkSum($individualIdentificationNumber), (int) $individualIdentificationNumber[11]);
$individualIdentificationNumber = $this->faker->individualIdentificationNumber($birthDate, Person::GENDER_FEMALE);
self::assertSame(Person::FEMALE_CENTURY_21ST, (int) $individualIdentificationNumber[6]);
self::assertSame(Person::checkSum($individualIdentificationNumber), (int) $individualIdentificationNumber[11]);
// 20th century.
$birthDate = DateTime::dateTimeBetween('1900-01-01', '1999-12-31');
$individualIdentificationNumber = $this->faker->individualIdentificationNumber($birthDate, Person::GENDER_MALE);
self::assertSame(Person::checkSum($individualIdentificationNumber), (int) $individualIdentificationNumber[11]);
self::assertSame(Person::MALE_CENTURY_20TH, (int) $individualIdentificationNumber[6]);
$individualIdentificationNumber = $this->faker->individualIdentificationNumber($birthDate, Person::GENDER_FEMALE);
self::assertSame(Person::checkSum($individualIdentificationNumber), (int) $individualIdentificationNumber[11]);
self::assertSame(Person::FEMALE_CENTURY_20TH, (int) $individualIdentificationNumber[6]);
// 19th century.
$birthDate = DateTime::dateTimeBetween('1800-01-01', '1899-12-31');
$individualIdentificationNumber = $this->faker->individualIdentificationNumber($birthDate, Person::GENDER_MALE);
self::assertSame(Person::checkSum($individualIdentificationNumber), (int) $individualIdentificationNumber[11]);
self::assertSame(Person::MALE_CENTURY_19TH, (int) $individualIdentificationNumber[6]);
$individualIdentificationNumber = $this->faker->individualIdentificationNumber($birthDate, Person::GENDER_FEMALE);
self::assertSame(Person::checkSum($individualIdentificationNumber), (int) $individualIdentificationNumber[11]);
self::assertSame(Person::FEMALE_CENTURY_19TH, (int) $individualIdentificationNumber[6]);
// 22th century
self::expectException(\InvalidArgumentException::class);
self::expectExceptionMessage('Unexpected century');
$birthDate = DateTime::dateTimeBetween('2100-01-01', '2199-12-31');
$this->faker->individualIdentificationNumber($birthDate);
}

protected function getProviders(): iterable
Expand Down

0 comments on commit a664100

Please sign in to comment.