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

refactor token parser #34

Merged
merged 28 commits into from
Dec 18, 2021
Merged
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
Prev Previous commit
Next Next commit
fix tests
  • Loading branch information
nick-zh committed Dec 16, 2021
commit 70dffba5d8289e47359cc5bc5f9bcc35666cb9fc
61 changes: 61 additions & 0 deletions tests/Integration/Parser/ClassParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace PhpKafka\PhpAvroSchemaGenerator\Tests\Integration\Parser;

use PhpKafka\PhpAvroSchemaGenerator\Parser\ClassParser;
use PhpKafka\PhpAvroSchemaGenerator\Parser\ClassPropertyParser;
use PhpKafka\PhpAvroSchemaGenerator\Parser\DocCommentParser;
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface;
use PhpParser\ParserFactory;
use PHPUnit\Framework\TestCase;

/**
* @covers PhpKafka\PhpAvroSchemaGenerator\Parser\ClassParser
*/
class ClassParserTest extends TestCase
{
public function testGetClassName()
{
$filePath = __DIR__ . '/../../../example/classes/SomeTestClass.php';
$propertyParser = new ClassPropertyParser(new DocCommentParser());
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
$parser->setCode(file_get_contents($filePath));
self::assertEquals('SomeTestClass', $parser->getClassName());
self::assertEquals('SomeTestClass', $parser->getClassName());
}

public function testGetClassNameForInterface()
{
$filePath = __DIR__ . '/../../../example/classes/SomeTestInterface.php';
$propertyParser = new ClassPropertyParser(new DocCommentParser());
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
$parser->setCode(file_get_contents($filePath));
self::assertNull($parser->getClassName());
}

public function testGetNamespace()
{
$filePath = __DIR__ . '/../../../example/classes/SomeTestClass.php';
$propertyParser = new ClassPropertyParser(new DocCommentParser());
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
$parser->setCode(file_get_contents($filePath));
self::assertEquals('PhpKafka\\PhpAvroSchemaGenerator\\Example', $parser->getNamespace());
self::assertEquals('PhpKafka\\PhpAvroSchemaGenerator\\Example', $parser->getNamespace());
}

public function testGetProperties()
{
$filePath = __DIR__ . '/../../../example/classes/SomeTestClass.php';
$propertyParser = new ClassPropertyParser(new DocCommentParser());
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
$parser->setCode(file_get_contents($filePath));
$properties = $parser->getProperties();
self::assertCount(15, $properties);

foreach($properties as $property) {
self::assertInstanceOf(PhpClassPropertyInterface::class, $property);
}
}
}
50 changes: 0 additions & 50 deletions tests/Integration/Parser/TokenParserTest.php

This file was deleted.

25 changes: 21 additions & 4 deletions tests/Integration/Registry/ClassRegistryTest.php
Original file line number Diff line number Diff line change
@@ -4,10 +4,15 @@

namespace PhpKafka\PhpAvroSchemaGenerator\Tests\Integration\Registry;

use PhpKafka\PhpAvroSchemaGenerator\Converter\PhpClassConverter;
use PhpKafka\PhpAvroSchemaGenerator\Exception\ClassRegistryException;
use PhpKafka\PhpAvroSchemaGenerator\Parser\ClassParser;
use PhpKafka\PhpAvroSchemaGenerator\Parser\ClassPropertyParser;
use PhpKafka\PhpAvroSchemaGenerator\Parser\DocCommentParser;
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassInterface;
use PhpKafka\PhpAvroSchemaGenerator\Registry\ClassRegistry;
use PhpKafka\PhpAvroSchemaGenerator\Registry\ClassRegistryInterface;
use PhpParser\ParserFactory;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use SplFileInfo;
@@ -19,7 +24,10 @@ class ClassRegistryTest extends TestCase
{
public function testClassDirectory()
{
$registry = new ClassRegistry();
$propertyParser = new ClassPropertyParser(new DocCommentParser());
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
$converter = new PhpClassConverter($parser);
$registry = new ClassRegistry($converter);
$result = $registry->addClassDirectory('/tmp');

self::assertInstanceOf(ClassRegistryInterface::class, $result);
@@ -30,7 +38,10 @@ public function testLoad()
{
$classDir = __DIR__ . '/../../../example/classes';

$registry = (new ClassRegistry())->addClassDirectory($classDir)->load();
$propertyParser = new ClassPropertyParser(new DocCommentParser());
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
$converter = new PhpClassConverter($parser);
$registry = (new ClassRegistry($converter))->addClassDirectory($classDir)->load();

self::assertInstanceOf(ClassRegistryInterface::class, $registry);

@@ -46,7 +57,10 @@ public function testLoad()
public function testRegisterSchemaFileThatDoesntExist()
{
$fileInfo = new SplFileInfo('somenonexistingfile');
$registry = new ClassRegistry();
$propertyParser = new ClassPropertyParser(new DocCommentParser());
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
$converter = new PhpClassConverter($parser);
$registry = new ClassRegistry($converter);

self::expectException(ClassRegistryException::class);
self::expectExceptionMessage(ClassRegistryException::FILE_PATH_EXCEPTION_MESSAGE);
@@ -64,7 +78,10 @@ public function testRegisterSchemaFileThatIsNotReadable()

$fileInfo = new SplFileInfo('testfile');

$registry = new ClassRegistry();
$propertyParser = new ClassPropertyParser(new DocCommentParser());
$parser = new ClassParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7), $propertyParser);
$converter = new PhpClassConverter($parser);
$registry = new ClassRegistry($converter);

self::expectException(ClassRegistryException::class);
self::expectExceptionMessage(
26 changes: 14 additions & 12 deletions tests/Unit/Generator/SchemaGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -17,7 +17,8 @@ public function testDefaultOutputDirectory()
{
$registry = $this->getMockForAbstractClass(ClassRegistryInterface::class);

$generator = new SchemaGenerator($registry);
$generator = new SchemaGenerator();
$generator->setClassRegistry($registry);

self::assertEquals($registry, $generator->getClassRegistry());
self::assertEquals('/tmp', $generator->getOutputDirectory());
@@ -28,7 +29,9 @@ public function testGetters()
$registry = $this->getMockForAbstractClass(ClassRegistryInterface::class);
$directory = '/tmp/foo';

$generator = new SchemaGenerator($registry, $directory);
$generator = new SchemaGenerator();
$generator->setClassRegistry($registry);
$generator->setOutputDirectory($directory);

self::assertEquals($registry, $generator->getClassRegistry());
self::assertEquals($directory, $generator->getOutputDirectory());
@@ -69,22 +72,19 @@ public function testGenerate()
];

$property1 = $this->getMockForAbstractClass(PhpClassPropertyInterface::class);
$property1->expects(self::exactly(3))->method('getPropertyType')->willReturn('array');
$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('getPropertyArrayType')->willReturn('test\\foo');
$property1->expects(self::exactly(1))->method('getPropertyDefault')->willReturn(PhpClassPropertyInterface::NO_DEFAULT);

$property2 = $this->getMockForAbstractClass(PhpClassPropertyInterface::class);
$property2->expects(self::exactly(6))->method('getPropertyType')->willReturn('string');
$property2->expects(self::exactly(2))->method('getPropertyType')->willReturn('string');
$property2->expects(self::exactly(2))->method('getPropertyName')->willReturn('name');

$property3 = $this->getMockForAbstractClass(PhpClassPropertyInterface::class);
$property3->expects(self::once())->method('getPropertyType')->willReturn('mixed');
$property3->expects(self::never())->method('getPropertyName');
$property2->expects(self::exactly(2))->method('getPropertyDefault')->willReturn(PhpClassPropertyInterface::NO_DEFAULT);

$class1 = $this->getMockForAbstractClass(PhpClassInterface::class);
$class1->expects(self::once())->method('getClassName')->willReturn('TestClass');
$class1->expects(self::once())->method('getClassNamespace')->willReturn('name\\space');
$class1->expects(self::once())->method('getClassProperties')->willReturn([$property1, $property2, $property3]);
$class1->expects(self::once())->method('getClassProperties')->willReturn([$property1, $property2]);

$class2 = $this->getMockForAbstractClass(PhpClassInterface::class);
$class2->expects(self::once())->method('getClassName')->willReturn('Test2Class');
@@ -94,7 +94,8 @@ public function testGenerate()
$registry = $this->getMockForAbstractClass(ClassRegistryInterface::class);
$registry->expects(self::once())->method('getClasses')->willReturn([$class1, $class2]);

$generator = new SchemaGenerator($registry);
$generator = new SchemaGenerator();
$generator->setClassRegistry($registry);
$result = $generator->generate();
self::assertEquals($expectedResult, $result);
self::assertCount(2, $result);
@@ -107,7 +108,8 @@ public function testExportSchemas()
];

$registry = $this->getMockForAbstractClass(ClassRegistryInterface::class);
$generator = new SchemaGenerator($registry);
$generator = new SchemaGenerator();
$generator->setClassRegistry($registry);
$fileCount = $generator->exportSchemas($schemas);

self::assertFileExists('/tmp/filename.avsc');
46 changes: 29 additions & 17 deletions tests/Unit/Merger/SchemaMergerTest.php
Original file line number Diff line number Diff line change
@@ -21,22 +21,21 @@ class SchemaMergerTest extends TestCase
public function testGetSchemaRegistry()
{
$schemaRegistry = $this->getMockForAbstractClass(SchemaRegistryInterface::class);
$merger = new SchemaMerger($schemaRegistry);
$merger = new SchemaMerger();
$merger->setSchemaRegistry($schemaRegistry);
self::assertEquals($schemaRegistry, $merger->getSchemaRegistry());
}

public function testGetOutputDirectoryDefault()
{
$schemaRegistry = $this->getMockForAbstractClass(SchemaRegistryInterface::class);
$merger = new SchemaMerger($schemaRegistry);
$merger = new SchemaMerger();
self::assertEquals('/tmp', $merger->getOutputDirectory());
}

public function testGetOutputDirectory()
{
$schemaRegistry = $this->getMockForAbstractClass(SchemaRegistryInterface::class);
$outputDirectory = '/root';
$merger = new SchemaMerger($schemaRegistry, $outputDirectory);
$merger = new SchemaMerger($outputDirectory);
self::assertEquals($outputDirectory, $merger->getOutputDirectory());
}

@@ -47,7 +46,8 @@ public function testGetResolvedSchemaTemplateThrowsException()
$schemaRegistry = $this->getMockForAbstractClass(SchemaRegistryInterface::class);
$schemaTemplate = $this->getMockForAbstractClass(SchemaTemplateInterface::class);
$schemaTemplate->expects(self::once())->method('getSchemaDefinition')->willReturn('{"type": 1}');
$merger = new SchemaMerger($schemaRegistry);
$merger = new SchemaMerger();
$merger->setSchemaRegistry($schemaRegistry);

self::assertEquals([], $merger->getResolvedSchemaTemplate($schemaTemplate));
}
@@ -71,7 +71,8 @@ public function testGetResolvedSchemaTemplateResolveEmbeddedException()
->expects(self::once())
->method('getSchemaDefinition')
->willReturn($definitionWithType);
$merger = new SchemaMerger($schemaRegistry);
$merger = new SchemaMerger();
$merger->setSchemaRegistry($schemaRegistry);

self::assertEquals([], $merger->getResolvedSchemaTemplate($schemaTemplate));
}
@@ -119,7 +120,8 @@ public function testGetResolvedSchemaTemplate()
->with($expectedResult)
->willReturn($rootSchemaTemplate);

