File tree Expand file tree Collapse file tree 2 files changed +67
-0
lines changed Expand file tree Collapse file tree 2 files changed +67
-0
lines changed Original file line number Diff line number Diff line change @@ -33,4 +33,17 @@ public function generate($number)
33
33
34
34
return (int ) (implode ('' , array_reverse ($ digits )) . abs ($ stack ));
35
35
}
36
+
37
+ /**
38
+ * Validates the given number.
39
+ *
40
+ * @param integer $number
41
+ * @return boolean
42
+ */
43
+ public function validate ($ number )
44
+ {
45
+ $ original = substr ($ number , 0 , strlen ($ number ) - 1 );
46
+
47
+ return $ this ->generate ($ original ) === $ number ;
48
+ }
36
49
}
Original file line number Diff line number Diff line change @@ -36,4 +36,58 @@ public function luhnProvider()
36
36
array (456 , 4564 ),
37
37
);
38
38
}
39
+
40
+ /**
41
+ * @test
42
+ * @dataProvider luhnValidProvider
43
+ *
44
+ * @param integer $number
45
+ * @param integer $expected
46
+ */
47
+ public function validatesLuhnChecksum ($ number )
48
+ {
49
+ $ luhn = new Luhn ();
50
+
51
+ $ this ->assertTrue ($ luhn ->validate ($ number ));
52
+ }
53
+
54
+
55
+ /**
56
+ * @return array
57
+ */
58
+ public function luhnValidProvider ()
59
+ {
60
+ return array (
61
+ array (799273982 ),
62
+ array (1230 ),
63
+ array (4564 ),
64
+ );
65
+ }
66
+
67
+ /**
68
+ * @test
69
+ * @dataProvider luhnInvalidProvider
70
+ *
71
+ * @param integer $number
72
+ * @param integer $expected
73
+ */
74
+ public function validatesInvalidLuhnChecksum ($ number )
75
+ {
76
+ $ luhn = new Luhn ();
77
+
78
+ $ this ->assertFalse ($ luhn ->validate ($ number ));
79
+ }
80
+
81
+
82
+ /**
83
+ * @return array
84
+ */
85
+ public function luhnInvalidProvider ()
86
+ {
87
+ return array (
88
+ array (799273983 ),
89
+ array (1231 ),
90
+ array (4565 ),
91
+ );
92
+ }
39
93
}
You can’t perform that action at this time.
0 commit comments