[PHP] Improve codestyle of phpunit tests

This commit is contained in:
Arne Jørgensen 2016-05-14 13:41:13 +02:00
parent 1f02fd281f
commit d3fe0ae28a
5 changed files with 168 additions and 156 deletions

View File

@ -1,6 +1,6 @@
<?php <?php
require_once('autoload.php'); namespace Swagger\Client;
// test object serializer // test object serializer
class ObjectSerializerTest extends \PHPUnit_Framework_TestCase class ObjectSerializerTest extends \PHPUnit_Framework_TestCase
@ -9,7 +9,7 @@ class ObjectSerializerTest extends \PHPUnit_Framework_TestCase
public function testSanitizeFilename() public function testSanitizeFilename()
{ {
// initialize the API client // initialize the API client
$s = new Swagger\Client\ObjectSerializer(); $s = new ObjectSerializer();
$this->assertSame("sun.gif", $s->sanitizeFilename("sun.gif")); $this->assertSame("sun.gif", $s->sanitizeFilename("sun.gif"));
$this->assertSame("sun.gif", $s->sanitizeFilename("../sun.gif")); $this->assertSame("sun.gif", $s->sanitizeFilename("../sun.gif"));
@ -22,8 +22,4 @@ class ObjectSerializerTest extends \PHPUnit_Framework_TestCase
$this->assertSame("sun.gif", $s->sanitizeFilename("c:\var\tmp\sun.gif")); $this->assertSame("sun.gif", $s->sanitizeFilename("c:\var\tmp\sun.gif"));
$this->assertSame("sun.gif", $s->sanitizeFilename(".\sun.gif")); $this->assertSame("sun.gif", $s->sanitizeFilename(".\sun.gif"));
} }
} }
?>

View File

