菜谱项目

PersonTest.php 7.0KB

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