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 (48 lines) | stat: -rw-r--r-- 1,301 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
<?php

namespace Faker\Test\Provider\fa_IR;

use Faker\Provider\fa_IR\Person;
use Faker\Test\TestCase;

/**
 * @group legacy
 */
final class PersonTest extends TestCase
{
    public function testNationalCode()
    {
        for ($i = 0; $i < 100; ++$i) {
            $nationalCode = $this->faker->nationalCode;

            // nationalCode should be in the format ##########
            self::assertMatchesRegularExpression('/^[0-9]{10}$/', $nationalCode);

            $areaCode = substr($nationalCode, 0, 3);
            $controlCode = substr($nationalCode, 9, 1);

            // the areaCode must in the format ###, excluding '000'
            self::assertNotEquals('000', $areaCode);

            // the controlCode should comply with the Iranian National Code validation algorithm
            $sum = 0;
            $count = 0;

            for ($j = 10; $j > 1; --$j) {
                $sum += $nationalCode[$count] * ($j);
                ++$count;
            }

            if (($sum % 11) < 2) {
                self::assertEquals($sum % 11, (int) $controlCode);
            } else {
                self::assertEquals(11 - ($sum % 11), (int) $controlCode);
            }
        }
    }

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