Skip to content

Added support for non required const-properties #82

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

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/PropertyProcessor/Property/ConstProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ public function process(string $propertyName, JsonSchema $propertySchema): Prope
$json['description'] ?? '',
);

$property->setRequired($this->propertyMetaDataCollection->isAttributeRequired($propertyName));
$isAttributeRequired = $this->propertyMetaDataCollection->isAttributeRequired($propertyName);
$isImplicitNullAllowed = $this->schemaProcessor->getGeneratorConfiguration()->isImplicitNullAllowed();
$property->setRequired($isAttributeRequired || !$isImplicitNullAllowed);

$check = $property->isRequired()
? '$value !== ' . var_export($json['const'], true)
Expand Down
113 changes: 103 additions & 10 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,6 +35,21 @@ 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', null, false, false);

new $className([]);
}

/**
* @dataProvider invalidPropertyDataProvider
*
Expand All @@ -47,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 @@ -61,9 +78,76 @@ public function invalidPropertyDataProvider(): array
'array' => [[]],
'object' => [new stdClass()],
'string' => ['null'],
'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
*
Expand All @@ -72,27 +156,36 @@ public function invalidPropertyDataProvider(): array
* @throws SchemaException
*/
public function testNotMatchingRequiredAndOptionalProvidedDataThrowsAnException(
bool $implicitNull,
string $reqPropertyValue,
?string $optPropertyValue,
string $exceptionMessage
): void
{
$className = $this->generateClassFromFile('RequiredAndOptionalConstProperties.json');
$className = $this->generateClassFromFile(
'RequiredAndOptionalConstProperties.json',
new GeneratorConfiguration(),
false,
$implicitNull,
);

$this->expectException(ValidationException::class);
$this->expectException(ErrorRegistryException::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'],
];
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'],
],
);
}
}
25 changes: 0 additions & 25 deletions tests/Objects/RequiredPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,29 +162,4 @@ 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],
];
}
}

This file was deleted.