merged from #902, rebuilt client

This commit is contained in:
Tony Tam
2015-06-25 10:17:28 -07:00
parent f1239aba6c
commit 07d5365c5c
24 changed files with 2145 additions and 1042 deletions

View File

@@ -22,19 +22,19 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
// new pet
$new_pet_id = 10005;
$new_pet = new Swagger\Client\Model\Pet;
$new_pet->id = $new_pet_id;
$new_pet->name = "PHP Unit Test";
$new_pet->setId($new_pet_id);
$new_pet->setName("PHP Unit Test");
// new tag
$tag= new Swagger\Client\Model\Tag;
$tag->id = $new_pet_id; // use the same id as pet
$tag->name = "test php tag";
$tag->setId($new_pet_id); // use the same id as pet
$tag->setName("test php tag");
// new category
$category = new Swagger\Client\Model\Category;
$category->id = $new_pet_id; // use the same id as pet
$category->name = "test php category";
$category->setId($new_pet_id); // use the same id as pet
$category->setName("test php category");
$new_pet->tags = array($tag);
$new_pet->category = $category;
$new_pet->setTags(array($tag));
$new_pet->setCategory($category);
$pet_api = new Swagger\Client\Api\PetAPI();
// add a new pet (model)
@@ -56,65 +56,53 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
$this->assertSame('application/yaml,application/xml', $api_client->selectHeaderContentType(array('application/yaml','application/xml')));
// test addDefaultHeader and getDefaultHeader
$api_client->addDefaultHeader('test1', 'value1');
$api_client->addDefaultHeader('test2', 200);
$defaultHeader = $api_client->getDefaultHeader();
$api_client->getConfig()->addDefaultHeader('test1', 'value1');
$api_client->getConfig()->addDefaultHeader('test2', 200);
$defaultHeader = $api_client->getConfig()->getDefaultHeaders();
$this->assertSame('value1', $defaultHeader['test1']);
$this->assertSame(200, $defaultHeader['test2']);
// test deleteDefaultHeader
$api_client->deleteDefaultHeader('test2');
$defaultHeader = $api_client->getDefaultHeader();
$api_client->getConfig()->deleteDefaultHeader('test2');
$defaultHeader = $api_client->getConfig()->getDefaultHeaders();
$this->assertFalse(isset($defaultHeader['test2']));
$pet_api = new Swagger\Client\Api\PetAPI();
$pet_api2 = new Swagger\Client\Api\PetAPI();
$apiClient3 = new Swagger\Client\ApiClient();
$apiClient3->setUserAgent = 'api client 3';
$apiClient3->getConfig()->setUserAgent('api client 3');
$apiClient4 = new Swagger\Client\ApiClient();
$apiClient4->setUserAgent = 'api client 4';
$apiClient4->getConfig()->setUserAgent('api client 4');
$pet_api3 = new Swagger\Client\Api\PetAPI($apiClient3);
// same default api client
$this->assertSame($pet_api->getApiClient(), $pet_api2->getApiClient());
// confirm using the default api client in the Configuration
$this->assertSame($pet_api->getApiClient(), Swagger\Client\Configuration::$apiClient);
// 2 different api clients are not the same
$this->assertNotEquals($apiClient3, $apiClient4);
// customized pet api not using the default (configuration) api client
$this->assertNotEquals($pet_api3->getApiClient(), Swagger\Client\Configuration::$apiClient);
// customied pet api not using the old pet api's api client
$this->assertNotEquals($pet_api2->getApiClient(), $pet_api3->getApiClient());
// both pet api and pet api2 share the same api client and confirm using timeout value
$pet_api->getApiClient()->setTimeout(999);
$this->assertSame(999, $pet_api2->getApiClient()->getTimeout());
}
// test getPetById with a Pet object (id 10005)
public function testGetPetById()
{
// initialize the API client without host
$api_client = new Swagger\Client\ApiClient();
Swagger\Client\Configuration::$apiKey['api_key'] = '111222333444555';
$pet_id = 10005; // ID of pet that needs to be fetched
$pet_api = new Swagger\Client\Api\PetAPI($api_client);
$pet_api = new Swagger\Client\Api\PetAPI();
$pet_api->getApiClient()->getConfig()->setApiKey('api_key', '111222333444555');
// return Pet (model)
$response = $pet_api->getPetById($pet_id);
$this->assertSame($response->id, $pet_id);
$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');
$this->assertSame($response->getId(), $pet_id);
$this->assertSame($response->getName(), 'PHP Unit Test');
$this->assertSame($response->getCategory()->getId(), $pet_id);
$this->assertSame($response->getCategory()->getName(), 'test php category');
$this->assertSame($response->getTags()[0]->getId(), $pet_id);
$this->assertSame($response->getTags()[0]->getName(), 'test php tag');
}
// test getPetByStatus and verify by the "id" of the response
public function testFindPetByStatus()
{
// initialize the API client
$api_client = new Swagger\Client\ApiClient('http://petstore.swagger.io/v2');
$config = (new Swagger\Client\Configuration())->setHost('http://petstore.swagger.io/v2');
$api_client = new Swagger\Client\ApiClient($config);
$pet_api = new Swagger\Client\Api\PetAPI($api_client);
// return Pet (model)
$response = $pet_api->findPetsByStatus("available");
@@ -133,30 +121,32 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
public function testUpdatePet()
{
// initialize the API client
$api_client = new Swagger\Client\ApiClient('http://petstore.swagger.io/v2');
$config = (new Swagger\Client\Configuration())->setHost('http://petstore.swagger.io/v2');
$api_client = new Swagger\Client\ApiClient($config);
$pet_id = 10001; // ID of pet that needs to be fetched
$pet_api = new Swagger\Client\Api\PetAPI($api_client);
// create updated pet object
$updated_pet = new Swagger\Client\Model\Pet;
$updated_pet->id = $pet_id;
$updated_pet->name = 'updatePet'; // new name
$updated_pet->status = 'pending'; // new status
$updated_pet->setId($pet_id);
$updated_pet->setName('updatePet'); // new name
$updated_pet->setStatus('pending'); // new status
// update Pet (model/json)
$update_response = $pet_api->updatePet($updated_pet);
// return nothing (void)
$this->assertSame($update_response, NULL);
// verify updated Pet
$response = $pet_api->getPetById($pet_id);
$this->assertSame($response->id, $pet_id);
$this->assertSame($response->status, 'pending');
$this->assertSame($response->name, 'updatePet');
$this->assertSame($response->getId(), $pet_id);
$this->assertSame($response->getStatus(), 'pending');
$this->assertSame($response->getName(), 'updatePet');
}
// test updatePet and verify by the "id" of the response
public function testUpdatePetWithForm()
{
// initialize the API client
$api_client = new Swagger\Client\ApiClient('http://petstore.swagger.io/v2');
$config = (new Swagger\Client\Configuration())->setHost('http://petstore.swagger.io/v2');
$api_client = new Swagger\Client\ApiClient($config);
$pet_id = 10001; // ID of pet that needs to be fetched
$pet_api = new Swagger\Client\Api\PetAPI($api_client);
// update Pet (form)
@@ -164,20 +154,21 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
// return nothing (void)
$this->assertSame($update_response, NULL);
$response = $pet_api->getPetById($pet_id);
$this->assertSame($response->id, $pet_id);
$this->assertSame($response->name, 'update pet with form');
$this->assertSame($response->status, 'sold');
$this->assertSame($response->getId(), $pet_id);
$this->assertSame($response->getName(), 'update pet with form');
$this->assertSame($response->getStatus(), 'sold');
}
// test addPet and verify by the "id" and "name" of the response
public function testAddPet()
{
// initialize the API client
$api_client = new Swagger\Client\ApiClient('http://petstore.swagger.io/v2');
$config = (new Swagger\Client\Configuration())->setHost('http://petstore.swagger.io/v2');
$api_client = new Swagger\Client\ApiClient($config);
$new_pet_id = 10001;
$new_pet = new Swagger\Client\Model\Pet;
$new_pet->id = $new_pet_id;
$new_pet->name = "PHP Unit Test";
$new_pet->setId($new_pet_id);
$new_pet->setName("PHP Unit Test");
$pet_api = new Swagger\Client\Api\PetAPI($api_client);
// add a new pet (model)
$add_response = $pet_api->addPet($new_pet);
@@ -185,15 +176,16 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
$this->assertSame($add_response, NULL);
// verify added Pet
$response = $pet_api->getPetById($new_pet_id);
$this->assertSame($response->id, $new_pet_id);
$this->assertSame($response->name, 'PHP Unit Test');
$this->assertSame($response->getId(), $new_pet_id);
$this->assertSame($response->getName(), 'PHP Unit Test');
}
// test upload file
public function testUploadFile()
{
// initialize the API client
$api_client = new Swagger\Client\ApiClient('http://petstore.swagger.io/v2');
$config = (new Swagger\Client\Configuration())->setHost('http://petstore.swagger.io/v2');
$api_client = new Swagger\Client\ApiClient($config);
$pet_api = new Swagger\Client\Api\PetAPI($api_client);
// upload file
$pet_id = 10001;
@@ -206,7 +198,9 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
public function testGetInventory()
{
// initialize the API client
$api_client = new Swagger\Client\APIClient('http://petstore.swagger.io/v2');
$config = new Swagger\Client\Configuration();
$config->setHost('http://petstore.swagger.io/v2');
$api_client = new Swagger\Client\APIClient($config);
$store_api = new Swagger\Client\Api\StoreAPI($api_client);
// get inventory
$get_response = $store_api->getInventory();