mirror of
https://github.com/valitydev/openapi-generator.git
synced 2024-11-08 11:23:58 +00:00
Merge pull request #1865 from wing328/php_fix_file_security
[PHP] better filename handling in ObjectSerializer
This commit is contained in:
commit
d35d97d145
@ -79,6 +79,23 @@ class ObjectSerializer
|
|||||||
return $sanitized;
|
return $sanitized;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sanitize filename by removing path.
|
||||||
|
* e.g. ../../sun.gif becomes sun.gif
|
||||||
|
*
|
||||||
|
* @param string $filename filename to be sanitized
|
||||||
|
*
|
||||||
|
* @return string the sanitized filename
|
||||||
|
*/
|
||||||
|
public function sanitizeFilename($filename)
|
||||||
|
{
|
||||||
|
if (preg_match("/.*[\/\\\\](.*)$/", $filename, $match)) {
|
||||||
|
return $match[1];
|
||||||
|
} else {
|
||||||
|
return $filename;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Take value and turn it into a string suitable for inclusion in
|
* Take value and turn it into a string suitable for inclusion in
|
||||||
* the path, by url-encoding.
|
* the path, by url-encoding.
|
||||||
@ -232,7 +249,7 @@ class ObjectSerializer
|
|||||||
} elseif ($class === '\SplFileObject') {
|
} elseif ($class === '\SplFileObject') {
|
||||||
// determine file name
|
// determine file name
|
||||||
if (array_key_exists('Content-Disposition', $httpHeaders) && preg_match('/inline; filename=[\'"]?([^\'"\s]+)[\'"]?$/i', $httpHeaders['Content-Disposition'], $match)) {
|
if (array_key_exists('Content-Disposition', $httpHeaders) && preg_match('/inline; filename=[\'"]?([^\'"\s]+)[\'"]?$/i', $httpHeaders['Content-Disposition'], $match)) {
|
||||||
$filename = Configuration::getDefaultConfiguration()->getTempFolderPath().$match[1];
|
$filename = Configuration::getDefaultConfiguration()->getTempFolderPath() . sanitizeFilename($match[1]);
|
||||||
} else {
|
} else {
|
||||||
$filename = tempnam(Configuration::getDefaultConfiguration()->getTempFolderPath(), '');
|
$filename = tempnam(Configuration::getDefaultConfiguration()->getTempFolderPath(), '');
|
||||||
}
|
}
|
||||||
|
@ -79,6 +79,23 @@ class ObjectSerializer
|
|||||||
return $sanitized;
|
return $sanitized;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sanitize filename by removing path.
|
||||||
|
* e.g. ../../sun.gif becomes sun.gif
|
||||||
|
*
|
||||||
|
* @param string $filename filename to be sanitized
|
||||||
|
*
|
||||||
|
* @return string the sanitized filename
|
||||||
|
*/
|
||||||
|
public function sanitizeFilename($filename)
|
||||||
|
{
|
||||||
|
if (preg_match("/.*[\/\\\\](.*)$/", $filename, $match)) {
|
||||||
|
return $match[1];
|
||||||
|
} else {
|
||||||
|
return $filename;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Take value and turn it into a string suitable for inclusion in
|
* Take value and turn it into a string suitable for inclusion in
|
||||||
* the path, by url-encoding.
|
* the path, by url-encoding.
|
||||||
@ -232,7 +249,7 @@ class ObjectSerializer
|
|||||||
} elseif ($class === '\SplFileObject') {
|
} elseif ($class === '\SplFileObject') {
|
||||||
// determine file name
|
// determine file name
|
||||||
if (array_key_exists('Content-Disposition', $httpHeaders) && preg_match('/inline; filename=[\'"]?([^\'"\s]+)[\'"]?$/i', $httpHeaders['Content-Disposition'], $match)) {
|
if (array_key_exists('Content-Disposition', $httpHeaders) && preg_match('/inline; filename=[\'"]?([^\'"\s]+)[\'"]?$/i', $httpHeaders['Content-Disposition'], $match)) {
|
||||||
$filename = Configuration::getDefaultConfiguration()->getTempFolderPath().$match[1];
|
$filename = Configuration::getDefaultConfiguration()->getTempFolderPath() . sanitizeFilename($match[1]);
|
||||||
} else {
|
} else {
|
||||||
$filename = tempnam(Configuration::getDefaultConfiguration()->getTempFolderPath(), '');
|
$filename = tempnam(Configuration::getDefaultConfiguration()->getTempFolderPath(), '');
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once('autoload.php');
|
||||||
|
|
||||||
|
// test object serializer
|
||||||
|
class ObjectSerializerTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
// test sanitizeFilename
|
||||||
|
public function testSanitizeFilename()
|
||||||
|
{
|
||||||
|
// initialize the API client
|
||||||
|
$s = new Swagger\Client\ObjectSerializer();
|
||||||
|
|
||||||
|
$this->assertSame("sun.gif", $s->sanitizeFilename("sun.gif"));
|
||||||
|
$this->assertSame("sun.gif", $s->sanitizeFilename("../sun.gif"));
|
||||||
|
$this->assertSame("sun.gif", $s->sanitizeFilename("/var/tmp/sun.gif"));
|
||||||
|
$this->assertSame("sun.gif", $s->sanitizeFilename("./sun.gif"));
|
||||||
|
|
||||||
|
$this->assertSame("sun", $s->sanitizeFilename("sun"));
|
||||||
|
$this->assertSame("sun.gif", $s->sanitizeFilename("..\sun.gif"));
|
||||||
|
$this->assertSame("sun.gif", $s->sanitizeFilename("\var\tmp\sun.gif"));
|
||||||
|
$this->assertSame("sun.gif", $s->sanitizeFilename("c:\var\tmp\sun.gif"));
|
||||||
|
$this->assertSame("sun.gif", $s->sanitizeFilename(".\sun.gif"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
|
|
@ -5,257 +5,256 @@ require_once('autoload.php');
|
|||||||
class PetApiTest extends \PHPUnit_Framework_TestCase
|
class PetApiTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
|
|
||||||
// add a new pet (id 10005) to ensure the pet object is available for all the tests
|
// add a new pet (id 10005) to ensure the pet object is available for all the tests
|
||||||
public static function setUpBeforeClass() {
|
public static function setUpBeforeClass() {
|
||||||
// for error reporting (need to run with php5.3 to get no warning)
|
// for error reporting (need to run with php5.3 to get no warning)
|
||||||
//ini_set('display_errors', 1);
|
//ini_set('display_errors', 1);
|
||||||
//error_reporting(~0);
|
//error_reporting(~0);
|
||||||
// when running with php5.5, comment out below to skip the warning about
|
// when running with php5.5, comment out below to skip the warning about
|
||||||
// using @ to handle file upload
|
// using @ to handle file upload
|
||||||
//ini_set('display_startup_errors',1);
|
//ini_set('display_startup_errors',1);
|
||||||
//ini_set('display_errors',1);
|
//ini_set('display_errors',1);
|
||||||
//error_reporting(-1);
|
//error_reporting(-1);
|
||||||
|
|
||||||
// enable debugging
|
// enable debugging
|
||||||
//Swagger\Client\Configuration::$debug = true;
|
//Swagger\Client\Configuration::$debug = true;
|
||||||
|
|
||||||
// skip initializing the API client as it should be automatic
|
// skip initializing the API client as it should be automatic
|
||||||
//$api_client = new Swagger\Client\ApiClient('http://petstore.swagger.io/v2');
|
//$api_client = new Swagger\Client\ApiClient('http://petstore.swagger.io/v2');
|
||||||
// new pet
|
// new pet
|
||||||
$new_pet_id = 10005;
|
$new_pet_id = 10005;
|
||||||
$new_pet = new Swagger\Client\Model\Pet;
|
$new_pet = new Swagger\Client\Model\Pet;
|
||||||
$new_pet->setId($new_pet_id);
|
$new_pet->setId($new_pet_id);
|
||||||
$new_pet->setName("PHP Unit Test");
|
$new_pet->setName("PHP Unit Test");
|
||||||
// new tag
|
// new tag
|
||||||
$tag= new Swagger\Client\Model\Tag;
|
$tag= new Swagger\Client\Model\Tag;
|
||||||
$tag->setId($new_pet_id); // use the same id as pet
|
$tag->setId($new_pet_id); // use the same id as pet
|
||||||
$tag->setName("test php tag");
|
$tag->setName("test php tag");
|
||||||
// new category
|
// new category
|
||||||
$category = new Swagger\Client\Model\Category;
|
$category = new Swagger\Client\Model\Category;
|
||||||
$category->setId($new_pet_id); // use the same id as pet
|
$category->setId($new_pet_id); // use the same id as pet
|
||||||
$category->setName("test php category");
|
$category->setName("test php category");
|
||||||
|
|
||||||
$new_pet->setTags(array($tag));
|
$new_pet->setTags(array($tag));
|
||||||
$new_pet->setCategory($category);
|
$new_pet->setCategory($category);
|
||||||
|
|
||||||
$pet_api = new Swagger\Client\Api\PetAPI();
|
$pet_api = new Swagger\Client\Api\PetAPI();
|
||||||
// add a new pet (model)
|
// add a new pet (model)
|
||||||
$add_response = $pet_api->addPet($new_pet);
|
$add_response = $pet_api->addPet($new_pet);
|
||||||
}
|
|
||||||
|
|
||||||
// test static functions defined in ApiClient
|
|
||||||
public function testApiClient()
|
|
||||||
{
|
|
||||||
// test selectHeaderAccept
|
|
||||||
$api_client = new Swagger\Client\ApiClient();
|
|
||||||
$this->assertSame('application/json', $api_client->selectHeaderAccept(array('application/xml','application/json')));
|
|
||||||
$this->assertSame(NULL, $api_client->selectHeaderAccept(array()));
|
|
||||||
$this->assertSame('application/yaml,application/xml', $api_client->selectHeaderAccept(array('application/yaml','application/xml')));
|
|
||||||
|
|
||||||
// test selectHeaderContentType
|
|
||||||
$this->assertSame('application/json', $api_client->selectHeaderContentType(array('application/xml','application/json')));
|
|
||||||
$this->assertSame('application/json', $api_client->selectHeaderContentType(array()));
|
|
||||||
$this->assertSame('application/yaml,application/xml', $api_client->selectHeaderContentType(array('application/yaml','application/xml')));
|
|
||||||
|
|
||||||
// test addDefaultHeader and 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->getConfig()->deleteDefaultHeader('test2');
|
|
||||||
$defaultHeader = $api_client->getConfig()->getDefaultHeaders();
|
|
||||||
$this->assertFalse(isset($defaultHeader['test2']));
|
|
||||||
|
|
||||||
$pet_api2 = new Swagger\Client\Api\PetAPI();
|
|
||||||
$config3 = new Swagger\Client\Configuration();
|
|
||||||
$apiClient3 = new Swagger\Client\ApiClient($config3);
|
|
||||||
$apiClient3->getConfig()->setUserAgent('api client 3');
|
|
||||||
$config4 = new Swagger\Client\Configuration();
|
|
||||||
$apiClient4 = new Swagger\Client\ApiClient($config4);
|
|
||||||
$apiClient4->getConfig()->setUserAgent('api client 4');
|
|
||||||
$pet_api3 = new Swagger\Client\Api\PetAPI($apiClient3);
|
|
||||||
|
|
||||||
// 2 different api clients are not the same
|
|
||||||
$this->assertNotEquals($apiClient3, $apiClient4);
|
|
||||||
// customied pet api not using the old pet api's api client
|
|
||||||
$this->assertNotEquals($pet_api2->getApiClient(), $pet_api3->getApiClient());
|
|
||||||
|
|
||||||
// test access token
|
|
||||||
$api_client->getConfig()->setAccessToken("testing_only");
|
|
||||||
$this->assertSame('testing_only', $api_client->getConfig()->getAccessToken());
|
|
||||||
}
|
|
||||||
|
|
||||||
// test getPetById with a Pet object (id 10005)
|
|
||||||
public function testGetPetById()
|
|
||||||
{
|
|
||||||
// initialize the API client without host
|
|
||||||
$pet_id = 10005; // ID of pet that needs to be fetched
|
|
||||||
$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->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 getPetById with a Pet object (id 10005)
|
|
||||||
public function testGetPetByIdWithHttpInfo()
|
|
||||||
{
|
|
||||||
// initialize the API client without host
|
|
||||||
$pet_id = 10005; // ID of pet that needs to be fetched
|
|
||||||
$pet_api = new Swagger\Client\Api\PetAPI();
|
|
||||||
$pet_api->getApiClient()->getConfig()->setApiKey('api_key', '111222333444555');
|
|
||||||
// return Pet (model)
|
|
||||||
list($response, $status_code, $response_headers) = $pet_api->getPetByIdWithHttpInfo($pet_id);
|
|
||||||
$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');
|
|
||||||
$this->assertSame($status_code, 200);
|
|
||||||
$this->assertSame($response_headers['Content-Type'], 'application/json');
|
|
||||||
}
|
|
||||||
|
|
||||||
// test getPetByStatus and verify by the "id" of the response
|
|
||||||
public function testFindPetByStatus()
|
|
||||||
{
|
|
||||||
// initialize the API client
|
|
||||||
$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");
|
|
||||||
$this->assertGreaterThan(0, count($response)); // at least one object returned
|
|
||||||
$this->assertSame(get_class($response[0]), "Swagger\\Client\\Model\\Pet"); // verify the object is Pet
|
|
||||||
// loop through result to ensure status is "available"
|
|
||||||
foreach ($response as $_pet) {
|
|
||||||
$this->assertSame($_pet['status'], "available");
|
|
||||||
}
|
}
|
||||||
// test invalid status
|
|
||||||
$response = $pet_api->findPetsByStatus("unknown_and_incorrect_status");
|
// test static functions defined in ApiClient
|
||||||
$this->assertSame(count($response), 0); // confirm no object returned
|
public function testApiClient()
|
||||||
}
|
{
|
||||||
|
// test selectHeaderAccept
|
||||||
// test updatePet (model/json)and verify by the "id" of the response
|
$api_client = new Swagger\Client\ApiClient();
|
||||||
public function testUpdatePet()
|
$this->assertSame('application/json', $api_client->selectHeaderAccept(array('application/xml','application/json')));
|
||||||
{
|
$this->assertSame(NULL, $api_client->selectHeaderAccept(array()));
|
||||||
// initialize the API client
|
$this->assertSame('application/yaml,application/xml', $api_client->selectHeaderAccept(array('application/yaml','application/xml')));
|
||||||
$config = (new Swagger\Client\Configuration())->setHost('http://petstore.swagger.io/v2');
|
|
||||||
$api_client = new Swagger\Client\ApiClient($config);
|
// test selectHeaderContentType
|
||||||
$pet_id = 10001; // ID of pet that needs to be fetched
|
$this->assertSame('application/json', $api_client->selectHeaderContentType(array('application/xml','application/json')));
|
||||||
$pet_api = new Swagger\Client\Api\PetAPI($api_client);
|
$this->assertSame('application/json', $api_client->selectHeaderContentType(array()));
|
||||||
// create updated pet object
|
$this->assertSame('application/yaml,application/xml', $api_client->selectHeaderContentType(array('application/yaml','application/xml')));
|
||||||
$updated_pet = new Swagger\Client\Model\Pet;
|
|
||||||
$updated_pet->setId($pet_id);
|
// test addDefaultHeader and getDefaultHeader
|
||||||
$updated_pet->setName('updatePet'); // new name
|
$api_client->getConfig()->addDefaultHeader('test1', 'value1');
|
||||||
$updated_pet->setStatus('pending'); // new status
|
$api_client->getConfig()->addDefaultHeader('test2', 200);
|
||||||
// update Pet (model/json)
|
$defaultHeader = $api_client->getConfig()->getDefaultHeaders();
|
||||||
$update_response = $pet_api->updatePet($updated_pet);
|
$this->assertSame('value1', $defaultHeader['test1']);
|
||||||
// return nothing (void)
|
$this->assertSame(200, $defaultHeader['test2']);
|
||||||
$this->assertSame($update_response, NULL);
|
|
||||||
// verify updated Pet
|
// test deleteDefaultHeader
|
||||||
$response = $pet_api->getPetById($pet_id);
|
$api_client->getConfig()->deleteDefaultHeader('test2');
|
||||||
$this->assertSame($response->getId(), $pet_id);
|
$defaultHeader = $api_client->getConfig()->getDefaultHeaders();
|
||||||
$this->assertSame($response->getStatus(), 'pending');
|
$this->assertFalse(isset($defaultHeader['test2']));
|
||||||
$this->assertSame($response->getName(), 'updatePet');
|
|
||||||
}
|
$pet_api2 = new Swagger\Client\Api\PetAPI();
|
||||||
|
$config3 = new Swagger\Client\Configuration();
|
||||||
// test updatePetWithFormWithHttpInfo and verify by the "name" of the response
|
$apiClient3 = new Swagger\Client\ApiClient($config3);
|
||||||
public function testUpdatePetWithFormWithHttpInfo()
|
$apiClient3->getConfig()->setUserAgent('api client 3');
|
||||||
{
|
$config4 = new Swagger\Client\Configuration();
|
||||||
// initialize the API client
|
$apiClient4 = new Swagger\Client\ApiClient($config4);
|
||||||
$config = (new Swagger\Client\Configuration())->setHost('http://petstore.swagger.io/v2');
|
$apiClient4->getConfig()->setUserAgent('api client 4');
|
||||||
$api_client = new Swagger\Client\ApiClient($config);
|
$pet_api3 = new Swagger\Client\Api\PetAPI($apiClient3);
|
||||||
$pet_id = 10001; // ID of pet that needs to be fetched
|
|
||||||
$pet_api = new Swagger\Client\Api\PetAPI($api_client);
|
// 2 different api clients are not the same
|
||||||
// update Pet (form)
|
$this->assertNotEquals($apiClient3, $apiClient4);
|
||||||
list($update_response, $status_code, $http_headers) = $pet_api->updatePetWithFormWithHttpInfo($pet_id, 'update pet with form with http info');
|
// customied pet api not using the old pet api's api client
|
||||||
// return nothing (void)
|
$this->assertNotEquals($pet_api2->getApiClient(), $pet_api3->getApiClient());
|
||||||
$this->assertNull($update_response);
|
|
||||||
$this->assertSame($status_code, 200);
|
// test access token
|
||||||
$this->assertSame($http_headers['Content-Type'], 'application/json');
|
$api_client->getConfig()->setAccessToken("testing_only");
|
||||||
$response = $pet_api->getPetById($pet_id);
|
$this->assertSame('testing_only', $api_client->getConfig()->getAccessToken());
|
||||||
$this->assertSame($response->getId(), $pet_id);
|
}
|
||||||
$this->assertSame($response->getName(), 'update pet with form with http info');
|
|
||||||
}
|
// test getPetById with a Pet object (id 10005)
|
||||||
|
public function testGetPetById()
|
||||||
// test updatePetWithForm and verify by the "name" and "status" of the response
|
{
|
||||||
public function testUpdatePetWithForm()
|
// initialize the API client without host
|
||||||
{
|
$pet_id = 10005; // ID of pet that needs to be fetched
|
||||||
// initialize the API client
|
$pet_api = new Swagger\Client\Api\PetAPI();
|
||||||
$config = (new Swagger\Client\Configuration())->setHost('http://petstore.swagger.io/v2');
|
$pet_api->getApiClient()->getConfig()->setApiKey('api_key', '111222333444555');
|
||||||
$api_client = new Swagger\Client\ApiClient($config);
|
// return Pet (model)
|
||||||
$pet_id = 10001; // ID of pet that needs to be fetched
|
$response = $pet_api->getPetById($pet_id);
|
||||||
$pet_api = new Swagger\Client\Api\PetAPI($api_client);
|
$this->assertSame($response->getId(), $pet_id);
|
||||||
// update Pet (form)
|
$this->assertSame($response->getName(), 'PHP Unit Test');
|
||||||
$update_response = $pet_api->updatePetWithForm($pet_id, 'update pet with form', 'sold');
|
$this->assertSame($response->getCategory()->getId(), $pet_id);
|
||||||
// return nothing (void)
|
$this->assertSame($response->getCategory()->getName(), 'test php category');
|
||||||
$this->assertSame($update_response, NULL);
|
$this->assertSame($response->getTags()[0]->getId(), $pet_id);
|
||||||
$response = $pet_api->getPetById($pet_id);
|
$this->assertSame($response->getTags()[0]->getName(), 'test php tag');
|
||||||
$this->assertSame($response->getId(), $pet_id);
|
}
|
||||||
$this->assertSame($response->getName(), 'update pet with form');
|
|
||||||
$this->assertSame($response->getStatus(), 'sold');
|
// test getPetById with a Pet object (id 10005)
|
||||||
}
|
public function testGetPetByIdWithHttpInfo()
|
||||||
|
{
|
||||||
// test addPet and verify by the "id" and "name" of the response
|
// initialize the API client without host
|
||||||
public function testAddPet()
|
$pet_id = 10005; // ID of pet that needs to be fetched
|
||||||
{
|
$pet_api = new Swagger\Client\Api\PetAPI();
|
||||||
// initialize the API client
|
$pet_api->getApiClient()->getConfig()->setApiKey('api_key', '111222333444555');
|
||||||
$config = (new Swagger\Client\Configuration())->setHost('http://petstore.swagger.io/v2');
|
// return Pet (model)
|
||||||
$api_client = new Swagger\Client\ApiClient($config);
|
list($response, $status_code, $response_headers) = $pet_api->getPetByIdWithHttpInfo($pet_id);
|
||||||
$new_pet_id = 10001;
|
$this->assertSame($response->getId(), $pet_id);
|
||||||
$new_pet = new Swagger\Client\Model\Pet;
|
$this->assertSame($response->getName(), 'PHP Unit Test');
|
||||||
$new_pet->setId($new_pet_id);
|
$this->assertSame($response->getCategory()->getId(), $pet_id);
|
||||||
$new_pet->setName("PHP Unit Test");
|
$this->assertSame($response->getCategory()->getName(), 'test php category');
|
||||||
$pet_api = new Swagger\Client\Api\PetAPI($api_client);
|
$this->assertSame($response->getTags()[0]->getId(), $pet_id);
|
||||||
// add a new pet (model)
|
$this->assertSame($response->getTags()[0]->getName(), 'test php tag');
|
||||||
$add_response = $pet_api->addPet($new_pet);
|
$this->assertSame($status_code, 200);
|
||||||
// return nothing (void)
|
$this->assertSame($response_headers['Content-Type'], 'application/json');
|
||||||
$this->assertSame($add_response, NULL);
|
}
|
||||||
// verify added Pet
|
|
||||||
$response = $pet_api->getPetById($new_pet_id);
|
// test getPetByStatus and verify by the "id" of the response
|
||||||
$this->assertSame($response->getId(), $new_pet_id);
|
public function testFindPetByStatus()
|
||||||
$this->assertSame($response->getName(), 'PHP Unit Test');
|
{
|
||||||
}
|
// initialize the API client
|
||||||
|
$config = (new Swagger\Client\Configuration())->setHost('http://petstore.swagger.io/v2');
|
||||||
// test upload file
|
$api_client = new Swagger\Client\ApiClient($config);
|
||||||
public function testUploadFile()
|
$pet_api = new Swagger\Client\Api\PetAPI($api_client);
|
||||||
{
|
// return Pet (model)
|
||||||
// initialize the API client
|
$response = $pet_api->findPetsByStatus("available");
|
||||||
$config = (new Swagger\Client\Configuration())->setHost('http://petstore.swagger.io/v2');
|
$this->assertGreaterThan(0, count($response)); // at least one object returned
|
||||||
$api_client = new Swagger\Client\ApiClient($config);
|
$this->assertSame(get_class($response[0]), "Swagger\\Client\\Model\\Pet"); // verify the object is Pet
|
||||||
$pet_api = new Swagger\Client\Api\PetAPI($api_client);
|
// loop through result to ensure status is "available"
|
||||||
// upload file
|
foreach ($response as $_pet) {
|
||||||
$pet_id = 10001;
|
$this->assertSame($_pet['status'], "available");
|
||||||
$add_response = $pet_api->uploadFile($pet_id, "test meta", "./composer.json");
|
}
|
||||||
// return nothing (void)
|
// test invalid status
|
||||||
$this->assertSame($add_response, NULL);
|
$response = $pet_api->findPetsByStatus("unknown_and_incorrect_status");
|
||||||
}
|
$this->assertSame(count($response), 0); // confirm no object returned
|
||||||
|
}
|
||||||
// test get inventory
|
|
||||||
public function testGetInventory()
|
// test updatePet (model/json)and verify by the "id" of the response
|
||||||
{
|
public function testUpdatePet()
|
||||||
// initialize the API client
|
{
|
||||||
$config = new Swagger\Client\Configuration();
|
// initialize the API client
|
||||||
$config->setHost('http://petstore.swagger.io/v2');
|
$config = (new Swagger\Client\Configuration())->setHost('http://petstore.swagger.io/v2');
|
||||||
$api_client = new Swagger\Client\APIClient($config);
|
$api_client = new Swagger\Client\ApiClient($config);
|
||||||
$store_api = new Swagger\Client\Api\StoreAPI($api_client);
|
$pet_id = 10001; // ID of pet that needs to be fetched
|
||||||
// get inventory
|
$pet_api = new Swagger\Client\Api\PetAPI($api_client);
|
||||||
$get_response = $store_api->getInventory();
|
// create updated pet object
|
||||||
|
$updated_pet = new Swagger\Client\Model\Pet;
|
||||||
$this->assertInternalType("int", $get_response['sold']);
|
$updated_pet->setId($pet_id);
|
||||||
$this->assertInternalType("int", $get_response['pending']);
|
$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->getId(), $pet_id);
|
||||||
|
$this->assertSame($response->getStatus(), 'pending');
|
||||||
|
$this->assertSame($response->getName(), 'updatePet');
|
||||||
|
}
|
||||||
|
|
||||||
|
// test updatePetWithFormWithHttpInfo and verify by the "name" of the response
|
||||||
|
public function testUpdatePetWithFormWithHttpInfo()
|
||||||
|
{
|
||||||
|
// initialize the API client
|
||||||
|
$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)
|
||||||
|
list($update_response, $status_code, $http_headers) = $pet_api->updatePetWithFormWithHttpInfo($pet_id, 'update pet with form with http info');
|
||||||
|
// return nothing (void)
|
||||||
|
$this->assertNull($update_response);
|
||||||
|
$this->assertSame($status_code, 200);
|
||||||
|
$this->assertSame($http_headers['Content-Type'], 'application/json');
|
||||||
|
$response = $pet_api->getPetById($pet_id);
|
||||||
|
$this->assertSame($response->getId(), $pet_id);
|
||||||
|
$this->assertSame($response->getName(), 'update pet with form with http info');
|
||||||
|
}
|
||||||
|
|
||||||
|
// test updatePetWithForm and verify by the "name" and "status" of the response
|
||||||
|
public function testUpdatePetWithForm()
|
||||||
|
{
|
||||||
|
// initialize the API client
|
||||||
|
$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)
|
||||||
|
$update_response = $pet_api->updatePetWithForm($pet_id, 'update pet with form', 'sold');
|
||||||
|
// return nothing (void)
|
||||||
|
$this->assertSame($update_response, NULL);
|
||||||
|
$response = $pet_api->getPetById($pet_id);
|
||||||
|
$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
|
||||||
|
$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->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);
|
||||||
|
// return nothing (void)
|
||||||
|
$this->assertSame($add_response, NULL);
|
||||||
|
// verify added Pet
|
||||||
|
$response = $pet_api->getPetById($new_pet_id);
|
||||||
|
$this->assertSame($response->getId(), $new_pet_id);
|
||||||
|
$this->assertSame($response->getName(), 'PHP Unit Test');
|
||||||
|
}
|
||||||
|
|
||||||
|
// test upload file
|
||||||
|
public function testUploadFile()
|
||||||
|
{
|
||||||
|
// initialize the API client
|
||||||
|
$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;
|
||||||
|
$add_response = $pet_api->uploadFile($pet_id, "test meta", "./composer.json");
|
||||||
|
// return nothing (void)
|
||||||
|
$this->assertSame($add_response, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
// test get inventory
|
||||||
|
public function testGetInventory()
|
||||||
|
{
|
||||||
|
// initialize the API client
|
||||||
|
$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();
|
||||||
|
|
||||||
|
$this->assertInternalType("int", $get_response['sold']);
|
||||||
|
$this->assertInternalType("int", $get_response['pending']);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
Loading…
Reference in New Issue
Block a user