@ -1,12 +1,13 @@
<?php <?php
require_once('autoload.php'); namespace Swagger\Client;
class OrderApiTest extends \PHPUnit_Framework_TestCase class OrderApiTest 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);
@ -15,15 +16,15 @@ class OrderApiTest extends \PHPUnit_Framework_TestCase
// test get inventory // test get inventory
public function testOrderEnum() public function testOrderEnum()
{ {
$this->assertSame(Swagger\Client\Model\Order::STATUS_PLACED, "placed"); $this->assertSame(Model\Order::STATUS_PLACED, "placed");
$this->assertSame(Swagger\Client\Model\Order::STATUS_APPROVED, "approved"); $this->assertSame(Model\Order::STATUS_APPROVED, "approved");
} }
// test get inventory // test get inventory
public function testOrder() public function testOrder()
{ {
// initialize the API client // initialize the API client
$order = new Swagger\Client\Model\Order(); $order = new Model\Order();
$order->setStatus("placed"); $order->setStatus("placed");
$this->assertSame("placed", $order->getStatus()); $this->assertSame("placed", $order->getStatus());
@ -31,11 +32,11 @@ class OrderApiTest extends \PHPUnit_Framework_TestCase
/** /**
* @expectedException InvalidArgumentException * @expectedException InvalidArgumentException
*/ */
public function testOrderException() public function testOrderException()
{ {
// initialize the API client // initialize the API client
$order = new Swagger\Client\Model\Order(); $order = new Model\Order();
$order->setStatus("invalid_value"); $order->setStatus("invalid_value");
} }
@ -52,13 +53,16 @@ class OrderApiTest extends \PHPUnit_Framework_TestCase
"complete": false "complete": false
} }
ORDER; ORDER;
$order = \Swagger\Client\ObjectSerializer::deserialize(json_decode($order_json), 'Swagger\Client\Model\Order'); $order = ObjectSerializer::deserialize(
json_decode($order_json),
'Swagger\Client\Model\Order'
);
$this->assertInstanceOf('Swagger\Client\Model\Order', $order); $this->assertInstanceOf('Swagger\Client\Model\Order', $order);
$this->assertSame(10, $order->getId()); $this->assertSame(10, $order->getId());
$this->assertSame(20, $order->getPetId()); $this->assertSame(20, $order->getPetId());
$this->assertSame(30, $order->getQuantity()); $this->assertSame(30, $order->getQuantity());
$this->assertTrue(new DateTime("2015-08-22T07:13:36.613Z") == $order->getShipDate()); $this->assertTrue(new \DateTime("2015-08-22T07:13:36.613Z") == $order->getShipDate());
$this->assertSame("placed", $order->getStatus()); $this->assertSame("placed", $order->getStatus());
$this->assertSame(false, $order->getComplete()); $this->assertSame(false, $order->getComplete());
} }
@ -76,16 +80,19 @@ ORDER;
"complete": false "complete": false
}]] }]]
ORDER; ORDER;
$order = \Swagger\Client\ObjectSerializer::deserialize(json_decode($order_json), 'Swagger\Client\Model\Order[][]'); $order = ObjectSerializer::deserialize(
json_decode($order_json),
'Swagger\Client\Model\Order[][]'
);
$this->assertArrayHasKey(0, $order); $this->assertArrayHasKey(0, $order);
$this->assertArrayHasKey(0, $order[0]); $this->assertArrayHasKey(0, $order[0]);
$_order = $order[0][0]; $_order = $order[0][0];
$this->assertInstanceOf('Swagger\Client\Model\Order', $_order); $this->assertInstanceOf('Swagger\Client\Model\Order', $_order);
$this->assertSame(10, $_order->getId()); $this->assertSame(10, $_order->getId());
$this->assertSame(20, $_order->getPetId()); $this->assertSame(20, $_order->getPetId());
$this->assertSame(30, $_order->getQuantity()); $this->assertSame(30, $_order->getQuantity());
$this->assertTrue(new DateTime("2015-08-22T07:13:36.613Z") == $_order->getShipDate()); $this->assertTrue(new \DateTime("2015-08-22T07:13:36.613Z") == $_order->getShipDate());
$this->assertSame("placed", $_order->getStatus()); $this->assertSame("placed", $_order->getStatus());
$this->assertSame(false, $_order->getComplete()); $this->assertSame(false, $_order->getComplete());
} }
@ -107,21 +114,20 @@ ORDER;
} }
} }
ORDER; ORDER;
$order = \Swagger\Client\ObjectSerializer::deserialize(json_decode($order_json), 'map[string,map[string,\Swagger\Client\Model\Order]]'); $order = ObjectSerializer::deserialize(
json_decode($order_json),
'map[string,map[string,\Swagger\Client\Model\Order]]'
);
$this->assertArrayHasKey('test', $order); $this->assertArrayHasKey('test', $order);
$this->assertArrayHasKey('test2', $order['test']); $this->assertArrayHasKey('test2', $order['test']);
$_order = $order['test']['test2']; $_order = $order['test']['test2'];
$this->assertInstanceOf('Swagger\Client\Model\Order', $_order); $this->assertInstanceOf('Swagger\Client\Model\Order', $_order);
$this->assertSame(10, $_order->getId()); $this->assertSame(10, $_order->getId());
$this->assertSame(20, $_order->getPetId()); $this->assertSame(20, $_order->getPetId());
$this->assertSame(30, $_order->getQuantity()); $this->assertSame(30, $_order->getQuantity());
$this->assertTrue(new DateTime("2015-08-22T07:13:36.613Z") == $_order->getShipDate()); $this->assertTrue(new \DateTime("2015-08-22T07:13:36.613Z") == $_order->getShipDate());
$this->assertSame("placed", $_order->getStatus()); $this->assertSame("placed", $_order->getStatus());
$this->assertSame(false, $_order->getComplete()); $this->assertSame(false, $_order->getComplete());
} }
} }
?>

View File

