-
Notifications
You must be signed in to change notification settings - Fork 0
adds enums #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
adds enums #1
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,312 @@ | ||
| <?php | ||
|
|
||
| use Neuron\Dto\Factory; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| class EnumValidationTest extends TestCase | ||
| { | ||
| public function testEnumValidationPassesWithValidValue() | ||
| { | ||
| $properties = [ | ||
| 'role' => [ | ||
| 'type' => 'string', | ||
| 'required' => true, | ||
| 'enum' => ['admin', 'editor', 'author', 'subscriber'] | ||
| ] | ||
| ]; | ||
|
|
||
| $factory = new Factory( $properties ); | ||
| $dto = $factory->create(); | ||
|
|
||
| // Test each valid value | ||
| $dto->role = 'admin'; | ||
| $dto->validate(); | ||
| $this->assertEquals( 'admin', $dto->role ); | ||
|
|
||
| $dto->role = 'editor'; | ||
| $dto->validate(); | ||
| $this->assertEquals( 'editor', $dto->role ); | ||
|
|
||
| $dto->role = 'subscriber'; | ||
| $dto->validate(); | ||
| $this->assertEquals( 'subscriber', $dto->role ); | ||
| } | ||
|
|
||
| public function testEnumValidationFailsWithInvalidValue() | ||
| { | ||
| $properties = [ | ||
| 'status' => [ | ||
| 'type' => 'string', | ||
| 'required' => true, | ||
| 'enum' => ['active', 'inactive', 'pending'] | ||
| ] | ||
| ]; | ||
|
|
||
| $factory = new Factory( $properties ); | ||
| $dto = $factory->create(); | ||
|
|
||
| try { | ||
| $dto->status = 'invalid_status'; | ||
| $dto->validate(); | ||
| $this->fail( 'Expected validation exception for invalid enum value' ); | ||
| } catch ( \Neuron\Core\Exceptions\Validation $e ) { | ||
| $this->assertNotEmpty( $e->errors ); | ||
| $this->assertStringContainsString( 'validation failed', $e->errors[0] ); | ||
| } | ||
| } | ||
|
|
||
| public function testEnumValidationIsCaseSensitive() | ||
| { | ||
| $properties = [ | ||
| 'priority' => [ | ||
| 'type' => 'string', | ||
| 'required' => true, | ||
| 'enum' => ['low', 'medium', 'high'] | ||
| ] | ||
| ]; | ||
|
|
||
| $factory = new Factory( $properties ); | ||
| $dto = $factory->create(); | ||
|
|
||
| // Valid lowercase | ||
| $dto->priority = 'low'; | ||
| $dto->validate(); | ||
| $this->assertEquals( 'low', $dto->priority ); | ||
|
|
||
| // Invalid uppercase | ||
| try { | ||
| $dto->priority = 'LOW'; | ||
| $dto->validate(); | ||
| $this->fail( 'Expected validation exception for case mismatch' ); | ||
| } catch ( \Neuron\Core\Exceptions\Validation $e ) { | ||
| $this->assertNotEmpty( $e->errors ); | ||
| } | ||
| } | ||
|
|
||
| public function testEnumWithIntegerValues() | ||
| { | ||
| $properties = [ | ||
| 'level' => [ | ||
| 'type' => 'integer', | ||
| 'required' => true, | ||
| 'enum' => [1, 2, 3, 4, 5] | ||
| ] | ||
| ]; | ||
|
|
||
| $factory = new Factory( $properties ); | ||
| $dto = $factory->create(); | ||
|
|
||
| // Valid integer | ||
| $dto->level = 3; | ||
| $dto->validate(); | ||
| $this->assertEquals( 3, $dto->level ); | ||
|
|
||
| // Invalid integer | ||
| try { | ||
| $dto->level = 10; | ||
| $dto->validate(); | ||
| $this->fail( 'Expected validation exception for invalid enum value' ); | ||
| } catch ( \Neuron\Core\Exceptions\Validation $e ) { | ||
| $this->assertNotEmpty( $e->errors ); | ||
| } | ||
| } | ||
|
|
||
| public function testEnumWithLooseComparison() | ||
| { | ||
| $properties = [ | ||
| 'code' => [ | ||
| 'type' => 'string', | ||
| 'required' => true, | ||
| 'enum' => ['low', 'medium', 'high'], | ||
| 'strict' => false | ||
| ] | ||
| ]; | ||
|
|
||
| $factory = new Factory( $properties ); | ||
| $dto = $factory->create(); | ||
|
|
||
| // With strict mode defaulting to true, this should work | ||
| $dto->code = 'low'; | ||
| $dto->validate(); | ||
| $this->assertEquals( 'low', $dto->code ); | ||
|
|
||
| // Note: Loose comparison primarily matters for mixed-type scenarios | ||
| // but since we have type validators (IsString, IsInteger), the type | ||
| // check happens first, so loose comparison has limited use in DTOs | ||
| } | ||
|
|
||
| public function testEnumWithStrictComparison() | ||
| { | ||
| $properties = [ | ||
| 'id' => [ | ||
| 'type' => 'integer', | ||
| 'required' => true, | ||
| 'enum' => [1, 2, 3], | ||
| 'strict' => true | ||
| ] | ||
| ]; | ||
|
|
||
| $factory = new Factory( $properties ); | ||
| $dto = $factory->create(); | ||
|
|
||
| // Valid integer | ||
| $dto->id = 1; | ||
| $dto->validate(); | ||
| $this->assertEquals( 1, $dto->id ); | ||
|
|
||
| // With strict comparison, string '1' should NOT match integer 1 | ||
| try { | ||
| $dto->id = '1'; | ||
| $dto->validate(); | ||
| $this->fail( 'Expected validation exception for type mismatch in strict mode' ); | ||
| } catch ( \Neuron\Core\Exceptions\Validation $e ) { | ||
| $this->assertNotEmpty( $e->errors ); | ||
| } | ||
| } | ||
|
|
||
| public function testEnumLoadedFromYamlFile() | ||
| { | ||
| // Create a temporary YAML file | ||
| $yamlContent = <<<YAML | ||
| dto: | ||
| role: | ||
| type: string | ||
| required: true | ||
| enum: ['admin', 'editor', 'author', 'subscriber'] | ||
| YAML; | ||
|
|
||
| $tempFile = sys_get_temp_dir() . '/test_enum_' . uniqid() . '.yaml'; | ||
| file_put_contents( $tempFile, $yamlContent ); | ||
|
|
||
| try { | ||
| $factory = new Factory( $tempFile ); | ||
| $dto = $factory->create(); | ||
|
|
||
| // Valid value | ||
| $dto->role = 'admin'; | ||
| $dto->validate(); | ||
| $this->assertEquals( 'admin', $dto->role ); | ||
|
|
||
| // Invalid value | ||
| try { | ||
| $dto->role = 'moderator'; | ||
| $dto->validate(); | ||
| $this->fail( 'Expected validation exception for invalid enum value' ); | ||
| } catch ( \Neuron\Core\Exceptions\Validation $e ) { | ||
| $this->assertNotEmpty( $e->errors ); | ||
| } | ||
| } finally { | ||
| // Clean up | ||
| if ( file_exists( $tempFile ) ) { | ||
| unlink( $tempFile ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public function testEnumWithOptionalProperty() | ||
| { | ||
| $properties = [ | ||
| 'category' => [ | ||
| 'type' => 'string', | ||
| 'required' => false, | ||
| 'enum' => ['news', 'sports', 'technology'] | ||
| ] | ||
| ]; | ||
|
|
||
| $factory = new Factory( $properties ); | ||
| $dto = $factory->create(); | ||
|
|
||
| // Validation should pass when optional property is not set | ||
| $dto->validate(); | ||
|
|
||
| // But if set, it must be a valid enum value | ||
| $dto->category = 'news'; | ||
| $dto->validate(); | ||
| $this->assertEquals( 'news', $dto->category ); | ||
|
|
||
| // Invalid value should fail | ||
| try { | ||
| $dto->category = 'politics'; | ||
| $dto->validate(); | ||
| $this->fail( 'Expected validation exception for invalid enum value' ); | ||
| } catch ( \Neuron\Core\Exceptions\Validation $e ) { | ||
| $this->assertNotEmpty( $e->errors ); | ||
| } | ||
| } | ||
|
|
||
| public function testEnumInNestedObject() | ||
| { | ||
| $properties = [ | ||
| 'user' => [ | ||
| 'type' => 'object', | ||
| 'required' => true, | ||
| 'properties' => [ | ||
| 'username' => [ | ||
| 'type' => 'string', | ||
| 'required' => true | ||
| ], | ||
| 'role' => [ | ||
| 'type' => 'string', | ||
| 'required' => true, | ||
| 'enum' => ['admin', 'user', 'guest'] | ||
| ] | ||
| ] | ||
| ] | ||
| ]; | ||
|
|
||
| $factory = new Factory( $properties ); | ||
| $dto = $factory->create(); | ||
|
|
||
| // Valid nested enum | ||
| $dto->user->username = 'johndoe'; | ||
| $dto->user->role = 'admin'; | ||
| $dto->validate(); | ||
| $this->assertEquals( 'admin', $dto->user->role ); | ||
|
|
||
| // Invalid nested enum | ||
| try { | ||
| $dto->user->role = 'superadmin'; | ||
| $dto->validate(); | ||
| $this->fail( 'Expected validation exception for invalid enum in nested object' ); | ||
| } catch ( \Neuron\Core\Exceptions\Validation $e ) { | ||
| $this->assertNotEmpty( $e->errors ); | ||
| } | ||
| } | ||
|
|
||
| public function testMultipleEnumProperties() | ||
| { | ||
| $properties = [ | ||
| 'status' => [ | ||
| 'type' => 'string', | ||
| 'required' => true, | ||
| 'enum' => ['draft', 'published', 'archived'] | ||
| ], | ||
| 'priority' => [ | ||
| 'type' => 'string', | ||
| 'required' => true, | ||
| 'enum' => ['low', 'medium', 'high'] | ||
| ] | ||
| ]; | ||
|
|
||
| $factory = new Factory( $properties ); | ||
| $dto = $factory->create(); | ||
|
|
||
| // Both valid | ||
| $dto->status = 'published'; | ||
| $dto->priority = 'high'; | ||
| $dto->validate(); | ||
|
|
||
| $this->assertEquals( 'published', $dto->status ); | ||
| $this->assertEquals( 'high', $dto->priority ); | ||
|
|
||
| // One invalid | ||
| try { | ||
| $dto->status = 'deleted'; // Invalid | ||
| $dto->priority = 'high'; // Valid | ||
| $dto->validate(); | ||
| $this->fail( 'Expected validation exception' ); | ||
| } catch ( \Neuron\Core\Exceptions\Validation $e ) { | ||
| $this->assertNotEmpty( $e->errors ); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: Neuron-PHP/dto
Length of output: 563
🏁 Script executed:
Repository: Neuron-PHP/dto
Length of output: 579
🏁 Script executed:
Repository: Neuron-PHP/dto
Length of output: 1363
🏁 Script executed:
Repository: Neuron-PHP/dto
Length of output: 4574
🏁 Script executed:
Repository: Neuron-PHP/dto
Length of output: 976
🏁 Script executed:
Repository: Neuron-PHP/dto
Length of output: 2250
Missing
IsInSetvalidator in external dependency.Line 204 uses
Validation\IsInSet, which should come from the externalneuron-php/validationpackage (currently required as^0.7.0). The class does not exist in this repository because validators are provided by that dependency, not implemented locally. Verify that the installed version ofneuron-php/validationincludes theIsInSetclass, or update the package version if this validator was added in a later release.🧰 Tools
🪛 GitHub Actions: CI
[error] 204-204: Class "Neuron\Validation\IsInSet" not found during Enum validation.