bug fix for post (model) and update php test cases

This commit is contained in:
wing328 2015-04-17 20:01:58 +08:00
parent cbbe29cf70
commit 7e37f3294a
9 changed files with 241 additions and 110 deletions

3
.gitignore vendored
View File

@ -28,3 +28,6 @@ target
.lib
atlassian-ide-plugin.xml
.DS_Store
samples/client/petstore/php/SwaggerClient-php/composer.lock
samples/client/petstore/php/SwaggerClient-php/vendor/

View File

@ -94,7 +94,11 @@ class APIClient {
$headers[] = $this->headerName . ": " . $this->headerValue;
}
if ((isset($headerName['Content-Type']) and strpos($headerName['Content-Type'], "multipart/form-data") === FALSE) and (is_object($postData) or is_array($postData))) {
// form data
if ($postData and in_array('Content-Type: application/x-www-form-urlencoded', $headers)) {
$postData = http_build_query($postData);
}
else if ((is_object($postData) or is_array($postData)) and !in_array('Content-Type: multipart/form-data', $headers)) { // json model
$postData = json_encode($this->sanitizeForSerialization($postData));
}

View File

@ -81,11 +81,9 @@ class {{classname}} {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}
} else if (count($formParams) > 0) {
// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
$httpBody = $formParams;
}
// make the API Call

View File

@ -94,7 +94,11 @@ class APIClient {
$headers[] = $this->headerName . ": " . $this->headerValue;
}
if ((isset($headerName['Content-Type']) and strpos($headerName['Content-Type'], "multipart/form-data") === FALSE) and (is_object($postData) or is_array($postData))) {
// form data
if ($postData and in_array('Content-Type: application/x-www-form-urlencoded', $headers)) {
$postData = http_build_query($postData);
}
else if ((is_object($postData) or is_array($postData)) and !in_array('Content-Type: multipart/form-data', $headers)) { // json model
$postData = json_encode($this->sanitizeForSerialization($postData));
}

View File

@ -67,11 +67,9 @@ class PetApi {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}
} else if (count($formParams) > 0) {
// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
$httpBody = $formParams;
}
// make the API Call
@ -120,11 +118,9 @@ class PetApi {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}
} else if (count($formParams) > 0) {
// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
$httpBody = $formParams;
}
// make the API Call
@ -172,11 +168,9 @@ class PetApi {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}
} else if (count($formParams) > 0) {
// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
$httpBody = $formParams;
}
// make the API Call
@ -230,11 +224,9 @@ class PetApi {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}
} else if (count($formParams) > 0) {
// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
$httpBody = $formParams;
}
// make the API Call
@ -289,11 +281,9 @@ class PetApi {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}
} else if (count($formParams) > 0) {
// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
$httpBody = $formParams;
}
// make the API Call
@ -356,11 +346,9 @@ class PetApi {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}
} else if (count($formParams) > 0) {
// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
$httpBody = $formParams;
}
// make the API Call
@ -413,11 +401,9 @@ class PetApi {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}
} else if (count($formParams) > 0) {
// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
$httpBody = $formParams;
}
// make the API Call
@ -474,11 +460,9 @@ class PetApi {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}
} else if (count($formParams) > 0) {
// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
$httpBody = $formParams;
}
// make the API Call

View File

@ -62,11 +62,9 @@ class StoreApi {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}
} else if (count($formParams) > 0) {
// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
$httpBody = $formParams;
}
// make the API Call
@ -121,11 +119,9 @@ class StoreApi {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}
} else if (count($formParams) > 0) {
// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
$httpBody = $formParams;
}
// make the API Call
@ -180,11 +176,9 @@ class StoreApi {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}
} else if (count($formParams) > 0) {
// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
$httpBody = $formParams;
}
// make the API Call
@ -239,11 +233,9 @@ class StoreApi {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}
} else if (count($formParams) > 0) {
// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
$httpBody = $formParams;
}
// make the API Call

View File

@ -67,11 +67,9 @@ class UserApi {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}
} else if (count($formParams) > 0) {
// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
$httpBody = $formParams;
}
// make the API Call
@ -120,11 +118,9 @@ class UserApi {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}
} else if (count($formParams) > 0) {
// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
$httpBody = $formParams;
}
// make the API Call
@ -173,11 +169,9 @@ class UserApi {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}
} else if (count($formParams) > 0) {
// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
$httpBody = $formParams;
}
// make the API Call
@ -229,11 +223,9 @@ class UserApi {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}
} else if (count($formParams) > 0) {
// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
$httpBody = $formParams;
}
// make the API Call
@ -283,11 +275,9 @@ class UserApi {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}
} else if (count($formParams) > 0) {
// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
$httpBody = $formParams;
}
// make the API Call
@ -336,11 +326,9 @@ class UserApi {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}
} else if (count($formParams) > 0) {
// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
$httpBody = $formParams;
}
// make the API Call
@ -400,11 +388,9 @@ class UserApi {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}
} else if (count($formParams) > 0) {
// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
$httpBody = $formParams;
}
// make the API Call
@ -453,11 +439,9 @@ class UserApi {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}
} else if (count($formParams) > 0) {
// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
$httpBody = $formParams;
}
// make the API Call

