|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use EdineiValdameri\Laravel\DynamicValidation\Models\Rule; |
| 6 | +use EdineiValdameri\Laravel\DynamicValidation\Repositories\RuleRepository; |
| 7 | +use EdineiValdameri\Laravel\DynamicValidation\Services\JsonSchemaService; |
| 8 | +use Illuminate\Support\Collection; |
| 9 | +use Mockery\MockInterface; |
| 10 | + |
| 11 | +it('generates a JSON schema based on rules', function () { |
| 12 | + $this->mock(RuleRepository::class, function (MockInterface $mock) { |
| 13 | + $mock->shouldReceive('getRulesByAction') |
| 14 | + ->with('users.store') |
| 15 | + ->andReturn(new Collection([ |
| 16 | + new Rule(['field' => 'name', 'rule' => 'string']), |
| 17 | + new Rule(['field' => 'name', 'rule' => 'required']), |
| 18 | + new Rule(['field' => 'name', 'rule' => 'max', 'value' => 255]), |
| 19 | + new Rule(['field' => 'password', 'rule' => 'string']), |
| 20 | + new Rule(['field' => 'password', 'rule' => 'required']), |
| 21 | + new Rule(['field' => 'password', 'rule' => 'min', 'value' => 6]), |
| 22 | + new Rule(['field' => 'password', 'rule' => 'max', 'value' => 20]), |
| 23 | + new Rule(['field' => 'email', 'rule' => 'string']), |
| 24 | + new Rule(['field' => 'email', 'rule' => 'required']), |
| 25 | + new Rule(['field' => 'email', 'rule' => 'email']), |
| 26 | + new Rule(['field' => 'age', 'rule' => 'integer']), |
| 27 | + new Rule(['field' => 'age', 'rule' => 'min', 'value' => 18]), |
| 28 | + new Rule(['field' => 'age', 'rule' => 'max', 'value' => 65]), |
| 29 | + new Rule(['field' => 'birth', 'rule' => 'date']), |
| 30 | + new Rule(['field' => 'birth', 'rule' => 'required']), |
| 31 | + new Rule(['field' => 'active', 'rule' => 'boolean']), |
| 32 | + ])); |
| 33 | + }); |
| 34 | + |
| 35 | + $generator = app(JsonSchemaService::class); |
| 36 | + $schema = $generator->generate('users.store'); |
| 37 | + |
| 38 | + expect($schema)->toMatchArray([ |
| 39 | + 'type' => 'object', |
| 40 | + 'required' => ['name', 'password', 'email', 'birth'], |
| 41 | + 'properties' => [ |
| 42 | + 'name' => [ |
| 43 | + 'type' => 'string', |
| 44 | + 'maxLength' => 255, |
| 45 | + ], |
| 46 | + 'password' => [ |
| 47 | + 'type' => 'string', |
| 48 | + 'minLength' => 6, |
| 49 | + 'maxLength' => 20, |
| 50 | + ], |
| 51 | + 'email' => [ |
| 52 | + 'type' => 'string', |
| 53 | + 'format' => 'email', |
| 54 | + ], |
| 55 | + 'age' => [ |
| 56 | + 'type' => 'integer', |
| 57 | + 'minimum' => 18, |
| 58 | + 'maximum' => 65, |
| 59 | + ], |
| 60 | + 'birth' => [ |
| 61 | + 'type' => 'string', |
| 62 | + 'format' => 'date', |
| 63 | + ], |
| 64 | + 'active' => ['type' => 'boolean'], |
| 65 | + ], |
| 66 | + ]); |
| 67 | +}); |
| 68 | + |
| 69 | +it('adds required fields correctly', function () { |
| 70 | + $this->mock(RuleRepository::class, function (MockInterface $mock) { |
| 71 | + $mock->shouldReceive('getRulesByAction') |
| 72 | + ->with('users.store') |
| 73 | + ->andReturn(new Collection([ |
| 74 | + new Rule(['field' => 'email', 'rule' => 'required']), |
| 75 | + new Rule(['field' => 'password', 'rule' => 'required']), |
| 76 | + ])); |
| 77 | + }); |
| 78 | + |
| 79 | + $generator = app(JsonSchemaService::class); |
| 80 | + $schema = $generator->generate('users.store'); |
| 81 | + |
| 82 | + expect($schema['required'])->toBe(['email', 'password']); |
| 83 | +}); |
| 84 | + |
| 85 | +it('handles optional fields correctly', function () { |
| 86 | + $this->mock(RuleRepository::class, function (MockInterface $mock) { |
| 87 | + $mock->shouldReceive('getRulesByAction') |
| 88 | + ->with('users.store') |
| 89 | + ->andReturn(new Collection([ |
| 90 | + new Rule(['field' => 'name', 'rule' => 'string']), |
| 91 | + new Rule(['field' => 'age', 'rule' => 'integer']), |
| 92 | + new Rule(['field' => 'age', 'rule' => 'min', 'value' => 18]), |
| 93 | + ])); |
| 94 | + }); |
| 95 | + |
| 96 | + $generator = app(JsonSchemaService::class); |
| 97 | + $schema = $generator->generate('users.store'); |
| 98 | + |
| 99 | + expect($schema['required'])->toBeEmpty() |
| 100 | + ->and($schema['properties'])->toMatchArray([ |
| 101 | + 'name' => ['type' => 'string'], |
| 102 | + 'age' => [ |
| 103 | + 'type' => 'integer', |
| 104 | + 'minimum' => 18, |
| 105 | + ], |
| 106 | + ]); |
| 107 | +}); |
0 commit comments