Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
add multiple server support
  • Loading branch information
wing328 committed Jan 22, 2019
commit 0e6c58361f55499385c27cde291e66e4ffabdc68
Original file line number Diff line number Diff line change
Expand Up @@ -419,4 +419,82 @@ class Configuration

return $keyWithPrefix;
}

/**
* Returns an array of host setting
*
* @return an array of host setting
*/
public function getHostSettings()
{
return array(
{{#servers}}
array(
"url" => "{{{url}}}",
"description" => "{{{description}}}{{^description}}No description provided{{/description}}",
{{#variables}}
{{#-first}}
"variables" => array(
{{/-first}}
"{{{name}}}" => array(
"description" => "{{{description}}}{{^description}}No description provided{{/description}}",
"default_value" => "{{{defaultValue}}}",
{{#enumValues}}
{{#-first}}
"enum_values" => array(
{{/-first}}
"{{{.}}}"{{^-last}},{{/-last}}
{{#-last}}
)
{{/-last}}
{{/enumValues}}
){{^-last}},{{/-last}}
{{#-last}}
)
{{/-last}}
{{/variables}}
){{^-last}},{{/-last}}
{{/servers}}
);
}

/**
* Returns URL based on index and variables
*
* @param index array index of the host settings
* @param variables hash of variable and the corresponding value
* @return URL based on host settings
*/
public function getHostFromSettings($index, $variables)
{
if (null === $variables) {
$variables = array();
}

$hosts = $this->getHostSettings();

// check array index out of bound
if ($index < 0 || $index > sizeof($hosts)) {
throw new \InvalidArgumentException("Invalid index $index when selecting the server. Must be less than ".sizeof(servers));
}

$host = $hosts[$index];
$url = $host["url"];

// go through variable and assign a value
foreach ($host["variables"] as $name => $variable) {
if (array_key_exists($name, $variables)) { // check to see if it's in the variables provided by the user
if (in_array($variables[$name], $variable["enum_values"])) { // check to see if the value is in the enum
$url = str_replace("{".$name."}", $variables[$name], $url);
} else {
throw new \InvalidArgumentException("The variable `$name` in the server URL has invalid value ".$variables[$name].". Must be ".join(',', $variable["enum_values"]).".");
}
} else {
// use default value
$url = str_replace("{".$name."}", $variable["default_value"], $url);
}
}

return $url;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -426,4 +426,92 @@ public function getApiKeyWithPrefix($apiKeyIdentifier)

return $keyWithPrefix;
}

/**
* Returns an array of host setting
*
* @return an array of host setting
*/
public function getHostSettings()
{
return array(
array(
"url" => "http://{server}.swagger.io:{port}/v2",
"description" => "petstore server",
"variables" => array(
"server" => array(
"description" => "No description provided",
"default_value" => "petstore",
"enum_values" => array(
"petstore",
"qa-petstore",
"dev-petstore"
)
),
"port" => array(
"description" => "No description provided",
"default_value" => "80",
"enum_values" => array(
"80",
"8080"
)
)
)
),
array(
"url" => "https://localhost:8080/{version}",
"description" => "The local server",
"variables" => array(
"version" => array(
"description" => "No description provided",
"default_value" => "v2",
"enum_values" => array(
"v1",
"v2"
)
)
)
)
);
}

/**
* Returns URL based on index and variables
*
* @param index array index of the host settings
* @param variables hash of variable and the corresponding value
* @return URL based on host settings
*/
public function getHostFromSettings($index, $variables)
{
if (null === $variables) {
$variables = array();
}

$hosts = $this->getHostSettings();

// check array index out of bound
if ($index < 0 || $index > sizeof($hosts)) {
throw new \InvalidArgumentException("Invalid index $index when selecting the server. Must be less than ".sizeof(servers));
}

$host = $hosts[$index];
$url = $host["url"];

// go through variable and assign a value
foreach ($host["variables"] as $name => $variable) {
if (array_key_exists($name, $variables)) { // check to see if it's in the variables provided by the user
if (in_array($variables[$name], $variable["enum_values"])) { // check to see if the value is in the enum
$url = str_replace("{".$name."}", $variables[$name], $url);
} else {
throw new \InvalidArgumentException("The variable `$name` in the server URL has invalid value ".$variables[$name].". Must be ".join(',', $variable["enum_values"]).".");
}
} else {
// use default value
$url = str_replace("{".$name."}", $variable["default_value"], $url);
}
}

return $url;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace OpenAPI\Client;

use GuzzleHttp\Client;
use PHPUnit\Framework\TestCase;

class ConfigurationTest extends TestCase
{
/**
* Test server settings
*/
public function testMultipleServers()
{
$config = new Configuration();
$servers = $config->getHostSettings();

$this->assertCount(2, $servers);
$this->assertSame("http://{server}.swagger.io:{port}/v2", $servers[0]["url"]);
$this->assertSame("petstore", $servers[0]["variables"]["server"]["default_value"]);
$this->assertSame("80", $servers[0]["variables"]["port"]["default_value"]);
$this->assertSame(array("80", "8080"), $servers[0]["variables"]["port"]["enum_values"]);
}

/**
* Test server settings
*/
public function testServerUrl()
{
$config = new Configuration();
// default value
$url = $config->getHostFromSettings(0, null);
$this->assertSame("http://petstore.swagger.io:80/v2", $url);

// using a variable
$url = $config->getHostFromSettings(0, array("server" => "dev-petstore"));
$this->assertSame("http://dev-petstore.swagger.io:80/v2", $url);

// using 2 variables
$url = $config->getHostFromSettings(0, array("server" => "dev-petstore", "port" => "8080"));
$this->assertSame("http://dev-petstore.swagger.io:8080/v2", $url);
}

/**
* Test server settings with invalid vaues
* @expectedException InvalidArgumentException
* @expectedExceptionMessage The variable `port` in the server URL has invalid value 8. Must be 80,8080
*/
public function testServerUrlWithInvalidValues()
{
// using 2 variables with invalid values
$config = new Configuration();
$url = $config->getHostFromSettings(0, array("server" => "dev-petstore", "port" => "8"));
$this->assertSame("http://dev-petstore.swagger.io:8080/v2", $url);
}
}