|
5 | 5 | * when used in MaplePHP framework you can skip the "bash code" at top and the "autoload file"!
|
6 | 6 | */
|
7 | 7 | use MaplePHP\Unitary\Unit;
|
| 8 | +use MaplePHP\Validate\Inp; |
| 9 | + |
| 10 | + |
8 | 11 |
|
9 | 12 |
|
10 | 13 | // If you add true to Unit it will run in quite mode
|
|
14 | 17 | // Add a title to your tests (not required)
|
15 | 18 | $unit->addTitle("Testing MaplePHP validation library!");
|
16 | 19 |
|
17 |
| -$unit->add("Validating values", function($inst) { |
| 20 | +$unit->add("Validating values", callback: function($inst) { |
| 21 | + |
| 22 | + $strVal = Inp::value("TestStringValue"); |
| 23 | + $testStrValidates = ["isString", "required", "hasValue"]; |
| 24 | + |
| 25 | + foreach ($testStrValidates as $validate) { |
| 26 | + $inst->add($strVal->{$validate}(), [ |
| 27 | + "equal" => [true], |
| 28 | + ], "Expect {$validate} to be true"); |
| 29 | + } |
| 30 | + |
| 31 | + $inst->add(Inp::value("8808218329")->socialNumber(), [ |
| 32 | + "equal" => [false], |
| 33 | + ], "Expect socialNumber to be false"); |
| 34 | + |
| 35 | + |
| 36 | + $inst->add(Inp::value("4030000010001234")->creditCard(), [ |
| 37 | + "equal" => [true], |
| 38 | + ], "Expect creditCard to be true"); |
| 39 | + |
| 40 | + $inst->add(Inp::value("john.doe-gmail.com")->email(), [ |
| 41 | + "equal" => [false], |
| 42 | + ], "Expect creditCard to be false"); |
| 43 | + |
| 44 | + $inst->add(Inp::value("Hello world!")->findInString("world"), [ |
| 45 | + "equal" => [true], |
| 46 | + ], "Expect findInString to be true"); |
| 47 | + |
| 48 | + $inst->add(Inp::value("+46 (0) 702-83 27 12")->phone(), [ |
| 49 | + "equal" => [true], |
| 50 | + ], "Expect phone to be true"); |
| 51 | + |
| 52 | + $inst->add(Inp::value("252522")->zip(5), [ |
| 53 | + "equal" => [true], |
| 54 | + ], "Expect zip to be true"); |
| 55 | + |
| 56 | + $testDataTypeValidations = ['isString', 'isInt', 'isFloat', 'isArray', 'isObject', 'isBool']; |
| 57 | + $inst->add(Inp::value("Is string")->isString(), [ |
| 58 | + "equal" => [true], |
| 59 | + ], "Expect isString to be true"); |
| 60 | + |
| 61 | + $inst->add(Inp::value(122)->isInt(), [ |
| 62 | + "equal" => [true], |
| 63 | + ], "Expect isInt to be true"); |
| 64 | + |
| 65 | + $inst->add(Inp::value(22.12)->isFloat(), [ |
| 66 | + "equal" => [true], |
| 67 | + ], "Expect isFloat to be true"); |
| 68 | + |
| 69 | + $inst->add(Inp::value([1, 2, 3])->isArray(), [ |
| 70 | + "equal" => [true], |
| 71 | + ], "Expect isArray to be true"); |
| 72 | + |
| 73 | + $inst->add(Inp::value(new stdClass())->isObject(), [ |
| 74 | + "equal" => [true], |
| 75 | + ], "Expect isObject to be true"); |
| 76 | + |
| 77 | + $inst->add(Inp::value(false)->isBool(), [ |
| 78 | + "equal" => [true], |
| 79 | + ], "Expect isBool to be true"); |
| 80 | + |
| 81 | + $inst->add(Inp::value("222.33")->number(), [ |
| 82 | + "equal" => [true], |
| 83 | + ], "Expect number to be true"); |
| 84 | + |
| 85 | + $inst->add(Inp::value(100)->positive(), [ |
| 86 | + "equal" => [true], |
| 87 | + ], "Expect positive to be true"); |
| 88 | + |
| 89 | + $inst->add(Inp::value(-100)->negative(), [ |
| 90 | + "equal" => [true], |
| 91 | + ], "Expect negative to be true"); |
| 92 | + |
| 93 | + $inst->add(Inp::value(10)->min(10), [ |
| 94 | + "equal" => [true], |
| 95 | + ], "Expect min to be true"); |
| 96 | + |
| 97 | + $inst->add(Inp::value(10)->max(10), [ |
| 98 | + "equal" => [true], |
| 99 | + ], "Expect max to be true"); |
18 | 100 |
|
19 |
| - $inst->add("TestValue", [ |
| 101 | + $inst->add(Inp::value("Lorem ipsum")->length(1, 11), [ |
| 102 | + "equal" => [true], |
| 103 | + ], "Expect length to be true"); |
| 104 | + |
| 105 | + $inst->add(Inp::value("22222")->equalLength(5), [ |
| 106 | + "equal" => [true], |
| 107 | + ], "Expect equalLength to be true"); |
| 108 | + |
| 109 | + $inst->add(Inp::value("hello")->equal("hello"), [ |
| 110 | + "equal" => [true], |
| 111 | + ], "Expect equal to be true"); |
| 112 | + |
| 113 | + $inst->add(Inp::value("world")->notEqual("hello"), [ |
| 114 | + "equal" => [true], |
| 115 | + ], "Expect notEqual to be true"); |
| 116 | + |
| 117 | + $inst->add(Inp::value("1.2.3")->validVersion(true), [ |
| 118 | + "equal" => [true], |
| 119 | + ], "Expect validVersion to be true"); |
| 120 | + |
| 121 | + $inst->add(Inp::value("1.2.0")->versionCompare("1.2.0"), [ |
| 122 | + "equal" => [true], |
| 123 | + ], "Expect versionCompare to be true"); |
| 124 | + |
| 125 | + $inst->add(Inp::value("MyStrongPass")->lossyPassword(), [ |
| 126 | + "equal" => [true], |
| 127 | + ], "Expect lossyPassword to be true"); |
| 128 | + |
| 129 | + $inst->add(Inp::value("My@StrongPass12")->strictPassword(), [ |
| 130 | + "equal" => [true], |
| 131 | + ], "Expect strictPassword to be true"); |
| 132 | + |
| 133 | + $inst->add(Inp::value("HelloWorld")->atoZ(), [ |
| 134 | + "equal" => [true], |
| 135 | + ], "Expect atoZ to be true"); |
| 136 | + |
| 137 | + $inst->add(Inp::value("welloworld")->lowerAtoZ(), [ |
| 138 | + "equal" => [true], |
| 139 | + ], "Expect lowerAtoZ to be true"); |
| 140 | + |
| 141 | + $inst->add(Inp::value("HELLOWORLD")->upperAtoZ(), [ |
| 142 | + "equal" => [true], |
| 143 | + ], "Expect upperAtoZ to be true"); |
| 144 | + |
| 145 | + $inst->add(Inp::value("#F1F1F1")->hex(), [ |
| 146 | + "equal" => [true], |
| 147 | + ], "Expect hex to be true"); |
| 148 | + |
| 149 | + $inst->add(Inp::value("1922-03-01")->date(), [ |
| 150 | + "equal" => [true], |
| 151 | + ], "Expect date to be true"); |
| 152 | + |
| 153 | + $inst->add(Inp::value("1988-08-21")->age(36), [ |
| 154 | + "equal" => [true], |
| 155 | + ], "Expect age to be true"); |
| 156 | + |
| 157 | + $inst->add(Inp::value("example.se")->domain(), [ |
| 158 | + "equal" => [true], |
| 159 | + ], "Expect domain to be true"); |
| 160 | + |
| 161 | + $inst->add(Inp::value("https://example.se")->url(), [ |
| 162 | + "equal" => [true], |
| 163 | + ], "Expect url to be true"); |
| 164 | + |
| 165 | + $inst->add(Inp::value("examplethatwillfail.se")->dns(), [ |
| 166 | + "equal" => [false], |
| 167 | + ], "Expect dns to be false"); |
| 168 | + |
| 169 | + $inst->add(Inp::value("Lorem ipsum")->oneOf([ |
| 170 | + "length" => [120, 200], |
20 | 171 | "isString" => []
|
21 |
| - ], "Is not a string"); |
| 172 | + ]), [ |
| 173 | + "equal" => [true], |
| 174 | + ], "Expect oneOf to be true"); |
22 | 175 |
|
23 |
| - $inst->add("600", [ |
24 |
| - "isInt" => [] |
25 |
| - ], "Is not int"); |
| 176 | + $inst->add(Inp::value("Lorem ipsum")->allOf([ |
| 177 | + "length" => [1, 200], |
| 178 | + "isString" => [] |
| 179 | + ]), [ |
| 180 | + "equal" => [true], |
| 181 | + ], "Expect allOf to be true"); |
26 | 182 |
|
27 |
| - $inst->add("600.33", [ |
28 |
| - "isFloat" => [] |
29 |
| - ], "Is not float"); |
| 183 | + $inst->add(Inp::value("required")->required(), [ |
| 184 | + "equal" => [true], |
| 185 | + ], "Expect required to be true"); |
30 | 186 |
|
31 |
| - $inst->add(true, [ |
32 |
| - "isBool" => [] |
33 |
| - ], "Is not bool"); |
| 187 | + $inst->add(Inp::value("required")->required(), [ |
| 188 | + "equal" => [true], |
| 189 | + ], "Expect required to be true"); |
34 | 190 |
|
35 |
| - $inst->add("yes", [ |
36 |
| - "isBoolVal" => [] |
37 |
| - ], "Is not bool"); |
| 191 | + $inst->add(Inp::value("required")->required(), [ |
| 192 | + "equal" => [true], |
| 193 | + ], "Expect required to be true"); |
| 194 | + |
| 195 | + $inst->add(Inp::value("required")->required(), [ |
| 196 | + "equal" => [true], |
| 197 | + ], "Expect required to be true"); |
38 | 198 |
|
39 | 199 | });
|
40 | 200 |
|
41 | 201 | $unit->execute();
|
42 |
| - |
|
0 commit comments