Skip to content
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

Added support for non required const-properties #82

24 changes: 14 additions & 10 deletions src/PropertyProcessor/Property/ConstProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
use PHPModelGenerator\Model\Property\PropertyType;
use PHPModelGenerator\Model\SchemaDefinition\JsonSchema;
use PHPModelGenerator\Model\Validator\PropertyValidator;
use PHPModelGenerator\PropertyProcessor\PropertyProcessorInterface;
use PHPModelGenerator\Utils\RenderHelper;
use PHPModelGenerator\Utils\TypeConverter;

/**
* Class ConstProcessor
*
* @package PHPModelGenerator\PropertyProcessor\Property
*/
class ConstProcessor implements PropertyProcessorInterface
class ConstProcessor extends AbstractPropertyProcessor
{
/**
* @inheritdoc
Expand All @@ -34,13 +34,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));

wol-soft marked this conversation as resolved.
Show resolved Hide resolved
$check = $property->isRequired()
? '$value !== ' . var_export($json['const'], true)
: '!in_array($value, ' . RenderHelper::varExportArray([$json['const'], null]) . ', true)';
wol-soft marked this conversation as resolved.
Show resolved Hide resolved

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
wol-soft marked this conversation as resolved.
Show resolved Hide resolved
{
$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"
]
}