From 4c0cd15a40003c36de3cb8bfd5def7455e51c6cc Mon Sep 17 00:00:00 2001 From: Russell Horton Date: Sun, 9 Sep 2012 09:01:42 -0700 Subject: [PATCH] updated and restructed PHP petstore sample app --- .../petstore/php/{ => petstore}/PetApi.php | 0 .../petstore/php/{ => petstore}/StoreApi.php | 0 .../petstore/php/{ => petstore}/Swagger.php | 6 + .../petstore/php/{ => petstore}/UserApi.php | 0 .../php/{ => petstore}/models/Category.php | 4 +- .../php/{ => petstore}/models/Order.php | 12 +- .../php/{ => petstore}/models/Pet.php | 4 +- .../php/{ => petstore}/models/Tag.php | 4 +- .../php/{ => petstore}/models/User.php | 4 +- samples/petstore/php/tests/BaseApiTest.php | 50 ++++ samples/petstore/php/tests/PetApiTest.php | 123 ++++++++++ samples/petstore/php/tests/StoreApiTest.php | 42 ++++ samples/petstore/php/tests/UserApiTest.php | 220 ++++++++++++++++++ 13 files changed, 455 insertions(+), 14 deletions(-) rename samples/petstore/php/{ => petstore}/PetApi.php (100%) rename samples/petstore/php/{ => petstore}/StoreApi.php (100%) rename samples/petstore/php/{ => petstore}/Swagger.php (97%) rename samples/petstore/php/{ => petstore}/UserApi.php (100%) rename samples/petstore/php/{ => petstore}/models/Category.php (95%) rename samples/petstore/php/{ => petstore}/models/Order.php (86%) rename samples/petstore/php/{ => petstore}/models/Pet.php (96%) rename samples/petstore/php/{ => petstore}/models/Tag.php (95%) rename samples/petstore/php/{ => petstore}/models/User.php (96%) create mode 100644 samples/petstore/php/tests/BaseApiTest.php create mode 100644 samples/petstore/php/tests/PetApiTest.php create mode 100644 samples/petstore/php/tests/StoreApiTest.php create mode 100644 samples/petstore/php/tests/UserApiTest.php diff --git a/samples/petstore/php/PetApi.php b/samples/petstore/php/petstore/PetApi.php similarity index 100% rename from samples/petstore/php/PetApi.php rename to samples/petstore/php/petstore/PetApi.php diff --git a/samples/petstore/php/StoreApi.php b/samples/petstore/php/petstore/StoreApi.php similarity index 100% rename from samples/petstore/php/StoreApi.php rename to samples/petstore/php/petstore/StoreApi.php diff --git a/samples/petstore/php/Swagger.php b/samples/petstore/php/petstore/Swagger.php similarity index 97% rename from samples/petstore/php/Swagger.php rename to samples/petstore/php/petstore/Swagger.php index 4b07e768af9..e314eeeec7f 100644 --- a/samples/petstore/php/Swagger.php +++ b/samples/petstore/php/petstore/Swagger.php @@ -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; diff --git a/samples/petstore/php/UserApi.php b/samples/petstore/php/petstore/UserApi.php similarity index 100% rename from samples/petstore/php/UserApi.php rename to samples/petstore/php/petstore/UserApi.php diff --git a/samples/petstore/php/models/Category.php b/samples/petstore/php/petstore/models/Category.php similarity index 95% rename from samples/petstore/php/models/Category.php rename to samples/petstore/php/petstore/models/Category.php index 0eaeb2d1f0d..adb99943fa3 100644 --- a/samples/petstore/php/models/Category.php +++ b/samples/petstore/php/petstore/models/Category.php @@ -24,12 +24,12 @@ class Category { static $swaggerTypes = array( - 'id' => 'long', + 'id' => 'int', 'name' => 'string' ); - public $id; // long + public $id; // int public $name; // string } diff --git a/samples/petstore/php/models/Order.php b/samples/petstore/php/petstore/models/Order.php similarity index 86% rename from samples/petstore/php/models/Order.php rename to samples/petstore/php/petstore/models/Order.php index 3db9dcb7a59..71f45865f5f 100644 --- a/samples/petstore/php/models/Order.php +++ b/samples/petstore/php/petstore/models/Order.php @@ -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 } diff --git a/samples/petstore/php/models/Pet.php b/samples/petstore/php/petstore/models/Pet.php similarity index 96% rename from samples/petstore/php/models/Pet.php rename to samples/petstore/php/petstore/models/Pet.php index e6d737c9e10..652c84f9040 100644 --- a/samples/petstore/php/models/Pet.php +++ b/samples/petstore/php/petstore/models/Pet.php @@ -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 /** diff --git a/samples/petstore/php/models/Tag.php b/samples/petstore/php/petstore/models/Tag.php similarity index 95% rename from samples/petstore/php/models/Tag.php rename to samples/petstore/php/petstore/models/Tag.php index 661d8bc2bb3..8550685e45e 100644 --- a/samples/petstore/php/models/Tag.php +++ b/samples/petstore/php/petstore/models/Tag.php @@ -24,12 +24,12 @@ class Tag { static $swaggerTypes = array( - 'id' => 'long', + 'id' => 'int', 'name' => 'string' ); - public $id; // long + public $id; // int public $name; // string } diff --git a/samples/petstore/php/models/User.php b/samples/petstore/php/petstore/models/User.php similarity index 96% rename from samples/petstore/php/models/User.php rename to samples/petstore/php/petstore/models/User.php index a1d9d7f6f87..f630388ac91 100644 --- a/samples/petstore/php/models/User.php +++ b/samples/petstore/php/petstore/models/User.php @@ -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 diff --git a/samples/petstore/php/tests/BaseApiTest.php b/samples/petstore/php/tests/BaseApiTest.php new file mode 100644 index 00000000000..ba9cc4d01c1 --- /dev/null +++ b/samples/petstore/php/tests/BaseApiTest.php @@ -0,0 +1,50 @@ +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); + } + +} + + +?> \ No newline at end of file diff --git a/samples/petstore/php/tests/PetApiTest.php b/samples/petstore/php/tests/PetApiTest.php new file mode 100644 index 00000000000..2d3b3ec130e --- /dev/null +++ b/samples/petstore/php/tests/PetApiTest.php @@ -0,0 +1,123 @@ +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); + } + + + + +} +?> \ No newline at end of file diff --git a/samples/petstore/php/tests/StoreApiTest.php b/samples/petstore/php/tests/StoreApiTest.php new file mode 100644 index 00000000000..a1db91de9d4 --- /dev/null +++ b/samples/petstore/php/tests/StoreApiTest.php @@ -0,0 +1,42 @@ +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); + } + +} +?> \ No newline at end of file diff --git a/samples/petstore/php/tests/UserApiTest.php b/samples/petstore/php/tests/UserApiTest.php new file mode 100644 index 00000000000..d9a454b34b2 --- /dev/null +++ b/samples/petstore/php/tests/UserApiTest.php @@ -0,0 +1,220 @@ +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); + + } + +} +?> \ No newline at end of file