From 36299ecd413d5561db51772f86d59c3a7193342f Mon Sep 17 00:00:00 2001 From: Sandro Keil Date: Thu, 12 Nov 2020 22:17:02 +0100 Subject: [PATCH] Add class builder factory methods - Close #1 --- composer.json | 2 +- src/PropertyFactory.php | 7 +- src/ValueObject/BooleanFactory.php | 50 +++++++++----- src/ValueObject/DateTimeFactory.php | 80 +++++++++++++++-------- src/ValueObject/IntegerFactory.php | 50 +++++++++----- src/ValueObject/NumberFactory.php | 50 +++++++++----- src/ValueObject/StringFactory.php | 50 +++++++++----- src/ValueObjectFactory.php | 23 +++++++ tests/ValueObject/BooleanFactoryTest.php | 17 +++++ tests/ValueObject/DateTimeFactoryTest.php | 17 +++++ tests/ValueObject/IntegerFactoryTest.php | 17 +++++ tests/ValueObject/NumberFactoryTest.php | 18 +++++ tests/ValueObject/StringFactoryTest.php | 17 +++++ 13 files changed, 310 insertions(+), 88 deletions(-) diff --git a/composer.json b/composer.json index 419d68a..a9ef8d1 100644 --- a/composer.json +++ b/composer.json @@ -34,7 +34,7 @@ "require": { "php": "^7.4 || ^8.0", "open-code-modeling/json-schema-to-php": "dev-master", - "open-code-modeling/php-code-ast": "^0.3.0|^0.4.0|dev-master" + "open-code-modeling/php-code-ast": "^0.8.2|dev-master" }, "require-dev": { "jangregor/phpstan-prophecy": "^0.8.0", diff --git a/src/PropertyFactory.php b/src/PropertyFactory.php index 99f8d42..6319754 100644 --- a/src/PropertyFactory.php +++ b/src/PropertyFactory.php @@ -93,11 +93,16 @@ public function nodeVisitorFromNative(string $name, string $type): array { return [ new \OpenCodeModeling\CodeAst\NodeVisitor\Property( - new PropertyGenerator($name, $type, null, $this->typed) + $this->propertyGenerator($name, $type) ), ]; } + public function propertyGenerator(string $name, string $type): PropertyGenerator + { + return new PropertyGenerator($name, $type, null, $this->typed); + } + /** * @param ReferenceType $type * @return array diff --git a/src/ValueObject/BooleanFactory.php b/src/ValueObject/BooleanFactory.php index d30f8ae..258a4e8 100644 --- a/src/ValueObject/BooleanFactory.php +++ b/src/ValueObject/BooleanFactory.php @@ -10,6 +10,7 @@ namespace OpenCodeModeling\JsonSchemaToPhpAst\ValueObject; +use OpenCodeModeling\CodeAst\Builder\ClassBuilder; use OpenCodeModeling\CodeAst\Code\BodyGenerator; use OpenCodeModeling\CodeAst\Code\MethodGenerator; use OpenCodeModeling\CodeAst\Code\ParameterGenerator; @@ -79,6 +80,13 @@ public function nodeVisitors(BooleanType $typeDefinition): array return $this->nodeVisitorsFromNative($name); } + public function classBuilder(BooleanType $typeDefinition): ClassBuilder + { + $name = $typeDefinition->name() ?: 'boolean'; + + return $this->classBuilderFromNative($name)->setTyped($this->typed); + } + /** * @param string $name * @return array @@ -86,16 +94,28 @@ public function nodeVisitors(BooleanType $typeDefinition): array public function nodeVisitorsFromNative(string $name): array { $nodeVisitors = $this->propertyFactory->nodeVisitorFromNative($name, 'bool'); - $nodeVisitors[] = $this->methodFromBool($name); - $nodeVisitors[] = $this->methodMagicConstruct($name); - $nodeVisitors[] = $this->methodToBool($name); - $nodeVisitors[] = $this->methodEquals($name); - $nodeVisitors[] = $this->methodMagicToString($name); + $nodeVisitors[] = new ClassMethod($this->methodFromBool($name)); + $nodeVisitors[] = new ClassMethod($this->methodMagicConstruct($name)); + $nodeVisitors[] = new ClassMethod($this->methodToBool($name)); + $nodeVisitors[] = new ClassMethod($this->methodEquals($name)); + $nodeVisitors[] = new ClassMethod($this->methodMagicToString($name)); return $nodeVisitors; } - public function methodFromBool(string $argumentName): NodeVisitor + public function classBuilderFromNative(string $name): ClassBuilder + { + return ClassBuilder::fromNodes( + $this->propertyFactory->propertyGenerator($name, 'bool')->generate(), + $this->methodFromBool($name)->generate(), + $this->methodMagicConstruct($name)->generate(), + $this->methodToBool($name)->generate(), + $this->methodEquals($name)->generate(), + $this->methodMagicToString($name)->generate(), + )->setTyped($this->typed); + } + + public function methodFromBool(string $argumentName): MethodGenerator { $method = new MethodGenerator( 'fromBool', @@ -108,10 +128,10 @@ public function methodFromBool(string $argumentName): NodeVisitor $method->setTyped($this->typed); $method->setReturnType('self'); - return new ClassMethod($method); + return $method; } - public function methodMagicConstruct(string $argumentName): NodeVisitor + public function methodMagicConstruct(string $argumentName): MethodGenerator { $method = new MethodGenerator( '__construct', @@ -123,10 +143,10 @@ public function methodMagicConstruct(string $argumentName): NodeVisitor ); $method->setTyped($this->typed); - return new ClassMethod($method); + return $method; } - public function methodToBool(string $argumentName): NodeVisitor + public function methodToBool(string $argumentName): MethodGenerator { $method = new MethodGenerator( 'toBool', @@ -137,10 +157,10 @@ public function methodToBool(string $argumentName): NodeVisitor $method->setTyped($this->typed); $method->setReturnType('bool'); - return new ClassMethod($method); + return $method; } - public function methodEquals(string $propertyName, string $argumentName = 'other'): NodeVisitor + public function methodEquals(string $propertyName, string $argumentName = 'other'): MethodGenerator { $body = <<setTyped($this->typed); $method->setReturnType('string'); - return new ClassMethod($method); + return $method; } } diff --git a/src/ValueObject/DateTimeFactory.php b/src/ValueObject/DateTimeFactory.php index 7d83514..86de9c2 100644 --- a/src/ValueObject/DateTimeFactory.php +++ b/src/ValueObject/DateTimeFactory.php @@ -10,6 +10,7 @@ namespace OpenCodeModeling\JsonSchemaToPhpAst\ValueObject; +use OpenCodeModeling\CodeAst\Builder\ClassBuilder; use OpenCodeModeling\CodeAst\Code\BodyGenerator; use OpenCodeModeling\CodeAst\Code\ClassConstGenerator; use OpenCodeModeling\CodeAst\Code\IdentifierGenerator; @@ -107,6 +108,13 @@ public function nodeVisitors(StringType $typeDefinition): array return $this->nodeVisitorsFromNative($name); } + public function classBuilder(StringType $typeDefinition): ClassBuilder + { + $name = $typeDefinition->name() ?: 'dateTime'; + + return $this->classBuilderFromNative($name)->setTyped($this->typed); + } + /** * @param string $name * @param string $outputFormat @@ -121,27 +129,47 @@ public function nodeVisitorsFromNative(string $name, string $outputFormat = DATE new ClassConstant( new IdentifierGenerator( 'OUTPUT_FORMAT', - new ClassConstGenerator( - 'OUTPUT_FORMAT', - $outputFormat, - ClassConstGenerator::FLAG_PRIVATE - ) + $this->classConstant($outputFormat) ) ) ); - $nodeVisitors[] = $this->methodFromDateTime($name); - $nodeVisitors[] = $this->methodFromString($name); - $nodeVisitors[] = $this->methodMagicConstruct($name); - $nodeVisitors[] = $this->methodToString($name); - $nodeVisitors[] = $this->methodDateTime($name); - $nodeVisitors[] = $this->methodMagicToString(); - $nodeVisitors[] = $this->methodEnsureUtc($name); + $nodeVisitors[] = new ClassMethod($this->methodFromDateTime($name)); + $nodeVisitors[] = new ClassMethod($this->methodFromString($name)); + $nodeVisitors[] = new ClassMethod($this->methodMagicConstruct($name)); + $nodeVisitors[] = new ClassMethod($this->methodToString($name)); + $nodeVisitors[] = new ClassMethod($this->methodDateTime($name)); + $nodeVisitors[] = new ClassMethod($this->methodMagicToString()); + $nodeVisitors[] = new ClassMethod($this->methodEnsureUtc($name)); return $nodeVisitors; } - public function methodFromDateTime(string $argumentName): NodeVisitor + public function classBuilderFromNative(string $name, string $outputFormat = DATE_ATOM): ClassBuilder + { + return ClassBuilder::fromNodes( + $this->classConstant($outputFormat)->generate(), + $this->propertyFactory->propertyGenerator($name, 'DateTimeImmutable')->generate(), + $this->methodFromDateTime($name)->generate(), + $this->methodFromString($name)->generate(), + $this->methodMagicConstruct($name)->generate(), + $this->methodToString($name)->generate(), + $this->methodDateTime($name)->generate(), + $this->methodMagicToString()->generate(), + $this->methodEnsureUtc($name)->generate(), + )->setTyped($this->typed); + } + + public function classConstant(string $outputFormat = DATE_ATOM): ClassConstGenerator + { + return new ClassConstGenerator( + 'OUTPUT_FORMAT', + $outputFormat, + ClassConstGenerator::FLAG_PRIVATE + ); + } + + public function methodFromDateTime(string $argumentName): MethodGenerator { $method = new MethodGenerator( 'fromDateTime', @@ -154,10 +182,10 @@ public function methodFromDateTime(string $argumentName): NodeVisitor $method->setTyped($this->typed); $method->setReturnType('self'); - return new ClassMethod($method); + return $method; } - public function methodFromString(string $argumentName): NodeVisitor + public function methodFromString(string $argumentName): MethodGenerator { $body = <<setTyped($this->typed); $method->setReturnType('self'); - return new ClassMethod($method); + return $method; } - public function methodMagicConstruct(string $argumentName): NodeVisitor + public function methodMagicConstruct(string $argumentName): MethodGenerator { $method = new MethodGenerator( '__construct', @@ -197,10 +225,10 @@ public function methodMagicConstruct(string $argumentName): NodeVisitor ); $method->setTyped($this->typed); - return new ClassMethod($method); + return $method; } - public function methodToString(string $argumentName): NodeVisitor + public function methodToString(string $argumentName): MethodGenerator { $method = new MethodGenerator( 'toString', @@ -211,10 +239,10 @@ public function methodToString(string $argumentName): NodeVisitor $method->setTyped($this->typed); $method->setReturnType('string'); - return new ClassMethod($method); + return $method; } - public function methodDateTime(string $argumentName): NodeVisitor + public function methodDateTime(string $argumentName): MethodGenerator { $method = new MethodGenerator( 'dateTime', @@ -225,10 +253,10 @@ public function methodDateTime(string $argumentName): NodeVisitor $method->setTyped($this->typed); $method->setReturnType('DateTimeImmutable'); - return new ClassMethod($method); + return $method; } - public function methodMagicToString(): NodeVisitor + public function methodMagicToString(): MethodGenerator { $method = new MethodGenerator( '__toString', @@ -239,10 +267,10 @@ public function methodMagicToString(): NodeVisitor $method->setTyped($this->typed); $method->setReturnType('string'); - return new ClassMethod($method); + return $method; } - public function methodEnsureUtc(string $argumentName): NodeVisitor + public function methodEnsureUtc(string $argumentName): MethodGenerator { $body = <<<'PHP' if ($argumentName->getTimezone()->getName() !== 'UTC') { @@ -264,6 +292,6 @@ public function methodEnsureUtc(string $argumentName): NodeVisitor $method->setTyped($this->typed); $method->setReturnType('DateTimeImmutable'); - return new ClassMethod($method); + return $method; } } diff --git a/src/ValueObject/IntegerFactory.php b/src/ValueObject/IntegerFactory.php index 6f1eb11..a980768 100644 --- a/src/ValueObject/IntegerFactory.php +++ b/src/ValueObject/IntegerFactory.php @@ -10,6 +10,7 @@ namespace OpenCodeModeling\JsonSchemaToPhpAst\ValueObject; +use OpenCodeModeling\CodeAst\Builder\ClassBuilder; use OpenCodeModeling\CodeAst\Code\BodyGenerator; use OpenCodeModeling\CodeAst\Code\MethodGenerator; use OpenCodeModeling\CodeAst\Code\ParameterGenerator; @@ -79,6 +80,13 @@ public function nodeVisitors(IntegerType $typeDefinition): array return $this->nodeVisitorsFromNative($name); } + public function classBuilder(IntegerType $typeDefinition): ClassBuilder + { + $name = $typeDefinition->name() ?: 'number'; + + return $this->classBuilderFromNative($name)->setTyped($this->typed); + } + /** * @param string $name * @return array @@ -86,16 +94,28 @@ public function nodeVisitors(IntegerType $typeDefinition): array public function nodeVisitorsFromNative(string $name): array { $nodeVisitors = $this->propertyFactory->nodeVisitorFromNative($name, 'int'); - $nodeVisitors[] = $this->methodFromInt($name); - $nodeVisitors[] = $this->methodMagicConstruct($name); - $nodeVisitors[] = $this->methodToInt($name); - $nodeVisitors[] = $this->methodEquals($name); - $nodeVisitors[] = $this->methodMagicToString($name); + $nodeVisitors[] = new ClassMethod($this->methodFromInt($name)); + $nodeVisitors[] = new ClassMethod($this->methodMagicConstruct($name)); + $nodeVisitors[] = new ClassMethod($this->methodToInt($name)); + $nodeVisitors[] = new ClassMethod($this->methodEquals($name)); + $nodeVisitors[] = new ClassMethod($this->methodMagicToString($name)); return $nodeVisitors; } - public function methodFromInt(string $argumentName): NodeVisitor + public function classBuilderFromNative(string $name): ClassBuilder + { + return ClassBuilder::fromNodes( + $this->propertyFactory->propertyGenerator($name, 'int')->generate(), + $this->methodFromInt($name)->generate(), + $this->methodMagicConstruct($name)->generate(), + $this->methodToInt($name)->generate(), + $this->methodEquals($name)->generate(), + $this->methodMagicToString($name)->generate(), + )->setTyped($this->typed); + } + + public function methodFromInt(string $argumentName): MethodGenerator { $method = new MethodGenerator( 'fromInt', @@ -108,10 +128,10 @@ public function methodFromInt(string $argumentName): NodeVisitor $method->setTyped($this->typed); $method->setReturnType('self'); - return new ClassMethod($method); + return $method; } - public function methodMagicConstruct(string $argumentName): NodeVisitor + public function methodMagicConstruct(string $argumentName): MethodGenerator { $method = new MethodGenerator( '__construct', @@ -123,10 +143,10 @@ public function methodMagicConstruct(string $argumentName): NodeVisitor ); $method->setTyped($this->typed); - return new ClassMethod($method); + return $method; } - public function methodToInt(string $argumentName): NodeVisitor + public function methodToInt(string $argumentName): MethodGenerator { $method = new MethodGenerator( 'toInt', @@ -137,10 +157,10 @@ public function methodToInt(string $argumentName): NodeVisitor $method->setTyped($this->typed); $method->setReturnType('int'); - return new ClassMethod($method); + return $method; } - public function methodEquals(string $propertyName, string $argumentName = 'other'): NodeVisitor + public function methodEquals(string $propertyName, string $argumentName = 'other'): MethodGenerator { $body = <<setTyped($this->typed); $method->setReturnType('string'); - return new ClassMethod($method); + return $method; } } diff --git a/src/ValueObject/NumberFactory.php b/src/ValueObject/NumberFactory.php index bed0cc4..87f6ac9 100644 --- a/src/ValueObject/NumberFactory.php +++ b/src/ValueObject/NumberFactory.php @@ -10,6 +10,7 @@ namespace OpenCodeModeling\JsonSchemaToPhpAst\ValueObject; +use OpenCodeModeling\CodeAst\Builder\ClassBuilder; use OpenCodeModeling\CodeAst\Code\BodyGenerator; use OpenCodeModeling\CodeAst\Code\MethodGenerator; use OpenCodeModeling\CodeAst\Code\ParameterGenerator; @@ -79,6 +80,13 @@ public function nodeVisitors(NumberType $typeDefinition): array return $this->nodeVisitorsFromNative($name); } + public function classBuilder(NumberType $typeDefinition): ClassBuilder + { + $name = $typeDefinition->name() ?: 'number'; + + return $this->classBuilderFromNative($name)->setTyped($this->typed); + } + /** * @param string $name * @return array @@ -86,16 +94,28 @@ public function nodeVisitors(NumberType $typeDefinition): array public function nodeVisitorsFromNative(string $name): array { $nodeVisitors = $this->propertyFactory->nodeVisitorFromNative($name, 'float'); - $nodeVisitors[] = $this->methodFromFloat($name); - $nodeVisitors[] = $this->methodMagicConstruct($name); - $nodeVisitors[] = $this->methodToFloat($name); - $nodeVisitors[] = $this->methodEquals($name); - $nodeVisitors[] = $this->methodMagicToString($name); + $nodeVisitors[] = new ClassMethod($this->methodFromFloat($name)); + $nodeVisitors[] = new ClassMethod($this->methodMagicConstruct($name)); + $nodeVisitors[] = new ClassMethod($this->methodToFloat($name)); + $nodeVisitors[] = new ClassMethod($this->methodEquals($name)); + $nodeVisitors[] = new ClassMethod($this->methodMagicToString($name)); return $nodeVisitors; } - public function methodFromFloat(string $argumentName): NodeVisitor + public function classBuilderFromNative(string $name): ClassBuilder + { + return ClassBuilder::fromNodes( + $this->propertyFactory->propertyGenerator($name, 'float')->generate(), + $this->methodFromFloat($name)->generate(), + $this->methodMagicConstruct($name)->generate(), + $this->methodToFloat($name)->generate(), + $this->methodEquals($name)->generate(), + $this->methodMagicToString($name)->generate(), + )->setTyped($this->typed); + } + + public function methodFromFloat(string $argumentName): MethodGenerator { $method = new MethodGenerator( 'fromFloat', @@ -108,10 +128,10 @@ public function methodFromFloat(string $argumentName): NodeVisitor $method->setTyped($this->typed); $method->setReturnType('self'); - return new ClassMethod($method); + return $method; } - public function methodMagicConstruct(string $argumentName): NodeVisitor + public function methodMagicConstruct(string $argumentName): MethodGenerator { $method = new MethodGenerator( '__construct', @@ -123,10 +143,10 @@ public function methodMagicConstruct(string $argumentName): NodeVisitor ); $method->setTyped($this->typed); - return new ClassMethod($method); + return $method; } - public function methodToFloat(string $argumentName): NodeVisitor + public function methodToFloat(string $argumentName): MethodGenerator { $method = new MethodGenerator( 'toFloat', @@ -137,10 +157,10 @@ public function methodToFloat(string $argumentName): NodeVisitor $method->setTyped($this->typed); $method->setReturnType('float'); - return new ClassMethod($method); + return $method; } - public function methodEquals(string $propertyName, string $argumentName = 'other'): NodeVisitor + public function methodEquals(string $propertyName, string $argumentName = 'other'): MethodGenerator { $body = <<setTyped($this->typed); $method->setReturnType('string'); - return new ClassMethod($method); + return $method; } } diff --git a/src/ValueObject/StringFactory.php b/src/ValueObject/StringFactory.php index 4e9cfda..86b180a 100644 --- a/src/ValueObject/StringFactory.php +++ b/src/ValueObject/StringFactory.php @@ -10,6 +10,7 @@ namespace OpenCodeModeling\JsonSchemaToPhpAst\ValueObject; +use OpenCodeModeling\CodeAst\Builder\ClassBuilder; use OpenCodeModeling\CodeAst\Code\BodyGenerator; use OpenCodeModeling\CodeAst\Code\MethodGenerator; use OpenCodeModeling\CodeAst\Code\ParameterGenerator; @@ -79,6 +80,13 @@ public function nodeVisitors(StringType $typeDefinition): array return $this->nodeVisitorsFromNative($name); } + public function classBuilder(StringType $typeDefinition): ClassBuilder + { + $name = $typeDefinition->name() ?: 'text'; + + return $this->classBuilderFromNative($name)->setTyped($this->typed); + } + /** * @param string $name * @return array @@ -86,16 +94,28 @@ public function nodeVisitors(StringType $typeDefinition): array public function nodeVisitorsFromNative(string $name): array { $nodeVisitors = $this->propertyFactory->nodeVisitorFromNative($name, 'string'); - $nodeVisitors[] = $this->methodFromString($name); - $nodeVisitors[] = $this->methodMagicConstruct($name); - $nodeVisitors[] = $this->methodToString($name); - $nodeVisitors[] = $this->methodEquals($name); - $nodeVisitors[] = $this->methodMagicToString($name); + $nodeVisitors[] = new ClassMethod($this->methodFromString($name)); + $nodeVisitors[] = new ClassMethod($this->methodMagicConstruct($name)); + $nodeVisitors[] = new ClassMethod($this->methodToString($name)); + $nodeVisitors[] = new ClassMethod($this->methodEquals($name)); + $nodeVisitors[] = new ClassMethod($this->methodMagicToString($name)); return $nodeVisitors; } - public function methodFromString(string $argumentName): NodeVisitor + public function classBuilderFromNative(string $name): ClassBuilder + { + return ClassBuilder::fromNodes( + $this->propertyFactory->propertyGenerator($name, 'string')->generate(), + $this->methodFromString($name)->generate(), + $this->methodMagicConstruct($name)->generate(), + $this->methodToString($name)->generate(), + $this->methodEquals($name)->generate(), + $this->methodMagicToString($name)->generate(), + )->setTyped($this->typed); + } + + public function methodFromString(string $argumentName): MethodGenerator { $method = new MethodGenerator( 'fromString', @@ -108,10 +128,10 @@ public function methodFromString(string $argumentName): NodeVisitor $method->setTyped($this->typed); $method->setReturnType('self'); - return new ClassMethod($method); + return $method; } - public function methodMagicConstruct(string $argumentName): NodeVisitor + public function methodMagicConstruct(string $argumentName): MethodGenerator { $method = new MethodGenerator( '__construct', @@ -123,10 +143,10 @@ public function methodMagicConstruct(string $argumentName): NodeVisitor ); $method->setTyped($this->typed); - return new ClassMethod($method); + return $method; } - public function methodToString(string $argumentName): NodeVisitor + public function methodToString(string $argumentName): MethodGenerator { $method = new MethodGenerator( 'toString', @@ -137,10 +157,10 @@ public function methodToString(string $argumentName): NodeVisitor $method->setTyped($this->typed); $method->setReturnType('string'); - return new ClassMethod($method); + return $method; } - public function methodEquals(string $propertyName, string $argumentName = 'other'): NodeVisitor + public function methodEquals(string $propertyName, string $argumentName = 'other'): MethodGenerator { $body = <<setTyped($this->typed); $method->setReturnType('string'); - return new ClassMethod($method); + return $method; } } diff --git a/src/ValueObjectFactory.php b/src/ValueObjectFactory.php index 318d6c3..a52b7d1 100644 --- a/src/ValueObjectFactory.php +++ b/src/ValueObjectFactory.php @@ -10,6 +10,7 @@ namespace OpenCodeModeling\JsonSchemaToPhpAst; +use OpenCodeModeling\CodeAst\Builder\ClassBuilder; use OpenCodeModeling\JsonSchemaToPhp\Type\BooleanType; use OpenCodeModeling\JsonSchemaToPhp\Type\IntegerType; use OpenCodeModeling\JsonSchemaToPhp\Type\NumberType; @@ -66,4 +67,26 @@ public function nodeVisitors(TypeDefinition $typeDefinition): array return []; } } + + public function classBuilder(TypeDefinition $typeDefinition): ClassBuilder + { + switch (true) { + case $typeDefinition instanceof StringType: + switch ($typeDefinition->format()) { + case TypeDefinition::FORMAT_DATETIME: + return $this->dateTimeFactory->classBuilder($typeDefinition); + default: + return $this->stringFactory->classBuilder($typeDefinition); + } + // no break + case $typeDefinition instanceof IntegerType: + return $this->integerFactory->classBuilder($typeDefinition); + case $typeDefinition instanceof BooleanType: + return $this->booleanFactory->classBuilder($typeDefinition); + case $typeDefinition instanceof NumberType: + return $this->numberFactory->classBuilder($typeDefinition); + default: + throw new \RuntimeException(\sprintf('Type "%s" not supported', \get_class($typeDefinition))); + } + } } diff --git a/tests/ValueObject/BooleanFactoryTest.php b/tests/ValueObject/BooleanFactoryTest.php index fad7774..549b6d0 100644 --- a/tests/ValueObject/BooleanFactoryTest.php +++ b/tests/ValueObject/BooleanFactoryTest.php @@ -45,6 +45,23 @@ public function it_generates_code_via_value_object_factory(): void $this->assertCode($voFactory->nodeVisitors(BooleanType::fromDefinition(['type' => 'boolean']))); } + /** + * @test + */ + public function it_generates_code_via_value_object_factory_with_class_builder(): void + { + $voFactory = new ValueObjectFactory($this->parser, true); + + $classBuilder = $voFactory->classBuilder( + BooleanType::fromDefinition(['type' => 'boolean']) + ); + $classBuilder->setName('BooleanVO'); + + $this->assertCode( + $classBuilder->generate($this->parser) + ); + } + /** * @param array $nodeVisitors */ diff --git a/tests/ValueObject/DateTimeFactoryTest.php b/tests/ValueObject/DateTimeFactoryTest.php index 8f1158e..2342092 100644 --- a/tests/ValueObject/DateTimeFactoryTest.php +++ b/tests/ValueObject/DateTimeFactoryTest.php @@ -50,6 +50,23 @@ public function it_generates_code_via_value_object_factory(): void ); } + /** + * @test + */ + public function it_generates_code_via_value_object_factory_with_class_builder(): void + { + $voFactory = new ValueObjectFactory($this->parser, true); + + $classBuilder = $voFactory->classBuilder( + StringType::fromDefinition(['type' => 'string', 'format' => TypeDefinition::FORMAT_DATETIME]) + ); + $classBuilder->setName('DateTimeVO'); + + $this->assertCode( + $classBuilder->generate($this->parser) + ); + } + /** * @param array $nodeVisitors */ diff --git a/tests/ValueObject/IntegerFactoryTest.php b/tests/ValueObject/IntegerFactoryTest.php index 272de2d..7a3faf1 100644 --- a/tests/ValueObject/IntegerFactoryTest.php +++ b/tests/ValueObject/IntegerFactoryTest.php @@ -45,6 +45,23 @@ public function it_generates_code_via_value_object_factory(): void $this->assertCode($voFactory->nodeVisitors(IntegerType::fromDefinition(['type' => 'integer']))); } + /** + * @test + */ + public function it_generates_code_via_value_object_factory_with_class_builder(): void + { + $voFactory = new ValueObjectFactory($this->parser, true); + + $classBuilder = $voFactory->classBuilder( + IntegerType::fromDefinition(['type' => 'integer']) + ); + $classBuilder->setName('IntegerVO'); + + $this->assertCode( + $classBuilder->generate($this->parser) + ); + } + /** * @param array $nodeVisitors */ diff --git a/tests/ValueObject/NumberFactoryTest.php b/tests/ValueObject/NumberFactoryTest.php index 645dcab..37c6e5e 100644 --- a/tests/ValueObject/NumberFactoryTest.php +++ b/tests/ValueObject/NumberFactoryTest.php @@ -4,6 +4,7 @@ namespace OpenCodeModelingTest\JsonSchemaToPhpAst\ValueObject; +use OpenCodeModeling\JsonSchemaToPhp\Type\IntegerType; use OpenCodeModeling\JsonSchemaToPhp\Type\NumberType; use OpenCodeModeling\JsonSchemaToPhpAst\ValueObject\NumberFactory; use OpenCodeModeling\JsonSchemaToPhpAst\ValueObjectFactory; @@ -45,6 +46,23 @@ public function it_generates_code_via_value_object_factory(): void $this->assertCode($voFactory->nodeVisitors(NumberType::fromDefinition(['type' => 'number']))); } + /** + * @test + */ + public function it_generates_code_via_value_object_factory_with_class_builder(): void + { + $voFactory = new ValueObjectFactory($this->parser, true); + + $classBuilder = $voFactory->classBuilder( + NumberType::fromDefinition(['type' => 'number']) + ); + $classBuilder->setName('NumberVO'); + + $this->assertCode( + $classBuilder->generate($this->parser) + ); + } + /** * @param array $nodeVisitors */ diff --git a/tests/ValueObject/StringFactoryTest.php b/tests/ValueObject/StringFactoryTest.php index 9019eb3..a95b652 100644 --- a/tests/ValueObject/StringFactoryTest.php +++ b/tests/ValueObject/StringFactoryTest.php @@ -45,6 +45,23 @@ public function it_generates_code_via_value_object_factory(): void $this->assertCode($voFactory->nodeVisitors(StringType::fromDefinition(['type' => 'string', 'name' => 'name']))); } + /** + * @test + */ + public function it_generates_code_via_value_object_factory_with_class_builder(): void + { + $voFactory = new ValueObjectFactory($this->parser, true); + + $classBuilder = $voFactory->classBuilder( + StringType::fromDefinition(['type' => 'string', 'name' => 'name']) + ); + $classBuilder->setName('StringVO'); + + $this->assertCode( + $classBuilder->generate($this->parser) + ); + } + /** * @param array $nodeVisitors */