|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Spiral\JsonSchemaGenerator\Tests\Unit; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Spiral\JsonSchemaGenerator\Generator; |
| 9 | +use Spiral\JsonSchemaGenerator\GeneratorConfig; |
| 10 | +use Spiral\JsonSchemaGenerator\Tests\Unit\Fixture\Movie; |
| 11 | +use Spiral\JsonSchemaGenerator\Tests\Unit\Fixture\ValidatedUser; |
| 12 | + |
| 13 | +final class GeneratorConfigTest extends TestCase |
| 14 | +{ |
| 15 | + public function testValidationConstraintsEnabled(): void |
| 16 | + { |
| 17 | + $config = new GeneratorConfig(enableValidationConstraints: true); |
| 18 | + $generator = new Generator(config: $config); |
| 19 | + |
| 20 | + $schema = $generator->generate(ValidatedUser::class); |
| 21 | + $result = $schema->jsonSerialize(); |
| 22 | + |
| 23 | + // Should have validation constraints |
| 24 | + $this->assertArrayHasKey('minimum', $result['properties']['age']); |
| 25 | + $this->assertEquals(1, $result['properties']['age']['minimum']); |
| 26 | + |
| 27 | + $this->assertArrayHasKey('minLength', $result['properties']['name']); |
| 28 | + $this->assertEquals(1, $result['properties']['name']['minLength']); |
| 29 | + } |
| 30 | + |
| 31 | + public function testValidationConstraintsDisabled(): void |
| 32 | + { |
| 33 | + $config = new GeneratorConfig(enableValidationConstraints: false); |
| 34 | + $generator = new Generator(config: $config); |
| 35 | + |
| 36 | + $schema = $generator->generate(ValidatedUser::class); |
| 37 | + $result = $schema->jsonSerialize(); |
| 38 | + |
| 39 | + // Should NOT have validation constraints |
| 40 | + $this->assertArrayNotHasKey('minimum', $result['properties']['age']); |
| 41 | + $this->assertArrayNotHasKey('minLength', $result['properties']['name']); |
| 42 | + |
| 43 | + // But should still have basic type information |
| 44 | + $this->assertEquals('integer', $result['properties']['age']['type']); |
| 45 | + $this->assertEquals('string', $result['properties']['name']['type']); |
| 46 | + } |
| 47 | + |
| 48 | + public function testDefaultConfigurationEnablesValidation(): void |
| 49 | + { |
| 50 | + $config = new GeneratorConfig(); // Default: enableValidationConstraints = true |
| 51 | + $generator = new Generator(config: $config); |
| 52 | + |
| 53 | + $schema = $generator->generate(ValidatedUser::class); |
| 54 | + $result = $schema->jsonSerialize(); |
| 55 | + |
| 56 | + // Should have validation constraints by default |
| 57 | + $this->assertArrayHasKey('minimum', $result['properties']['age']); |
| 58 | + $this->assertEquals(1, $result['properties']['age']['minimum']); |
| 59 | + } |
| 60 | + |
| 61 | + public function testBackwardCompatibilityWithoutConfig(): void |
| 62 | + { |
| 63 | + $generator = new Generator(); // No config provided - should use default |
| 64 | + |
| 65 | + $schema = $generator->generate(ValidatedUser::class); |
| 66 | + $result = $schema->jsonSerialize(); |
| 67 | + |
| 68 | + // Should have validation constraints (backward compatible) |
| 69 | + $this->assertArrayHasKey('minimum', $result['properties']['age']); |
| 70 | + $this->assertEquals(1, $result['properties']['age']['minimum']); |
| 71 | + } |
| 72 | + |
| 73 | + public function testConfigurationProperties(): void |
| 74 | + { |
| 75 | + $config = new GeneratorConfig( |
| 76 | + enableValidationConstraints: false, |
| 77 | + ); |
| 78 | + |
| 79 | + $this->assertFalse($config->enableValidationConstraints); |
| 80 | + } |
| 81 | + |
| 82 | + public function testPerformanceWithDisabledConstraints(): void |
| 83 | + { |
| 84 | + // Test that disabling constraints doesn't break existing functionality |
| 85 | + $config = new GeneratorConfig(enableValidationConstraints: false); |
| 86 | + $generator = new Generator(config: $config); |
| 87 | + |
| 88 | + // Use a class without PHPDoc constraints |
| 89 | + $schema = $generator->generate(Movie::class); |
| 90 | + $result = $schema->jsonSerialize(); |
| 91 | + |
| 92 | + // Should work normally for non-constrained classes |
| 93 | + $this->assertEquals('object', $result['type']); |
| 94 | + $this->assertArrayHasKey('properties', $result); |
| 95 | + $this->assertArrayHasKey('title', $result['properties']); |
| 96 | + } |
| 97 | +} |
0 commit comments