Skip to content

Commit

Permalink
Refactor SimpleWithMod11 validator and update IdCardRuleTest
Browse files Browse the repository at this point in the history
  • Loading branch information
parsilver committed Mar 31, 2024
1 parent f49951b commit 5987f5b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 22 deletions.
5 changes: 0 additions & 5 deletions src/Algorithms/SimpleWithMod11.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ class SimpleWithMod11 implements Validator
*/
public function validate(string $id): void
{
// Check if the id length is exactly 13
if (strlen($id) !== 13) {
throw new InvalidThaiCitizenIdException('The id must be 13 digits.');
}

// Calculate the sum of each digit multiplied by the weight
$sum = 0;
for ($i = 0; $i < 12; $i++) {
Expand Down
27 changes: 10 additions & 17 deletions tests/Laravel/IdCardRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,21 @@
$validator->validate();
})->throws('The id must be 13 digits.');

it('should match implementation when using Laravel 8', function () {
if (version_compare(app()->version(), '8.0.0', '<')) {
$this->markTestSkipped('This test only run on Laravel 8 or above.');
}

it('should match implementation when using Laravel', function () {
$validator = Validator::make(
['id_card' => '1410127443236'],
['id_card' => new IdCard]
['id_card' => $rule = new IdCard]
);

expect($validator->passes())->toBeTrue();
});

it('should match implementation when using Laravel 10', function () {
if (version_compare(app()->version(), '8.0.0', '>=')) {
$this->markTestSkipped('This test only run on Laravel 10 or below.');
if (version_compare(app()->version(), '10.0.0', '<')) {
expect($rule)->toBeInstanceOf(Illuminate\Contracts\Validation\Rule::class);
expect($rule->message())->toBeString();
} else {
expect($rule)->toBeInstanceOf(Illuminate\Contracts\Validation\ValidationRule::class);
$rule->validate('id_card', '1410127443236', function ($message) {
expect($message)->toBeString();
});
}

$validator = Validator::make(
['id_card' => '1410127443236'],
['id_card' => new IdCard]
);

expect($validator->passes())->toBeTrue();
});

0 comments on commit 5987f5b

Please sign in to comment.