Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PHP] Add support for async requests #5771

Merged

Conversation

ackintosh
Copy link
Contributor

@ackintosh ackintosh commented Jun 4, 2017

closes #5306

PR checklist

  • Read the contribution guidelines.
  • Ran the shell/batch script under ./bin/ to update Petstore sample so that CIs can verify the change. (For instance, only need to run ./bin/{LANG}-petstore.sh and ./bin/security/{LANG}-petstore.sh if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates)
  • Filed the PR against the correct branch: master for non-breaking changes and 2.3.0 branch for breaking (non-backward compatible) changes.

Description of the PR

(details of the change, additional tests that have been done, reference to the issue for tracking, etc)

Async requests

  • xxxAsync
  • xxxAsyncWithHttpInfo

Usage

$promise = $api->getPeByIdtAsyncWithHttpInfo(1)->then(function ($response) {
    var_dump($response);
    // array(3) {
    //   [0] =>
    //   class Swagger\Client\Model\Pet
    //   [1] =>
    //   int(200)
    //   [2] =>
    //   array(7) {
    //     'Date' =>
    // ...
}, function ($exception) {
    var_dump($exception);
    // class Swagger\Client\ApiException
});

var_dump($promise);
// class GuzzleHttp\Promise\Promise

$promise->wait();

@ackintosh ackintosh changed the title WIP: [PHP] Add support for async requests [PHP] Add support for async requests Jun 4, 2017
@wing328 wing328 modified the milestones: v2.3.0, v2.2.3 Jun 4, 2017
@wing328
Copy link
Contributor

wing328 commented Jun 5, 2017

@ackintosh thanks for the enhancement.

cc @baartosz @dkarlovi @arnested

Copy link
Contributor

@baartosz baartosz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good and works fine. Exceptions are working as expected.

Please just tweak @return type annotations.

*
* @param string $test_code_inject____end____rn_n_r To test code injection *_/ ' \" =end -- \\r\\n \\n \\r (optional)
* @throws \InvalidArgumentException
* @return void
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PromiseInterface is returned here, annotation is wrong

*
* @param string $test_code_inject____end____rn_n_r To test code injection *_/ ' \" =end -- \\r\\n \\n \\r (optional)
* @throws \InvalidArgumentException
* @return \GuzzleHttp\Promise\Promise
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it returns \GuzzleHttp\Promise\PromiseInterface

@baartosz
Copy link
Contributor

baartosz commented Jun 6, 2017

@wing328 tag 2.2.3 is probably wrong as this branch depends on guzzle which is in 2.3.0 only

@wing328
Copy link
Contributor

wing328 commented Jun 6, 2017

@baartosz already labelled as 2.3.0. Can you check again?

@baartosz
Copy link
Contributor

baartosz commented Jun 6, 2017

@wing328 Sorry, I was thinking about milestone but wrote tag instead:
@wing328 wing328 modified the milestone: v2.3.0, v2.2.3 2 days ago
it looks like mistake for me.

@wing328
Copy link
Contributor

wing328 commented Jun 6, 2017

@baartosz no worry and thanks for reviewing the change.

@ackintosh
Copy link
Contributor Author

@baartosz Thanks for your comment ✨ I fixed it.

@baartosz
Copy link
Contributor

baartosz commented Jun 7, 2017

@ackintosh I wrote some tests for this feature yesterday, please add them to this PR. Or I'll create another PR later.

samples/client/petstore/php/SwaggerClient-php/tests/AsyncTest.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();
    }
}

@wing328 wing328 merged commit 4612c12 into swagger-api:2.3.0 Jun 7, 2017
@wing328
Copy link
Contributor

wing328 commented Jun 7, 2017

@ackintosh thanks for the PR, which has been merged into master.

@baartosz thanks for reviewing the change and adding the test cases to cover the change.

@ackintosh ackintosh deleted the php-add-support-for-async-requests branch June 7, 2017 12:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants