Skip to content

Commit

Permalink
Merge bf31841 into 538c566
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemadisonweb committed Jul 16, 2020
2 parents 538c566 + bf31841 commit 8f39e01
Show file tree
Hide file tree
Showing 23 changed files with 160 additions and 76 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [1.0.1] - 2020-07-16
### Fixed
- Fix for date and date-time fields parsing

## [1.0.0] - 2020-07-09
### Added
- Initial API client generator release
6 changes: 1 addition & 5 deletions src/Builder/MethodBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public function __construct(string $name, PhpVersionResolver $versionResolver)
public function composeDocBlock(
array $parameters = [],
string $returnType = '',
array $exceptions = [],
bool $excludeFromCodeCoverage = false
array $exceptions = []
): self {
if (empty($parameters) && $returnType === '' && empty($exceptions)) {
return $this;
Expand All @@ -41,9 +40,6 @@ public function composeDocBlock(
foreach ($exceptions as $exception) {
$docLines[] = sprintf(' * @throws %s', $exception);
}
if ($excludeFromCodeCoverage) {
$docLines[] = ' * @codeCoverageIgnore';
}
$docLines[] = '*/';

$this->setDocComment(new Doc(implode("\n", $docLines)));
Expand Down
6 changes: 1 addition & 5 deletions src/Entity/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,6 @@ public function getPhpDocType(bool $allowNullable = true): string
return 'mixed';
}

if ($this->isComposite()) {
return $this->getPhpTypeHint();
}

$nullableSuffix = '';
$arraySuffix = '';
if ($this->isComposite() || $this->isDate()) {
Expand All @@ -145,7 +141,7 @@ public function getPhpDocType(bool $allowNullable = true): string
$nullableSuffix = '|null';
}

if ($this->isArray()) {
if ($this->isArray() && !$this->isArrayOfObjects()) {
$arraySuffix = '[]';
$typeHint = $this->getStructure()->getArrayItem()->getPhpTypeHint();
}
Expand Down
3 changes: 3 additions & 0 deletions src/Entity/FieldStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public function setArrayItem(Field $arrayItem): self
return $this;
}

/**
* @return Field[]
*/
public function getObjectProperties(): array
{
if (empty($this->objectProperties)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Generator/ClientFactoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ protected function generateRegisterResponseMappers(Specification $specification)
->method('registerResponseMappers')
->addParam($param)
->addStmts($statements)
->composeDocBlock([$param], '', [], true)
->composeDocBlock([$param], '', [])
->getNode();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Generator/MutatorAccessorClassGeneratorAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected function generateSet(Field $field, string $baseNamespace): ClassMethod
$thrownExceptionMap['RequestValidationException'] = true;
}

$docType = $field->getPhpDocType(false);
$docType = $field->getPhpDocType($field->isNullable());
$param = $this->builder
->param($field->getPhpVariableName())
->setType($field->getPhpTypeHint())
Expand Down
19 changes: 8 additions & 11 deletions src/Generator/ResponseMapperGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,11 @@ protected function generateMap(PhpFileCollection $fileRegistry, Field $root): Cl
->addParam($responseParam)
->addStmts($statements)
->setReturnType($root->getPhpTypeHint())
->composeDocBlock([$responseParam], $root->getPhpDocType(), array_keys($this->mapMethodThrownExceptions))
->composeDocBlock(
[$responseParam],
$root->getPhpDocType(false),
array_keys($this->mapMethodThrownExceptions)
)
->getNode();
}

Expand Down Expand Up @@ -258,6 +262,7 @@ protected function generateMapStatementsForObject(Field $root, Variable $respons

$requiredVars = [];
foreach ($requiredFields as $i => $field) {
/** @var Field $field */
if ($field->isComposite()) {
$requiredVars[] = $this->builder->methodCall(
$this->builder->localPropertyFetch(ResponseMapperNaming::getPropertyName($field)),
Expand All @@ -266,11 +271,7 @@ protected function generateMapStatementsForObject(Field $root, Variable $respons
);
} elseif ($field->isDate()) {
$this->addImport(DateTimeImmutable::class);
$requiredVars[] = $this->builder->staticCall(
'DateTimeImmutable',
'createFromFormat',
[$this->builder->constFetch('DATE_RFC3339'), $requiredResponseItems[$i]]
);
$requiredVars[] = $this->builder->new('DateTimeImmutable', [$requiredResponseItems[$i]]);
} else {
$requiredVars[] = $requiredResponseItems[$i];
}
Expand All @@ -287,11 +288,7 @@ protected function generateMapStatementsForObject(Field $root, Variable $respons
$optionalVar = $this->builder->methodCall($mapper, 'map', [$optionalResponseItems[$i]]);
} elseif ($field->isDate()) {
$this->addImport(DateTimeImmutable::class);
$optionalVar = $this->builder->staticCall(
'DateTimeImmutable',
'createFromFormat',
[$this->builder->constFetch('DATE_RFC3339'), $optionalResponseItems[$i]]
);
$optionalVar = $this->builder->new('DateTimeImmutable', [$optionalResponseItems[$i]]);
} else {
$optionalVar = $optionalResponseItems[$i];
}
Expand Down
6 changes: 3 additions & 3 deletions test/suite/functional/Generator/AbstractGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public function testGenerate(
): void {
$absoluteSpecificationPath = __DIR__ . $specificationFilePath;
$absoluteExpectedResultPath = __DIR__ . $expectedResultFilePath;
$this->assertFileExists($absoluteSpecificationPath);
$this->assertFileExists($absoluteExpectedResultPath);
self::assertFileExists($absoluteSpecificationPath);
self::assertFileExists($absoluteExpectedResultPath);

$data = $this->specificationReader->read($absoluteSpecificationPath);
$specification = $this->specificationParser->parse($data, $absoluteSpecificationPath);
Expand All @@ -58,7 +58,7 @@ public function testGenerate(

$result = $this->printer->prettyPrintFile($this->fileRegistry->get($resultClassName)->getNodes());

$this->assertStringEqualsFile($absoluteExpectedResultPath, $result);
self::assertStringEqualsFile($absoluteExpectedResultPath, $result);
}

abstract public function exampleProvider(): array;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ function create(string $baseUri, array $options = array()) : SwaggerPetstoreClie
}
/**
* @param ResponseMapperRegistryInterface $registry
* @codeCoverageIgnore
*/
function registerResponseMappers(ResponseMapperRegistryInterface $registry)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class PutResourceByIdRequest implements RequestInterface
private $booleanParameter;
/** @var int[]|null */
private $arrayParameter;
/** @var EmbeddedObject */
/** @var EmbeddedObject|null */
private $objectParameter;
/** @var int */
private $mandatoryIntegerParameter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function map(array $response) : Item
$missingFields = implode(', ', array_diff(array('mandatoryInteger', 'mandatoryString', 'mandatoryEnum', 'mandatoryDate', 'mandatoryFloat', 'mandatoryBoolean', 'mandatoryArray', 'mandatoryObject'), array_keys($response)));
throw new UnexpectedResponseBodyException('Required attributes for `Item` missing in the response body: ' . $missingFields);
}
$schema = new Item($response['mandatoryInteger'], $response['mandatoryString'], $response['mandatoryEnum'], DateTimeImmutable::createFromFormat(DATE_RFC3339, $response['mandatoryDate']), $response['mandatoryFloat'], $response['mandatoryBoolean'], $response['mandatoryArray'], $this->embeddedObjectResponseMapper->map($response['mandatoryObject']));
$schema = new Item($response['mandatoryInteger'], $response['mandatoryString'], $response['mandatoryEnum'], new DateTimeImmutable($response['mandatoryDate']), $response['mandatoryFloat'], $response['mandatoryBoolean'], $response['mandatoryArray'], $this->embeddedObjectResponseMapper->map($response['mandatoryObject']));
if (isset($response['optionalInteger'])) {
$schema->setOptionalInteger($response['optionalInteger']);
}
Expand All @@ -40,7 +40,7 @@ public function map(array $response) : Item
$schema->setOptionalEnum($response['optionalEnum']);
}
if (isset($response['optionalDate'])) {
$schema->setOptionalDate(DateTimeImmutable::createFromFormat(DATE_RFC3339, $response['optionalDate']));
$schema->setOptionalDate(new DateTimeImmutable($response['optionalDate']));
}
if (isset($response['optionalFloat'])) {
$schema->setOptionalFloat($response['optionalFloat']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public function map(array $response) : Resource
$missingFields = implode(', ', array_diff(array('mandatoryInteger', 'mandatoryString', 'mandatoryEnum', 'mandatoryDate'), array_keys($response)));
throw new UnexpectedResponseBodyException('Required attributes for `Resource` missing in the response body: ' . $missingFields);
}
return new Resource($response['mandatoryInteger'], $response['mandatoryString'], $response['mandatoryEnum'], DateTimeImmutable::createFromFormat(DATE_RFC3339, $response['mandatoryDate']));
return new Resource($response['mandatoryInteger'], $response['mandatoryString'], $response['mandatoryEnum'], new DateTimeImmutable($response['mandatoryDate']));
}
}
13 changes: 8 additions & 5 deletions test/suite/functional/Generator/Schema/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Item implements JsonSerializable
private $mandatoryArray;
/** @var EmbeddedObject */
private $mandatoryObject;
/** @var */
/** @var NullableObject|null */
private $nullableObject;
/** @var DateTimeInterface|null */
private $nullableDate;
Expand All @@ -48,7 +48,7 @@ class Item implements JsonSerializable
private $optionalBoolean;
/** @var string[]|null */
private $optionalArray;
/** @var EmbeddedObject */
/** @var EmbeddedObject|null */
private $optionalObject;
/**
* @param int $mandatoryInteger
Expand Down Expand Up @@ -76,7 +76,7 @@ public function __construct(int $mandatoryInteger, string $mandatoryString, stri
$this->mandatoryObject = $mandatoryObject;
}
/**
* @param $nullableObject
* @param NullableObject|null $nullableObject
* @return self
*/
public function setNullableObject($nullableObject) : self
Expand All @@ -85,7 +85,7 @@ public function setNullableObject($nullableObject) : self
return $this;
}
/**
* @param DateTimeInterface $nullableDate
* @param DateTimeInterface|null $nullableDate
* @return self
*/
public function setNullableDate($nullableDate) : self
Expand Down Expand Up @@ -225,6 +225,9 @@ public function getMandatoryObject() : EmbeddedObject
{
return $this->mandatoryObject;
}
/**
* @return NullableObject|null
*/
public function getNullableObject()
{
return $this->nullableObject;
Expand Down Expand Up @@ -286,7 +289,7 @@ public function getOptionalArray()
return $this->optionalArray;
}
/**
* @return EmbeddedObject
* @return EmbeddedObject|null
*/
public function getOptionalObject()
{
Expand Down
2 changes: 1 addition & 1 deletion test/suite/functional/Input/FileReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static function (int $code, string $message) {
*/
public function testParseValidFile(string $filePath): void
{
$this->assertNotNull($this->sut->read($filePath));
self::assertNotNull($this->sut->read($filePath));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/suite/functional/Input/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static function (int $code, string $message) {
*/
public function testParseValidSpecification(array $data): void
{
$this->assertNotNull($this->sut->parse($data, '/openapi.yaml'));
self::assertNotNull($this->sut->parse($data, '/openapi.yaml'));
}

/**
Expand Down
6 changes: 3 additions & 3 deletions test/suite/functional/Meta/AbstractTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public function testGenerate(
): void {
$absoluteSpecificationPath = __DIR__ . $specificationFilePath;
$absoluteExpectedResultPath = __DIR__ . $expectedResultFilePath;
$this->assertFileExists($absoluteSpecificationPath);
$this->assertFileExists($absoluteExpectedResultPath);
self::assertFileExists($absoluteSpecificationPath);
self::assertFileExists($absoluteExpectedResultPath);

$data = $this->specificationReader->read($absoluteSpecificationPath);
$specification = $this->specificationParser->parse($data, $absoluteSpecificationPath);
Expand All @@ -54,7 +54,7 @@ public function testGenerate(

$result = $this->fileRegistry->get($resultFileName)->getContent();

$this->assertStringEqualsFile($absoluteExpectedResultPath, $result);
self::assertStringEqualsFile($absoluteExpectedResultPath, $result);
}

abstract public function exampleProvider(): array;
Expand Down
86 changes: 86 additions & 0 deletions test/suite/unit/Entity/FieldTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php declare(strict_types=1);

namespace DoclerLabs\ApiClientGenerator\Test\Unit\Entity;

use DoclerLabs\ApiClientGenerator\Entity\FieldType;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

/**
* @coversDefaultClass FieldType
*/
class FieldTypeTest extends TestCase
{
public function testStatic(): void
{
self::assertTrue(FieldType::isSpecificationTypeString('string'));
self::assertTrue(FieldType::isSpecificationTypeMixed(null));
self::assertTrue(FieldType::isSpecificationTypeArray('array'));
self::assertTrue(FieldType::isSpecificationTypeFloat('number'));
self::assertTrue(FieldType::isSpecificationTypeBoolean('boolean'));
self::assertTrue(FieldType::isSpecificationTypeInteger('integer'));
self::assertTrue(FieldType::isSpecificationTypeObject('object'));
}

public function testIsString(): void
{
$sut = new FieldType(FieldType::SPEC_TYPE_STRING);
self::assertTrue($sut->isString());
self::assertEquals(FieldType::PHP_TYPE_STRING, $sut->toPhpType());
self::assertEquals(FieldType::SPEC_TYPE_STRING, $sut->toSpecificationType());
}

public function testIsMixed(): void
{
$sut = new FieldType(null);
self::assertTrue($sut->isMixed());
self::assertEquals('', $sut->toPhpType());
self::assertEquals(null, $sut->toSpecificationType());
}

public function testIsFloat(): void
{
$sut = new FieldType(FieldType::SPEC_TYPE_FLOAT);
self::assertTrue($sut->isFloat());
self::assertEquals(FieldType::PHP_TYPE_FLOAT, $sut->toPhpType());
self::assertEquals(FieldType::SPEC_TYPE_FLOAT, $sut->toSpecificationType());
}

public function testIsInteger(): void
{
$sut = new FieldType(FieldType::SPEC_TYPE_INTEGER);
self::assertTrue($sut->isInteger());
self::assertEquals(FieldType::PHP_TYPE_INTEGER, $sut->toPhpType());
self::assertEquals(FieldType::SPEC_TYPE_INTEGER, $sut->toSpecificationType());
}

public function testIsObject(): void
{
$sut = new FieldType(FieldType::SPEC_TYPE_OBJECT);
self::assertTrue($sut->isObject());
self::assertEquals(FieldType::PHP_TYPE_OBJECT, $sut->toPhpType());
self::assertEquals(FieldType::SPEC_TYPE_OBJECT, $sut->toSpecificationType());
}

public function testIsBoolean(): void
{
$sut = new FieldType(FieldType::SPEC_TYPE_BOOLEAN);
self::assertTrue($sut->isBoolean());
self::assertEquals(FieldType::PHP_TYPE_BOOLEAN, $sut->toPhpType());
self::assertEquals(FieldType::SPEC_TYPE_BOOLEAN, $sut->toSpecificationType());
}

public function testIsArray(): void
{
$sut = new FieldType(FieldType::SPEC_TYPE_ARRAY);
self::assertTrue($sut->isArray());
self::assertEquals(FieldType::PHP_TYPE_ARRAY, $sut->toPhpType());
self::assertEquals(FieldType::SPEC_TYPE_ARRAY, $sut->toSpecificationType());
}

public function testInvalidType(): void
{
$this->expectException(InvalidArgumentException::class);
new FieldType('invalid');
}
}
16 changes: 8 additions & 8 deletions test/suite/unit/Input/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ public function testValidConfiguration(
$readmeMdTemplateDir,
);

$this->assertEquals($openapiFilePath, $sut->getFilePath());
$this->assertEquals($namespace, $sut->getNamespace());
$this->assertEquals($outputDirectory, $sut->getOutputDirectory());
$this->assertEquals($codeStyleConfig, $sut->getCodeStyleConfig());
$this->assertEquals($packageName, $sut->getPackageName());
$this->assertEquals($phpVersion, $sut->getPhpVersion());
$this->assertEquals($composerJsonTemplateDir, $sut->getComposerJsonTemplateDir());
$this->assertEquals($readmeMdTemplateDir, $sut->getReadmeMdTemplateDir());
self::assertEquals($openapiFilePath, $sut->getFilePath());
self::assertEquals($namespace, $sut->getNamespace());
self::assertEquals($outputDirectory, $sut->getOutputDirectory());
self::assertEquals($codeStyleConfig, $sut->getCodeStyleConfig());
self::assertEquals($packageName, $sut->getPackageName());
self::assertEquals($phpVersion, $sut->getPhpVersion());
self::assertEquals($composerJsonTemplateDir, $sut->getComposerJsonTemplateDir());
self::assertEquals($readmeMdTemplateDir, $sut->getReadmeMdTemplateDir());
}

public function validConfigurationOptions(): array
Expand Down
Loading

0 comments on commit 8f39e01

Please sign in to comment.