| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Give\Vendors\StellarWP\Validation\Tests\Unit; |
| 6 |
|
| 7 |
use Closure; |
| 8 |
use InvalidArgumentException; |
| 9 |
use Give\Vendors\StellarWP\Validation\Commands\ExcludeValue; |
| 10 |
use Give\Vendors\StellarWP\Validation\Commands\SkipValidationRules; |
| 11 |
use Give\Vendors\StellarWP\Validation\Config; |
| 12 |
use Give\Vendors\StellarWP\Validation\Contracts\Sanitizer; |
| 13 |
use Give\Vendors\StellarWP\Validation\Contracts\ValidationRule; |
| 14 |
use Give\Vendors\StellarWP\Validation\Tests\TestCase; |
| 15 |
use Give\Vendors\StellarWP\Validation\ValidationRulesRegistrar; |
| 16 |
use Give\Vendors\StellarWP\Validation\Validator; |
| 17 |
|
| 18 |
/** |
| 19 |
* @covers \Give\Vendors\StellarWP\Validation\Validator |
| 20 |
* |
| 21 |
* @since 1.0.0 |
| 22 |
*/ |
| 23 |
class ValidatorTest extends TestCase |
| 24 |
{ |
| 25 |
public function setUp(): void |
| 26 |
{ |
| 27 |
parent::setUp(); |
| 28 |
|
| 29 |
$this->mockValidationRulesRegister(); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* @since 1.0.0 |
| 34 |
*/ |
| 35 |
public function testValidatorPasses() |
| 36 |
{ |
| 37 |
$validator = new Validator( |
| 38 |
[ |
| 39 |
'name' => ['required'], |
| 40 |
'email' => ['required'], |
| 41 |
], |
| 42 |
[ |
| 43 |
'name' => 'Bill Murray', |
| 44 |
'email' => '[email protected]', |
| 45 |
] |
| 46 |
); |
| 47 |
|
| 48 |
self::assertTrue($validator->passes()); |
| 49 |
self::assertFalse($validator->fails()); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* @since 1.0.0 |
| 54 |
*/ |
| 55 |
public function testValidatorAcceptsArraysAsRules() |
| 56 |
{ |
| 57 |
$validator = new Validator([ |
| 58 |
'foo' => ['required'], |
| 59 |
'bar' => ['required'], |
| 60 |
], [ |
| 61 |
'foo' => 'foo', |
| 62 |
'bar' => 'bar', |
| 63 |
]); |
| 64 |
|
| 65 |
$this->assertTrue($validator->passes()); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* @since 1.0.0 |
| 70 |
*/ |
| 71 |
public function testFailingValidations() |
| 72 |
{ |
| 73 |
$validator = new Validator([ |
| 74 |
'foo' => ['required'], |
| 75 |
'bar' => ['required'], |
| 76 |
], [ |
| 77 |
'foo' => 'foo', |
| 78 |
'bar' => '', |
| 79 |
]); |
| 80 |
|
| 81 |
self::assertTrue($validator->fails()); |
| 82 |
self::assertFalse($validator->passes()); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* @since 1.0.0 |
| 87 |
*/ |
| 88 |
public function testReturnsErrorsForFailedValidations() |
| 89 |
{ |
| 90 |
$validator = new Validator([ |
| 91 |
'foo' => ['required'], |
| 92 |
'bar' => ['required'], |
| 93 |
], [ |
| 94 |
'foo' => 'foo', |
| 95 |
'bar' => '', |
| 96 |
]); |
| 97 |
|
| 98 |
self::assertEquals([ |
| 99 |
'bar' => 'bar required', |
| 100 |
], $validator->errors()); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* @since 1.0.0 |
| 105 |
*/ |
| 106 |
public function testUsesLabelsWhenAvailableInErrorMessage() |
| 107 |
{ |
| 108 |
$validator = new Validator([ |
| 109 |
'foo' => ['required'], |
| 110 |
'bar' => ['required'], |
| 111 |
], [ |
| 112 |
'foo' => '', |
| 113 |
'bar' => '', |
| 114 |
], [ |
| 115 |
'bar' => 'Bar', |
| 116 |
]); |
| 117 |
|
| 118 |
self::assertEquals([ |
| 119 |
'foo' => 'foo required', |
| 120 |
'bar' => 'Bar required', |
| 121 |
], $validator->errors()); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* @since 1.0.0 |
| 126 |
*/ |
| 127 |
public function testReturnsValidatedValues() |
| 128 |
{ |
| 129 |
$validator = new Validator([ |
| 130 |
'foo' => ['required'], |
| 131 |
'bar' => ['required'], |
| 132 |
], [ |
| 133 |
'foo' => 'foo', |
| 134 |
'bar' => 'bar', |
| 135 |
]); |
| 136 |
|
| 137 |
self::assertEquals([ |
| 138 |
'foo' => 'foo', |
| 139 |
'bar' => 'bar', |
| 140 |
], $validator->validated()); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* @since 1.0.0 |
| 145 |
*/ |
| 146 |
public function testValuesWithoutRulesAreOmitted() |
| 147 |
{ |
| 148 |
$validator = new Validator([ |
| 149 |
'foo' => ['required'], |
| 150 |
], [ |
| 151 |
'foo' => 'foo', |
| 152 |
'bar' => 'bar', |
| 153 |
]); |
| 154 |
|
| 155 |
self::assertEquals([ |
| 156 |
'foo' => 'foo', |
| 157 |
], $validator->validated()); |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* @since 1.0.0 |
| 162 |
*/ |
| 163 |
public function testRuleArraysWithoutRulesAreConsideredOptional() |
| 164 |
{ |
| 165 |
// When no rules are specified, the field is considered optional. This simply means that |
| 166 |
// whatever value is passed in will be returned as validated. Keep in mind that if the rule |
| 167 |
// is entirely omitted then the value will also be omitted. |
| 168 |
$validator = new Validator([ |
| 169 |
'foo' => [], |
| 170 |
], [ |
| 171 |
'foo' => 'foo', |
| 172 |
'bar' => 'bar', |
| 173 |
]); |
| 174 |
|
| 175 |
self::assertEquals([ |
| 176 |
'foo' => 'foo', |
| 177 |
], $validator->validated()); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* @since 1.1.0 |
| 182 |
*/ |
| 183 |
public function testRulesThatReturnExcludeValuePreventValidation() |
| 184 |
{ |
| 185 |
$validator = new Validator([ |
| 186 |
'foo' => ['exclude', 'required'], |
| 187 |
], [ |
| 188 |
'foo' => '', |
| 189 |
]); |
| 190 |
|
| 191 |
self::assertEquals([], $validator->validated()); |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* @since 1.0.0 |
| 196 |
*/ |
| 197 |
public function testRulesWithSanitizationAreApplied() |
| 198 |
{ |
| 199 |
$validator = new Validator([ |
| 200 |
'name' => ['required'], |
| 201 |
'age' => ['required', 'integer'], |
| 202 |
], [ |
| 203 |
'name' => 'Bill Murray', |
| 204 |
'age' => '72', |
| 205 |
]); |
| 206 |
|
| 207 |
self::assertSame([ |
| 208 |
'name' => 'Bill Murray', |
| 209 |
'age' => 72, |
| 210 |
], $validator->validated()); |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* @since 1.1.0 |
| 215 |
*/ |
| 216 |
public function testWithSkipValidationRulesSkipsRemainingRules() |
| 217 |
{ |
| 218 |
$validator = new Validator([ |
| 219 |
'foo' => ['skip', 'required'], |
| 220 |
'bar' => ['required'], |
| 221 |
], [ |
| 222 |
'foo' => '', |
| 223 |
'bar' => 'bar', |
| 224 |
]); |
| 225 |
|
| 226 |
self::assertSame([ |
| 227 |
'foo' => '', |
| 228 |
'bar' => 'bar', |
| 229 |
], $validator->validated()); |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* @since 1.0.0 |
| 234 |
*/ |
| 235 |
public function testInvalidRulesThrowInvalidArgumentException() |
| 236 |
{ |
| 237 |
$this->expectException(InvalidArgumentException::class); |
| 238 |
$this->expectExceptionMessage( |
| 239 |
'Validation rules must be an instance of ValidationRuleSet or a compatible array' |
| 240 |
); |
| 241 |
|
| 242 |
new Validator([ |
| 243 |
'foo' => 'wrong', |
| 244 |
], [ |
| 245 |
'foo' => 'foo', |
| 246 |
]); |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Adds the validation register to the container, and adds a mock validation rule |
| 251 |
* |
| 252 |
* @since 1.0.0 |
| 253 |
*/ |
| 254 |
private function mockValidationRulesRegister() |
| 255 |
{ |
| 256 |
Config::getServiceContainer()->singleton( |
| 257 |
ValidationRulesRegistrar::class, |
| 258 |
function () { |
| 259 |
$register = new ValidationRulesRegistrar(); |
| 260 |
$register->register( |
| 261 |
MockRequiredRule::class, |
| 262 |
MockIntegerRule::class, |
| 263 |
MockExcludeRule::class, |
| 264 |
MockSkipRule::class |
| 265 |
); |
| 266 |
|
| 267 |
return $register; |
| 268 |
} |
| 269 |
); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
class MockSkipRule implements ValidationRule |
| 274 |
{ |
| 275 |
public static function id(): string |
| 276 |
{ |
| 277 |
return 'skip'; |
| 278 |
} |
| 279 |
|
| 280 |
public static function fromString(?string $options = null): ValidationRule |
| 281 |
{ |
| 282 |
return new self(); |
| 283 |
} |
| 284 |
|
| 285 |
public function __invoke($value, Closure $fail, string $key, array $values): SkipValidationRules |
| 286 |
{ |
| 287 |
return new SkipValidationRules(); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
class MockRequiredRule implements ValidationRule |
| 292 |
{ |
| 293 |
/** |
| 294 |
* @inheritDoc |
| 295 |
*/ |
| 296 |
public static function id(): string |
| 297 |
{ |
| 298 |
return 'required'; |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* @inheritDoc |
| 303 |
*/ |
| 304 |
public static function fromString(?string $options = null): ValidationRule |
| 305 |
{ |
| 306 |
return new self(); |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* @inheritDoc |
| 311 |
*/ |
| 312 |
public function __invoke($value, Closure $fail, string $key, array $values) |
| 313 |
{ |
| 314 |
if (empty($value)) { |
| 315 |
$fail('{field} required'); |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
class MockIntegerRule implements ValidationRule, Sanitizer |
| 321 |
{ |
| 322 |
/** |
| 323 |
* @inheritDoc |
| 324 |
*/ |
| 325 |
public static function id(): string |
| 326 |
{ |
| 327 |
return 'integer'; |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* @inheritDoc |
| 332 |
*/ |
| 333 |
public static function fromString(?string $options = null): ValidationRule |
| 334 |
{ |
| 335 |
return new self(); |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* @inheritDoc |
| 340 |
*/ |
| 341 |
public function __invoke($value, Closure $fail, string $key, array $values) |
| 342 |
{ |
| 343 |
if (!is_numeric($value)) { |
| 344 |
$fail('{field} must be an integer'); |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
/** |
| 349 |
* @inheritDoc |
| 350 |
*/ |
| 351 |
public function sanitize($value) |
| 352 |
{ |
| 353 |
return (int)$value; |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
class MockExcludeRule implements ValidationRule |
| 358 |
{ |
| 359 |
public static function id(): string |
| 360 |
{ |
| 361 |
return 'exclude'; |
| 362 |
} |
| 363 |
|
| 364 |
public static function fromString(?string $options = null): ValidationRule |
| 365 |
{ |
| 366 |
return new self(); |
| 367 |
} |
| 368 |
|
| 369 |
public function __invoke($value, Closure $fail, string $key, array $values) |
| 370 |
{ |
| 371 |
return new ExcludeValue(); |
| 372 |
} |
| 373 |
} |
| 374 |
|