|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Webdevcave\SchemaValidator\Tests\Schemas; |
| 4 | + |
| 5 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 6 | +use stdClass; |
| 7 | +use Webdevcave\SchemaValidator\Schemas\BaseSchema; |
| 8 | +use Webdevcave\SchemaValidator\Schemas\StringSchema; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use Webdevcave\SchemaValidator\Validator; |
| 11 | + |
| 12 | +#[CoversClass(Validator::class)] |
| 13 | +#[CoversClass(BaseSchema::class)] |
| 14 | +#[CoversClass(StringSchema::class)] |
| 15 | +class StringSchemaTest extends TestCase |
| 16 | +{ |
| 17 | + public function testMustValidateOnlyStrings(): void |
| 18 | + { |
| 19 | + $schema = Validator::string(); |
| 20 | + |
| 21 | + $this->assertFalse($schema->validate(1), 'Should not validate integers'); |
| 22 | + $this->assertFalse($schema->validate(1.2), 'Should not validate floats'); |
| 23 | + $this->assertFalse($schema->validate([]), 'Should not validate arrays'); |
| 24 | + $this->assertFalse($schema->validate(new stdClass), 'Should not validate objects'); |
| 25 | + $this->assertFalse($schema->validate(null), 'Should not validate null'); |
| 26 | + $this->assertTrue($schema->validate('my string'), 'Should validate strings'); |
| 27 | + } |
| 28 | + |
| 29 | + public function testMinLengthValidation(): void |
| 30 | + { |
| 31 | + $myStr = str_repeat('a', 5); |
| 32 | + |
| 33 | + $this->assertTrue( |
| 34 | + Validator::string()->min(5)->validate($myStr), |
| 35 | + 'Should validate when length is equal to minimum' |
| 36 | + ); |
| 37 | + $this->assertTrue( |
| 38 | + Validator::string()->min(4)->validate($myStr), |
| 39 | + 'Should validate when length is greater than minimum' |
| 40 | + ); |
| 41 | + $this->assertFalse( |
| 42 | + Validator::string()->min(6)->validate($myStr), |
| 43 | + 'Should not validate when length is lesser than minimum' |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + public function testMaxLengthValidation(): void |
| 48 | + { |
| 49 | + $myStr = str_repeat('a', 5); |
| 50 | + |
| 51 | + $this->assertTrue( |
| 52 | + Validator::string()->max(5)->validate($myStr), |
| 53 | + 'Should validate when length is equal to maximum' |
| 54 | + ); |
| 55 | + $this->assertFalse( |
| 56 | + Validator::string()->max(4)->validate($myStr), |
| 57 | + 'Should not validate when length is greater than maximum' |
| 58 | + ); |
| 59 | + $this->assertTrue( |
| 60 | + Validator::string()->max(6)->validate($myStr), |
| 61 | + 'Should not validate when length is lesser than maximum' |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + public function testPatternValidation(): void |
| 66 | + { |
| 67 | + $myString = 'Hello world'; |
| 68 | + $validPattern = '/Hello/'; |
| 69 | + $invalidPattern = '/Goodbye/'; |
| 70 | + |
| 71 | + $this->assertTrue( |
| 72 | + Validator::string()->pattern($validPattern)->validate($myString), |
| 73 | + 'Should validate a valid pattern' |
| 74 | + ); |
| 75 | + $this->assertFalse( |
| 76 | + Validator::string()->pattern($invalidPattern)->validate($myString), |
| 77 | + 'Should not validate an invalid pattern' |
| 78 | + ); |
| 79 | + } |
| 80 | +} |
0 commit comments