File: PersonTest.php

package info (click to toggle)
php-faker 1.20.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 12,412 kB
  • sloc: php: 115,671; xml: 213; makefile: 49
file content (238 lines) | stat: -rw-r--r-- 6,517 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php

namespace Faker\Test\Provider\ro_RO;

use Faker\Provider\DateTime;
use Faker\Provider\ro_RO\Person;
use Faker\Test\TestCase;

/**
 * @group legacy
 */
final class PersonTest extends TestCase
{
    public 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]$/';

    protected function setUp(): void
    {
        parent::setUp();
        $this->skipOn32bitSystem();
    }

    public function invalidGenderProvider()
    {
        return [
            ['elf'],
            ['ent'],
            ['fmle'],
            ['mal'],
        ];
    }

    public function invalidYearProvider()
    {
        return [
            [1652],
            [1799],
            [2100],
            [2252],
        ];
    }

    public function validYearProvider()
    {
        return [
            [null],
            [''],
            [1800],
            [1850],
            [1900],
            [1990],
            [2000],
            [2099],
        ];
    }

    public function validCountyCodeProvider()
    {
        return [
            ['AB'], ['AR'], ['AG'], ['B'], ['BC'], ['BH'], ['BN'], ['BT'],
            ['BV'], ['BR'], ['BZ'], ['CS'], ['CL'], ['CJ'], ['CT'], ['CV'],
            ['DB'], ['DJ'], ['GL'], ['GR'], ['GJ'], ['HR'], ['HD'], ['IL'],
            ['IS'], ['IF'], ['MM'], ['MH'], ['MS'], ['NT'], ['OT'], ['PH'],
            ['SM'], ['SJ'], ['SB'], ['SV'], ['TR'], ['TM'], ['TL'], ['VS'],
            ['VL'], ['VN'], ['B1'], ['B2'], ['B3'], ['B4'], ['B5'], ['B6'],
        ];
    }

    public function invalidCountyCodeProvider()
    {
        return [
            ['JK'], ['REW'], ['x'], ['FF'], ['aaaddadaada'],
        ];
    }

    public function validInputDataProvider()
    {
        return [
            [Person::GENDER_MALE, '1981-06-16', 'B2', true, '181061642'],
            [Person::GENDER_FEMALE, '1981-06-16', 'B2', true, '281061642'],
            [Person::GENDER_MALE, '1981-06-16', 'B2', false, '981061642'],
            [Person::GENDER_FEMALE, '1981-06-16', 'B2', false, '981061642'],
        ];
    }

    public function testAllRandomReturnsValidCnp()
    {
        $cnp = $this->faker->cnp;
        self::assertTrue(
            $this->isValidCnp($cnp),
            sprintf("Invalid CNP '%' generated", $cnp)
        );
    }

    public function testValidGenderReturnsValidCnp()
    {
        $cnp = $this->faker->cnp(Person::GENDER_MALE);
        self::assertTrue(
            $this->isValidMaleCnp($cnp),
            sprintf("Invalid CNP '%' generated for '%s' gender", $cnp, Person::GENDER_MALE)
        );

        $cnp = $this->faker->cnp(Person::GENDER_FEMALE);
        self::assertTrue(
            $this->isValidFemaleCnp($cnp),
            sprintf("Invalid CNP '%' generated for '%s' gender", $cnp, Person::GENDER_FEMALE)
        );
    }

    /**
     * @param string $value
     *
     * @dataProvider invalidGenderProvider
     */
    public function testInvalidGenderThrowsException($value)
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->faker->cnp($value);
    }

    /**
     * @param string $value year of birth
     *
     * @dataProvider validYearProvider
     */
    public function testValidYearReturnsValidCnp($value)
    {
        $cnp = $this->faker->cnp(null, $value);
        self::assertTrue(
            $this->isValidCnp($cnp),
            sprintf("Invalid CNP '%' generated for valid year '%s'", $cnp, $value)
        );
    }

    /**
     * @param string $value year of birth
     *
     * @dataProvider invalidYearProvider
     */
    public function testInvalidYearThrowsException($value)
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->faker->cnp(null, $value);
    }

    /**
     * @param string $value
     * @dataProvider validCountyCodeProvider
     */
    public function testValidCountyCodeReturnsValidCnp($value)
    {
        $cnp = $this->faker->cnp(null, null, $value);
        self::assertTrue(
            $this->isValidCnp($cnp),
            sprintf("Invalid CNP '%' generated for valid year '%s'", $cnp, $value)
        );
    }

    /**
     * @param string $value
     * @dataProvider invalidCountyCodeProvider
     */
    public function testInvalidCountyCodeThrowsException($value)
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->faker->cnp(null, null, $value);
    }

    public function testNonResidentReturnsValidCnp()
    {
        $cnp = $this->faker->cnp(null, null, null, false);
        self::assertTrue(
            $this->isValidCnp($cnp),
            sprintf("Invalid CNP '%' generated for non resident", $cnp)
        );
        self::assertStringStartsWith(
            '9',
            $cnp,
            sprintf("Invalid CNP '%' generated for non resident (should start with 9)", $cnp)
        );
    }

    /**
     * @param string $gender
     * @param string $dateOfBirth
     * @param string $county
     * @param bool   $isResident
     * @param string $expectedCnpStart
     *
     * @dataProvider validInputDataProvider
     */
    public function testValidInputDataReturnsValidCnp($gender, $dateOfBirth, $county, $isResident, $expectedCnpStart)
    {
        $cnp = $this->faker->cnp($gender, $dateOfBirth, $county, $isResident);
        self::assertStringStartsWith(
            $expectedCnpStart,
            $cnp,
            sprintf("Invalid CNP '%' generated for non valid data", $cnp)
        );
    }

    protected function isValidFemaleCnp($value)
    {
        return $this->isValidCnp($value) && in_array($value[0], [2, 4, 6, 8, 9], false);
    }

    protected function isValidMaleCnp($value)
    {
        return $this->isValidCnp($value) && in_array($value[0], [1, 3, 5, 7, 9], false);
    }

    protected function isValidCnp($cnp)
    {
        if (preg_match(static::TEST_CNP_REGEX, $cnp) !== false) {
            $checkNumber = 279146358279;

            $checksum = 0;

            foreach (range(0, 11) as $digit) {
                $checksum += (int) substr($cnp, $digit, 1) * (int) substr($checkNumber, $digit, 1);
            }
            $checksum %= 11;
            $checksum = $checksum == 10 ? 1 : $checksum;

            if ($checksum == substr($cnp, -1)) {
                return true;
            }
        }

        return false;
    }

    protected function getProviders(): iterable
    {
        yield new Person($this->faker);

        yield new DateTime($this->faker);
    }
}