forked from loafle/openapi-generator-original
add test case for upload file, improve error handling
This commit is contained in:
@@ -142,21 +142,20 @@ class APIClient {
|
|||||||
$response_info = curl_getinfo($curl);
|
$response_info = curl_getinfo($curl);
|
||||||
|
|
||||||
// Handle the response
|
// Handle the response
|
||||||
if ($response_info['http_code'] == 0) {
|
if ($response === false) { // error, likely in the client side
|
||||||
throw new APIClientException("TIMEOUT: api call to " . $url .
|
throw new APIClientException("API Error ($url): ".curl_error($curl), 0, $response_info, $response);
|
||||||
" took more than 5s to return", 0, $response_info, $response);
|
|
||||||
} else if ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299 ) {
|
} else if ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299 ) {
|
||||||
$data = json_decode($response);
|
$data = json_decode($response);
|
||||||
if (json_last_error() > 0) { // if response is a string
|
if (json_last_error() > 0) { // if response is a string
|
||||||
$data = $response;
|
$data = $response;
|
||||||
}
|
}
|
||||||
} else if ($response_info['http_code'] == 401) {
|
} else if ($response_info['http_code'] == 401) { // server returns 401
|
||||||
throw new APIClientException("Unauthorized API request to " . $url .
|
throw new APIClientException("Unauthorized API request to " . $url .
|
||||||
": " . serialize($response), 0, $response_info, $response);
|
": " . serialize($response), 0, $response_info, $response);
|
||||||
} else if ($response_info['http_code'] == 404) {
|
} else if ($response_info['http_code'] == 404) { // server returns 404
|
||||||
$data = null;
|
$data = null;
|
||||||
} else {
|
} else {
|
||||||
throw new APIClientException("Can't connect to the api: " . $url .
|
throw new APIClientException("Can't connect to the API: " . $url .
|
||||||
" response code: " .
|
" response code: " .
|
||||||
$response_info['http_code'], 0, $response_info, $response);
|
$response_info['http_code'], 0, $response_info, $response);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,15 +9,26 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
|
|||||||
public static function setUpBeforeClass() {
|
public static function setUpBeforeClass() {
|
||||||
// initialize the API client
|
// initialize the API client
|
||||||
$api_client = new SwaggerClient\APIClient('http://petstore.swagger.io/v2');
|
$api_client = new SwaggerClient\APIClient('http://petstore.swagger.io/v2');
|
||||||
|
// new pet
|
||||||
$new_pet_id = 10005;
|
$new_pet_id = 10005;
|
||||||
$new_pet = new SwaggerClient\models\Pet;
|
$new_pet = new SwaggerClient\models\Pet;
|
||||||
$new_pet->id = $new_pet_id;
|
$new_pet->id = $new_pet_id;
|
||||||
$new_pet->name = "PHP Unit Test";
|
$new_pet->name = "PHP Unit Test";
|
||||||
|
// new tag
|
||||||
|
$tag= new SwaggerClient\models\Tag;
|
||||||
|
$tag->id = $new_pet_id; // use the same id as pet
|
||||||
|
$tag->name = "test php tag";
|
||||||
|
// new category
|
||||||
|
$category = new SwaggerClient\models\Category;
|
||||||
|
$category->id = $new_pet_id; // use the same id as pet
|
||||||
|
$category->name = "test php category";
|
||||||
|
|
||||||
|
$new_pet->tags = [$tag];
|
||||||
|
$new_pet->category = $category;
|
||||||
|
|
||||||
$pet_api = new SwaggerClient\PetAPI($api_client);
|
$pet_api = new SwaggerClient\PetAPI($api_client);
|
||||||
// add a new pet (model)
|
// add a new pet (model)
|
||||||
$add_response = $pet_api->addPet($new_pet);
|
$add_response = $pet_api->addPet($new_pet);
|
||||||
// return nothing (void)
|
|
||||||
//$this->assertSame($add_response, NULL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// test getPetById with a Pet object (id 10005)
|
// test getPetById with a Pet object (id 10005)
|
||||||
@@ -31,6 +42,10 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
|
|||||||
$response = $pet_api->getPetById($pet_id);
|
$response = $pet_api->getPetById($pet_id);
|
||||||
$this->assertSame($response->id, $pet_id);
|
$this->assertSame($response->id, $pet_id);
|
||||||
$this->assertSame($response->name, 'PHP Unit Test');
|
$this->assertSame($response->name, 'PHP Unit Test');
|
||||||
|
$this->assertSame($response->category->id, $pet_id);
|
||||||
|
$this->assertSame($response->category->name, 'test php category');
|
||||||
|
$this->assertSame($response->tags[0]->id, $pet_id);
|
||||||
|
$this->assertSame($response->tags[0]->name, 'test php tag');
|
||||||
}
|
}
|
||||||
|
|
||||||
// test getPetByStatus and verify by the "id" of the response
|
// test getPetByStatus and verify by the "id" of the response
|
||||||
@@ -112,6 +127,19 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertSame($response->name, 'PHP Unit Test');
|
$this->assertSame($response->name, 'PHP Unit Test');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// test
|
||||||
|
public function testUploadFile()
|
||||||
|
{
|
||||||
|
// initialize the API client
|
||||||
|
$api_client = new SwaggerClient\APIClient('http://petstore.swagger.io/v2');
|
||||||
|
$pet_api = new SwaggerClient\PetAPI($api_client);
|
||||||
|
// upload file
|
||||||
|
$pet_id = 10001;
|
||||||
|
$add_response = $pet_api->uploadFile($pet_id, "test meta", "./composer.json");
|
||||||
|
// return nothing (void)
|
||||||
|
$this->assertSame($add_response, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|||||||
Reference in New Issue
Block a user