File: LuhnTest.php

package info (click to toggle)
php-faker 1.20.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 12,704 kB
  • sloc: php: 115,692; xml: 213; makefile: 49
file content (66 lines) | stat: -rw-r--r-- 1,894 bytes parent folder | download
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
<?php

namespace Faker\Test\Calculator;

use Faker\Calculator\Luhn;
use Faker\Test\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;

final class LuhnTest extends TestCase
{
    public static function checkDigitProvider()
    {
        return [
            ['7992739871', '3'],
            ['3852000002323', '7'],
            ['37144963539843', '1'],
            ['561059108101825', '0'],
            ['601100099013942', '4'],
            ['510510510510510', '0'],
            [7992739871, '3'],
            [3852000002323, '7'],
            ['37144963539843', '1'],
            ['561059108101825', '0'],
            ['601100099013942', '4'],
            ['510510510510510', '0'],
        ];
    }

    #[DataProvider('checkDigitProvider')]
    public function testComputeCheckDigit($partialNumber, $checkDigit)
    {
        self::assertIsString($checkDigit);
        self::assertEquals($checkDigit, Luhn::computeCheckDigit($partialNumber));
    }

    public static function validatorProvider()
    {
        return [
            ['79927398710', false],
            ['79927398711', false],
            ['79927398712', false],
            ['79927398713', true],
            ['79927398714', false],
            ['79927398715', false],
            ['79927398716', false],
            ['79927398717', false],
            ['79927398718', false],
            ['79927398719', false],
            [79927398713, true],
            [79927398714, false],
        ];
    }

    #[DataProvider('validatorProvider')]
    public function testIsValid($number, $isValid)
    {
        self::assertEquals($isValid, Luhn::isValid($number));
    }

    public function testGenerateLuhnNumberWithInvalidPrefix()
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage('Argument should be an integer.');
        Luhn::generateLuhnNumber('abc');
    }
}