Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Sep 17, 2024
1 parent 06d4611 commit 2b537df
Show file tree
Hide file tree
Showing 16 changed files with 184 additions and 264 deletions.
35 changes: 17 additions & 18 deletions src/Generator/Client/Language/php-operation.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,14 @@
$body = $e->getResponse()->getBody();
$statusCode = $e->getResponse()->getStatusCode();

switch (true) {
{% for code, throw in operation.throws %}
case {% if code == 999 %}$statusCode >= 0 && $statusCode <= 999{% elseif code == 499 %}$statusCode >= 400 && $statusCode <= 499{% elseif code == 599 %}$statusCode >= 500 && $statusCode <= 599{% else %}$statusCode === {{ code }}{% endif %}:
if ({% if code == 999 %}$statusCode >= 0 && $statusCode <= 999{% elseif code == 499 %}$statusCode >= 400 && $statusCode <= 499{% elseif code == 599 %}$statusCode >= 500 && $statusCode <= 599{% else %}$statusCode === {{ code }}{% endif %}) {
{{ _self.response(operation.return, true) }}
throw new {{ throw.className|raw }}($data);
{% endfor %}
default:
throw new UnknownStatusCodeException('The server returned an unknown status code');
throw new {{ throw.className|raw }}($data);
}

{% endfor %}
throw new UnknownStatusCodeException('The server returned an unknown status code');
} catch (\Throwable $e) {
throw new ClientException('An unknown error occurred: ' . $e->getMessage());
}
Expand All @@ -82,27 +81,27 @@

