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));

$check = $this->isImplicitNullAllowed($property)
? "array_key_exists('{$property->getName()}', \$modelData) && " . '!in_array($value, ' . RenderHelper::varExportArray([$json['const'], null]) . ', true)'
: "array_key_exists('{$property->getName()}', \$modelData) && \$value !== " . var_export($json['const'], true);

return $property->addValidator(new PropertyValidator(
$property,
$check,
InvalidConstException::class,
[$json['const']],
));
}
}
140 changes: 124 additions & 16 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 All @@ -33,21 +35,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 All @@ -62,7 +49,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 +66,125 @@ 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'],
],
);
}

/**
* @throws FileSystemException
* @throws RenderException
* @throws SchemaException
*/
public function testProvidedNullValueConstPropertyIsValid(): void
{
$className = $this->generateClassFromFile('NullValueConstProperty.json', null, false, false);

$object = new $className(['nullProperty' => null]);

$this->assertNull($object->getNullProperty());
}
}
11 changes: 11 additions & 0 deletions tests/Schema/ConstPropertyTest/NullValueConstProperty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"type": "object",
"properties": {
"nullProperty": {
"const": null
}
},
"required": [
"nullProperty"
]
}
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"
]
}