Skip to content

Commit

Permalink
Added support for non required const-properties
Browse files Browse the repository at this point in the history
  • Loading branch information
kolodkinvalentin authored and Kolodkin Valentin committed Sep 16, 2024
1 parent da0bc3e commit 5b1771a
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 24 deletions.
36 changes: 28 additions & 8 deletions src/PropertyProcessor/Property/ConstProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
use PHPModelGenerator\Model\Property\PropertyType;
use PHPModelGenerator\Model\SchemaDefinition\JsonSchema;
use PHPModelGenerator\Model\Validator\PropertyValidator;
use PHPModelGenerator\PropertyProcessor\PropertyMetaDataCollection;
use PHPModelGenerator\PropertyProcessor\PropertyProcessorInterface;
use PHPModelGenerator\Utils\RenderHelper;
use PHPModelGenerator\Utils\TypeConverter;

/**
Expand All @@ -20,6 +22,20 @@
*/
class ConstProcessor implements PropertyProcessorInterface
{
/** @var PropertyMetaDataCollection */
protected $propertyMetaDataCollection;

/**
* ConstProcessor constructor.
*
* @param PropertyMetaDataCollection $propertyMetaDataCollection
*/
public function __construct(
PropertyMetaDataCollection $propertyMetaDataCollection,
) {
$this->propertyMetaDataCollection = $propertyMetaDataCollection;
}

/**
* @inheritdoc
*/
Expand All @@ -34,13 +50,17 @@ public function process(string $propertyName, JsonSchema $propertySchema): Prope
$json['description'] ?? '',
);

return $property
->setRequired(true)
->addValidator(new PropertyValidator(
$property,
'$value !== ' . var_export($json['const'], true),
InvalidConstException::class,
[$json['const']],
));
$property->setRequired($this->propertyMetaDataCollection->isAttributeRequired($propertyName));

$check = $property->isRequired()
? '$value !== ' . var_export($json['const'], true)
: '!in_array($value, ' . RenderHelper::varExportArray([$json['const'], null]) . ', true)';

return $property->addValidator(new PropertyValidator(
$property,
$check,
InvalidConstException::class,
[$json['const']],
));
}
}
48 changes: 32 additions & 16 deletions tests/Objects/ConstPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,6 @@ public function testProvidedConstPropertyIsValid(): void
$this->assertSame(42, $object->getIntegerProperty());
}

/**
* @throws FileSystemException
* @throws RenderException
* @throws SchemaException
*/
public function testNotProvidedConstPropertyThrowsAnException(): void
{
$this->expectException(ValidationException::class);
$this->expectExceptionMessage('Invalid value for stringProperty declined by const constraint');

$className = $this->generateClassFromFile('ConstProperty.json');

new $className([]);
}

/**
* @dataProvider invalidPropertyDataProvider
*
Expand Down Expand Up @@ -76,7 +61,38 @@ public function invalidPropertyDataProvider(): array
'array' => [[]],
'object' => [new stdClass()],
'string' => ['null'],
'null' => [null],
];
}

/**
* @dataProvider invalidRequiredAndOptionalConstPropertiesDataProvider
*
* @throws FileSystemException
* @throws RenderException
* @throws SchemaException
*/
public function testNotMatchingRequiredAndOptionalProvidedDataThrowsAnException(
string $reqPropertyValue,
?string $optPropertyValue,
string $exceptionMessage
): void
{
$className = $this->generateClassFromFile('RequiredAndOptionalConstProperties.json');

$this->expectException(ValidationException::class);
$this->expectExceptionMessage($exceptionMessage);

new $className(['requiredProperty' => $reqPropertyValue, 'optionalProperty' => $optPropertyValue]);
}

public function invalidRequiredAndOptionalConstPropertiesDataProvider(): array
{
return [
['blue', 'green', 'Invalid value for requiredProperty declined by const constraint'],
['blue', null, 'Invalid value for requiredProperty declined by const constraint'],
['red', 'blue', 'Invalid value for optionalProperty declined by const constraint'],
['red', '0', 'Invalid value for optionalProperty declined by const constraint'],
['red', '', 'Invalid value for optionalProperty declined by const constraint'],
];
}
}
25 changes: 25 additions & 0 deletions tests/Objects/RequiredPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,29 @@ public function requiredStringPropertyDataProvider(): array
],
);
}

/**
* @dataProvider requiredAndOptionalPropertiesDataProvider
*
* @throws FileSystemException
* @throws RenderException
* @throws SchemaException
*/
public function testProvidedConstPropertiesIsValid(string $reqPropertyValue, ?string $optPropertyValue): void
{
$className = $this->generateClassFromFile('RequiredAndOptionalConstProperties.json');

$object = new $className(['requiredProperty' => $reqPropertyValue, 'optionalProperty' => $optPropertyValue]);

$this->assertSame($reqPropertyValue, $object->getRequiredProperty());
$this->assertSame($optPropertyValue, $object->getOptionalProperty());
}

public function requiredAndOptionalPropertiesDataProvider(): array
{
return [
['red', 'green'],
['red', null],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"type": "object",
"properties": {
"requiredProperty": {
"const": "red"
},
"optionalProperty": {
"const": "green"
}
},
"required": [
"requiredProperty"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"type": "object",
"properties": {
"requiredProperty": {
"const": "red"
},
"optionalProperty": {
"const": "green"
}
},
"required": [
"requiredProperty"
]
}

0 comments on commit 5b1771a

Please sign in to comment.