PersonTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace Faker\Test\Provider\ro_RO;
  3. use Faker\Generator;
  4. use Faker\Provider\DateTime;
  5. use Faker\Provider\ro_RO\Person;
  6. class PersonTest extends \PHPUnit_Framework_TestCase
  7. {
  8. const TEST_CNP_REGEX = '/^([1-9])([0-9]{2}(?:0[1-9]|1[012])(?:0[1-9]|[12][0-9]|3[01]))(0[1-9]|[123][0-9]|4[0-6]|5[12])([0-9]{3})([0-9])$/';
  9. public function setUp()
  10. {
  11. $faker = new Generator();
  12. $faker->seed(1);
  13. $faker->addProvider(new DateTime($faker));
  14. $faker->addProvider(new Person($faker));
  15. $this->faker = $faker;
  16. }
  17. public function testCnpReturnsValidCnp()
  18. {
  19. $cnp = $this->faker->cnp;
  20. $this->assertTrue($this->isValidCnp($cnp));
  21. }
  22. public function testCnpReturnsMaleCnp()
  23. {
  24. $cnp = $this->faker->cnp('m');
  25. $this->assertRegExp('/^[1357]\d{12}$/', $cnp);
  26. }
  27. public function testCnpReturnsFemaleCnp()
  28. {
  29. $cnp = $this->faker->cnp('f');
  30. $this->assertRegExp('/^[2468]\d{12}$/', $cnp);
  31. }
  32. public function testCnpReturns1800sCnp()
  33. {
  34. $cnp = $this->faker->cnp(null, 1800);
  35. $this->assertRegExp('/^[34]\d{12}$/', $cnp);
  36. }
  37. public function testCnpReturns1900sCnp()
  38. {
  39. $cnp = $this->faker->cnp(null, 1900);
  40. $this->assertRegExp('/^[12]\d{12}$/', $cnp);
  41. }
  42. public function testCnpReturns2000sCnp()
  43. {
  44. $cnp = $this->faker->cnp(null, 2000);
  45. $this->assertRegExp('/^[56]\d{12}$/', $cnp);
  46. }
  47. public function testCnpReturnsBrasovCnp()
  48. {
  49. $cnp = $this->faker->cnp(null, null, 'BV');
  50. $this->assertRegExp('/^\d{7}08\d{4}$/', $cnp);
  51. }
  52. public function testCnpReturns2000sClujFemaleCnp()
  53. {
  54. $cnp = $this->faker->cnp('f', 2000, 'CJ');
  55. $this->assertRegExp('/^6\d{6}12\d{4}$/', $cnp);
  56. }
  57. protected function isValidCnp($cnp)
  58. {
  59. if (
  60. is_string($cnp)
  61. && (bool) preg_match(static::TEST_CNP_REGEX, $cnp)
  62. && checkdate(substr($cnp, 3, 2), substr($cnp, 5, 2), substr($cnp, 1, 2))
  63. ){
  64. $checkNumber = 279146358279;
  65. $checksum = 0;
  66. foreach (range(0, 11) as $digit) {
  67. $checksum += substr($cnp, $digit, 1) * substr($checkNumber, $digit, 1);
  68. }
  69. $checksum = $checksum % 11;
  70. if (
  71. ($checksum < 10 && $checksum == substr($cnp, -1))
  72. || ($checksum == 10 && substr($cnp, -1) == 1)
  73. ){
  74. return true;
  75. }
  76. }
  77. return false;
  78. }
  79. }