Skip to content

Commit 691838c

Browse files
committed
add test case for api client instance
1 parent 612abf1 commit 691838c

File tree

7 files changed

+129
-17
lines changed

7 files changed

+129
-17
lines changed

modules/swagger-codegen/src/main/resources/php/APIClient.mustache

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,36 @@ class APIClient {
9292
}
9393

9494
/**
95-
* @param integer $seconds Number of seconds before timing out [set to 0 for no timeout]
95+
* get the user agent of the api client
96+
*
97+
* @return string user agent
98+
*/
99+
public function getUserAgent($user_agent) {
100+
return $this->user_agent;
101+
}
102+
103+
/**
104+
* set the HTTP timeout value
105+
*
106+
* @param integer $seconds Number of seconds before timing out [set to 0 for no timeout]
96107
*/
97108
public function setTimeout($seconds) {
98-
if (!is_numeric($seconds))
99-
throw new \InvalidArgumentException('Timeout variable must be numeric.');
109+
if (!is_numeric($seconds) || $seconds < 0)
110+
throw new \InvalidArgumentException('Timeout value must be numeric and a non-negative number.');
100111
101112
$this->curl_timeout = $seconds;
102113
}
103114

115+
/**
116+
* get the HTTP timeout value
117+
*
118+
* @return string HTTP timeout value
119+
*/
120+
public function getTimeout() {
121+
return $this->curl_timeout;
122+
}
123+
124+
104125
/**
105126
* Get API key (with prefix if set)
106127
* @param string key name

modules/swagger-codegen/src/main/resources/php/api.mustache

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,22 @@ class {{classname}} {
3838
}
3939
}
4040

41+
private $apiClient; // instance of the APIClient
42+
43+
/**
44+
* get the API client
45+
*/
46+
public function getApiClient() {
47+
return $this->apiClient;
48+
}
49+
50+
/**
51+
* set the API client
52+
*/
53+
public function setApiClient($apiClient) {
54+
$this->apiClient = $apiClient;
55+
}
56+
4157
{{#operation}}
4258
/**
4359
* {{{nickname}}}

samples/client/petstore/php/SwaggerClient-php/lib/APIClient.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,36 @@ public function setUserAgent($user_agent) {
9292
}
9393

9494
/**
95-
* @param integer $seconds Number of seconds before timing out [set to 0 for no timeout]
95+
* get the user agent of the api client
96+
*
97+
* @return string user agent
98+
*/
99+
public function getUserAgent($user_agent) {
100+
return $this->user_agent;
101+
}
102+
103+
/**
104+
* set the HTTP timeout value
105+
*
106+
* @param integer $seconds Number of seconds before timing out [set to 0 for no timeout]
96107
*/
97108
public function setTimeout($seconds) {
98-
if (!is_numeric($seconds))
99-
throw new \InvalidArgumentException('Timeout variable must be numeric.');
109+
if (!is_numeric($seconds) || $seconds < 0)
110+
throw new \InvalidArgumentException('Timeout value must be numeric and a non-negative number.');
100111

101112
$this->curl_timeout = $seconds;
102113
}
103114

115+
/**
116+
* get the HTTP timeout value
117+
*
118+
* @return string HTTP timeout value
119+
*/
120+
public function getTimeout() {
121+
return $this->curl_timeout;
122+
}
123+
124+
104125
/**
105126
* Get API key (with prefix if set)
106127
* @param string key name

samples/client/petstore/php/SwaggerClient-php/lib/PetApi.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
class PetApi {
2626

27-
2827
function __construct($apiClient = null) {
2928
if (null === $apiClient) {
3029
if (Configuration::$apiClient === null) {
@@ -38,8 +37,7 @@ function __construct($apiClient = null) {
3837
}
3938
}
4039

41-
42-
private $apiClient;
40+
private $apiClient; // instance of the APIClient
4341

4442
/**
4543
* get the API client
@@ -51,10 +49,11 @@ public function getApiClient() {
5149
/**
5250
* set the API client
5351
*/
54-
public function getApiClient($apiClient) {
52+
public function setApiClient($apiClient) {
5553
$this->apiClient = $apiClient;
5654
}
5755

56+
5857
/**
5958
* updatePet
6059
*

samples/client/petstore/php/SwaggerClient-php/lib/StoreApi.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ function __construct($apiClient = null) {
3737
}
3838
}
3939

40+
private $apiClient; // instance of the APIClient
41+
42+
/**
43+
* get the API client
44+
*/
45+
public function getApiClient() {
46+
return $this->apiClient;
47+
}
48+
49+
/**
50+
* set the API client
51+
*/
52+
public function setApiClient($apiClient) {
53+
$this->apiClient = $apiClient;
54+
}
55+
4056

4157
/**
4258
* getInventory

samples/client/petstore/php/SwaggerClient-php/lib/UserApi.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ function __construct($apiClient = null) {
3737
}
3838
}
3939

40+
private $apiClient; // instance of the APIClient
41+
42+
/**
43+
* get the API client
44+
*/
45+
public function getApiClient() {
46+
return $this->apiClient;
47+
}
48+
49+
/**
50+
* set the API client
51+
*/
52+
public function setApiClient($apiClient) {
53+
$this->apiClient = $apiClient;
54+
}
55+
4056

4157
/**
4258
* createUser

samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
77

88
// add a new pet (id 10005) to ensure the pet object is available for all the tests
99
public static function setUpBeforeClass() {
10-
// initialize the API client
11-
$api_client = new SwaggerClient\APIClient('http://petstore.swagger.io/v2');
10+
// skip initializing the API client as it should be automatic
11+
//$api_client = new SwaggerClient\APIClient('http://petstore.swagger.io/v2');
1212
// new pet
1313
$new_pet_id = 10005;
1414
$new_pet = new SwaggerClient\models\Pet;
@@ -26,34 +26,57 @@ public static function setUpBeforeClass() {
2626
$new_pet->tags = [$tag];
2727
$new_pet->category = $category;
2828

29-
$pet_api = new SwaggerClient\PetAPI($api_client);
29+
$pet_api = new SwaggerClient\PetAPI();
3030
// add a new pet (model)
3131
$add_response = $pet_api->addPet($new_pet);
3232
}
3333

3434
// test static functions defined in APIClient
3535
public function testAPIClient()
3636
{
37-
# test selectHeaderAccept
37+
// test selectHeaderAccept
3838
$this->assertSame('application/json', SwaggerClient\APIClient::selectHeaderAccept(array('application/xml','application/json')));
3939
$this->assertSame(NULL, SwaggerClient\APIClient::selectHeaderAccept(array()));
4040
$this->assertSame('application/yaml,application/xml', SwaggerClient\APIClient::selectHeaderAccept(array('application/yaml','application/xml')));
4141

42-
# test selectHeaderContentType
42+
// test selectHeaderContentType
4343
$this->assertSame('application/json', SwaggerClient\APIClient::selectHeaderContentType(array('application/xml','application/json')));
4444
$this->assertSame('application/json', SwaggerClient\APIClient::selectHeaderContentType(array()));
4545
$this->assertSame('application/yaml,application/xml', SwaggerClient\APIClient::selectHeaderContentType(array('application/yaml','application/xml')));
4646

47-
# test addDefaultHeader and getDefaultHeader
47+
// test addDefaultHeader and getDefaultHeader
4848
SwaggerClient\APIClient::addDefaultHeader('test1', 'value1');
4949
SwaggerClient\APIClient::addDefaultHeader('test2', 200);
5050
$this->assertSame('value1', SwaggerClient\APIClient::getDefaultHeader()['test1']);
5151
$this->assertSame(200, SwaggerClient\APIClient::getDefaultHeader()['test2']);
5252

53-
# test deleteDefaultHeader
53+
// test deleteDefaultHeader
5454
SwaggerClient\APIClient::deleteDefaultHeader('test2');
5555
$this->assertFalse(isset(SwaggerClient\APIClient::getDefaultHeader()['test2']));
5656

57+
$pet_api = new SwaggerClient\PetAPI();
58+
$pet_api2 = new SwaggerClient\PetAPI();
59+
$apiClient3 = new SwaggerClient\APIClient();
60+
$apiClient3->setUserAgent = 'api client 3';
61+
$apiClient4 = new SwaggerClient\APIClient();
62+
$apiClient4->setUserAgent = 'api client 4';
63+
$pet_api3 = new SwaggerClient\PetAPI($apiClient3);
64+
65+
// same default api client
66+
$this->assertSame($pet_api->getApiClient(), $pet_api2->getApiClient());
67+
// confirm using the default api client in the Configuration
68+
$this->assertSame($pet_api->getApiClient(), SwaggerClient\Configuration::$apiClient);
69+
// 2 different api clients are not the same
70+
$this->assertNotEquals($apiClient3, $apiClient4);
71+
// customized pet api not using the default (configuration) api client
72+
$this->assertNotEquals($pet_api3->getApiClient(), SwaggerClient\Configuration::$apiClient);
73+
// customied pet api not using the old pet api's api client
74+
$this->assertNotEquals($pet_api2->getApiClient(), $pet_api3->getApiClient());
75+
76+
// both pet api and pet api2 share the same api client and confirm using timeout value
77+
$pet_api->getApiClient()->setTimeout(999);
78+
$this->assertSame(999, $pet_api2->getApiClient()->getTimeout());
79+
5780
}
5881

5982
// test getPetById with a Pet object (id 10005)

0 commit comments

Comments
 (0)