@ -1,16 +1,17 @@
<?php <?php
require_once('autoload.php'); namespace Swagger\Client;
// increase memory limit to avoid fatal error due to findPetByStatus
// returning a lot of data
ini_set('memory_limit', '256M');
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()
{
// increase memory limit to avoid fatal error due to findPetByStatus
// returning a lot of data
ini_set('memory_limit', '256M');
// 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);
@ -19,48 +20,60 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
//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; //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 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 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_pet->setPhotoUrls(array("http://test_php_unit_test.com")); $new_pet->setPhotoUrls(array("http://test_php_unit_test.com"));
// new tag // new tag
$tag= new Swagger\Client\Model\Tag; $tag= new 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 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 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 // test static functions defined in ApiClient
public function testApiClient() public function testApiClient()
{ {
// test selectHeaderAccept // test selectHeaderAccept
$api_client = new Swagger\Client\ApiClient(); $api_client = new ApiClient();
$this->assertSame('application/json', $api_client->selectHeaderAccept(array('application/xml','application/json'))); $this->assertSame('application/json', $api_client->selectHeaderAccept(array(
$this->assertSame(NULL, $api_client->selectHeaderAccept(array())); 'application/xml',
$this->assertSame('application/yaml,application/xml', $api_client->selectHeaderAccept(array('application/yaml','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 // test selectHeaderContentType
$this->assertSame('application/json', $api_client->selectHeaderContentType(array('application/xml','application/json'))); $this->assertSame('application/json', $api_client->selectHeaderContentType(array(
'application/xml',
'application/json'
)));
$this->assertSame('application/json', $api_client->selectHeaderContentType(array())); $this->assertSame('application/json', $api_client->selectHeaderContentType(array()));
$this->assertSame('application/yaml,application/xml', $api_client->selectHeaderContentType(array('application/yaml','application/xml'))); $this->assertSame('application/yaml,application/xml', $api_client->selectHeaderContentType(array(
'application/yaml',
'application/xml'
)));
// test addDefaultHeader and getDefaultHeader // test addDefaultHeader and getDefaultHeader
$api_client->getConfig()->addDefaultHeader('test1', 'value1'); $api_client->getConfig()->addDefaultHeader('test1', 'value1');
@ -74,16 +87,16 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
$defaultHeader = $api_client->getConfig()->getDefaultHeaders(); $defaultHeader = $api_client->getConfig()->getDefaultHeaders();
$this->assertFalse(isset($defaultHeader['test2'])); $this->assertFalse(isset($defaultHeader['test2']));
$pet_api2 = new Swagger\Client\Api\PetApi(); $pet_api2 = new Api\PetApi();
$config3 = new Swagger\Client\Configuration(); $config3 = new Configuration();
$apiClient3 = new Swagger\Client\ApiClient($config3); $apiClient3 = new ApiClient($config3);
$apiClient3->getConfig()->setUserAgent('api client 3'); $apiClient3->getConfig()->setUserAgent('api client 3');
$config4 = new Swagger\Client\Configuration(); $config4 = new Configuration();
$apiClient4 = new Swagger\Client\ApiClient($config4); $apiClient4 = new ApiClient($config4);
$apiClient4->getConfig()->setUserAgent('api client 4'); $apiClient4->getConfig()->setUserAgent('api client 4');
$pet_api3 = new Swagger\Client\Api\PetApi($apiClient3); $pet_api3 = new Api\PetApi($apiClient3);
// 2 different api clients are not the same // 2 different api clients are not the same
$this->assertNotEquals($apiClient3, $apiClient4); $this->assertNotEquals($apiClient3, $apiClient4);
// customied pet api not using the old pet api's api client // customied pet api not using the old pet api's api client
$this->assertNotEquals($pet_api2->getApiClient(), $pet_api3->getApiClient()); $this->assertNotEquals($pet_api2->getApiClient(), $pet_api3->getApiClient());
@ -98,7 +111,7 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
{ {
// initialize the API client without host // initialize the API client without host
$pet_id = 10005; // ID of pet that needs to be fetched $pet_id = 10005; // ID of pet that needs to be fetched
$pet_api = new Swagger\Client\Api\PetApi(); $pet_api = new Api\PetApi();
$pet_api->getApiClient()->getConfig()->setApiKey('api_key', '111222333444555'); $pet_api->getApiClient()->getConfig()->setApiKey('api_key', '111222333444555');
// return Pet (model) // return Pet (model)
$response = $pet_api->getPetById($pet_id); $response = $pet_api->getPetById($pet_id);
@ -111,7 +124,7 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
$this->assertSame($response->getTags()[0]->getName(), 'test php tag'); $this->assertSame($response->getTags()[0]->getName(), 'test php tag');
} }
/* /**
* comment out as we've removed invalid endpoints from the spec, we'll introduce something * comment out as we've removed invalid endpoints from the spec, we'll introduce something
* similar in the future when we've time to update the petstore server * similar in the future when we've time to update the petstore server
* *
@ -120,7 +133,7 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
{ {
// initialize the API client without host // initialize the API client without host
$pet_id = 10005; // ID of pet that needs to be fetched $pet_id = 10005; // ID of pet that needs to be fetched
$pet_api = new Swagger\Client\Api\PetApi(); $pet_api = new Api\PetApi();
$pet_api->getApiClient()->getConfig()->setApiKey('api_key', '111222333444555'); $pet_api->getApiClient()->getConfig()->setApiKey('api_key', '111222333444555');
// return Pet (inline model) // return Pet (inline model)
$response = $pet_api->getPetByIdInObject($pet_id); $response = $pet_api->getPetByIdInObject($pet_id);
@ -144,7 +157,7 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
{ {
// initialize the API client without host // initialize the API client without host
$pet_id = 10005; // ID of pet that needs to be fetched $pet_id = 10005; // ID of pet that needs to be fetched
$pet_api = new Swagger\Client\Api\PetApi(); $pet_api = new Api\PetApi();
$pet_api->getApiClient()->getConfig()->setApiKey('api_key', '111222333444555'); $pet_api->getApiClient()->getConfig()->setApiKey('api_key', '111222333444555');
// return Pet (model) // return Pet (model)
list($response, $status_code, $response_headers) = $pet_api->getPetByIdWithHttpInfo($pet_id); list($response, $status_code, $response_headers) = $pet_api->getPetByIdWithHttpInfo($pet_id);
@ -162,18 +175,18 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
public function testFindPetByStatus() public function testFindPetByStatus()
{ {
// initialize the API client // initialize the API client
$config = (new Swagger\Client\Configuration())->setHost('http://petstore.swagger.io/v2'); $config = (new Configuration())->setHost('http://petstore.swagger.io/v2');
$api_client = new Swagger\Client\ApiClient($config); $api_client = new ApiClient($config);
$pet_api = new Swagger\Client\Api\PetApi($api_client); $pet_api = new Api\PetApi($api_client);
// return Pet (model) // return Pet (model)
$response = $pet_api->findPetsByStatus("available"); $response = $pet_api->findPetsByStatus("available");
$this->assertGreaterThan(0, count($response)); // at least one object returned $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 $this->assertSame(get_class($response[0]), "Swagger\\Client\\Model\\Pet"); // verify the object is Pet
// loop through result to ensure status is "available" // loop through result to ensure status is "available"
foreach ($response as $_pet) { foreach ($response as $_pet) {
$this->assertSame($_pet['status'], "available"); $this->assertSame($_pet['status'], "available");
} }
// test invalid status // test invalid status
$response = $pet_api->findPetsByStatus("unknown_and_incorrect_status"); $response = $pet_api->findPetsByStatus("unknown_and_incorrect_status");
$this->assertSame(count($response), 0); // confirm no object returned $this->assertSame(count($response), 0); // confirm no object returned
} }
@ -182,18 +195,18 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
public function testFindPetsByTags() public function testFindPetsByTags()
{ {
// initialize the API client // initialize the API client
$config = (new Swagger\Client\Configuration())->setHost('http://petstore.swagger.io/v2'); $config = (new Configuration())->setHost('http://petstore.swagger.io/v2');
$api_client = new Swagger\Client\ApiClient($config); $api_client = new ApiClient($config);
$pet_api = new Swagger\Client\Api\PetApi($api_client); $pet_api = new Api\PetApi($api_client);
// return Pet (model) // return Pet (model)
$response = $pet_api->findPetsByTags("test php tag"); $response = $pet_api->findPetsByTags("test php tag");
$this->assertGreaterThan(0, count($response)); // at least one object returned $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 $this->assertSame(get_class($response[0]), "Swagger\\Client\\Model\\Pet"); // verify the object is Pet
// loop through result to ensure status is "available" // loop through result to ensure status is "available"
foreach ($response as $_pet) { foreach ($response as $_pet) {
$this->assertSame($_pet['tags'][0]['name'], "test php tag"); $this->assertSame($_pet['tags'][0]['name'], "test php tag");
} }
// test invalid status // test invalid status
$response = $pet_api->findPetsByTags("unknown_and_incorrect_tag"); $response = $pet_api->findPetsByTags("unknown_and_incorrect_tag");
$this->assertSame(count($response), 0); // confirm no object returned $this->assertSame(count($response), 0); // confirm no object returned
} }
@ -202,19 +215,19 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
public function testUpdatePet() public function testUpdatePet()
{ {
// initialize the API client // initialize the API client
$config = (new Swagger\Client\Configuration())->setHost('http://petstore.swagger.io/v2'); $config = (new Configuration())->setHost('http://petstore.swagger.io/v2');
$api_client = new Swagger\Client\ApiClient($config); $api_client = new ApiClient($config);
$pet_id = 10001; // ID of pet that needs to be fetched $pet_id = 10001; // ID of pet that needs to be fetched
$pet_api = new Swagger\Client\Api\PetApi($api_client); $pet_api = new Api\PetApi($api_client);
// create updated pet object // create updated pet object
$updated_pet = new Swagger\Client\Model\Pet; $updated_pet = new Model\Pet;
$updated_pet->setId($pet_id); $updated_pet->setId($pet_id);
$updated_pet->setName('updatePet'); // new name $updated_pet->setName('updatePet'); // new name
$updated_pet->setStatus('pending'); // new status $updated_pet->setStatus('pending'); // new status
// update Pet (model/json) // update Pet (model/json)
$update_response = $pet_api->updatePet($updated_pet); $update_response = $pet_api->updatePet($updated_pet);
// return nothing (void) // return nothing (void)
$this->assertSame($update_response, NULL); $this->assertSame($update_response, null);
// verify updated Pet // verify updated Pet
$response = $pet_api->getPetById($pet_id); $response = $pet_api->getPetById($pet_id);
$this->assertSame($response->getId(), $pet_id); $this->assertSame($response->getId(), $pet_id);
@ -226,12 +239,15 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
public function testUpdatePetWithFormWithHttpInfo() public function testUpdatePetWithFormWithHttpInfo()
{ {
// initialize the API client // initialize the API client
$config = (new Swagger\Client\Configuration())->setHost('http://petstore.swagger.io/v2'); $config = (new Configuration())->setHost('http://petstore.swagger.io/v2');
$api_client = new Swagger\Client\ApiClient($config); $api_client = new ApiClient($config);
$pet_id = 10001; // ID of pet that needs to be fetched $pet_id = 10001; // ID of pet that needs to be fetched
$pet_api = new Swagger\Client\Api\PetApi($api_client); $pet_api = new Api\PetApi($api_client);
// update Pet (form) // update Pet (form)
list($update_response, $status_code, $http_headers) = $pet_api->updatePetWithFormWithHttpInfo($pet_id, 'update pet with form with http info'); list($update_response, $status_code, $http_headers) = $pet_api->updatePetWithFormWithHttpInfo(
$pet_id,
'update pet with form with http info'
);
// return nothing (void) // return nothing (void)
$this->assertNull($update_response); $this->assertNull($update_response);
$this->assertSame($status_code, 200); $this->assertSame($status_code, 200);
@ -245,14 +261,14 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
public function testUpdatePetWithForm() public function testUpdatePetWithForm()
{ {
// initialize the API client // initialize the API client
$config = (new Swagger\Client\Configuration())->setHost('http://petstore.swagger.io/v2'); $config = (new Configuration())->setHost('http://petstore.swagger.io/v2');
$api_client = new Swagger\Client\ApiClient($config); $api_client = new ApiClient($config);
$pet_id = 10001; // ID of pet that needs to be fetched $pet_id = 10001; // ID of pet that needs to be fetched
$pet_api = new Swagger\Client\Api\PetApi($api_client); $pet_api = new Api\PetApi($api_client);
// update Pet (form) // update Pet (form)
$update_response = $pet_api->updatePetWithForm($pet_id, 'update pet with form', 'sold'); $update_response = $pet_api->updatePetWithForm($pet_id, 'update pet with form', 'sold');
// return nothing (void) // return nothing (void)
$this->assertSame($update_response, NULL); $this->assertSame($update_response, null);
$response = $pet_api->getPetById($pet_id); $response = $pet_api->getPetById($pet_id);
$this->assertSame($response->getId(), $pet_id); $this->assertSame($response->getId(), $pet_id);
$this->assertSame($response->getName(), 'update pet with form'); $this->assertSame($response->getName(), 'update pet with form');
@ -263,17 +279,17 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
public function testAddPet() public function testAddPet()
{ {
// initialize the API client // initialize the API client
$config = (new Swagger\Client\Configuration())->setHost('http://petstore.swagger.io/v2'); $config = (new Configuration())->setHost('http://petstore.swagger.io/v2');
$api_client = new Swagger\Client\ApiClient($config); $api_client = new ApiClient($config);
$new_pet_id = 10005; $new_pet_id = 10005;
$new_pet = new Swagger\Client\Model\Pet; $new_pet = new Model\Pet;
$new_pet->setId($new_pet_id); $new_pet->setId($new_pet_id);
$new_pet->setName("PHP Unit Test 2"); $new_pet->setName("PHP Unit Test 2");
$pet_api = new Swagger\Client\Api\PetApi($api_client); $pet_api = new Api\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) // return nothing (void)
$this->assertSame($add_response, NULL); $this->assertSame($add_response, null);
// verify added Pet // verify added Pet
$response = $pet_api->getPetById($new_pet_id); $response = $pet_api->getPetById($new_pet_id);
$this->assertSame($response->getId(), $new_pet_id); $this->assertSame($response->getId(), $new_pet_id);
@ -288,28 +304,28 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
public function testAddPetUsingByteArray() public function testAddPetUsingByteArray()
{ {
// initialize the API client // initialize the API client
$config = (new Swagger\Client\Configuration())->setHost('http://petstore.swagger.io/v2'); $config = (new Configuration())->setHost('http://petstore.swagger.io/v2');
$api_client = new Swagger\Client\ApiClient($config); $api_client = new ApiClient($config);
$new_pet_id = 10005; $new_pet_id = 10005;
$new_pet = new Swagger\Client\Model\Pet; $new_pet = new Model\Pet;
$new_pet->setId($new_pet_id); $new_pet->setId($new_pet_id);
$new_pet->setName("PHP Unit Test 3"); $new_pet->setName("PHP Unit Test 3");
// new tag // new tag
$tag= new Swagger\Client\Model\Tag; $tag= new 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 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($api_client); $pet_api = new Api\PetApi($api_client);
// add a new pet (model) // add a new pet (model)
$object_serializer = new Swagger\Client\ObjectSerializer(); $object_serializer = new ObjectSerializer();
$pet_json_string = json_encode($object_serializer->sanitizeForSerialization($new_pet)); $pet_json_string = json_encode($object_serializer->sanitizeForSerialization($new_pet));
$add_response = $pet_api->addPetUsingByteArray($pet_json_string); $add_response = $pet_api->addPetUsingByteArray($pet_json_string);
// return nothing (void) // return nothing (void)
@ -325,13 +341,13 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
public function testUploadFile() public function testUploadFile()
{ {
// initialize the API client // initialize the API client
$config = (new Swagger\Client\Configuration())->setHost('http://petstore.swagger.io/v2'); $config = (new Configuration())->setHost('http://petstore.swagger.io/v2');
$api_client = new Swagger\Client\ApiClient($config); $api_client = new ApiClient($config);
$pet_api = new Swagger\Client\Api\PetApi($api_client); $pet_api = new Api\PetApi($api_client);
// upload file // upload file
$pet_id = 10001; $pet_id = 10001;
$response = $pet_api->uploadFile($pet_id, "test meta", "./composer.json"); $response = $pet_api->uploadFile($pet_id, "test meta", "./composer.json");
// return ApiResponse // return ApiResponse
$this->assertInstanceOf('Swagger\Client\Model\ApiResponse', $response); $this->assertInstanceOf('Swagger\Client\Model\ApiResponse', $response);
} }
@ -340,10 +356,10 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
public function testGetInventory() public function testGetInventory()
{ {
// initialize the API client // initialize the API client
$config = new Swagger\Client\Configuration(); $config = new Configuration();
$config->setHost('http://petstore.swagger.io/v2'); $config->setHost('http://petstore.swagger.io/v2');
$api_client = new Swagger\Client\APIClient($config); $api_client = new APIClient($config);
$store_api = new Swagger\Client\Api\StoreApi($api_client); $store_api = new Api\StoreApi($api_client);
// get inventory // get inventory
$get_response = $store_api->getInventory(); $get_response = $store_api->getInventory();
@ -359,10 +375,10 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
public function testGetPetByIdWithByteArray() public function testGetPetByIdWithByteArray()
{ {
// initialize the API client // initialize the API client
$config = new Swagger\Client\Configuration(); $config = new Configuration();
$config->setHost('http://petstore.swagger.io/v2'); $config->setHost('http://petstore.swagger.io/v2');
$api_client = new Swagger\Client\APIClient($config); $api_client = new APIClient($config);
$pet_api = new Swagger\Client\Api\PetApi($api_client); $pet_api = new Api\PetApi($api_client);
// test getPetByIdWithByteArray // test getPetByIdWithByteArray
$pet_id = 10005; $pet_id = 10005;
$bytes = $pet_api->petPetIdtestingByteArraytrueGet($pet_id); $bytes = $pet_api->petPetIdtestingByteArraytrueGet($pet_id);
@ -383,7 +399,7 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
// test empty object serialization // test empty object serialization
public function testEmptyPetSerialization() public function testEmptyPetSerialization()
{ {
$new_pet = new Swagger\Client\Model\Pet; $new_pet = new Model\Pet;
// the empty object should be serialised to {} // the empty object should be serialised to {}
$this->assertSame("{}", "$new_pet"); $this->assertSame("{}", "$new_pet");
@ -392,7 +408,7 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
// test inheritance in the model // test inheritance in the model
public function testInheritance() public function testInheritance()
{ {
$new_dog = new Swagger\Client\Model\Dog; $new_dog = new Model\Dog;
// the object should be an instance of the derived class // the object should be an instance of the derived class
$this->assertInstanceOf('Swagger\Client\Model\Dog', $new_dog); $this->assertInstanceOf('Swagger\Client\Model\Dog', $new_dog);
// the object should also be an instance of the parent class // the object should also be an instance of the parent class
@ -408,7 +424,7 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
'class_name' => 'Dog', 'class_name' => 'Dog',
'breed' => 'Great Dane' 'breed' => 'Great Dane'
); );
$new_dog = new Swagger\Client\Model\Dog($data); $new_dog = new Model\Dog($data);
// the property on the derived class should be set // the property on the derived class should be set
$this->assertSame('Great Dane', $new_dog->getBreed()); $this->assertSame('Great Dane', $new_dog->getBreed());
@ -419,7 +435,7 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
// test if discriminator is initialized automatically // test if discriminator is initialized automatically
public function testDiscriminatorInitialization() public function testDiscriminatorInitialization()
{ {
$new_dog = new Swagger\Client\Model\Dog(); $new_dog = new Model\Dog();
$this->assertSame('Dog', $new_dog->getClassName()); $this->assertSame('Dog', $new_dog->getClassName());
} }
@ -428,13 +444,13 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
{ {
// create an AnimalFarm which is an object implementing the // create an AnimalFarm which is an object implementing the
// ArrayAccess interface // ArrayAccess interface
$farm = new Swagger\Client\Model\AnimalFarm(); $farm = new Model\AnimalFarm();
// add some animals to the farm to make sure the ArrayAccess // add some animals to the farm to make sure the ArrayAccess
// interface works // interface works
$farm[] = new Swagger\Client\Model\Dog(); $farm[] = new Model\Dog();
$farm[] = new Swagger\Client\Model\Cat(); $farm[] = new Model\Cat();
$farm[] = new Swagger\Client\Model\Animal(); $farm[] = new Model\Animal();
// assert we can look up the animals in the farm by array // assert we can look up the animals in the farm by array
// indices (let's try a random order) // indices (let's try a random order)
@ -455,16 +471,12 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
{ {
// add some animals to the farm to make sure the ArrayAccess // add some animals to the farm to make sure the ArrayAccess
// interface works // interface works
$dog = new Swagger\Client\Model\Dog(); $dog = new Model\Dog();
$animal = new Swagger\Client\Model\Animal(); $animal = new Model\Animal();
// assert we can look up the animals in the farm by array // assert we can look up the animals in the farm by array
// indices (let's try a random order) // indices (let's try a random order)
$this->assertSame('red', $dog->getColor()); $this->assertSame('red', $dog->getColor());
$this->assertSame('red', $animal->getColor()); $this->assertSame('red', $animal->getColor());
} }
} }
?>

View File

@ -1,34 +1,35 @@
<?php <?php
require_once('autoload.php'); namespace Swagger\Client;
class StoreApiTest extends \PHPUnit_Framework_TestCase class StoreApiTest 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);
// new pet // new pet
$new_pet_id = 10005; $new_pet_id = 10005;
$new_pet = new Swagger\Client\Model\Pet; $new_pet = new 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_pet->setStatus("available"); $new_pet->setStatus("available");
// new tag // new tag
$tag= new Swagger\Client\Model\Tag; $tag= new 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 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 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);
} }
@ -37,9 +38,9 @@ class StoreApiTest extends \PHPUnit_Framework_TestCase
public function testGetInventory() public function testGetInventory()
{ {
// initialize the API client // initialize the API client
$config = (new Swagger\Client\Configuration())->setHost('http://petstore.swagger.io/v2'); $config = (new Configuration())->setHost('http://petstore.swagger.io/v2');
$api_client = new Swagger\Client\ApiClient($config); $api_client = new ApiClient($config);
$store_api = new Swagger\Client\Api\StoreApi($api_client); $store_api = new Api\StoreApi($api_client);
// get inventory // get inventory
$get_response = $store_api->getInventory(); $get_response = $store_api->getInventory();
@ -55,9 +56,9 @@ class StoreApiTest extends \PHPUnit_Framework_TestCase
public function testGetInventoryInObject() public function testGetInventoryInObject()
{ {
// initialize the API client // initialize the API client
//$config = (new Swagger\Client\Configuration())->setHost('http://petstore.swagger.io/v2'); //$config = (new Configuration())->setHost('http://petstore.swagger.io/v2');
$api_client = new Swagger\Client\ApiClient(); $api_client = new ApiClient();
$store_api = new Swagger\Client\Api\StoreApi($api_client); $store_api = new Api\StoreApi($api_client);
// get inventory // get inventory
$get_response = $store_api->getInventoryInObject(); $get_response = $store_api->getInventoryInObject();
@ -65,8 +66,4 @@ class StoreApiTest extends \PHPUnit_Framework_TestCase
$this->assertInternalType("int", $get_response['available']); $this->assertInternalType("int", $get_response['available']);
} }
*/ */
} }
?>

View File

@ -1,33 +1,34 @@
<?php <?php
require_once('autoload.php'); namespace Swagger\Client;
class UserApiTest extends \PHPUnit_Framework_TestCase class UserApiTest 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) {
//ini_set('display_errors', 1); // for error reporting (need to run with php5.3 to get no warning)
//error_reporting(~0); //ini_set('display_errors', 1);
} //error_reporting(~0);
}
// test login user // test login user
public function testLoginUser() public function testLoginUser()
{ {
// initialize the API client // initialize the API client
$config = (new Swagger\Client\Configuration())->setHost('http://petstore.swagger.io/v2'); $config = (new Configuration())->setHost('http://petstore.swagger.io/v2');
$api_client = new Swagger\Client\ApiClient($config); $api_client = new ApiClient($config);
$user_api = new Swagger\Client\Api\UserApi($api_client); $user_api = new Api\UserApi($api_client);
// login // login
$response = $user_api->loginUser("xxxxx", "yyyyyyyy"); $response = $user_api->loginUser("xxxxx", "yyyyyyyy");
$this->assertInternalType("string", $response); $this->assertInternalType("string", $response);
$this->assertRegExp("/^logged in user session/", $response, "response string starts with 'logged in user session'"); $this->assertRegExp(
"/^logged in user session/",
} $response,
"response string starts with 'logged in user session'"
);
}
} }
?>