updated and restructed PHP petstore sample app

This commit is contained in:
Russell Horton 2012-09-09 09:01:42 -07:00
parent a38fc6eb06
commit 4c0cd15a40
13 changed files with 455 additions and 14 deletions

View File

@ -148,6 +148,10 @@ class APIClient {
*/
public static function deserialize($object, $class) {
if (gettype($object) == "NULL") {
return $object;
}
if (substr($class, 0, 6) == 'array[') {
$sub_class = substr($class, 6, -1);
$sub_objects = array();
@ -156,6 +160,8 @@ class APIClient {
$sub_class);
}
return $sub_objects;
} elseif ($class == 'DateTime') {
return new DateTime($object);
} elseif (in_array($class, array('string', 'int', 'float', 'bool'))) {
settype($object, $class);
return $object;

View File

@ -24,12 +24,12 @@
class Category {
static $swaggerTypes = array(
'id' => 'long',
'id' => 'int',
'name' => 'string'
);
public $id; // long
public $id; // int
public $name; // string
}

View File

@ -24,21 +24,21 @@
class Order {
static $swaggerTypes = array(
'id' => 'long',
'petId' => 'long',
'id' => 'int',
'petId' => 'int',
'status' => 'string',
'quantity' => 'int',
'shipDate' => 'string'
'shipDate' => 'DateTime'
);
public $id; // long
public $petId; // long
public $id; // int
public $petId; // int
/**
* Order Status
*/
public $status; // string
public $quantity; // int
public $shipDate; // string
public $shipDate; // DateTime
}

View File

@ -24,7 +24,7 @@
class Pet {
static $swaggerTypes = array(
'id' => 'long',
'id' => 'int',
'tags' => 'array[Tag]',
'category' => 'Category',
'status' => 'string',
@ -33,7 +33,7 @@ class Pet {
);
public $id; // long
public $id; // int
public $tags; // array[Tag]
public $category; // Category
/**

View File

@ -24,12 +24,12 @@
class Tag {
static $swaggerTypes = array(
'id' => 'long',
'id' => 'int',
'name' => 'string'
);
public $id; // long
public $id; // int
public $name; // string
}

View File

@ -24,7 +24,7 @@
class User {
static $swaggerTypes = array(
'id' => 'long',
'id' => 'int',
'lastName' => 'string',
'username' => 'string',
'phone' => 'string',
@ -35,7 +35,7 @@ class User {
);
public $id; // long
public $id; // int
public $lastName; // string
public $username; // string
public $phone; // string

View File

@ -0,0 +1,50 @@
<?php
// Unit tests for Swagger Petsore sample app PHP client
// Run all tests:
//
// phpunit tests/PetApiTest.php
// phpunit tests/StoreApiTest.php
// phpunit tests/UserApiTest.php
// If you require PHPUnit to be installed, first get PEAR:
//
// $ wget http://pear.php.net/go-pear.phar
// $ php -d detect_unicode=0 go-pear.phar
//
// Then install PHPUnit:
//
// $ pear config-set auto_discover
// $ pear install pear.phpunit.de/PHPUnit
// If you want to see all the JSON, make sure you append the fake api-key:
// http://petstore.swagger.wordnik.com/api/pet.json?api_key=special-key
// You need to set this for your local time zone if not already set in
// global config.
date_default_timezone_set("America/Los_Angeles");
require_once 'petstore/Swagger.php';
class BaseApiTest extends PHPUnit_Framework_TestCase {
public function setUp() {
$this->apiKey = "special-key";
$this->apiUrl = "http://petstore.swagger.wordnik.com/api";
$this->username = "test";
$this->password = "test";
$this->client = new APIClient($this->apiKey, $this->apiUrl);
$this->petApi = new PetApi($this->client);
$this->storeApi = new StoreApi($this->client);
$this->userApi = new UserApi($this->client);
}
public function tearDown() {
unset($this->client);
}
}
?>

View File

@ -0,0 +1,123 @@
<?php
require_once 'BaseApiTest.php';
class PetApiTest extends BaseApiTest {
// Choose a random id for new pet insertion
public static $randomId;
public static function setUpBeforeClass() {
self::$randomId = rand(10000, 100000);
print "Pet id is " . self::$randomId . "\n\n";
}
public function testPetApis() {
$ch = curl_init("http://petstore.swagger.wordnik.com/api/pet.json");
if (! $ch) {
die("No php curl handle");
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
$doc = json_decode($data);
$this->assertEquals(3, count($doc->apis));
}
public function testPetApisWithKey() {
$ch = curl_init("http://petstore.swagger.wordnik.com/api/pet.json?api_key=special-key");
if (! $ch) {
die("No php curl handle");
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
$doc = json_decode($data);
$this->assertEquals(4, count($doc->apis));
}
public function testGetPetById() {
$res = $this->petApi->getPetById(1);
$this->assertEquals($res->id, 1);
}
public function testAddPet() {
$pet = new Pet();
$pet->id = self::$randomId;
$tag1 = new Tag();
$tag1->name = "tag1";
$tag2 = new Tag();
$tag2->name = "some tag";
$pet->tags = array($tag1, $tag2);
$category = new Category();
$category->name = "Cats";
$pet->category = $category;
$pet->status = "sold";
$pet->name = "Shermie";
$pet->photoUrls = array("http://foo.com/1.jpg", "http://foo.com/1.jpg");
$res = $this->petApi->addPet($pet);
$new_pet = $this->petApi->getPetById($pet->id);
$this->assertEquals($new_pet->id, $pet->id);
$this->assertEquals($new_pet->name, $pet->name);
$this->assertEquals($new_pet->tags, $pet->tags);
$this->assertEquals($new_pet->status, $pet->status);
$this->assertEquals($new_pet->category, $pet->category);
$this->assertEquals($new_pet->photoUrls, $pet->photoUrls);
}
public function testUpdatePet() {
$pet = new Pet();
$pet->id = self::$randomId;
$tag1 = new Tag();
$tag1->name = rand(10000, 100000);
$tag2 = new Tag();
$tag2->name = "special-tag";
$pet->tags = array($tag1, $tag2);
$category = new Category();
$category->name = rand(10000, 100000);
$pet->category = $category;
$pet->status = "sold";
$pet->name = rand(10000, 100000);
$pet->photoUrls = array(rand(10000, 100000), rand(10000, 100000));
$res = $this->petApi->updatePet($pet);
$updated_pet = $this->petApi->getPetById($pet->id);
$this->assertEquals($updated_pet->id, $pet->id);
$this->assertEquals($updated_pet->name, $pet->name);
$this->assertEquals($updated_pet->tags, $pet->tags);
$this->assertEquals($updated_pet->status, $pet->status);
$this->assertEquals($updated_pet->category, $pet->category);
$this->assertEquals($updated_pet->photoUrls, $pet->photoUrls); }
public function testFindPetsByTags() {
$res = $this->petApi->findPetsByTags("special-tag");
$tag_found = false;
foreach ($res as $found_pet) {
if ($found_pet->id == self::$randomId) {
$tag_found = true;
}
}
$this->assertEquals(true, $tag_found);
}
public function testFindPetsByStatus() {
$res = $this->petApi->findPetsByStatus("sold");
$tag_found = false;
foreach ($res as $found_pet) {
if ($found_pet->id == self::$randomId) {
$tag_found = true;
}
}
$this->assertEquals(true, $tag_found);
}
}
?>

View File

@ -0,0 +1,42 @@
<?php
require_once 'BaseApiTest.php';
class StoreApiTest extends BaseApiTest {
public function testGetOrderById() {
$res = $this->storeApi->getOrderById(1);
$this->assertEquals(1, $res->petId);
$this->assertEquals('DateTime', get_class($res->shipDate));
}
public function testDeleteOrder() {
$res = $this->storeApi->deleteOrder(3);
$res = $this->storeApi->deleteOrder("foo");
// We just want to make sure there are no errors in this test.
// To verify you are getting back a 200, you might want to add
// something like this to Swagger.php callAPI():
// print "Response for call to $resourcePath : ";
// print_r($data);
}
public function testPlaceOrder() {
$order = new Order();
$order->petId = 1;
$order->status = "ordered";
$order->quantity = 10;
// $order->shipDate = "1/1/2013";
$res = $this->storeApi->placeOrder($order);
// We just want to make sure there are no errors in this test.
// To verify you are getting back a 200, you might want to add
// something like this to Swagger.php callAPI():
// print "Response for call to $resourcePath : ";
// print_r($data);
}
}
?>

View File

@ -0,0 +1,220 @@
<?php
require_once 'BaseApiTest.php';
class PetApiTest extends BaseApiTest {
// Choose a random username for new pet insertion
public static $randomUsername1;
public static $randomUsername2;
public static $randomUsername3;
public static $randomUsername4;
public static $randomUsername5;
public static function setUpBeforeClass() {
$letters = 'ABCDEFGHIJKLMNOPQRSTUVXYZ';
$letter_arr = str_split($letters);
shuffle($letter_arr);
self::$randomUsername1 = implode('', $letter_arr);
print "Username1 is " . self::$randomUsername1 . "\n\n";
shuffle($letter_arr);
self::$randomUsername2 = implode('', $letter_arr);
print "Username2 is " . self::$randomUsername2 . "\n\n";
shuffle($letter_arr);
self::$randomUsername3 = implode('', $letter_arr);
print "Username3 is " . self::$randomUsername3 . "\n\n";
shuffle($letter_arr);
self::$randomUsername4 = implode('', $letter_arr);
print "Username4 is " . self::$randomUsername4 . "\n\n";
shuffle($letter_arr);
self::$randomUsername5 = implode('', $letter_arr);
print "Username5 is " . self::$randomUsername5 . "\n\n";
}
public function testCreateUsersWithArrayInput() {
$user = new User();
$user->id = rand(10000, 100000);
$user->lastName = rand(10000, 100000);
$user->username = self::$randomUsername1;
$user->phone = rand(10000, 100000);
$user->email = rand(10000, 100000);
$user->userStatus = rand(10000, 100000);
$user->firstName = rand(10000, 100000);
$user->password = rand(10000, 100000);
$otherUser = new User();
$otherUser->id = rand(10000, 100000);
$otherUser->lastName = rand(10000, 100000);
$otherUser->username = self::$randomUsername2;
$otherUser->phone = rand(10000, 100000);
$otherUser->email = rand(10000, 100000);
$otherUser->userStatus = rand(10000, 100000);
$otherUser->firstName = rand(10000, 100000);
$otherUser->password = rand(10000, 100000);
$users = array($user, $otherUser);
$res = $this->userApi->createUsersWithArrayInput($users);
$new_user = $this->userApi->getUserByName(self::$randomUsername1);
$this->assertEquals($new_user->id, $user->id);
$this->assertEquals($new_user->lastName, $user->lastName);
$this->assertEquals($new_user->username, $user->username);
$this->assertEquals($new_user->phone, $user->phone);
$this->assertEquals($new_user->email, $user->email);
$this->assertEquals($new_user->userStatus, $user->userStatus);
$this->assertEquals($new_user->firstName, $user->firstName);
$this->assertEquals($new_user->password, $user->password);
$new_user = $this->userApi->getUserByName(self::$randomUsername2);
$this->assertEquals($new_user->id, $otherUser->id);
$this->assertEquals($new_user->lastName, $otherUser->lastName);
$this->assertEquals($new_user->username, $otherUser->username);
$this->assertEquals($new_user->phone, $otherUser->phone);
$this->assertEquals($new_user->email, $otherUser->email);
$this->assertEquals($new_user->userStatus, $otherUser->userStatus);
$this->assertEquals($new_user->firstName, $otherUser->firstName);
$this->assertEquals($new_user->password, $otherUser->password);
}
public function testCreateUsersWithListInput() {
$user = new User();
$user->id = rand(10000, 100000);
$user->lastName = rand(10000, 100000);
$user->username = self::$randomUsername4;
$user->phone = rand(10000, 100000);
$user->email = rand(10000, 100000);
$user->userStatus = rand(10000, 100000);
$user->firstName = rand(10000, 100000);
$user->password = rand(10000, 100000);
$otherUser = new User();
$otherUser->id = rand(10000, 100000);
$otherUser->lastName = rand(10000, 100000);
$otherUser->username = self::$randomUsername5;
$otherUser->phone = rand(10000, 100000);
$otherUser->email = rand(10000, 100000);
$otherUser->userStatus = rand(10000, 100000);
$otherUser->firstName = rand(10000, 100000);
$otherUser->password = rand(10000, 100000);
$users = array($user, $otherUser);
$res = $this->userApi->createUsersWithArrayInput($users);
$new_user = $this->userApi->getUserByName(self::$randomUsername4);
$this->assertEquals($new_user->id, $user->id);
$this->assertEquals($new_user->lastName, $user->lastName);
$this->assertEquals($new_user->username, $user->username);
$this->assertEquals($new_user->phone, $user->phone);
$this->assertEquals($new_user->email, $user->email);
$this->assertEquals($new_user->userStatus, $user->userStatus);
$this->assertEquals($new_user->firstName, $user->firstName);
$this->assertEquals($new_user->password, $user->password);
$new_user = $this->userApi->getUserByName(self::$randomUsername5);
$this->assertEquals($new_user->id, $otherUser->id);
$this->assertEquals($new_user->lastName, $otherUser->lastName);
$this->assertEquals($new_user->username, $otherUser->username);
$this->assertEquals($new_user->phone, $otherUser->phone);
$this->assertEquals($new_user->email, $otherUser->email);
$this->assertEquals($new_user->userStatus, $otherUser->userStatus);
$this->assertEquals($new_user->firstName, $otherUser->firstName);
$this->assertEquals($new_user->password, $otherUser->password);
}
public function testCreateUser() {
$user = new User();
$user->id = rand(10000, 100000);
$user->lastName = rand(10000, 100000);
$user->username = self::$randomUsername3;
$user->phone = rand(10000, 100000);
$user->email = rand(10000, 100000);
$user->userStatus = rand(10000, 100000);
$user->firstName = rand(10000, 100000);
$user->password = rand(10000, 100000);
$res = $this->userApi->createUser($user);
$new_user = $this->userApi->getUserByName(self::$randomUsername3);
$this->assertEquals($new_user->id, $user->id);
$this->assertEquals($new_user->lastName, $user->lastName);
$this->assertEquals($new_user->username, $user->username);
$this->assertEquals($new_user->phone, $user->phone);
$this->assertEquals($new_user->email, $user->email);
$this->assertEquals($new_user->userStatus, $user->userStatus);
$this->assertEquals($new_user->firstName, $user->firstName);
$this->assertEquals($new_user->password, $user->password);
}
public function testUpdateUser() {
$user = $this->userApi->getUserByName(self::$randomUsername1);
$user->lastName = rand(10000, 100000);
$user->phone = rand(10000, 100000);
$user->email = rand(10000, 100000);
$user->userStatus = rand(10000, 100000);
$user->firstName = rand(10000, 100000);
$user->password = rand(10000, 100000);
$res = $this->userApi->updateUser(self::$randomUsername1, $user);
$updated_user = $this->userApi->getUserByName(self::$randomUsername1);
$this->assertEquals($updated_user->lastName, $user->lastName);
$this->assertEquals($updated_user->username, $user->username);
$this->assertEquals($updated_user->phone, $user->phone);
$this->assertEquals($updated_user->email, $user->email);
$this->assertEquals($updated_user->userStatus, $user->userStatus);
$this->assertEquals($updated_user->firstName, $user->firstName);
$this->assertEquals($updated_user->password, $user->password);
}
public function testDeleteUser() {
$res = $this->userApi->deleteUser(self::$randomUsername1);
$deleted_user = $this->userApi->getUserByName(self::$randomUsername1);
$this->assertEquals($deleted_user, null);
}
public function testLoginUser() {
$res = $this->userApi->loginUser("anyusername", "anypassword");
$this->assertEquals(substr($res, 0, 23), "logged in user session:");
}
public function testLogoutUser() {
$res = $this->userApi->logoutUser();
// We just want to make sure there are no errors in this test.
// To verify you are getting back a 200, you might want to add
// something like this to Swagger.php callAPI():
// print "Response for call to $resourcePath : ";
// print_r($data);
}
}
?>