|
| 1 | +<?php |
| 2 | + |
| 3 | +use Webhkp\Pvalidate\Rules\EndWith; |
| 4 | +use Webhkp\Pvalidate\Rules\StartWith; |
| 5 | +use Webhkp\Pvalidate\ValidationBuilder; |
| 6 | +use Webhkp\Pvalidate\Validator; |
| 7 | + |
| 8 | +beforeEach(function () { |
| 9 | + $this->testObj = new class { |
| 10 | + public function __construct( |
| 11 | + ) { |
| 12 | + |
| 13 | + } |
| 14 | + |
| 15 | + #[StartWith('my')] |
| 16 | + public string $firstString = 'my first string'; |
| 17 | + |
| 18 | + #[StartWith('my')] |
| 19 | + public string $secondString = 'My second string'; |
| 20 | + |
| 21 | + #[StartWith('MY', true)] |
| 22 | + public string $thirdString = 'My third string'; |
| 23 | + |
| 24 | + #[EndWith('string')] |
| 25 | + public string $fourthString = 'my fourth string'; |
| 26 | + |
| 27 | + #[EndWith('STRING')] |
| 28 | + public string $fifthString = 'My fifth StrinG'; |
| 29 | + |
| 30 | + #[EndWith('string', true)] |
| 31 | + public string $sixthString = 'My sixth String'; |
| 32 | + |
| 33 | + #[StartWith('MY')] |
| 34 | + #[EndWith('string')] |
| 35 | + public string $seventhString = 'My seventh String'; |
| 36 | + |
| 37 | + #[StartWith('MY', true)] |
| 38 | + #[EndWith('STRING', true)] |
| 39 | + public string $eighthString = 'My eighth String'; |
| 40 | + }; |
| 41 | +}); |
| 42 | + |
| 43 | +describe("Start with Validation", function () { |
| 44 | + describe("Object attribute validation", function () { |
| 45 | + it('Should be valid', function () { |
| 46 | + $this->testObj->firstString = "my test string"; |
| 47 | + $this->testObj->secondString = "my test string"; |
| 48 | + $this->testObj->thirdString = "MY TEST string"; |
| 49 | + $this->testObj->fourthString = "my test string"; |
| 50 | + $this->testObj->fifthString = "my test STRING"; |
| 51 | + $this->testObj->sixthString = "my test STRING"; |
| 52 | + $this->testObj->seventhString = "MY test string"; |
| 53 | + $this->testObj->eighthString = "my test STRING"; |
| 54 | + |
| 55 | + $validationResponse = Validator::validate($this->testObj); |
| 56 | + |
| 57 | + expect($validationResponse->isValid())->toBeTrue(); |
| 58 | + expect($validationResponse->getErrors())->toBeEmpty(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('Should return errors', function () { |
| 62 | + $validationResponse = Validator::validate($this->testObj); |
| 63 | + |
| 64 | + expect($validationResponse->isValid())->toBeFalse(); |
| 65 | + expect($validationResponse->getErrors())->toHaveKeys(['secondString', 'fifthString', 'seventhString']); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + describe("Validation builder parsing", function () { |
| 70 | + it('Should return error for startwith violation', function () { |
| 71 | + $validation = ValidationBuilder::startWith("wrong wrong")->safeParse('quick brown fox jumps over the lazy dog'); |
| 72 | + |
| 73 | + expect($validation->isValid())->toBeFalse(); |
| 74 | + expect($validation->getErrors())->toHaveKeys(['startWith.errors.startWith']); |
| 75 | + }); |
| 76 | + |
| 77 | + it('Should return error for endWtith violation', function () { |
| 78 | + $validation = ValidationBuilder::endWith('wrong')->safeParse('quick brown fox jumps over the lazy dog'); |
| 79 | + |
| 80 | + expect($validation->isValid())->toBeFalse(); |
| 81 | + expect($validation->getErrors())->toHaveKeys(['endWith.errors.endWith']); |
| 82 | + }); |
| 83 | + |
| 84 | + it('Should return error for both startWith and endWith violation', function () { |
| 85 | + $validation = ValidationBuilder::startWith("wrong")->endWith('Something else'); |
| 86 | + |
| 87 | + $validationResult = $validation->safeParse('abc'); |
| 88 | + |
| 89 | + expect($validationResult->isValid())->toBeFalse(); |
| 90 | + expect($validationResult->getErrors())->toHaveKeys(['startWith.errors.startWith', 'endWith.errors.endWith']); |
| 91 | + }); |
| 92 | + }); |
| 93 | +}); |
| 94 | + |
| 95 | + |
0 commit comments