$merger = new SchemaMerger($schemaRegistry);
$merger = new SchemaMerger();
$merger->setSchemaRegistry($schemaRegistry);

$merger->getResolvedSchemaTemplate($rootSchemaTemplate);
}
@@ -289,7 +291,8 @@ public function testGetResolvedSchemaTemplateWithMultiEmbedd()
->with($expectedResult)
->willReturn($rootSchemaTemplate);

$merger = new SchemaMerger($schemaRegistry);
$merger = new SchemaMerger();
$merger->setSchemaRegistry($schemaRegistry);

$merger->getResolvedSchemaTemplate($rootSchemaTemplate);
}
@@ -339,7 +342,8 @@ public function testGetResolvedSchemaTemplateWithDifferentNamespaceForEmbeddedSc
->with($expectedResult)
->willReturn($rootSchemaTemplate);

$merger = new SchemaMerger($schemaRegistry);
$merger = new SchemaMerger();
$merger->setSchemaRegistry($schemaRegistry);

$merger->getResolvedSchemaTemplate($rootSchemaTemplate);
}
@@ -368,7 +372,9 @@ public function testMergeException()
->expects(self::once())
->method('getRootSchemas')
->willReturn([$schemaTemplate]);
$merger = new SchemaMerger($schemaRegistry);
$merger = new SchemaMerger();
$merger->setSchemaRegistry($schemaRegistry);