{% macro response(payload, in_throw) %}
{% if payload.contentType == 'application/octet-stream' %}
{% if in_throw %} {% endif %}$data = $body;
{% if in_throw %} {% endif %}$data = $body;
{% elseif payload.contentType == 'application/x-www-form-urlencoded' %}
{% if in_throw %} {% endif %}$data = [];
{% if in_throw %} {% endif %}parse_str((string) $body, $data);
{% if in_throw %} {% endif %}$data = [];
{% if in_throw %} {% endif %}parse_str((string) $body, $data);
{% elseif payload.contentType == 'application/json' %}
{% if in_throw %} {% endif %}$data = \json_decode((string) $body);
{% if in_throw %} {% endif %}$data = \json_decode((string) $body);
{% elseif payload.contentType == 'multipart/form-data' %}
{% if in_throw %} {% endif %}// @TODO currently not possible, please create an issue at https://github.com/apioo/psx-api if needed
{% if in_throw %} {% endif %}$data = [];
{% if in_throw %} {% endif %}// @TODO currently not possible, please create an issue at https://github.com/apioo/psx-api if needed
{% if in_throw %} {% endif %}$data = [];
{% elseif payload.contentType == 'text/plain' %}
{% if in_throw %} {% endif %}$data = (string) $body;
{% if in_throw %} {% endif %}$data = (string) $body;
{% elseif payload.contentType == 'application/xml' %}
{% if in_throw %} {% endif %}$data = new \DOMDocument();
{% if in_throw %} {% endif %}$data->loadXML((string) $body);
{% if in_throw %} {% endif %}$data = new \DOMDocument();
{% if in_throw %} {% endif %}$data->loadXML((string) $body);
{% else %}
{% if payload.innerSchema.isMap %}
{% if in_throw %} {% endif %}$data = $this->parser->parse((string) $body, {{ payload.innerSchema.type }}::class, isMap: true);
{% if in_throw %} {% endif %}$data = $this->parser->parse((string) $body, {{ payload.innerSchema.type }}::class, isMap: true);
{% elseif payload.innerSchema.isArray %}
{% if in_throw %} {% endif %}$data = $this->parser->parse((string) $body, {{ payload.innerSchema.type }}::class, isArray: true);
{% if in_throw %} {% endif %}$data = $this->parser->parse((string) $body, {{ payload.innerSchema.type }}::class, isArray: true);
{% else %}
{% if in_throw %} {% endif %}$data = $this->parser->parse((string) $body, {{ payload.schema.type }}::class);
{% if in_throw %} {% endif %}$data = $this->parser->parse((string) $body, {{ payload.schema.type }}::class);
{% endif %}
{% endif %}
{% endmacro %}
11 changes: 5 additions & 6 deletions src/Generator/Client/Language/typescript-operation.ts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,13 @@
} else if (axios.isAxiosError(error) && error.response) {
const statusCode = error.response.status;

switch (true) {
{% for code, throw in operation.throws %}
case {% if code == 999 %}statusCode >= 0 && statusCode <= 999{% elseif code == 499 %}statusCode >= 400 && statusCode <= 499{% elseif code == 599 %}statusCode >= 500 && statusCode <= 599{% else %}statusCode === {{ code }}{% endif %}:
throw new {{ throw.className|raw }}(error.response.data);
{% endfor %}
default:
throw new UnknownStatusCodeException('The server returned an unknown status code');
if ({% if code == 999 %}statusCode >= 0 && statusCode <= 999{% elseif code == 499 %}statusCode >= 400 && statusCode <= 499{% elseif code == 599 %}statusCode >= 500 && statusCode <= 599{% else %}statusCode === {{ code }}{% endif %}) {
throw new {{ throw.className|raw }}(error.response.data);
}

{% endfor %}
throw new UnknownStatusCodeException('The server returned an unknown status code');
} else {
throw new ClientException('An unknown error occurred: ' + String(error));
}
Expand Down
67 changes: 32 additions & 35 deletions tests/Generator/Client/resource/php/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,7 @@ public function get(string $name, string $type, ?int $startIndex = null, ?float
$body = $e->getResponse()->getBody();
$statusCode = $e->getResponse()->getStatusCode();

switch (true) {
default:
throw new UnknownStatusCodeException('The server returned an unknown status code');
}
throw new UnknownStatusCodeException('The server returned an unknown status code');
} catch (\Throwable $e) {
throw new ClientException('An unknown error occurred: ' . $e->getMessage());
}
Expand Down Expand Up @@ -111,18 +108,19 @@ public function create(string $name, string $type, EntryCreate $payload): EntryM
$body = $e->getResponse()->getBody();
$statusCode = $e->getResponse()->getStatusCode();

switch (true) {
case $statusCode === 400:
$data = $this->parser->parse((string) $body, EntryMessage::class);
if ($statusCode === 400) {
$data = $this->parser->parse((string) $body, EntryMessage::class);

throw new EntryMessageException($data);
}

throw new EntryMessageException($data);
case $statusCode === 500:
$data = $this->parser->parse((string) $body, EntryMessage::class);
if ($statusCode === 500) {
$data = $this->parser->parse((string) $body, EntryMessage::class);

throw new EntryMessageException($data);
default:
throw new UnknownStatusCodeException('The server returned an unknown status code');
throw new EntryMessageException($data);
}

throw new UnknownStatusCodeException('The server returned an unknown status code');
} catch (\Throwable $e) {
throw new ClientException('An unknown error occurred: ' . $e->getMessage());
}
Expand Down Expand Up @@ -166,18 +164,19 @@ public function update(string $name, string $type, \PSX\Record\Record $payload):
$body = $e->getResponse()->getBody();
$statusCode = $e->getResponse()->getStatusCode();

switch (true) {
case $statusCode === 400:
$data = $this->parser->parse((string) $body, EntryMessage::class, isMap: true);
if ($statusCode === 400) {
$data = $this->parser->parse((string) $body, EntryMessage::class, isMap: true);

throw new EntryMessageException($data);
}

throw new EntryMessageException($data);
case $statusCode === 500:
$data = $this->parser->parse((string) $body, EntryMessage::class, isMap: true);
if ($statusCode === 500) {
$data = $this->parser->parse((string) $body, EntryMessage::class, isMap: true);

throw new MapEntryMessageException($data);
default:
throw new UnknownStatusCodeException('The server returned an unknown status code');
throw new MapEntryMessageException($data);
}

throw new UnknownStatusCodeException('The server returned an unknown status code');
} catch (\Throwable $e) {
throw new ClientException('An unknown error occurred: ' . $e->getMessage());
}
Expand Down Expand Up @@ -214,10 +213,7 @@ public function delete(string $name, string $type): void
$body = $e->getResponse()->getBody();
$statusCode = $e->getResponse()->getStatusCode();

switch (true) {
default:
throw new UnknownStatusCodeException('The server returned an unknown status code');
}
throw new UnknownStatusCodeException('The server returned an unknown status code');
} catch (\Throwable $e) {
throw new ClientException('An unknown error occurred: ' . $e->getMessage());
}
Expand Down Expand Up @@ -261,18 +257,19 @@ public function patch(string $name, string $type, array $payload): array
$body = $e->getResponse()->getBody();
$statusCode = $e->getResponse()->getStatusCode();

