|
3 | 3 | namespace Webdevcave\SchemaValidator\Tests\Schemas; |
4 | 4 |
|
5 | 5 | use PHPUnit\Framework\Attributes\CoversClass; |
| 6 | +use PHPUnit\Framework\TestCase; |
6 | 7 | use stdClass; |
7 | 8 | use Webdevcave\SchemaValidator\Schemas\ArraySchema; |
8 | | -use PHPUnit\Framework\TestCase; |
9 | 9 | use Webdevcave\SchemaValidator\Schemas\BaseSchema; |
| 10 | +use Webdevcave\SchemaValidator\Schemas\NumericSchema; |
| 11 | +use Webdevcave\SchemaValidator\Schemas\StringSchema; |
10 | 12 | use Webdevcave\SchemaValidator\Validator; |
11 | 13 |
|
12 | 14 | #[CoversClass(Validator::class)] |
13 | 15 | #[CoversClass(BaseSchema::class)] |
14 | 16 | #[CoversClass(ArraySchema::class)] |
| 17 | +#[CoversClass(NumericSchema::class)] |
| 18 | +#[CoversClass(StringSchema::class)] |
15 | 19 | class ArraySchemaTest extends TestCase |
16 | 20 | { |
17 | 21 | public function testTypeCheck(): void |
18 | 22 | { |
19 | | - $schema = new ArraySchema(); |
| 23 | + $schema = Validator::array(); |
20 | 24 |
|
21 | 25 | $this->assertFalse($schema->validate(1), "Array schema should not validate integers"); |
22 | 26 | $this->assertFalse($schema->validate(1.1), "Array schema should not validate floats"); |
23 | 27 | $this->assertFalse($schema->validate('str'), "Array schema should not validate strings"); |
24 | 28 | $this->assertFalse($schema->validate(new stdClass()), "Array schema should not validate objects"); |
25 | 29 |
|
26 | | - $this->assertTrue($schema->validate([1,2,3]), "Array schema should validate arrays"); |
| 30 | + $this->assertTrue($schema->validate([1, 2, 3]), "Array schema should validate arrays"); |
| 31 | + } |
| 32 | + |
| 33 | + public function testDataValidation(): void |
| 34 | + { |
| 35 | + $positiveIntCheckMessage = 'must be positive'; |
| 36 | + $schema = Validator::array(['name' => Validator::string()->min(3), 'age' => Validator::numeric()->integer()->positive($positiveIntCheckMessage),]); |
| 37 | + |
| 38 | + $validData = ['name' => 'Carlos', 'age' => 35,]; |
| 39 | + $this->assertTrue($schema->validate($validData), "Should return true when dataset is valid"); |
| 40 | + |
| 41 | + $invalidData = ['name' => 'John', 'age' => -10,]; |
| 42 | + $this->assertFalse($schema->validate($invalidData), "Should return false when dataset is not valid"); |
| 43 | + $this->assertEquals(['age' => [$positiveIntCheckMessage]], $schema->errorMessages(), 'Error messages doesn\'t match'); |
| 44 | + } |
| 45 | + |
| 46 | + public function testWildcardIndexValidation() |
| 47 | + { |
| 48 | + $errorMessage = 'Only integers are allowed'; |
| 49 | + $schema = Validator::array(['*' => Validator::numeric()->integer($errorMessage)]); |
| 50 | + |
| 51 | + $validData = [1, 2, 3]; |
| 52 | + $this->assertTrue($schema->validate($validData), "Error evaluating wildcard index with valid data"); |
| 53 | + |
| 54 | + $invalidData = [null]; |
| 55 | + $this->assertFalse($schema->validate($invalidData), "Error evaluating wildcard index with invalid data"); |
| 56 | + } |
| 57 | + |
| 58 | + public function testOptionalCheckShouldPassOnEmptyArray(): void |
| 59 | + { |
| 60 | + $schema = Validator::array()->optional(); |
| 61 | + |
| 62 | + $this->assertTrue($schema->validate([]), "Array schema should accept empty array when it's optional"); |
| 63 | + } |
| 64 | + |
| 65 | + public function testDoubleRuleValidation(): void |
| 66 | + { |
| 67 | + $schema = Validator::array([ |
| 68 | + 'name' => [ |
| 69 | + Validator::string()->min(1), |
| 70 | + Validator::string()->max(10), |
| 71 | + ], |
| 72 | + ]); |
| 73 | + |
| 74 | + $data = [ |
| 75 | + 'name' => str_repeat('a', 10), |
| 76 | + ]; |
| 77 | + $this->assertTrue($schema->validate($data), "Array schema should accept double rule"); |
27 | 78 | } |
28 | 79 | } |
0 commit comments