diff --git a/src/Avro/AvroField.php b/src/Avro/AvroField.php new file mode 100644 index 0000000..5cf1588 --- /dev/null +++ b/src/Avro/AvroField.php @@ -0,0 +1,69 @@ +fieldDefault = $fieldDefault; + $this->fieldDoc = $fieldDoc; + $this->fieldLogicalType = $fieldLogicalType; + $this->fieldName = $fieldName; + $this->fieldType = $fieldType; + } + + /** + * @return mixed + */ + public function getFieldDefault() + { + return $this->fieldDefault; + } + + public function getFieldDoc(): ?string + { + return $this->fieldDoc; + } + + public function getFieldLogicalType(): ?string + { + return $this->fieldLogicalType; + } + + public function getFieldName(): string + { + return $this->fieldName; + } + + /** + * @return string[]|string + */ + public function getFieldType() + { + return $this->fieldType; + } +} diff --git a/src/Avro/AvroFieldInterface.php b/src/Avro/AvroFieldInterface.php new file mode 100644 index 0000000..1768cfd --- /dev/null +++ b/src/Avro/AvroFieldInterface.php @@ -0,0 +1,28 @@ +recordName = $recordName; + $this->recordNamespace = $recordNamespace; + $this->recordFields = $recordFields; + } + + /** + * @return string + */ + public function getRecordNamespace(): ?string + { + return $this->recordNamespace; + } + + /** + * @return string + */ + public function getRecordName(): string + { + return $this->recordName; + } + + /** + * @return AvroFieldInterface[] + */ + public function getRecordFields(): array + { + return $this->recordFields; + } +} diff --git a/src/Avro/AvroRecordInterface.php b/src/Avro/AvroRecordInterface.php new file mode 100644 index 0000000..cf26e0c --- /dev/null +++ b/src/Avro/AvroRecordInterface.php @@ -0,0 +1,17 @@ +parser->setCode($phpClass); @@ -55,38 +55,37 @@ public function convert(string $phpClass): ?PhpClassInterface $convertedProperties = $this->getConvertedProperties($this->parser->getProperties()); - return new PhpClass( + return new AvroRecord( $this->parser->getClassName(), $this->parser->getNamespace(), - $phpClass, $convertedProperties ); } /** - * @param PhpClassPropertyInterface[] $properties - * @return PhpClassPropertyInterface[] + * @param AvroFieldInterface[] $properties + * @return AvroFieldInterface[] */ private function getConvertedProperties(array $properties): array { $convertedProperties = []; foreach ($properties as $property) { - if (false === is_string($property->getPropertyType())) { + if (false === is_string($property->getFieldType())) { continue; } - $convertedType = $this->getConvertedType($property->getPropertyType()); + $convertedType = $this->getConvertedType($property->getFieldType()); if (null === $convertedType) { continue; } - $convertedProperties[] = new PhpClassProperty( - $property->getPropertyName(), + $convertedProperties[] = new AvroField( + $property->getFieldName(), $convertedType, - $property->getPropertyDefault(), - $property->getPropertyDoc(), - $property->getPropertyLogicalType() + $property->getFieldDefault(), + $property->getFieldDoc(), + $property->getFieldLogicalType() ); } diff --git a/src/Converter/PhpClassConverterInterface.php b/src/Converter/PhpClassConverterInterface.php index 8f8730d..3f996f2 100644 --- a/src/Converter/PhpClassConverterInterface.php +++ b/src/Converter/PhpClassConverterInterface.php @@ -4,9 +4,9 @@ namespace PhpKafka\PhpAvroSchemaGenerator\Converter; -use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassInterface; +use PhpKafka\PhpAvroSchemaGenerator\Avro\AvroRecordInterface; interface PhpClassConverterInterface { - public function convert(string $phpClass): ?PhpClassInterface; + public function convert(string $phpClass): ?AvroRecordInterface; } diff --git a/src/Generator/SchemaGenerator.php b/src/Generator/SchemaGenerator.php index 9e99bc6..2156dcd 100644 --- a/src/Generator/SchemaGenerator.php +++ b/src/Generator/SchemaGenerator.php @@ -5,8 +5,8 @@ namespace PhpKafka\PhpAvroSchemaGenerator\Generator; use PhpKafka\PhpAvroSchemaGenerator\Avro\Avro; -use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassInterface; -use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface; +use PhpKafka\PhpAvroSchemaGenerator\Avro\AvroRecordInterface; +use PhpKafka\PhpAvroSchemaGenerator\Avro\AvroFieldInterface; use PhpKafka\PhpAvroSchemaGenerator\Registry\ClassRegistryInterface; use RuntimeException; @@ -67,18 +67,18 @@ public function generate(): array throw new RuntimeException('Please set a ClassRegistry for the generator'); } - /** @var PhpClassInterface $class */ + /** @var AvroRecordInterface $class */ foreach ($this->getClassRegistry()->getClasses() as $class) { $schema = []; $schema['type'] = 'record'; - $schema['name'] = $class->getClassName(); - if (null !== $this->convertNamespace($class->getClassNamespace())) { - $schema['namespace'] = $this->convertNamespace($class->getClassNamespace()); + $schema['name'] = $class->getRecordName(); + if (null !== $this->convertNamespace($class->getRecordNamespace())) { + $schema['namespace'] = $this->convertNamespace($class->getRecordNamespace()); } $schema['fields'] = []; - /** @var PhpClassPropertyInterface $property */ - foreach ($class->getClassProperties() as $property) { + /** @var AvroFieldInterface $property */ + foreach ($class->getRecordFields() as $property) { $field = $this->getFieldForProperty($property); $schema['fields'][] = $field; } @@ -96,24 +96,24 @@ public function generate(): array } /** - * @param PhpClassPropertyInterface $property + * @param AvroFieldInterface $property * @return array */ - private function getFieldForProperty(PhpClassPropertyInterface $property): array + private function getFieldForProperty(AvroFieldInterface $property): array { - $field = ['name' => $property->getPropertyName()]; - $field['type'] = $property->getPropertyType(); + $field = ['name' => $property->getFieldName()]; + $field['type'] = $property->getFieldType(); - if (PhpClassPropertyInterface::NO_DEFAULT !== $property->getPropertyDefault()) { - $field['default'] = $property->getPropertyDefault(); + if (AvroFieldInterface::NO_DEFAULT !== $property->getFieldDefault()) { + $field['default'] = $property->getFieldDefault(); } - if (null !== $property->getPropertyDoc() && '' !== $property->getPropertyDoc()) { - $field['doc'] = $property->getPropertyDoc(); + if (null !== $property->getFieldDoc() && '' !== $property->getFieldDoc()) { + $field['doc'] = $property->getFieldDoc(); } - if (null !== $property->getPropertyLogicalType()) { - $field['logicalType'] = $property->getPropertyLogicalType(); + if (null !== $property->getFieldLogicalType()) { + $field['logicalType'] = $property->getFieldLogicalType(); } return $field; diff --git a/src/Parser/ClassParser.php b/src/Parser/ClassParser.php index d6baa50..c83c345 100644 --- a/src/Parser/ClassParser.php +++ b/src/Parser/ClassParser.php @@ -4,7 +4,7 @@ namespace PhpKafka\PhpAvroSchemaGenerator\Parser; -use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface; +use PhpKafka\PhpAvroSchemaGenerator\Avro\AvroFieldInterface; use PhpParser\Node\Identifier; use PhpParser\Node\Name; use PhpParser\Node\Stmt; @@ -136,7 +136,7 @@ public function getNamespace(): ?string } /** - * @return PhpClassPropertyInterface[] + * @return AvroFieldInterface[] */ public function getProperties(): array { @@ -153,7 +153,7 @@ public function getProperties(): array /** * @param Stmt[] $statements - * @return PhpClassPropertyInterface[] + * @return AvroFieldInterface[] */ private function getClassProperties(array $statements): array { @@ -176,8 +176,8 @@ private function getClassProperties(array $statements): array /** * @param Class_ $class - * @param PhpClassPropertyInterface[] $properties - * @return PhpClassPropertyInterface[] + * @param AvroFieldInterface[] $properties + * @return AvroFieldInterface[] */ private function getAllClassProperties(Class_ $class, array $properties): array { diff --git a/src/Parser/ClassParserInterface.php b/src/Parser/ClassParserInterface.php index 1d685d4..fff2d12 100644 --- a/src/Parser/ClassParserInterface.php +++ b/src/Parser/ClassParserInterface.php @@ -4,7 +4,7 @@ namespace PhpKafka\PhpAvroSchemaGenerator\Parser; -use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassProperty; +use PhpKafka\PhpAvroSchemaGenerator\Avro\AvroField; interface ClassParserInterface { @@ -13,7 +13,7 @@ public function getClassName(): ?string; public function getNamespace(): ?string; /** - * @return PhpClassProperty[] + * @return AvroField[] */ public function getProperties(): array; diff --git a/src/Parser/ClassPropertyParser.php b/src/Parser/ClassPropertyParser.php index 5c813e1..aaa41d7 100644 --- a/src/Parser/ClassPropertyParser.php +++ b/src/Parser/ClassPropertyParser.php @@ -5,8 +5,8 @@ namespace PhpKafka\PhpAvroSchemaGenerator\Parser; use PhpKafka\PhpAvroSchemaGenerator\Avro\Avro; -use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassProperty; -use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface; +use PhpKafka\PhpAvroSchemaGenerator\Avro\AvroField; +use PhpKafka\PhpAvroSchemaGenerator\Avro\AvroFieldInterface; use PhpParser\Comment\Doc; use PhpParser\Node\Identifier; use PhpParser\Node\NullableType; @@ -28,9 +28,9 @@ public function __construct(DocCommentParserInterface $docParser) /** * @param Property|mixed $property - * @return PhpClassPropertyInterface + * @return AvroFieldInterface */ - public function parseProperty($property): PhpClassPropertyInterface + public function parseProperty($property): AvroFieldInterface { if (false === $property instanceof Property) { throw new RuntimeException(sprintf('Property must be of type: %s', Property::class)); @@ -38,7 +38,7 @@ public function parseProperty($property): PhpClassPropertyInterface $propertyAttributes = $this->getPropertyAttributes($property); - return new PhpClassProperty( + return new AvroField( $propertyAttributes['name'], $propertyAttributes['types'], $propertyAttributes['default'], @@ -55,7 +55,7 @@ private function getPropertyAttributes(Property $property): array { $attributes = $this->getEmptyAttributesArray(); $docComments = $this->getAllPropertyDocComments($property); - $attributes['name'] = $this->getPropertyName($property); + $attributes['name'] = $this->getFieldName($property); $attributes['types'] = $this->getTypeFromDocComment($docComments); if (null === $attributes['types']) { @@ -69,7 +69,7 @@ private function getPropertyAttributes(Property $property): array return $attributes; } - private function getPropertyName(Property $property): string + private function getFieldName(Property $property): string { return $property->props[0]->name->name; } @@ -129,10 +129,10 @@ private function getTypeFromDocComment(array $docComments): ?string private function getDefaultFromDocComment(array $docComments) { if (false === isset($docComments['avro-default'])) { - return PhpClassPropertyInterface::NO_DEFAULT; + return AvroFieldInterface::NO_DEFAULT; } - if (PhpClassPropertyInterface::EMPTY_STRING_DEFAULT === $docComments['avro-default']) { + if (AvroFieldInterface::EMPTY_STRING_DEFAULT === $docComments['avro-default']) { return ''; } @@ -208,7 +208,7 @@ private function getEmptyAttributesArray(): array return [ 'name' => null, 'types' => null, - 'default' => PhpClassPropertyInterface::NO_DEFAULT, + 'default' => AvroFieldInterface::NO_DEFAULT, 'logicalType' => null, 'doc' => null ]; diff --git a/src/Parser/ClassPropertyParserInterface.php b/src/Parser/ClassPropertyParserInterface.php index f180109..c7ab9da 100644 --- a/src/Parser/ClassPropertyParserInterface.php +++ b/src/Parser/ClassPropertyParserInterface.php @@ -4,14 +4,14 @@ namespace PhpKafka\PhpAvroSchemaGenerator\Parser; -use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface; +use PhpKafka\PhpAvroSchemaGenerator\Avro\AvroFieldInterface; use PhpParser\Node\Stmt\Property; interface ClassPropertyParserInterface { /** * @param Property|mixed $property - * @return PhpClassPropertyInterface + * @return AvroFieldInterface */ - public function parseProperty($property): PhpClassPropertyInterface; + public function parseProperty($property): AvroFieldInterface; } diff --git a/src/PhpClass/PhpClass.php b/src/PhpClass/PhpClass.php deleted file mode 100644 index 15b52e6..0000000 --- a/src/PhpClass/PhpClass.php +++ /dev/null @@ -1,63 +0,0 @@ -className = $className; - $this->classNamespace = $classNamespace; - $this->classBody = $classBody; - $this->classProperties = $classProperties; - } - - /** - * @return string - */ - public function getClassNamespace(): ?string - { - return $this->classNamespace; - } - - /** - * @return string - */ - public function getClassName(): string - { - return $this->className; - } - - /** - * @return string - */ - public function getClassBody(): string - { - return $this->classBody; - } - - /** - * @return PhpClassPropertyInterface[] - */ - public function getClassProperties(): array - { - return $this->classProperties; - } -} diff --git a/src/PhpClass/PhpClassInterface.php b/src/PhpClass/PhpClassInterface.php deleted file mode 100644 index 126d3b9..0000000 --- a/src/PhpClass/PhpClassInterface.php +++ /dev/null @@ -1,19 +0,0 @@ -propertyDefault = $propertyDefault; - $this->propertyDoc = $propertyDoc; - $this->propertyLogicalType = $propertyLogicalType; - $this->propertyName = $propertyName; - $this->propertyType = $propertyType; - } - - /** - * @return mixed - */ - public function getPropertyDefault() - { - return $this->propertyDefault; - } - - public function getPropertyDoc(): ?string - { - return $this->propertyDoc; - } - - public function getPropertyLogicalType(): ?string - { - return $this->propertyLogicalType; - } - - public function getPropertyName(): string - { - return $this->propertyName; - } - - /** - * @return string[]|string - */ - public function getPropertyType() - { - return $this->propertyType; - } -} diff --git a/src/PhpClass/PhpClassPropertyInterface.php b/src/PhpClass/PhpClassPropertyInterface.php deleted file mode 100644 index 5a4d716..0000000 --- a/src/PhpClass/PhpClassPropertyInterface.php +++ /dev/null @@ -1,28 +0,0 @@ -getClassName()); } - public function testGetClassNameForInterface() + public function testgetClassNameForInterface() { $filePath = __DIR__ . '/../../../example/classes/SomeTestInterface.php'; $propertyParser = new ClassPropertyParser(new DocCommentParser()); @@ -55,7 +55,7 @@ public function testGetProperties() self::assertCount(16, $properties); foreach($properties as $property) { - self::assertInstanceOf(PhpClassPropertyInterface::class, $property); + self::assertInstanceOf(AvroFieldInterface::class, $property); } } @@ -96,7 +96,7 @@ class foo { '); $properties = $parser->getProperties(); self::assertEquals(1, count($properties)); - self::assertEquals('null|string', $properties[0]->getPropertyType()); + self::assertEquals('null|string', $properties[0]->getFieldType()); } public function testClassWithUnionType(): void @@ -111,7 +111,7 @@ class foo { '); $properties = $parser->getProperties(); self::assertEquals(1, count($properties)); - self::assertEquals('int|string', $properties[0]->getPropertyType()); + self::assertEquals('int|string', $properties[0]->getFieldType()); } public function testClassWithDocUnionType(): void @@ -129,7 +129,7 @@ class foo { '); $properties = $parser->getProperties(); self::assertEquals(1, count($properties)); - self::assertEquals('int|string', $properties[0]->getPropertyType()); + self::assertEquals('int|string', $properties[0]->getFieldType()); } public function testClassWithAnnotations(): void @@ -150,9 +150,9 @@ class foo { '); $properties = $parser->getProperties(); self::assertEquals(1, count($properties)); - self::assertEquals('string', $properties[0]->getPropertyType()); - self::assertEquals('abc def', $properties[0]->getPropertyDefault()); - self::assertEquals('some doc bla bla', $properties[0]->getPropertyDoc()); + self::assertEquals('string', $properties[0]->getFieldType()); + self::assertEquals('abc def', $properties[0]->getFieldDefault()); + self::assertEquals('some doc bla bla', $properties[0]->getFieldDoc()); } @@ -163,6 +163,6 @@ public function testClassWithNoParentFile(): void $parser->setCode('getProperties(); self::assertEquals(1, count($properties)); - self::assertEquals('string', $properties[0]->getPropertyType()); + self::assertEquals('string', $properties[0]->getFieldType()); } } diff --git a/tests/Integration/Parser/ClassPropertyParserTest.php b/tests/Integration/Parser/ClassPropertyParserTest.php index 751e11c..4075d21 100644 --- a/tests/Integration/Parser/ClassPropertyParserTest.php +++ b/tests/Integration/Parser/ClassPropertyParserTest.php @@ -7,7 +7,7 @@ use PhpKafka\PhpAvroSchemaGenerator\Parser\ClassParser; use PhpKafka\PhpAvroSchemaGenerator\Parser\ClassPropertyParser; use PhpKafka\PhpAvroSchemaGenerator\Parser\DocCommentParser; -use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface; +use PhpKafka\PhpAvroSchemaGenerator\Avro\AvroFieldInterface; use PhpParser\ParserFactory; use PHPUnit\Framework\TestCase; @@ -31,7 +31,7 @@ class foo { '); $properties = $parser->getProperties(); self::assertEquals(1, count($properties)); - self::assertNull($properties[0]->getPropertyDefault()); + self::assertNull($properties[0]->getFieldDefault()); } public function testIntDefaultProperty(): void @@ -49,7 +49,7 @@ class foo { '); $properties = $parser->getProperties(); self::assertEquals(1, count($properties)); - self::assertIsInt($properties[0]->getPropertyDefault()); + self::assertIsInt($properties[0]->getFieldDefault()); } public function testFloatDefaultProperty(): void @@ -67,7 +67,7 @@ class foo { '); $properties = $parser->getProperties(); self::assertEquals(1, count($properties)); - self::assertIsFloat($properties[0]->getPropertyDefault()); + self::assertIsFloat($properties[0]->getFieldDefault()); } public function testEmptyStringDefaultProperty(): void @@ -85,6 +85,6 @@ class foo { '); $properties = $parser->getProperties(); self::assertEquals(1, count($properties)); - self::assertEquals('', $properties[0]->getPropertyDefault()); + self::assertEquals('', $properties[0]->getFieldDefault()); } } diff --git a/tests/Integration/Registry/ClassRegistryTest.php b/tests/Integration/Registry/ClassRegistryTest.php index 7812c9c..44b0e2b 100644 --- a/tests/Integration/Registry/ClassRegistryTest.php +++ b/tests/Integration/Registry/ClassRegistryTest.php @@ -9,7 +9,7 @@ use PhpKafka\PhpAvroSchemaGenerator\Parser\ClassParser; use PhpKafka\PhpAvroSchemaGenerator\Parser\ClassPropertyParser; use PhpKafka\PhpAvroSchemaGenerator\Parser\DocCommentParser; -use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassInterface; +use PhpKafka\PhpAvroSchemaGenerator\Avro\AvroRecordInterface; use PhpKafka\PhpAvroSchemaGenerator\Registry\ClassRegistry; use PhpKafka\PhpAvroSchemaGenerator\Registry\ClassRegistryInterface; use PhpParser\ParserFactory; @@ -50,7 +50,7 @@ public function testLoad() self::assertCount(4, $classes); foreach ($classes as $class) { - self::assertInstanceOf(PhpClassInterface::class, $class); + self::assertInstanceOf(AvroRecordInterface::class, $class); } } diff --git a/tests/Unit/Avro/AvroFieldTest.php b/tests/Unit/Avro/AvroFieldTest.php new file mode 100644 index 0000000..00dffd9 --- /dev/null +++ b/tests/Unit/Avro/AvroFieldTest.php @@ -0,0 +1,25 @@ +getFieldName()); + self::assertEquals('array', $field->getFieldType()); + self::assertEquals('default', $field->getFieldDefault()); + self::assertEquals('doc', $field->getFieldDoc()); + self::assertEquals('logicalType', $field->getFieldLogicalType()); + } +} diff --git a/tests/Unit/Avro/AvroRecordTest.php b/tests/Unit/Avro/AvroRecordTest.php new file mode 100644 index 0000000..7ff7b21 --- /dev/null +++ b/tests/Unit/Avro/AvroRecordTest.php @@ -0,0 +1,23 @@ +getRecordName()); + self::assertEquals('Test\\Space', $avroRecord->getRecordNamespace()); + self::assertEquals([], $avroRecord->getRecordFields()); + } +} diff --git a/tests/Unit/Generator/SchemaGeneratorTest.php b/tests/Unit/Generator/SchemaGeneratorTest.php index 0a798e6..47551fc 100644 --- a/tests/Unit/Generator/SchemaGeneratorTest.php +++ b/tests/Unit/Generator/SchemaGeneratorTest.php @@ -3,8 +3,8 @@ namespace PhpKafka\PhpAvroSchemaGenerator\Tests\Unit\Generator; use PhpKafka\PhpAvroSchemaGenerator\Generator\SchemaGenerator; -use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassInterface; -use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface; +use PhpKafka\PhpAvroSchemaGenerator\Avro\AvroRecordInterface; +use PhpKafka\PhpAvroSchemaGenerator\Avro\AvroFieldInterface; use PhpKafka\PhpAvroSchemaGenerator\Registry\ClassRegistryInterface; use PHPUnit\Framework\TestCase; @@ -76,28 +76,28 @@ public function testGenerate() ]) ]; - $property1 = $this->getMockForAbstractClass(PhpClassPropertyInterface::class); - $property1->expects(self::exactly(1))->method('getPropertyType')->willReturn(["type" => "array","items" => "test.foo"]); - $property1->expects(self::exactly(1))->method('getPropertyName')->willReturn('items'); - $property1->expects(self::exactly(1))->method('getPropertyDefault')->willReturn(PhpClassPropertyInterface::NO_DEFAULT); + $property1 = $this->getMockForAbstractClass(AvroFieldInterface::class); + $property1->expects(self::exactly(1))->method('getFieldType')->willReturn(["type" => "array","items" => "test.foo"]); + $property1->expects(self::exactly(1))->method('getFieldName')->willReturn('items'); + $property1->expects(self::exactly(1))->method('getFieldDefault')->willReturn(AvroFieldInterface::NO_DEFAULT); - $property2 = $this->getMockForAbstractClass(PhpClassPropertyInterface::class); - $property2->expects(self::exactly(2))->method('getPropertyType')->willReturn('string'); - $property2->expects(self::exactly(2))->method('getPropertyName')->willReturn('name'); - $property2->expects(self::exactly(4))->method('getPropertyDefault')->willReturn('test'); - $property2->expects(self::exactly(6))->method('getPropertyDoc')->willReturn('test'); - $property2->expects(self::exactly(4))->method('getPropertyLogicalType')->willReturn('test'); + $property2 = $this->getMockForAbstractClass(AvroFieldInterface::class); + $property2->expects(self::exactly(2))->method('getFieldType')->willReturn('string'); + $property2->expects(self::exactly(2))->method('getFieldName')->willReturn('name'); + $property2->expects(self::exactly(4))->method('getFieldDefault')->willReturn('test'); + $property2->expects(self::exactly(6))->method('getFieldDoc')->willReturn('test'); + $property2->expects(self::exactly(4))->method('getFieldLogicalType')->willReturn('test'); - $class1 = $this->getMockForAbstractClass(PhpClassInterface::class); - $class1->expects(self::once())->method('getClassName')->willReturn('TestClass'); - $class1->expects(self::exactly(2))->method('getClassNamespace')->willReturn('name\\space'); - $class1->expects(self::once())->method('getClassProperties')->willReturn([$property1, $property2]); + $class1 = $this->getMockForAbstractClass(AvroRecordInterface::class); + $class1->expects(self::once())->method('getRecordName')->willReturn('TestClass'); + $class1->expects(self::exactly(2))->method('getRecordNamespace')->willReturn('name\\space'); + $class1->expects(self::once())->method('getRecordFields')->willReturn([$property1, $property2]); - $class2 = $this->getMockForAbstractClass(PhpClassInterface::class); - $class2->expects(self::once())->method('getClassName')->willReturn('Test2Class'); - $class2->expects(self::once())->method('getClassNamespace')->willReturn(null); - $class2->expects(self::once())->method('getClassProperties')->willReturn([$property2]); + $class2 = $this->getMockForAbstractClass(AvroRecordInterface::class); + $class2->expects(self::once())->method('getRecordName')->willReturn('Test2Class'); + $class2->expects(self::once())->method('getRecordNamespace')->willReturn(null); + $class2->expects(self::once())->method('getRecordFields')->willReturn([$property2]); $registry = $this->getMockForAbstractClass(ClassRegistryInterface::class); $registry->expects(self::once())->method('getClasses')->willReturn([$class1, $class2]); diff --git a/tests/Unit/Parser/ClassPropertyParserTest.php b/tests/Unit/Parser/ClassPropertyParserTest.php index 1811e1f..a31c40f 100644 --- a/tests/Unit/Parser/ClassPropertyParserTest.php +++ b/tests/Unit/Parser/ClassPropertyParserTest.php @@ -6,7 +6,7 @@ use PhpKafka\PhpAvroSchemaGenerator\Parser\ClassPropertyParser; use PhpKafka\PhpAvroSchemaGenerator\Parser\DocCommentParserInterface; -use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface; +use PhpKafka\PhpAvroSchemaGenerator\Avro\AvroFieldInterface; use PhpParser\Comment\Doc; use PhpParser\Node\Identifier; use PhpParser\Node\NullableType; @@ -48,10 +48,10 @@ public function testParseProperty(): void $property4->props = [$propertyProperty]; $cpp = new ClassPropertyParser($docParser); - self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($property1)); - self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($property2)); - self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($property3)); - self::assertInstanceOf(PhpClassPropertyInterface::class, $cpp->parseProperty($property4)); + self::assertInstanceOf(AvroFieldInterface::class, $cpp->parseProperty($property1)); + self::assertInstanceOf(AvroFieldInterface::class, $cpp->parseProperty($property2)); + self::assertInstanceOf(AvroFieldInterface::class, $cpp->parseProperty($property3)); + self::assertInstanceOf(AvroFieldInterface::class, $cpp->parseProperty($property4)); } public function testParsePropertyExceptionOnNonProperty(): void diff --git a/tests/Unit/PhpClass/PhpClassPropertyTest.php b/tests/Unit/PhpClass/PhpClassPropertyTest.php deleted file mode 100644 index 4a59531..0000000 --- a/tests/Unit/PhpClass/PhpClassPropertyTest.php +++ /dev/null @@ -1,25 +0,0 @@ -getPropertyName()); - self::assertEquals('array', $property->getPropertyType()); - self::assertEquals('default', $property->getPropertyDefault()); - self::assertEquals('doc', $property->getPropertyDoc()); - self::assertEquals('logicalType', $property->getPropertyLogicalType()); - } -} diff --git a/tests/Unit/PhpClass/PhpClassTest.php b/tests/Unit/PhpClass/PhpClassTest.php deleted file mode 100644 index fbe9d40..0000000 --- a/tests/Unit/PhpClass/PhpClassTest.php +++ /dev/null @@ -1,24 +0,0 @@ -getClassName()); - self::assertEquals('Test\\Space', $phpClass->getClassNamespace()); - self::assertEquals('some php code', $phpClass->getClassBody()); - self::assertEquals([], $phpClass->getClassProperties()); - } -} diff --git a/tests/Unit/PhpClassConverterTest.php b/tests/Unit/PhpClassConverterTest.php index 154971f..240e127 100644 --- a/tests/Unit/PhpClassConverterTest.php +++ b/tests/Unit/PhpClassConverterTest.php @@ -6,26 +6,26 @@ use PhpKafka\PhpAvroSchemaGenerator\Converter\PhpClassConverter; use PhpKafka\PhpAvroSchemaGenerator\Parser\ClassParserInterface; -use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassInterface; -use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface; +use PhpKafka\PhpAvroSchemaGenerator\Avro\AvroRecordInterface; +use PhpKafka\PhpAvroSchemaGenerator\Avro\AvroFieldInterface; use PHPUnit\Framework\TestCase; class PhpClassConverterTest extends TestCase { public function testConvert(): void { - $property1 = $this->getMockForAbstractClass(PhpClassPropertyInterface::class); - $property1->expects(self::once())->method('getPropertyType')->willReturn(1); - $property2 = $this->getMockForAbstractClass(PhpClassPropertyInterface::class); - $property2->expects(self::exactly(2))->method('getPropertyType')->willReturn('string|array|int[]|mixed[]'); - $property3 = $this->getMockForAbstractClass(PhpClassPropertyInterface::class); - $property3->expects(self::exactly(2))->method('getPropertyType')->willReturn('string'); - $property4 = $this->getMockForAbstractClass(PhpClassPropertyInterface::class); - $property4->expects(self::exactly(2))->method('getPropertyType')->willReturn('object|XYZ|UC'); - $property5 = $this->getMockForAbstractClass(PhpClassPropertyInterface::class); - $property5->expects(self::exactly(2))->method('getPropertyType')->willReturn('mixed'); - $property6 = $this->getMockForAbstractClass(PhpClassPropertyInterface::class); - $property6->expects(self::exactly(2))->method('getPropertyType')->willReturn('array|mixed[]'); + $property1 = $this->getMockForAbstractClass(AvroFieldInterface::class); + $property1->expects(self::once())->method('getFieldType')->willReturn(1); + $property2 = $this->getMockForAbstractClass(AvroFieldInterface::class); + $property2->expects(self::exactly(2))->method('getFieldType')->willReturn('string|array|int[]|mixed[]'); + $property3 = $this->getMockForAbstractClass(AvroFieldInterface::class); + $property3->expects(self::exactly(2))->method('getFieldType')->willReturn('string'); + $property4 = $this->getMockForAbstractClass(AvroFieldInterface::class); + $property4->expects(self::exactly(2))->method('getFieldType')->willReturn('object|XYZ|UC'); + $property5 = $this->getMockForAbstractClass(AvroFieldInterface::class); + $property5->expects(self::exactly(2))->method('getFieldType')->willReturn('mixed'); + $property6 = $this->getMockForAbstractClass(AvroFieldInterface::class); + $property6->expects(self::exactly(2))->method('getFieldType')->willReturn('array|mixed[]'); $parser = $this->getMockForAbstractClass(ClassParserInterface::class); @@ -38,13 +38,13 @@ public function testConvert(): void $parser->expects(self::exactly(3))->method('getNamespace')->willReturn('x\\y'); $converter = new PhpClassConverter($parser); - self::assertInstanceOf(PhpClassInterface::class, $converter->convert('some class stuff')); + self::assertInstanceOf(AvroRecordInterface::class, $converter->convert('some class stuff')); } public function testConvertWithNoNamesace(): void { - $property1 = $this->getMockForAbstractClass(PhpClassPropertyInterface::class); - $property1->expects(self::exactly(2))->method('getPropertyType')->willReturn('ABC'); + $property1 = $this->getMockForAbstractClass(AvroFieldInterface::class); + $property1->expects(self::exactly(2))->method('getFieldType')->willReturn('ABC'); $parser = $this->getMockForAbstractClass(ClassParserInterface::class); @@ -55,7 +55,7 @@ public function testConvertWithNoNamesace(): void $parser->expects(self::exactly(2))->method('getNamespace')->willReturn(null); $converter = new PhpClassConverter($parser); - self::assertInstanceOf(PhpClassInterface::class, $converter->convert('some class stuff')); + self::assertInstanceOf(AvroRecordInterface::class, $converter->convert('some class stuff')); } public function testConvertOfNonClass(): void