LuhnTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /*
  3. * By adding type hints and enabling strict type checking, code can become
  4. * easier to read, self-documenting and reduce the number of potential bugs.
  5. * By default, type declarations are non-strict, which means they will attempt
  6. * to change the original type to match the type specified by the
  7. * type-declaration.
  8. *
  9. * In other words, if you pass a string to a function requiring a float,
  10. * it will attempt to convert the string value to a float.
  11. *
  12. * To enable strict mode, a single declare directive must be placed at the top
  13. * of the file.
  14. * This means that the strictness of typing is configured on a per-file basis.
  15. * This directive not only affects the type declarations of parameters, but also
  16. * a function's return type.
  17. *
  18. * For more info review the Concept on strict type checking in the PHP track
  19. * <link>.
  20. *
  21. * To disable strict typing, comment out the directive below.
  22. */
  23. declare(strict_types=1);
  24. class LuhnTest extends PHPUnit\Framework\TestCase
  25. {
  26. public static function setUpBeforeClass(): void
  27. {
  28. require_once 'Luhn.php';
  29. }
  30. public function testSingleDigitInvalid(): void
  31. {
  32. $this->assertFalse(isValid("1"));
  33. }
  34. public function testSingleZeroInvalid(): void
  35. {
  36. $this->assertFalse(isValid("0"));
  37. }
  38. public function testSimpleValidNumber(): void
  39. {
  40. $this->assertTrue(isValid("59"));
  41. }
  42. public function testSpaceHandling(): void
  43. {
  44. $this->assertTrue(isValid(" 5 9 "));
  45. }
  46. public function testValidCanadianSocialInsuranceNumber(): void
  47. {
  48. $this->assertTrue(isValid("046 454 286"));
  49. }
  50. public function testInvalidCanadianSocialInsuranceNumber(): void
  51. {
  52. $this->assertFalse(isValid("046 454 287"));
  53. }
  54. public function testInvalidCreditCard(): void
  55. {
  56. $this->assertFalse(isValid("8273 1232 7352 0569"));
  57. }
  58. public function testNonDigitCharacterInValidStringInvalidatesTheString(): void
  59. {
  60. $this->assertFalse(isValid("046a 454 286"));
  61. }
  62. public function testThatStringContainingSymbolsIsInvalid(): void
  63. {
  64. $this->assertFalse(isValid("055£ 444$ 285"));
  65. }
  66. public function testThatStringContainingPunctuationIsInvalid(): void
  67. {
  68. $this->assertFalse(isValid("055-444-285"));
  69. }
  70. public function testSpaceAndSingleZeroIsInvalid(): void
  71. {
  72. $this->assertFalse(isValid(" 0"));
  73. }
  74. public function testMultipleZerosIsValid(): void
  75. {
  76. $this->assertTrue(isValid(" 00000"));
  77. }
  78. public function testThatDoublingNineIsHandledCorrectly(): void
  79. {
  80. $this->assertTrue(isValid("091"));
  81. }
  82. public function testThatStringContainingSymbolsWhichCouldBeZeroIsInvalid(): void
  83. {
  84. $this->assertFalse(isValid(" ABCDEF"));
  85. }
  86. }