Akihito Nakano 4612c12560 [PHP] Add support for async requests (#5771)
* Add xxxAsyncWithHttpInfo

* Add xxxAsync

* Tweak doc comment

* apply DRY principle to the creation process for request

* Fix undefined variable $statusCode

* error handling

* Fix undefined variable $url

* Tweak syntax

* Update samples

run './bin/php-petstore.sh'

* Update samples (security)

run './bin/security/php-petstore.sh'

* Fix doc comment

xxx(Async|AsyncWithHttpInfo) returns \GuzzleHttp\Promise\PromiseInterface

* Update samples (./bin/php-petstore.sh)

* Update samples (./bin/security/php-petstore.sh)

* Add AsyncTest
2017-06-07 20:35:28 +08:00

86 lines
2.2 KiB
PHP

<?php
namespace Swagger\Client;
use Swagger\Client\Api\PetApi;
use Swagger\Client\Model\Pet;
class AsyncTest extends \PHPUnit_Framework_TestCase
{
/** @var PetApi */
private $api;
/** @var int */
private $petId;
public function setUp()
{
$this->api = new Api\PetApi();
$this->petId = 10005;
$pet = new Model\Pet;
$pet->setId($this->petId);
$pet->setName("PHP Unit Test");
$pet->setPhotoUrls(array("http://test_php_unit_test.com"));
// new tag
$tag= new Model\Tag;
$tag->setId($this->petId); // use the same id as pet
$tag->setName("test php tag");
// new category
$category = new Model\Category;
$category->setId($this->petId); // use the same id as pet
$category->setName("test php category");
$pet->setTags(array($tag));
$pet->setCategory($category);
$pet_api = new Api\PetApi();
// add a new pet (model)
$add_response = $pet_api->addPet($pet);
}
public function testAsyncRequest()
{
$promise = $this->api->getPetByIdAsync(10005);
$promise2 = $this->api->getPetByIdAsync(10005);
$pet = $promise->wait();
$pet2 = $promise2->wait();
$this->assertInstanceOf(Pet::class, $pet);
$this->assertInstanceOf(Pet::class, $pet2);
}
public function testAsyncRequestWithHttpInfo()
{
$promise = $this->api->getPetByIdAsyncWithHttpInfo($this->petId);
list($pet, $status, $headers) = $promise->wait();
$this->assertEquals(200, $status);
$this->assertInternalType('array', $headers);
$this->assertInstanceOf(Pet::class, $pet);
}
public function testAsyncThrowingException()
{
$this->setExpectedException(ApiException::class);
$promise = $this->api->getPetByIdAsync(0);
$promise->wait();
}
public function testAsyncApiExceptionWithoutWaitIsNotThrown()
{
$promise = $this->api->getPetByIdAsync(0);
sleep(1);
}
public function testAsyncHttpInfoThrowingException()
{
$this->setExpectedException(ApiException::class);
$promise = $this->api->getPetByIdAsyncWithHttpInfo(0);
$promise->wait();
}
}