Skip to content

Commit

Permalink
Map the result of gettype for const properties to internal types to g…
Browse files Browse the repository at this point in the history
…enerate valid code (fixes #29)
  • Loading branch information
wol-soft committed Jan 28, 2021
1 parent 72ace3b commit 466523c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/Model/GeneratorConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,14 @@ public function getClassNameGenerator(): ClassNameGeneratorInterface

/**
* @param ClassNameGeneratorInterface $classNameGenerator
*
* @return $this
*/
public function setClassNameGenerator(ClassNameGeneratorInterface $classNameGenerator): void
public function setClassNameGenerator(ClassNameGeneratorInterface $classNameGenerator): self
{
$this->classNameGenerator = $classNameGenerator;

return $this;
}

/**
Expand Down
9 changes: 8 additions & 1 deletion src/PropertyProcessor/Property/ConstProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,23 @@
*/
class ConstProcessor implements PropertyProcessorInterface
{
const TYPE_MAP = [
'boolean' => 'bool',
'integer' => 'int',
'double' => 'float',
];

/**
* @inheritdoc
*/
public function process(string $propertyName, JsonSchema $propertySchema): PropertyInterface
{
$json = $propertySchema->getJson();
$type = gettype($json['const']);

$property = new Property(
$propertyName,
gettype($json['const']),
self::TYPE_MAP[gettype($json['const'])] ?? $type,
$propertySchema,
$json['description'] ?? ''
);
Expand Down
11 changes: 6 additions & 5 deletions tests/Objects/ConstPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ public function testProvidedConstPropertyIsValid(): void
{
$className = $this->generateClassFromFile('ConstProperty.json');

$object = new $className(['property' => 'MyConstValue']);
$object = new $className(['stringProperty' => 'MyConstValue', 'integerProperty' => 42]);

$this->assertSame('MyConstValue', $object->getProperty());
$this->assertSame('MyConstValue', $object->getStringProperty());
$this->assertSame(42, $object->getIntegerProperty());
}

/**
Expand All @@ -38,7 +39,7 @@ public function testProvidedConstPropertyIsValid(): void
public function testNotProvidedConstPropertyThrowsAnException(): void
{
$this->expectException(ValidationException::class);
$this->expectExceptionMessage('Invalid value for property declined by const constraint');
$this->expectExceptionMessage('Invalid value for stringProperty declined by const constraint');

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

Expand All @@ -57,11 +58,11 @@ public function testNotProvidedConstPropertyThrowsAnException(): void
public function testNotMatchingProvidedDataThrowsAnException($propertyValue): void
{
$this->expectException(ValidationException::class);
$this->expectExceptionMessage('Invalid value for property declined by const constraint');
$this->expectExceptionMessage('Invalid value for stringProperty declined by const constraint');

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

new $className(['property' => $propertyValue]);
new $className(['stringProperty' => $propertyValue]);
}

public function invalidPropertyDataProvider(): array
Expand Down
5 changes: 4 additions & 1 deletion tests/Schema/ConstPropertyTest/ConstProperty.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"type": "object",
"properties": {
"property": {
"stringProperty": {
"const": "MyConstValue"
},
"integerProperty": {
"const": 42
}
}
}

0 comments on commit 466523c

Please sign in to comment.