PersonTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. use PHPUnit\Framework\TestCase;
  7. class PersonTest extends TestCase
  8. {
  9. 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]$/';
  10. /**
  11. * @var \Faker\Generator
  12. *
  13. */
  14. protected $faker;
  15. public function setUp()
  16. {
  17. $faker = new Generator();
  18. $faker->addProvider(new DateTime($faker));
  19. $faker->addProvider(new Person($faker));
  20. $faker->setDefaultTimezone('Europe/Bucharest');
  21. $this->faker = $faker;
  22. }
  23. public function tearDown()
  24. {
  25. $this->faker->setDefaultTimezone();
  26. }
  27. public function invalidGenderProvider()
  28. {
  29. return array(
  30. array('elf'),
  31. array('ent'),
  32. array('fmle'),
  33. array('mal'),
  34. );
  35. }
  36. public function invalidYearProvider()
  37. {
  38. return array(
  39. array(1652),
  40. array(1799),
  41. array(2100),
  42. array(2252),
  43. );
  44. }
  45. public function validYearProvider()
  46. {
  47. return array(
  48. array(null),
  49. array(''),
  50. array(1800),
  51. array(1850),
  52. array(1900),
  53. array(1990),
  54. array(2000),
  55. array(2099),
  56. );
  57. }
  58. public function validCountyCodeProvider()
  59. {
  60. return array(
  61. array('AB'), array('AR'), array('AG'), array('B'), array('BC'), array('BH'), array('BN'), array('BT'),
  62. array('BV'), array('BR'), array('BZ'), array('CS'), array('CL'), array('CJ'), array('CT'), array('CV'),
  63. array('DB'), array('DJ'), array('GL'), array('GR'), array('GJ'), array('HR'), array('HD'), array('IL'),
  64. array('IS'), array('IF'), array('MM'), array('MH'), array('MS'), array('NT'), array('OT'), array('PH'),
  65. array('SM'), array('SJ'), array('SB'), array('SV'), array('TR'), array('TM'), array('TL'), array('VS'),
  66. array('VL'), array('VN'), array('B1'), array('B2'), array('B3'), array('B4'), array('B5'), array('B6')
  67. );
  68. }
  69. public function invalidCountyCodeProvider()
  70. {
  71. return array(
  72. array('JK'), array('REW'), array('x'), array('FF'), array('aaaddadaada')
  73. );
  74. }
  75. public function validInputDataProvider()
  76. {
  77. return array(
  78. array(Person::GENDER_MALE, '1981-06-16','B2', true, '181061642'),
  79. array(Person::GENDER_FEMALE, '1981-06-16','B2', true, '281061642'),
  80. array(Person::GENDER_MALE, '1981-06-16','B2', false, '981061642'),
  81. array(Person::GENDER_FEMALE, '1981-06-16','B2', false, '981061642'),
  82. );
  83. }
  84. /**
  85. *
  86. */
  87. public function test_allRandom_returnsValidCnp()
  88. {
  89. $cnp = $this->faker->cnp;
  90. $this->assertTrue(
  91. $this->isValidCnp($cnp),
  92. sprintf("Invalid CNP '%' generated", $cnp)
  93. );
  94. }
  95. /**
  96. *
  97. */
  98. public function test_validGender_returnsValidCnp()
  99. {
  100. $cnp = $this->faker->cnp(Person::GENDER_MALE);
  101. $this->assertTrue(
  102. $this->isValidMaleCnp($cnp),
  103. sprintf("Invalid CNP '%' generated for '%s' gender", $cnp, Person::GENDER_MALE)
  104. );
  105. $cnp = $this->faker->cnp(Person::GENDER_FEMALE);
  106. $this->assertTrue(
  107. $this->isValidFemaleCnp($cnp),
  108. sprintf("Invalid CNP '%' generated for '%s' gender", $cnp, Person::GENDER_FEMALE)
  109. );
  110. }
  111. /**
  112. * @param string $value
  113. *
  114. * @dataProvider invalidGenderProvider
  115. */
  116. public function test_invalidGender_throwsException($value)
  117. {
  118. $this->setExpectedException('InvalidArgumentException');
  119. $this->faker->cnp($value);
  120. }
  121. /**
  122. * @param string $value year of birth
  123. *
  124. * @dataProvider validYearProvider
  125. */
  126. public function test_validYear_returnsValidCnp($value)
  127. {
  128. $cnp = $this->faker->cnp(null, $value);
  129. $this->assertTrue(
  130. $this->isValidCnp($cnp),
  131. sprintf("Invalid CNP '%' generated for valid year '%s'", $cnp, $value)
  132. );
  133. }
  134. /**
  135. * @param string $value year of birth
  136. *
  137. * @dataProvider invalidYearProvider
  138. */
  139. public function test_invalidYear_throwsException($value)
  140. {
  141. $this->setExpectedException('InvalidArgumentException');
  142. $this->faker->cnp(null, $value);
  143. }
  144. /**
  145. * @param $value
  146. * @dataProvider validCountyCodeProvider
  147. */
  148. public function test_validCountyCode_returnsValidCnp($value)
  149. {
  150. $cnp = $this->faker->cnp(null, null, $value);
  151. $this->assertTrue(
  152. $this->isValidCnp($cnp),
  153. sprintf("Invalid CNP '%' generated for valid year '%s'", $cnp, $value)
  154. );
  155. }
  156. /**
  157. * @param $value
  158. * @dataProvider invalidCountyCodeProvider
  159. */
  160. public function test_invalidCountyCode_throwsException($value)
  161. {
  162. $this->setExpectedException('InvalidArgumentException');
  163. $this->faker->cnp(null, null, $value);
  164. }
  165. /**
  166. *
  167. */
  168. public function test_nonResident_returnsValidCnp()
  169. {
  170. $cnp = $this->faker->cnp(null, null, null, false);
  171. $this->assertTrue(
  172. $this->isValidCnp($cnp),
  173. sprintf("Invalid CNP '%' generated for non resident", $cnp)
  174. );
  175. $this->assertStringStartsWith(
  176. '9',
  177. $cnp,
  178. sprintf("Invalid CNP '%' generated for non resident (should start with 9)", $cnp)
  179. );
  180. }
  181. /**
  182. *
  183. * @param $gender
  184. * @param $dateOfBirth
  185. * @param $county
  186. * @param $isResident
  187. * @param $expectedCnpStart
  188. *
  189. * @dataProvider validInputDataProvider
  190. */
  191. public function test_validInputData_returnsValidCnp($gender, $dateOfBirth, $county, $isResident, $expectedCnpStart)
  192. {
  193. $cnp = $this->faker->cnp($gender, $dateOfBirth, $county, $isResident);
  194. $this->assertStringStartsWith(
  195. $expectedCnpStart,
  196. $cnp,
  197. sprintf("Invalid CNP '%' generated for non valid data", $cnp)
  198. );
  199. }
  200. protected function isValidFemaleCnp($value)
  201. {
  202. return $this->isValidCnp($value) && in_array($value[0], array(2, 4, 6, 8, 9));
  203. }
  204. protected function isValidMaleCnp($value)
  205. {
  206. return $this->isValidCnp($value) && in_array($value[0], array(1, 3, 5, 7, 9));
  207. }
  208. protected function isValidCnp($cnp)
  209. {
  210. if (preg_match(static::TEST_CNP_REGEX, $cnp) !== false) {
  211. $checkNumber = 279146358279;
  212. $checksum = 0;
  213. foreach (range(0, 11) as $digit) {
  214. $checksum += (int)substr($cnp, $digit, 1) * (int)substr($checkNumber, $digit, 1);
  215. }
  216. $checksum = $checksum % 11;
  217. $checksum = $checksum == 10 ? 1 : $checksum;
  218. if ($checksum == substr($cnp, -1)) {
  219. return true;
  220. }
  221. }
  222. return false;
  223. }
  224. }