Skip to content

Commit

Permalink
Make as many unit tests as possible pass
Browse files Browse the repository at this point in the history
  • Loading branch information
WyriHaximus committed Oct 21, 2023
1 parent a39ed4d commit a5afe62
Show file tree
Hide file tree
Showing 119 changed files with 4,956 additions and 1,497 deletions.
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"jawira/case-converter": "^3.5",
"kwn/number-to-words": "^2.6",
"league/openapi-psr7-validator": "^0.21",
"league/uri": "^6.8 || ^7.3",
"nikic/php-parser": "^4.15",
"nunomaduro/termwind": "^1.15",
"ondram/ci-detector": "^4.1",
Expand Down Expand Up @@ -55,10 +56,10 @@
],
"config": {
"allow-plugins": {
"wyrihaximus/composer-update-bin-autoload-path": true,
"infection/extension-installer": true,
"dealerdirect/phpcodesniffer-composer-installer": true,
"ergebnis/composer-normalize": true
"ergebnis/composer-normalize": true,
"infection/extension-installer": true,
"wyrihaximus/composer-update-bin-autoload-path": true
},
"platform": {
"php": "8.2.13"
Expand Down
646 changes: 360 additions & 286 deletions composer.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions example/templates/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"devizzent/cebe-php-openapi": "^1",
"eventsauce/object-hydrator": "^1.1",
"league/openapi-psr7-validator": "^0.21",
"league/uri": "^7.3 || ^6.8",
"psr/http-message": "^1.0",
"react/http": "^1.8",
"react/async": "^4.0",
Expand Down
32 changes: 31 additions & 1 deletion src/Gatherer/ExampleData.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@
use ReverseRegex\Lexer;
use ReverseRegex\Parser;

use function explode;
use function gettype;
use function is_array;
use function is_string;
use function Safe\date;
use function Safe\json_encode;
use function strlen;
use function strpos;

final class ExampleData
{
public static function gather(mixed $exampleData, PropertyType $type, string $propertyName): Representation\ExampleData
{
if ($type->type === 'array') {
if ($type->type === 'array' || $type->type === 'union') {
if ($type->payload instanceof Schema) {
$exampleData = ArrayMerger::doMerge(
$type->payload->example,
Expand All @@ -34,6 +36,11 @@ public static function gather(mixed $exampleData, PropertyType $type, string $pr
);
} elseif ($type->payload instanceof PropertyType) {
return self::gather($exampleData, $type->payload, $propertyName);

// } elseif (is_array($type->payload)) {
// $exampleData = [
// self::gather($exampleData, $type->payload[0], $propertyName),
// ];
}

return new Representation\ExampleData($exampleData, $exampleData instanceof Node\Expr ? $exampleData : self::turnArrayIntoNode((array) $exampleData));
Expand Down Expand Up @@ -90,6 +97,12 @@ public static function determiteType(mixed $exampleData): Representation\Example
/** @phpstan-ignore-next-line */
public static function scalarData(int $seed, string $type, string|null $format, string|null $pattern = null): Representation\ExampleData
{
if (strpos($type, '|') !== false) {
[$firstType] = explode('|', $type);

return self::scalarData($seed, $firstType, $format, $pattern);
}

if ($type === 'int' || $type === '?int') {
return new Representation\ExampleData($seed, new Node\Scalar\LNumber($seed));
}
Expand Down Expand Up @@ -149,6 +162,23 @@ public static function scalarData(int $seed, string $type, string|null $format,
return new Representation\ExampleData('generated', new Node\Scalar\String_('generated'));
}

if ($type === 'array' || $type === '?array') {
$string = self::scalarData($seed, 'string', $format, $pattern);

return new Representation\ExampleData(
[
$string->raw,
],
new Node\Expr\Array_(
[
new Node\Expr\ArrayItem(
$string->node,
),
],
),
);
}

return new Representation\ExampleData(
null,
new Node\Expr\ConstFetch(
Expand Down
15 changes: 13 additions & 2 deletions src/Gatherer/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use ApiClients\Tools\OpenApiClientGenerator\ClassString;
use ApiClients\Tools\OpenApiClientGenerator\Configuration\Namespace_;
use ApiClients\Tools\OpenApiClientGenerator\Registry\Contract as ContractRegistry;
use ApiClients\Tools\OpenApiClientGenerator\Registry\Schema as SchemaRegistry;
use ApiClients\Tools\OpenApiClientGenerator\Registry\ThrowableSchema;
use ApiClients\Tools\OpenApiClientGenerator\Representation\Header;
Expand Down Expand Up @@ -45,11 +46,20 @@ public static function gather(
openAPIOperation $operation,
ThrowableSchema $throwableSchemaRegistry,
SchemaRegistry $schemaRegistry,
ContractRegistry $contractRegistry,
): \ApiClients\Tools\OpenApiClientGenerator\Representation\Operation {
$returnType = [];
$parameters = [];
$empties = [];
foreach ($operation->parameters as $parameter) {
$types = is_array($parameter->schema->type) ? $parameter->schema->type : [$parameter->schema->type];
if (count($parameter->schema->oneOf ?? []) > 0) {
$types = [];
foreach ($parameter->schema->oneOf as $oneOfSchema) {
$types[] = $oneOfSchema->type;
}
}

$parameterType = str_replace([
'integer',
'any',
Expand All @@ -58,7 +68,7 @@ public static function gather(
'int',
'string|object',
'bool',
], implode('|', is_array($parameter->schema->type) ? $parameter->schema->type : [$parameter->schema->type]));
], implode('|', $types));

$parameters[] = new Parameter(
(new Convert($parameter->name))->toCamel(),
Expand All @@ -82,7 +92,7 @@ public static function gather(
);
$requestBody[] = new OperationRequestBody(
$contentType,
Schema::gather($baseNamespace, $requestBodyClassname, $requestBodyDetails->schema, $schemaRegistry),
Schema::gather($baseNamespace, $requestBodyClassname, $requestBodyDetails->schema, $schemaRegistry, $contractRegistry),
);
}
}
Expand Down Expand Up @@ -115,6 +125,7 @@ public static function gather(
$contentTypeMediaType->schema,
true,
$schemaRegistry,
$contractRegistry,
),
);
if ($isError) {
Expand Down
5 changes: 5 additions & 0 deletions src/Gatherer/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use ApiClients\Tools\OpenApiClientGenerator\ClassString;
use ApiClients\Tools\OpenApiClientGenerator\Configuration\Namespace_;
use ApiClients\Tools\OpenApiClientGenerator\Configuration\Voter;
use ApiClients\Tools\OpenApiClientGenerator\Registry\Contract as ContractRegistry;
use ApiClients\Tools\OpenApiClientGenerator\Registry\Schema as SchemaRegistry;
use ApiClients\Tools\OpenApiClientGenerator\Registry\ThrowableSchema;
use ApiClients\Tools\OpenApiClientGenerator\Utils;
Expand All @@ -23,6 +24,7 @@ public static function gather(
string $path,
PathItem $pathItem,
SchemaRegistry $schemaRegistry,
ContractRegistry $contractRegistry,
ThrowableSchema $throwableSchemaRegistry,
Voter|null $voters,
): \ApiClients\Tools\OpenApiClientGenerator\Representation\Path {
Expand All @@ -45,6 +47,7 @@ public static function gather(
$operation,
$throwableSchemaRegistry,
$schemaRegistry,
$contractRegistry,
);

if ($voters !== null && is_array($voters->listOperation)) {
Expand Down Expand Up @@ -75,6 +78,7 @@ public static function gather(
$operation,
$throwableSchemaRegistry,
$schemaRegistry,
$contractRegistry,
);
}
}
Expand Down Expand Up @@ -105,6 +109,7 @@ public static function gather(
$operation,
$throwableSchemaRegistry,
$schemaRegistry,
$contractRegistry,
);
}

Expand Down
10 changes: 9 additions & 1 deletion src/Gatherer/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace ApiClients\Tools\OpenApiClientGenerator\Gatherer;

use ApiClients\Tools\OpenApiClientGenerator\Configuration\Namespace_;
use ApiClients\Tools\OpenApiClientGenerator\Registry\Contract as ContractRegistry;
use ApiClients\Tools\OpenApiClientGenerator\Registry\Schema as SchemaRegistry;
use ApiClients\Tools\OpenApiClientGenerator\Representation;
use cebe\openapi\spec\Schema as baseSchema;
Expand All @@ -13,6 +14,7 @@
use PhpParser\Node;

use function array_filter;
use function array_key_exists;
use function array_values;
use function count;
use function is_array;
Expand All @@ -30,6 +32,7 @@ public static function gather(
bool $required,
baseSchema $property,
SchemaRegistry $schemaRegistry,
ContractRegistry $contractRegistry,
): Representation\Property {
$enum = [];
$exampleData = null;
Expand Down Expand Up @@ -80,14 +83,19 @@ static function ($matches) {
$property,
$required,
$schemaRegistry,
$contractRegistry,
);

if ($property->type === 'array' && is_array($type->payload)) {
$arrayItemsRaw = [];
$arrayItemsNode = [];

foreach ($type->payload as $index => $arrayItem) {
$arrayItemExampleData = ExampleData::gather($exampleData, $arrayItem, $propertyName . str_pad('', $index + 1, '_'));
$arrayItemExampleData = ExampleData::gather(
$exampleData,
$arrayItem->type === 'union' ? $arrayItem->payload[(array_key_exists($index, $arrayItem->payload) ? $index : 0)] : $arrayItem,
$propertyName . str_pad('', $index + 1, '_'),
);
$arrayItemsRaw[] = $arrayItemExampleData->raw;
$arrayItemsNode[] = new Node\Expr\ArrayItem($arrayItemExampleData->node);
}
Expand Down
10 changes: 9 additions & 1 deletion src/Gatherer/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use ApiClients\Tools\OpenApiClientGenerator\ClassString;
use ApiClients\Tools\OpenApiClientGenerator\Configuration\Namespace_;
use ApiClients\Tools\OpenApiClientGenerator\Registry\Contract as ContractRegistry;
use ApiClients\Tools\OpenApiClientGenerator\Registry\Schema as SchemaRegistry;
use ApiClients\Tools\OpenApiClientGenerator\Utils;
use cebe\openapi\spec\Schema as baseSchema;
Expand All @@ -22,6 +23,7 @@ public static function gather(
string $className,
baseSchema $schema,
SchemaRegistry $schemaRegistry,
ContractRegistry $contractRegistry,
): \ApiClients\Tools\OpenApiClientGenerator\Representation\Schema {
$className = Utils::className($className);
$isArray = $schema->type === 'array';
Expand All @@ -38,12 +40,13 @@ public static function gather(
$className,
(string) $propertyName,
in_array(
$propertyName,
(string) $propertyName,
$schema->required ?? [],
false,
),
$property,
$schemaRegistry,
$contractRegistry,
);
$properties[] = $gatheredProperty;

Expand All @@ -60,6 +63,11 @@ public static function gather(
}

$example[$gatheredProperty->sourceName] = $gatheredProperty->example->raw;

foreach ($property->enum ?? [] as $value) {
$example[$gatheredProperty->sourceName] = $value;
break;
}
}

return new \ApiClients\Tools\OpenApiClientGenerator\Representation\Schema(
Expand Down
Loading

0 comments on commit a5afe62

Please sign in to comment.