View File

@ -0,0 +1,60 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.wordnik</groupId>
<artifactId>PhpPetstoreClientTests</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>PHP Swagger Petstore Client</name>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>bundle-install</id>
<phase>pre-integration-test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>composer</executable>
<arguments>
<argument>install</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>bundle-test</id>
<phase>integration-test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>vendor/bin/phpunit</executable>
<arguments>
<argument>tests</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -1,16 +1,118 @@
<?php
require_once('SwaggerClient.php');
class PetApiTest extends \PHPUnit_Framework_TestCase
{
// add a new pet (id 10005) to ensure the pet object is available for all the tests
public static function setUpBeforeClass() {
// initialize the API client
$api_client = new SwaggerClient\APIClient('http://petstore.swagger.io/v2');
$new_pet_id = 10005;
$new_pet = new SwaggerClient\models\Pet;
$new_pet->id = $new_pet_id;
$new_pet->name = "PHP Unit Test";
$pet_api = new SwaggerClient\PetAPI($api_client);
// add a new pet (model)
$add_response = $pet_api->addPet($new_pet);
// return nothing (void)
//$this->assertSame($add_response, NULL);
}
// test getPetById with a Pet object (id 10005)
public function testGetPetById()
{
// initialize the API client
$api_client = new SwaggerClient\APIClient('http://petstore.swagger.io/v2');
$petId = 10005; // ID of pet that needs to be fetched
$pet_id = 10005; // ID of pet that needs to be fetched
$pet_api = new SwaggerClient\PetAPI($api_client);
// return Pet (model)
$response = $pet_api->getPetById($petId);
$this->assertSame($response->id, $petId);
$response = $pet_api->getPetById($pet_id);
$this->assertSame($response->id, $pet_id);
$this->assertSame($response->name, 'PHP Unit Test');
}
// test getPetByStatus and verify by the "id" of the response
public function testFindPetByStatus()
{
// initialize the API client
$api_client = new SwaggerClient\APIClient('http://petstore.swagger.io/v2');
$pet_api = new SwaggerClient\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]), "SwaggerClient\models\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");
$this->assertSame(count($response), 0); // confirm no object returned
}
// test updatePet (model/json)and verify by the "id" of the response
public function testUpdatePet()
{
// initialize the API client
$api_client = new SwaggerClient\APIClient('http://petstore.swagger.io/v2');
$pet_id = 10001; // ID of pet that needs to be fetched
$pet_api = new SwaggerClient\PetAPI($api_client);
// create updated pet object
$updated_pet = new SwaggerClient\models\Pet;
$updated_pet->id = $pet_id;
$updated_pet->status = "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');
}
// test updatePet and verify by the "id" of the response
public function testUpdatePetWithForm()
{
// initialize the API client
$api_client = new SwaggerClient\APIClient('http://petstore.swagger.io/v2');
$pet_id = 10001; // ID of pet that needs to be fetched
$pet_api = new SwaggerClient\PetAPI($api_client);
// update Pet (form)
$update_response = $pet_api->updatePetWithForm($pet_id, null, 'sold');
// return nothing (void)
$this->assertSame($update_response, NULL);
// TODO commented out for the time being since it's broken
// https://github.com/swagger-api/swagger-codegen/issues/656
// verify updated Pet
//$response = $pet_api->getPetById($pet_id);
//$this->assertSame($response->id, $pet_id);
//$this->assertSame($response->status, 'sold');
}
// test addPet and verify by the "id" and "name" of the response
public function testAddPet()
{
// initialize the API client
$api_client = new SwaggerClient\APIClient('http://petstore.swagger.io/v2');
$new_pet_id = 10001;
$new_pet = new SwaggerClient\models\Pet;
$new_pet->id = $new_pet_id;
$new_pet->name = "PHP Unit Test";
$pet_api = new SwaggerClient\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->id, $new_pet_id);
$this->assertSame($response->name, 'PHP Unit Test');
}
}
?>