LuhnTest.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace Faker\Test\Calculator;
  3. use Faker\Calculator\Luhn;
  4. use PHPUnit\Framework\TestCase;
  5. class LuhnTest extends TestCase
  6. {
  7. public function checkDigitProvider()
  8. {
  9. return array(
  10. array('7992739871', '3'),
  11. array('3852000002323', '7'),
  12. array('37144963539843', '1'),
  13. array('561059108101825', '0'),
  14. array('601100099013942', '4'),
  15. array('510510510510510', '0'),
  16. array(7992739871, '3'),
  17. array(3852000002323, '7'),
  18. array('37144963539843', '1'),
  19. array('561059108101825', '0'),
  20. array('601100099013942', '4'),
  21. array('510510510510510', '0')
  22. );
  23. }
  24. /**
  25. * @dataProvider checkDigitProvider
  26. */
  27. public function testComputeCheckDigit($partialNumber, $checkDigit)
  28. {
  29. $this->assertInternalType('string', $checkDigit);
  30. $this->assertEquals($checkDigit, Luhn::computeCheckDigit($partialNumber));
  31. }
  32. public function validatorProvider()
  33. {
  34. return array(
  35. array('79927398710', false),
  36. array('79927398711', false),
  37. array('79927398712', false),
  38. array('79927398713', true),
  39. array('79927398714', false),
  40. array('79927398715', false),
  41. array('79927398716', false),
  42. array('79927398717', false),
  43. array('79927398718', false),
  44. array('79927398719', false),
  45. array(79927398713, true),
  46. array(79927398714, false),
  47. );
  48. }
  49. /**
  50. * @dataProvider validatorProvider
  51. */
  52. public function testIsValid($number, $isValid)
  53. {
  54. $this->assertEquals($isValid, Luhn::isValid($number));
  55. }
  56. /**
  57. * @expectedException InvalidArgumentException
  58. * @expectedExceptionMessage Argument should be an integer.
  59. */
  60. public function testGenerateLuhnNumberWithInvalidPrefix()
  61. {
  62. Luhn::generateLuhnNumber('abc');
  63. }
  64. }