示例#1
0
 public function testApiClient()
 {
     // test selectHeaderAccept
     $this->assertSame('application/json', SwaggerClient\ApiClient::selectHeaderAccept(array('application/xml', 'application/json')));
     $this->assertSame(NULL, SwaggerClient\ApiClient::selectHeaderAccept(array()));
     $this->assertSame('application/yaml,application/xml', SwaggerClient\ApiClient::selectHeaderAccept(array('application/yaml', 'application/xml')));
     // test selectHeaderContentType
     $this->assertSame('application/json', SwaggerClient\ApiClient::selectHeaderContentType(array('application/xml', 'application/json')));
     $this->assertSame('application/json', SwaggerClient\ApiClient::selectHeaderContentType(array()));
     $this->assertSame('application/yaml,application/xml', SwaggerClient\ApiClient::selectHeaderContentType(array('application/yaml', 'application/xml')));
     // test addDefaultHeader and getDefaultHeader
     SwaggerClient\ApiClient::addDefaultHeader('test1', 'value1');
     SwaggerClient\ApiClient::addDefaultHeader('test2', 200);
     $defaultHeader = SwaggerClient\ApiClient::getDefaultHeader();
     $this->assertSame('value1', $defaultHeader['test1']);
     $this->assertSame(200, $defaultHeader['test2']);
     // test deleteDefaultHeader
     SwaggerClient\ApiClient::deleteDefaultHeader('test2');
     $defaultHeader = SwaggerClient\ApiClient::getDefaultHeader();
     $this->assertFalse(isset($defaultHeader['test2']));
     $pet_api = new SwaggerClient\PetAPI();
     $pet_api2 = new SwaggerClient\PetAPI();
     $apiClient3 = new SwaggerClient\ApiClient();
     $apiClient3->setUserAgent = 'api client 3';
     $apiClient4 = new SwaggerClient\ApiClient();
     $apiClient4->setUserAgent = 'api client 4';
     $pet_api3 = new SwaggerClient\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(), SwaggerClient\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(), SwaggerClient\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());
 }
示例#2
0
//require_once('vendor/autoload.php');
require_once 'SwaggerClient-php/SwaggerClient.php';
// initialize the API client
//$api_client = new SwaggerClient\ApiClient('http://petstore.swagger.io/v2');
//$api_client->addDefaultHeader("test1", "value1");
// to enable logging
//SwaggerClient\Configuration::$debug = true;
//SwaggerClient\Configuration::$debug_file = '/var/tmp/php_debug.log';
$petId = 10005;
// ID of pet that needs to be fetched
try {
    // get pet by id
    //$pet_api = new SwaggerClient\PetAPI($api_client);
    $pet_api = new SwaggerClient\PetAPI();
    // test default header
    $pet_api->getApiClient()->addDefaultHeader("TEST_API_KEY", "09182sdkanafndsl903");
    // return Pet (model)
    $response = $pet_api->getPetById($petId);
    var_dump($response);
    // test upload file (exception)
    $upload_response = $pet_api->uploadFile($petId, "test meta", NULL);
    // add pet (post json)
    $new_pet_id = 10005;
    $new_pet = new SwaggerClient\models\Pet();
    $new_pet->id = $new_pet_id;
    $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";