onmoon / openapi-server-bundle

OpenApi Server Bundle for Symfony
MIT License
9 stars 4 forks source link

Implement high-level API for code generation testing #149

Closed sspat closed 3 years ago

sspat commented 4 years ago

The current TypesGenerationTest.testStringTypeGeneration needs to look like this:

public function testStringTypeGeneration(): void
{
    $generatedFiles = $this->generateCodeFromSpec(__DIR__ . '/specification.yaml');

    $okDto = new GeneratedClassAsserter(
        $generatedFiles,
        '/test/Apis/TestApi/GetTest/Dto/Response/OK/GetTestOKDto.php'
    );

    $okDto->assertHasMethod('getStringProperty');
    $okDto->assertMethodReturns('getStringProperty', 'string');
}

For this to work, the GeneratedClassAsserter needs to be implemented with the following API:

assertHasName(string $name); // class/interface name
assertIsInNamespace(string $namespace); // namespace without class/interface name
assertHasUseStatement(string $fqcn, ?string $alias = null); // alias to assert "smth" in "use xxx as smth"
assertExtends(string $extendedClassFQCN);
assertImplements(string $implementedInterfaceFQCN);
assertHasProperty(string $propertyName, string $type, bool $isNullable); // type can be php scalar type name or fqcn
assertPropertyDocblockContaints(string $propertyName, string $docblockRow);
assertHasMethod(string $methodName);
assertMethodDocblockContaints(string $methodName, string $docblockRow); // example call - assertMethodDocblockContaints('getString', '@returns string') or assertMethodDocblockContaints('getString', 'some comment')
assertMethodReturns(string $methodName, string $type, bool $isNullable); // type can be php scalar type name or fqcn
assertMethodHasArgument(string $methodName, string $argumentName, string $type, bool $isNullable); // type can be php scalar type name or fqcn

All method assertions should also work for making assertions on the constructor. Assertions should internally use the phpunit Assert class with static calls. TypesGenerationTest should be completed demonstrating the above assertions one time each (just for demonstration, this does not need to cover every class generated)

egorzot commented 4 years ago

@sspat assertIsInNamespace - should we compare the total or partial match of the namespace ? assertHasUseStatement - could you give an example of using that API?

sspat commented 4 years ago

@sspat assertIsInNamespace - should we compare the total or partial match of the namespace ?

Without class name, only namespace, complete

assertHasUseStatement - could you give an example of using that API?

use Namespace\Class:
assertHasUseStatement('Namespace\Class');
use Namespace\Class as OtherClass;
assertHasUseStatement('Namespace\Class', 'OtherClass');