Skip to content

Commit b000072

Browse files
authored
feat(php): Add client tests for PHP (#625)
1 parent 0e207a4 commit b000072

File tree

19 files changed

+250
-49
lines changed

19 files changed

+250
-49
lines changed

clients/algoliasearch-client-php/lib/Configuration/AbtestingConfig.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44

55
class AbtestingConfig extends ConfigWithRegion
66
{
7+
protected $clientName = 'Abtesting';
78
}

clients/algoliasearch-client-php/lib/Configuration/AnalyticsConfig.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44

55
class AnalyticsConfig extends ConfigWithRegion
66
{
7+
protected $clientName = 'Analytics';
78
}

clients/algoliasearch-client-php/lib/Configuration/ConfigWithRegion.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,21 @@ public static function create(
1212
$region = null,
1313
$allowedRegions = null
1414
) {
15-
if ($region !== null && !in_array($region, $allowedRegions, true)) {
16-
throw new AlgoliaException('Specified region is not allowed.');
15+
if (
16+
$region !== null &&
17+
$allowedRegions !== null &&
18+
!in_array($region, $allowedRegions, true)
19+
) {
20+
throw new AlgoliaException(
21+
'`region` must be one of the following: ' .
22+
implode(', ', $allowedRegions)
23+
);
1724
}
1825

1926
$config = [
2027
'appId' => $appId,
2128
'apiKey' => $apiKey,
22-
'region' => null !== $region ? $region : 'us',
29+
'region' => $region,
2330
];
2431

2532
return new static($config);

clients/algoliasearch-client-php/lib/Configuration/InsightsConfig.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44

55
class InsightsConfig extends ConfigWithRegion
66
{
7+
protected $clientName = 'Insights';
78
}

clients/algoliasearch-client-php/lib/Configuration/PersonalizationConfig.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44

55
class PersonalizationConfig extends ConfigWithRegion
66
{
7+
protected $clientName = 'Personalization';
78
}

clients/algoliasearch-client-php/lib/Configuration/QuerySuggestionsConfig.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44

55
class QuerySuggestionsConfig extends ConfigWithRegion
66
{
7+
protected $clientName = 'QuerySuggestions';
78
}

clients/algoliasearch-client-php/lib/Configuration/RecommendConfig.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44

55
class RecommendConfig extends Configuration
66
{
7+
protected $clientName = 'Recommend';
78
}

clients/algoliasearch-client-php/lib/Configuration/SearchConfig.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
class SearchConfig extends Configuration
66
{
7+
protected $clientName = 'Search';
78
private $defaultWaitTaskTimeBeforeRetry = 5000; // 5 sec in milliseconds
89
private $defaultMaxRetries = 50;
910

clients/algoliasearch-client-php/lib/RequestOptions/RequestOptionsFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private function normalize($options)
5252
'x-algolia-api-key' => $this->config->getAlgoliaApiKey(),
5353
'User-Agent' => $this->config->getAlgoliaAgent() !== null
5454
? $this->config->getAlgoliaAgent()
55-
: AlgoliaAgent::get(),
55+
: AlgoliaAgent::get($this->config->getClientName()),
5656
'Content-Type' => 'application/json',
5757
],
5858
'queryParameters' => [],

clients/algoliasearch-client-php/lib/Support/AlgoliaAgent.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,29 @@ final class AlgoliaAgent
1010

1111
private static $customSegments = [];
1212

13-
public static function get()
13+
public static function get($clientName)
1414
{
15-
if (null === self::$value) {
16-
self::$value = self::getComputedValue();
15+
if (!isset(self::$value[$clientName])) {
16+
self::$value[$clientName] = self::getComputedValue($clientName);
1717
}
1818

19-
return self::$value;
19+
return self::$value[$clientName];
2020
}
2121

22-
public static function addCustomAlgoliaAgent($segment, $version)
23-
{
24-
self::$value = null;
22+
public static function addCustomAlgoliaAgent(
23+
$clientName,
24+
$segment,
25+
$version
26+
) {
27+
self::$value[$clientName] = null;
2528
self::$customSegments[trim($segment, ' ')] = trim($version, ' ');
2629
}
2730

28-
private static function getComputedValue()
31+
private static function getComputedValue($clientName)
2932
{
3033
$ua = [];
3134
$segments = array_merge(
32-
self::getDefaultSegments(),
35+
self::getDefaultSegments($clientName),
3336
self::$customSegments
3437
);
3538

@@ -40,11 +43,12 @@ private static function getComputedValue()
4043
return implode('; ', $ua);
4144
}
4245

43-
private static function getDefaultSegments()
46+
private static function getDefaultSegments($clientName)
4447
{
4548
$segments = [];
4649

4750
$segments['Algolia for PHP'] = Algolia::VERSION;
51+
$segments[$clientName] = Algolia::VERSION;
4852
$segments['PHP'] = rtrim(
4953
str_replace(PHP_EXTRA_VERSION, '', PHP_VERSION),
5054
'-'

templates/php/Configuration.mustache

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace {{invokerPackage}}\Configuration;
44

5+
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
6+
57
/**
68
* Configuration Class Doc Comment
79
* PHP version 7.3
@@ -12,11 +14,11 @@ namespace {{invokerPackage}}\Configuration;
1214
abstract class Configuration
1315
{
1416
/**
15-
* Associate array to store API key(s)
17+
* Associate array to store auth(s)
1618
*
1719
* @var string[]
1820
*/
19-
protected $apiKeys = [];
21+
protected $auths = [];
2022
2123
/**
2224
* Access token for OAuth/Bearer authentication
@@ -54,18 +56,24 @@ abstract class Configuration
5456
5557
protected $defaultConnectTimeout = 2;
5658
59+
protected $clientName;
60+
5761
public function __construct(array $config = [])
5862
{
59-
if(isset($config['apiKey'])) {
60-
$this->setAlgoliaApiKey($config['apiKey']);
61-
$this->setApiKey('x-algolia-api-key', $config['apiKey']);
63+
if (!isset($config['appId']) || $config['appId'] === '') {
64+
throw new AlgoliaException('`appId` is missing.');
6265
}
6366

64-
if(isset($config['appId'])) {
65-
$this->setAppId($config['appId']);
66-
$this->setApiKey('x-algolia-application-id', $config['appId']);
67+
if (!isset($config['apiKey']) || $config['apiKey'] === '') {
68+
throw new AlgoliaException('`apiKey` is missing.');
6769
}
6870

71+
$this->setAlgoliaApiKey($config['apiKey']);
72+
$this->setAuth('x-algolia-api-key', $config['apiKey']);
73+
74+
$this->setAppId($config['appId']);
75+
$this->setAuth('x-algolia-application-id', $config['appId']);
76+
6977
$config += $this->getDefaultConfiguration();
7078
$this->config = $config;
7179
}
@@ -81,29 +89,29 @@ abstract class Configuration
8189
}
8290

8391
/**
84-
* Sets API key
92+
* Sets Auth
8593
*
86-
* @param string $apiKeyIdentifier API key identifier (authentication scheme)
87-
* @param string $key API key or token
94+
* @param string $authIdentifier API key identifier (authentication scheme)
95+
* @param string $key API key or token
8896
*
8997
* @return $this
9098
*/
91-
public function setApiKey($apiKeyIdentifier, $key)
99+
public function setAuth($authIdentifier, $key)
92100
{
93-
$this->apiKeys[$apiKeyIdentifier] = $key;
101+
$this->auths[$authIdentifier] = $key;
94102
return $this;
95103
}
96104

97105
/**
98-
* Gets API key
106+
* Gets Auth
99107
*
100-
* @param string $apiKeyIdentifier API key identifier (authentication scheme)
108+
* @param string $authIdentifier Auth identifier (authentication scheme)
101109
*
102110
* @return null|string API key or token
103111
*/
104-
public function getApiKey($apiKeyIdentifier)
112+
public function getAuth($authIdentifier)
105113
{
106-
return isset($this->apiKeys[$apiKeyIdentifier]) ? $this->apiKeys[$apiKeyIdentifier] : null;
114+
return isset($this->auths[$authIdentifier]) ? $this->auths[$authIdentifier] : null;
107115
}
108116

109117
/**
@@ -278,4 +286,14 @@ abstract class Configuration
278286
{
279287
return $this->algoliaAgent;
280288
}
289+
290+
/**
291+
* Gets the name of the client which the config belongs to
292+
*
293+
* @return string client name
294+
*/
295+
public function getClientName()
296+
{
297+
return $this->clientName;
298+
}
281299
}

templates/php/api.mustache

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,19 @@ use {{invokerPackage}}\Support\Helpers;
5050
*/
5151
public static function create($appId = null, $apiKey = null, $region = null)
5252
{
53-
$allowedRegions = [{{#allowedRegions}}'{{.}}'{{^-last}},{{/-last}}{{/allowedRegions}}];
53+
$allowedRegions = self::getAllowedRegions();
5454
$config = {{configClassname}}::create($appId, $apiKey, $region, $allowedRegions);
5555

5656
return static::createWithConfig($config);
5757
}
58+
59+
/**
60+
* Returns the allowed regions for the config
61+
*/
62+
public static function getAllowedRegions()
63+
{
64+
return [{{#allowedRegions}}'{{.}}'{{^-last}},{{/-last}}{{/allowedRegions}}];
65+
}
5866
{{/hasRegionalHost}}
5967

6068
{{^hasRegionalHost}}
@@ -79,6 +87,24 @@ use {{invokerPackage}}\Support\Helpers;
7987
{
8088
$config = clone $config;
8189
90+
$apiWrapper = new ApiWrapper(
91+
Algolia::getHttpClient(),
92+
$config,
93+
self::getClusterHosts($config)
94+
);
95+
96+
return new static($apiWrapper, $config);
97+
}
98+
99+
/**
100+
* Gets the cluster hosts depending on the config
101+
*
102+
* @param {{configClassname}} $config
103+
*
104+
* @return ClusterHosts
105+
*/
106+
public static function getClusterHosts({{configClassname}} $config)
107+
{
82108
{{#useCache}}
83109
$cacheKey = sprintf('%s-clusterHosts-%s', __CLASS__, $config->getAppId());
84110

@@ -98,18 +124,14 @@ use {{invokerPackage}}\Support\Helpers;
98124
// If a list of hosts was passed, we ignore the cache
99125
$clusterHosts = ClusterHosts::create($hosts);
100126
} else {
101-
$url = str_replace('{region}', $config->getRegion(), '{{{host}}}');
127+
$url = $config->getRegion() !== null && $config->getRegion() !== '' ?
128+
str_replace('{region}', $config->getRegion(), '{{{host}}}') :
129+
'{{{hostWithFallback}}}';
102130
$clusterHosts = ClusterHosts::create($url);
103131
}
104132
{{/useCache}}
105133

106-
$apiWrapper = new ApiWrapper(
107-
Algolia::getHttpClient(),
108-
$config,
109-
$clusterHosts
110-
);
111-
112-
return new static($apiWrapper, $config);
134+
return $clusterHosts;
113135
}
114136

115137
/**
@@ -158,9 +180,9 @@ use {{invokerPackage}}\Support\Helpers;
158180
{{#allParams}}
159181
{{#required}}
160182
// verify the required parameter '{{paramName}}' is set
161-
if (${{paramName}} === null || (is_array(${{paramName}}) && count(${{paramName}}) === 0)) {
183+
if (${{paramName}} === null) {
162184
throw new \InvalidArgumentException(
163-
'Missing the required parameter ${{paramName}} when calling {{operationId}}'
185+
'Parameter `{{paramName}}` is required when calling `{{operationId}}`.'
164186
);
165187
}
166188
{{/required}}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
$client = $this->createClient(
2+
{{#parametersWithDataTypeMap.appId}}"{{parametersWithDataTypeMap.appId.value}}"{{/parametersWithDataTypeMap.appId}}{{^parametersWithDataTypeMap.appId}}null{{/parametersWithDataTypeMap.appId}},
3+
{{#parametersWithDataTypeMap.apiKey}}"{{parametersWithDataTypeMap.apiKey.value}}"{{/parametersWithDataTypeMap.apiKey}}{{^parametersWithDataTypeMap.apiKey}}null{{/parametersWithDataTypeMap.apiKey}},
4+
{{#parametersWithDataTypeMap.region}}"{{parametersWithDataTypeMap.region.value}}"{{/parametersWithDataTypeMap.region}}{{^parametersWithDataTypeMap.region}}null{{/parametersWithDataTypeMap.region}}
5+
);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
$client->{{{path}}}{{^parametersWithDataType}}();{{/parametersWithDataType}}({{#parametersWithDataType}}
2+
{{> generateParams}}
3+
{{/parametersWithDataType}});

0 commit comments

Comments
 (0)