-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5850721
commit 7dace66
Showing
6 changed files
with
204 additions
and
5 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
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,115 @@ | ||
<?php | ||
|
||
namespace Personnummer\Tests; | ||
|
||
use Jchook\AssertThrows\AssertThrows; | ||
use Personnummer\Personnummer; | ||
use Personnummer\PersonnummerException; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class InterimNumberTest extends TestCase | ||
{ | ||
use AssertThrows; | ||
|
||
private static ?array $interim = null; | ||
|
||
private array $options = [ | ||
'allowInterimNumber' => true, | ||
]; | ||
|
||
private static function init(): void | ||
{ | ||
if (self::$interim === null) { | ||
$data = json_decode(file_get_contents('https://raw.githubusercontent.com/personnummer/meta/master/testdata/interim.json'), true, 512, JSON_THROW_ON_ERROR); // phpcs:ignore | ||
self::$interim = array_map( | ||
static fn(array $p) => new PersonnummerData($p), | ||
$data, | ||
); | ||
} | ||
} | ||
|
||
public static function validProvider(): array | ||
{ | ||
self::init(); | ||
return array_map( | ||
static fn(PersonnummerData $p) => ['num' => $p], | ||
array_filter(self::$interim, static fn($p) => $p->valid) | ||
); | ||
} | ||
public static function invalidProvider(): array | ||
{ | ||
self::init(); | ||
return array_map( | ||
static fn(PersonnummerData $p) => ['num' => $p], | ||
array_filter(self::$interim, static fn($p) => !$p->valid) | ||
); | ||
} | ||
|
||
#[DataProvider('validProvider')] | ||
public function testValidateInterim(PersonnummerData $num): void | ||
{ | ||
self::assertTrue(Personnummer::valid($num->longFormat, $this->options)); | ||
self::assertTrue(Personnummer::valid($num->separatedFormat, $this->options)); | ||
} | ||
|
||
#[DataProvider('invalidProvider')] | ||
public function testValidateInvalidInterim(PersonnummerData $num): void | ||
{ | ||
self::assertFalse(Personnummer::valid($num->longFormat, $this->options)); | ||
self::assertFalse(Personnummer::valid($num->separatedFormat, $this->options)); | ||
} | ||
|
||
#[DataProvider('validProvider')] | ||
public function testIsInterim(PersonnummerData $num): void | ||
{ | ||
self::assertTrue(Personnummer::parse($num->longFormat, $this->options)->isInterimNumber()); | ||
self::assertTrue(Personnummer::parse($num->separatedFormat, $this->options)->isInterimNumber()); | ||
} | ||
|
||
#[DataProvider('validProvider')] | ||
public function testFormatLongInterim(PersonnummerData $num): void | ||
{ | ||
$p = Personnummer::parse($num->longFormat, $this->options); | ||
self::assertEquals($p->format(true), $num->longFormat); | ||
self::assertEquals($p->format(false), $num->separatedFormat); | ||
} | ||
|
||
#[DataProvider('validProvider')] | ||
public function testFormatShortInterim(PersonnummerData $num): void | ||
{ | ||
$p = Personnummer::parse($num->separatedFormat, $this->options); | ||
self::assertEquals($p->format(true), $num->longFormat); | ||
self::assertEquals($p->format(false), $num->separatedFormat); | ||
} | ||
|
||
#[DataProvider('invalidProvider')] | ||
public function testInvalidInterimThrows(PersonnummerData $num): void | ||
{ | ||
$this->assertThrows( | ||
PersonnummerException::class, | ||
fn () => Personnummer::parse($num->longFormat, $this->options) | ||
); | ||
$this->assertThrows( | ||
PersonnummerException::class, | ||
fn () => Personnummer::parse($num->separatedFormat, $this->options) | ||
); | ||
} | ||
|
||
#[DataProvider('validProvider')] | ||
public function testInterimThrowsIfNotActive(PersonnummerData $num): void | ||
{ | ||
$this->assertThrows( | ||
PersonnummerException::class, | ||
fn () => Personnummer::parse($num->longFormat, [ | ||
'allowInterimNumber' => false, | ||
]) | ||
); | ||
$this->assertThrows( | ||
PersonnummerException::class, | ||
fn () => Personnummer::parse($num->separatedFormat, [ | ||
'allowInterimNumber' => false, | ||
]) | ||
); | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
|
||
namespace Personnummer\Tests; | ||
|
||
class PersonnummerData | ||
{ | ||
public function __construct(array $p) | ||
{ | ||
$this->longFormat = $p['long_format']; | ||
$this->shortFormat = $p['short_format']; | ||
$this->separatedFormat = $p['separated_format']; | ||
$this->separatedLong = $p['separated_long']; | ||
$this->valid = $p['valid']; | ||
$this->type = $p['type']; | ||
$this->isMale = $p['isMale']; | ||
$this->isFemale = $p['isFemale']; | ||
} | ||
public string $longFormat; | ||
public string $shortFormat; | ||
public string $separatedFormat; | ||
public string $separatedLong; | ||
public bool $valid; | ||
public string $type; | ||
public bool $isMale; | ||
public bool $isFemale; | ||
} |
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