forked from fzaninotto/Faker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Valid National Code Generator to fa_IR
- Loading branch information
1 parent
76b1c25
commit cb01380
Showing
3 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace Faker\Test\Provider\fa_IR; | ||
|
||
use Faker\Provider\fa_IR\Person; | ||
use Faker\Generator; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class PersonTest extends TestCase | ||
{ | ||
|
||
/** | ||
* @var Generator | ||
*/ | ||
private $faker; | ||
|
||
public function setUp() | ||
{ | ||
$faker = new Generator(); | ||
$faker->addProvider(new Person($faker)); | ||
$this->faker = $faker; | ||
} | ||
|
||
public function testNationalCode() | ||
{ | ||
for ($i = 0; $i < 100; $i++) { | ||
$nationalCode = $this->faker->nationalCode; | ||
|
||
// nationalCode should be in the format ########## | ||
$this->assertRegExp('/^[0-9]{10}$/', $nationalCode); | ||
|
||
$areaCode = substr($nationalCode, 0, 3); | ||
$controlCode = substr($nationalCode, 9, 1); | ||
|
||
// the areaCode must in the format ###, excluding '000' | ||
$this->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) { | ||
$this->assertEquals($sum % 11, (int)$controlCode); | ||
} else { | ||
$this->assertEquals(11 - ($sum % 11), (int)$controlCode); | ||
} | ||
} | ||
} | ||
} |