$merger->merge();
}

@@ -401,7 +407,8 @@ public function testMerge()
->willReturn([$schemaTemplate]);
$optimizer = $this->getMockForAbstractClass(OptimizerInterface::class);
$optimizer->expects(self::once())->method('optimize')->with($schemaTemplate)->willReturn($schemaTemplate);
$merger = new SchemaMerger($schemaRegistry, '/tmp/foobar');
$merger = new SchemaMerger('/tmp/foobar');
$merger->setSchemaRegistry($schemaRegistry);
$merger->addOptimizer($optimizer);
$mergedFiles = $merger->merge(true);

@@ -437,7 +444,8 @@ public function testMergePrimitive()
->expects(self::once())
->method('getRootSchemas')
->willReturn([$schemaTemplate]);
$merger = new SchemaMerger($schemaRegistry, '/tmp/foobar');
$merger = new SchemaMerger('/tmp/foobar');
$merger->setSchemaRegistry($schemaRegistry);
$merger->merge(false, true);

self::assertFileExists('/tmp/foobar/primitive-type.avsc');
@@ -477,7 +485,8 @@ public function testMergePrimitiveWithOptimizerEnabled()
->willReturn([$schemaTemplate]);
$optimizer = $this->getMockBuilder(PrimitiveSchemaOptimizer::class)->getMock();
$optimizer->expects(self::once())->method('optimize')->with($schemaTemplate)->willReturn($schemaTemplate);
$merger = new SchemaMerger($schemaRegistry, '/tmp/foobar');
$merger = new SchemaMerger('/tmp/foobar');
$merger->setSchemaRegistry($schemaRegistry);
$merger->addOptimizer($optimizer);
$merger->merge(true);

@@ -517,7 +526,8 @@ public function testMergeWithFilenameOption()
->expects(self::once())
->method('getRootSchemas')
->willReturn([$schemaTemplate]);
$merger = new SchemaMerger($schemaRegistry, '/tmp/foobar');
$merger = new SchemaMerger('/tmp/foobar');
$merger->setSchemaRegistry($schemaRegistry);
$merger->merge(true, true);

self::assertFileExists('/tmp/foobar/bla.avsc');
@@ -534,7 +544,8 @@ public function testExportSchema()
->willReturn('{"name": "test"}');
$schemaRegistry = $this->getMockForAbstractClass(SchemaRegistryInterface::class);

$merger = new SchemaMerger($schemaRegistry);
$merger = new SchemaMerger();
$merger->setSchemaRegistry($schemaRegistry);
$merger->exportSchema($schemaTemplate);

self::assertFileExists('/tmp/test.avsc');
@@ -559,7 +570,8 @@ public function testExportSchemaPrimitiveWithWrongOptions()
->willReturn('test.avsc');
$schemaRegistry = $this->getMockForAbstractClass(SchemaRegistryInterface::class);

$merger = new SchemaMerger($schemaRegistry);
$merger = new SchemaMerger();
$merger->setSchemaRegistry($schemaRegistry);
$merger->exportSchema($schemaTemplate, true);

self::assertFileExists('/tmp/test.avsc');
Loading