Skip to content

feat(php): Add tests for query params #475

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

Merged
merged 10 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,7 @@ final class RequestOptionsFactory
{
private $config;

private $validQueryParameters = [
'forwardToReplicas',
'replaceExistingSynonyms',
'clearExistingRules',
'getVersion',
];

private $validHeaders = ['Content-type', 'User-Agent', 'createIfNotExists'];
private $validHeaders = ['Content-type', 'User-Agent'];

public function __construct(Configuration $config)
{
Expand Down Expand Up @@ -100,7 +93,7 @@ private function normalize($options)
private function format($options)
{
foreach ($options as $name => $value) {
if (in_array($name, ['attributesToRetrieve', 'type'], true)) {
if (in_array($name, self::getAttributesToFormat(), true)) {
if (is_array($value)) {
$options[$name] = implode(',', $value);
}
Expand All @@ -114,8 +107,6 @@ private function getOptionType($optionName)
{
if ($this->isValidHeaderName($optionName)) {
return 'headers';
} elseif (in_array($optionName, $this->validQueryParameters, true)) {
return 'query';
} elseif (
in_array(
$optionName,
Expand All @@ -126,7 +117,7 @@ private function getOptionType($optionName)
return $optionName;
}

return 'body';
return 'query';
}

private function isValidHeaderName($name)
Expand All @@ -141,4 +132,9 @@ private function isValidHeaderName($name)

return false;
}

public static function getAttributesToFormat()
{
return ['attributesToRetrieve', 'type'];
}
}
5 changes: 3 additions & 2 deletions specs/search/common/parameters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ HitsPerPage:
description: Number of hits per page.
default: 20

UserIDInQuery:
# headers
UserIDInHeader:
name: X-Algolia-User-ID
description: userID to assign.
in: query
in: header
required: true
schema:
type: string
Expand Down
2 changes: 1 addition & 1 deletion specs/search/paths/multiclusters/batchAssignUserIds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ post:

A successful response indicates that the operation has been taken into account, and the userIDs are directly usable.
parameters:
- $ref: '../../common/parameters.yml#/UserIDInQuery'
- $ref: '../../common/parameters.yml#/UserIDInHeader'
requestBody:
required: true
content:
Expand Down
2 changes: 1 addition & 1 deletion specs/search/paths/multiclusters/userIds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ post:

A successful response indicates that the operation has been taken into account, and the userID is directly usable.
parameters:
- $ref: '../../common/parameters.yml#/UserIDInQuery'
- $ref: '../../common/parameters.yml#/UserIDInHeader'
requestBody:
required: true
content:
Expand Down
4 changes: 3 additions & 1 deletion templates/javascript/api-single.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ export function create{{capitalizedApiName}}(options: CreateClientOptions{{#hasR
{{/queryParams}}{{/vendorExtensions.x-is-custom-request}}

{{#headerParams}}
headers['{{baseName}}'] = {{paramName}};
if ({{paramName}} !== undefined) {
headers['{{baseName}}'] = {{paramName}}.toString();
}
{{/headerParams}}

const request: Request = {
Expand Down
26 changes: 14 additions & 12 deletions templates/php/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@

namespace {{apiPackage}};

use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Psr7\MultipartStream;
use GuzzleHttp\RequestOptions;
use GuzzleHttp\Utils;
use {{invokerPackage}}\Algolia;
use {{invokerPackage}}\ApiException;
use {{invokerPackage}}\Configuration\{{configClassname}};
use {{invokerPackage}}\ObjectSerializer;
use {{invokerPackage}}\RequestOptions\RequestOptionsFactory;
use {{invokerPackage}}\RetryStrategy\ApiWrapper;
use {{invokerPackage}}\RetryStrategy\ApiWrapperInterface;
use {{invokerPackage}}\RetryStrategy\ClusterHosts;
Expand Down Expand Up @@ -143,13 +139,14 @@ use {{invokerPackage}}\RetryStrategy\ClusterHosts;
* @see {{{dataType}}}
{{/isModel}}
{{/allParams}}
* @param array $requestOptions Request Options
*
* @return {{#returnType}}{{#responses}}{{#dataType}}{{#-first}}array<string, mixed>|{{{dataType}}}{{/-first}}{{/dataType}}{{/responses}}{{/returnType}}{{^returnType}}void{{/returnType}}
{{#isDeprecated}}
* @deprecated
{{/isDeprecated}}
*/
public function {{operationId}}({{^vendorExtensions.x-group-parameters}}{{#allParams}}${{paramName}}{{^required}} = {{{defaultValue}}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{^-last}}, {{/-last}}{{/allParams}}{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}$associative_array{{/vendorExtensions.x-group-parameters}})
public function {{operationId}}({{^vendorExtensions.x-group-parameters}}{{#allParams}}${{paramName}}{{^required}} = null{{/required}}, {{/allParams}}{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}$associative_array{{/vendorExtensions.x-group-parameters}} $requestOptions = [])
{
{{#vendorExtensions.x-group-parameters}}
// unbox the parameters from the associative array
Expand Down Expand Up @@ -215,7 +212,7 @@ use {{invokerPackage}}\RetryStrategy\ClusterHosts;
{{#isExplode}}
if (${{paramName}} !== null) {
{{#style}}
if('form' === '{{style}}' && is_array(${{paramName}})) {
if(is_array(${{paramName}}) && ! in_array('{{paramName}}', RequestOptionsFactory::getAttributesToFormat())) {
foreach(${{paramName}} as $key => $value) {
$queryParams[$key] = $value;
}
Expand Down Expand Up @@ -259,7 +256,9 @@ use {{invokerPackage}}\RetryStrategy\ClusterHosts;
$httpBody = ${{paramName}};
}
{{/bodyParams}}

{{#headerParams}}
$queryParams['{{baseName}}'] = ${{paramName}};
{{/headerParams}}
{{#servers.0}}
$operationHosts = [{{#servers}}"{{{url}}}"{{^-last}}, {{/-last}}{{/servers}}];
if ($this->hostIndex < 0 || $this->hostIndex >= sizeof($operationHosts)) {
Expand All @@ -268,26 +267,29 @@ use {{invokerPackage}}\RetryStrategy\ClusterHosts;
$operationHost = $operationHosts[$this->hostIndex];

{{/servers.0}}
$requestOptions += $queryParams;

return $this->sendRequest('{{httpMethod}}', $resourcePath, $queryParams, $httpBody);
return $this->sendRequest('{{httpMethod}}', $resourcePath, $queryParams, $httpBody, $requestOptions);
}

{{/operation}}

private function sendRequest($method, $resourcePath, $queryParams, $httpBody)
private function sendRequest($method, $resourcePath, $queryParams, $httpBody, $requestOptions)
{
$query = \GuzzleHttp\Psr7\Query::build($queryParams);

if($method == 'GET') {
$request = $this->api->read(
$method,
$resourcePath . ($query ? "?{$query}" : '')
$resourcePath . ($query ? "?{$query}" : ''),
$requestOptions
);
} else {
$request = $this->api->write(
$method,
$resourcePath . ($query ? "?{$query}" : ''),
$httpBody
$httpBody,
$requestOptions
);
}

Expand Down
3 changes: 0 additions & 3 deletions tests/CTS/methods/requests/search/assignUserId.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
"method": "POST",
"body": {
"cluster": "theCluster"
},
"queryParameters": {
"X-Algolia-User-ID": "userID"
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions tests/CTS/methods/requests/search/batchAssignUserIds.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
"user1",
"user2"
]
},
"queryParameters": {
"X-Algolia-User-ID": "userID"
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion tests/CTS/methods/requests/templates/php/requests.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use Algolia\AlgoliaSearch\Http\HttpClientInterface;
use Algolia\AlgoliaSearch\Http\Psr7\Response;
use Algolia\AlgoliaSearch\RetryStrategy\ApiWrapper;
use Algolia\AlgoliaSearch\RetryStrategy\ClusterHosts;
use GuzzleHttp\Psr7\Query;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface;

Expand Down Expand Up @@ -42,6 +43,13 @@ class {{clientPrefix}}Test extends TestCase implements HttpClientInterface
$recordedRequest->getBody()->getContents()
);
}

if (isset($request['queryParameters'])) {
$this->assertEquals(
Query::build($request['queryParameters']),
$recordedRequest->getUri()->getQuery()
);
}
}
}

Expand Down Expand Up @@ -82,7 +90,7 @@ class {{clientPrefix}}Test extends TestCase implements HttpClientInterface
"body" => json_decode("{{#lambda.escapequotes}}{{{request.body}}}{{/lambda.escapequotes}}"),
{{/request.body}}
{{#request.queryParameters}}
"queryParameters" => json_decode("{{#lambda.escapequotes}}{{{request.queryParameters}}}{{/lambda.escapequotes}}"),
"queryParameters" => json_decode("{{#lambda.escapequotes}}{{{request.queryParameters}}}{{/lambda.escapequotes}}", true),
{{/request.queryParameters}}
],
]);
Expand Down