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

26 changes: 16 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,19 @@ 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']],
));
$isAttributeRequired = $this->propertyMetaDataCollection->isAttributeRequired($propertyName);
$isImplicitNullAllowed = $this->schemaProcessor->getGeneratorConfiguration()->isImplicitNullAllowed();
$property->setRequired($isAttributeRequired || !$isImplicitNullAllowed);

$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']],
));
}
}
113 changes: 111 additions & 2 deletions tests/Objects/ConstPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

namespace PHPModelGenerator\Tests\Objects;

use PHPModelGenerator\Exception\ErrorRegistryException;
use PHPModelGenerator\Exception\FileSystemException;
use PHPModelGenerator\Exception\ValidationException;
use PHPModelGenerator\Exception\RenderException;
use PHPModelGenerator\Exception\SchemaException;
use PHPModelGenerator\Model\GeneratorConfiguration;
use PHPModelGenerator\Tests\AbstractPHPModelGeneratorTestCase;
use stdClass;

Expand Down Expand Up @@ -43,7 +45,7 @@ public function testNotProvidedConstPropertyThrowsAnException(): void
$this->expectException(ValidationException::class);
$this->expectExceptionMessage('Invalid value for stringProperty declined by const constraint');

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

new $className([]);
}
Expand All @@ -62,7 +64,7 @@ public function testNotMatchingProvidedDataThrowsAnException($propertyValue): vo
$this->expectException(ValidationException::class);
$this->expectExceptionMessage('Invalid value for stringProperty declined by const constraint');

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

new $className(['stringProperty' => $propertyValue]);
}
Expand All @@ -79,4 +81,111 @@ public function invalidPropertyDataProvider(): array
'null' => [null],
];
}

/**
* @throws FileSystemException
* @throws RenderException
* @throws SchemaException
*/
public function testProvidedConstOnlyRequiredPropertyIsValid(): void
{
$className = $this->generateClassFromFile('RequiredAndOptionalConstProperties.json');

$object = new $className(['requiredProperty' => 'red']);

$this->assertSame('red', $object->getRequiredProperty());
$this->assertNull($object->getOptionalProperty());
}

/**
* @throws FileSystemException
* @throws RenderException
* @throws SchemaException
*/
public function testProvidedNullOptionalPropertyConstPropertyIsValid(): void
{
$className = $this->generateClassFromFile('RequiredAndOptionalConstProperties.json');

$object = new $className(['requiredProperty' => 'red', 'optionalProperty' => null]);

$this->assertSame('red', $object->getRequiredProperty());
$this->assertNull($object->getOptionalProperty());
}

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

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

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

public function requiredAndOptionalPropertiesDataProvider(): array
{
return $this->combineDataProvider(
$this->implicitNullDataProvider(),
[
['red', 'green'],
],
);
}

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

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

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

public function invalidRequiredAndOptionalConstPropertiesDataProvider(): array
{
return $this->combineDataProvider(
$this->implicitNullDataProvider(),
[
['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'],
],
);
}
}
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"
]
}