switch (true) {
case $statusCode === 400:
$data = $this->parser->parse((string) $body, EntryMessage::class, isArray: true);
if ($statusCode === 400) {
$data = $this->parser->parse((string) $body, EntryMessage::class, isArray: true);

throw new EntryMessageException($data);
case $statusCode === 500:
$data = $this->parser->parse((string) $body, EntryMessage::class, isArray: true);
throw new EntryMessageException($data);
}

throw new ArrayEntryMessageException($data);
default:
throw new UnknownStatusCodeException('The server returned an unknown status code');
if ($statusCode === 500) {
$data = $this->parser->parse((string) $body, EntryMessage::class, isArray: true);

throw new ArrayEntryMessageException($data);
}

throw new UnknownStatusCodeException('The server returned an unknown status code');
} catch (\Throwable $e) {
throw new ClientException('An unknown error occurred: ' . $e->getMessage());
}
Expand Down
10 changes: 2 additions & 8 deletions tests/Generator/Client/resource/php_collection/BarTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ public function find(string $foo): EntryCollection
$body = $e->getResponse()->getBody();
$statusCode = $e->getResponse()->getStatusCode();

switch (true) {
default:
throw new UnknownStatusCodeException('The server returned an unknown status code');
}
throw new UnknownStatusCodeException('The server returned an unknown status code');
} catch (\Throwable $e) {
throw new ClientException('An unknown error occurred: ' . $e->getMessage());
}
Expand Down Expand Up @@ -89,10 +86,7 @@ public function put(EntryCreate $payload): EntryMessage
$body = $e->getResponse()->getBody();
$statusCode = $e->getResponse()->getStatusCode();

switch (true) {
default:
throw new UnknownStatusCodeException('The server returned an unknown status code');
}
throw new UnknownStatusCodeException('The server returned an unknown status code');
} catch (\Throwable $e) {
throw new ClientException('An unknown error occurred: ' . $e->getMessage());
}
Expand Down
24 changes: 11 additions & 13 deletions tests/Generator/Client/resource/php_collection/FooBarTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ public function get(): EntryCollection
$body = $e->getResponse()->getBody();
$statusCode = $e->getResponse()->getStatusCode();

switch (true) {
default:
throw new UnknownStatusCodeException('The server returned an unknown status code');
}
throw new UnknownStatusCodeException('The server returned an unknown status code');
} catch (\Throwable $e) {
throw new ClientException('An unknown error occurred: ' . $e->getMessage());
}
Expand Down Expand Up @@ -88,18 +85,19 @@ public function create(EntryCreate $payload): EntryMessage
$body = $e->getResponse()->getBody();
$statusCode = $e->getResponse()->getStatusCode();

switch (true) {
case $statusCode === 400:
$data = $this->parser->parse((string) $body, EntryMessage::class);
if ($statusCode === 400) {
$data = $this->parser->parse((string) $body, EntryMessage::class);

throw new EntryMessageException($data);
}

throw new EntryMessageException($data);
case $statusCode === 500:
$data = $this->parser->parse((string) $body, EntryMessage::class);
if ($statusCode === 500) {
$data = $this->parser->parse((string) $body, EntryMessage::class);

throw new EntryMessageException($data);
default:
throw new UnknownStatusCodeException('The server returned an unknown status code');
throw new EntryMessageException($data);
}

throw new UnknownStatusCodeException('The server returned an unknown status code');
} catch (\Throwable $e) {
throw new ClientException('An unknown error occurred: ' . $e->getMessage());
}
Expand Down
10 changes: 2 additions & 8 deletions tests/Generator/Client/resource/php_collection/FooBazTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ public function get(string $year): EntryCollection
$body = $e->getResponse()->getBody();
$statusCode = $e->getResponse()->getStatusCode();

switch (true) {
default:
throw new UnknownStatusCodeException('The server returned an unknown status code');
}
throw new UnknownStatusCodeException('The server returned an unknown status code');
} catch (\Throwable $e) {
throw new ClientException('An unknown error occurred: ' . $e->getMessage());
}
Expand Down Expand Up @@ -89,10 +86,7 @@ public function create(EntryCreate $payload): EntryMessage
$body = $e->getResponse()->getBody();
$statusCode = $e->getResponse()->getStatusCode();

switch (true) {
default:
throw new UnknownStatusCodeException('The server returned an unknown status code');
}
throw new UnknownStatusCodeException('The server returned an unknown status code');
} catch (\Throwable $e) {
throw new ClientException('An unknown error occurred: ' . $e->getMessage());
}
Expand Down
Loading

0 comments on commit 2b537df

Please sign in to comment.