Test PHP clients in Github workflow (#21643)

* test php clients in github workflow

* trigger build failure

* update workflow

* Revert "trigger build failure"

This reverts commit 572a69f6bcbab426103c77de55c069bd74b5e7fb.

* update tests

* update tests

* update test

* fix tests

* fix async test

* update tests
This commit is contained in:
William Cheng 2025-07-28 18:33:52 +08:00 committed by GitHub
parent a60d3d4f81
commit 3453c7ba10
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 116 additions and 20 deletions

View File

@ -0,0 +1,45 @@
name: Samples PHP clients
on:
push:
paths:
- samples/client/petstore/php/OpenAPIClient-php/**
pull_request:
paths:
- samples/client/petstore/php/OpenAPIClient-php/**
jobs:
build:
name: Build PHP projects
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php:
- "8.1"
- "8.2"
- "8.3"
- "8.4"
sample:
# clients
- samples/client/petstore/php/OpenAPIClient-php/
services:
petstore-api:
image: swaggerapi/petstore
ports:
- 80:8080
env:
SWAGGER_HOST: http://petstore.swagger.io
SWAGGER_BASE_PATH: /v2
steps:
- uses: actions/checkout@v4
- name: Setup PHP with tools
uses: shivammathur/setup-php@v2
with:
php-version: "${{ matrix.php }}"
tools: php-cs-fixer, phpunit
- name: composer install
working-directory: ${{ matrix.sample }}
run: composer install
- name: phpunit
working-directory: ${{ matrix.sample }}
run: vendor/bin/phpunit tests

View File

@ -31,6 +31,7 @@ paths:
$ref: '#/components/schemas/Foo'
/pet:
servers:
- url: 'http://localhost/v2'
- url: 'http://petstore.swagger.io/v2'
- url: 'http://path-server-test.petstore.local/v2'
- url: 'http://{server}.swagger.io:{port}/v2'

View File

@ -31,6 +31,7 @@ paths:
$ref: '#/components/schemas/Foo'
/pet:
servers:
- url: 'http://localhost/v2'
- url: 'http://petstore.swagger.io/v2'
- url: 'http://path-server-test.petstore.local/v2'
- url: 'http://{server}.swagger.io:{port}/v2'

View File

@ -21,7 +21,7 @@ All URIs are relative to http://petstore.swagger.io:80/v2, except if the operati
addPet($pet)
```
### URI(s):
- http://petstore.swagger.io/v2 - http://path-server-test.petstore.local/v2 - http://{server}.swagger.io:{port}/v2 test server with variables
- http://localhost/v2 - http://petstore.swagger.io/v2 - http://path-server-test.petstore.local/v2 - http://{server}.swagger.io:{port}/v2 test server with variables
- Variables:
- server: target server
- Allowed values:
@ -80,6 +80,7 @@ try {
### Return type
void (empty response body)
@ -346,7 +347,7 @@ try {
updatePet($pet)
```
### URI(s):
- http://petstore.swagger.io/v2 - http://path-server-test.petstore.local/v2 - http://{server}.swagger.io:{port}/v2 test server with variables
- http://localhost/v2 - http://petstore.swagger.io/v2 - http://path-server-test.petstore.local/v2 - http://{server}.swagger.io:{port}/v2 test server with variables
- Variables:
- server: target server
- Allowed values:
@ -405,6 +406,7 @@ try {
### Return type
void (empty response body)

View File

@ -158,6 +158,7 @@ class PetApi
*
* This operation contains host(s) defined in the OpenAPI spec. Use 'hostIndex' to select the host.
* if needed, use the 'variables' parameter to pass variables to the host.
* URL: http://localhost/v2
* URL: http://petstore.swagger.io/v2
* URL: http://path-server-test.petstore.local/v2
* URL: http://{server}.swagger.io:{port}/v2
@ -198,6 +199,7 @@ class PetApi
*
* This operation contains host(s) defined in the OpenAPI spec. Use 'hostIndex' to select the host.
* if needed, use the 'variables' parameter to pass variables to the host.
* URL: http://localhost/v2
* URL: http://petstore.swagger.io/v2
* URL: http://path-server-test.petstore.local/v2
* URL: http://{server}.swagger.io:{port}/v2
@ -269,6 +271,7 @@ class PetApi
*
* This operation contains host(s) defined in the OpenAPI spec. Use 'hostIndex' to select the host.
* if needed, use the 'variables' parameter to pass variables to the host.
* URL: http://localhost/v2
* URL: http://petstore.swagger.io/v2
* URL: http://path-server-test.petstore.local/v2
* URL: http://{server}.swagger.io:{port}/v2
@ -313,6 +316,7 @@ class PetApi
*
* This operation contains host(s) defined in the OpenAPI spec. Use 'hostIndex' to select the host.
* if needed, use the 'variables' parameter to pass variables to the host.
* URL: http://localhost/v2
* URL: http://petstore.swagger.io/v2
* URL: http://path-server-test.petstore.local/v2
* URL: http://{server}.swagger.io:{port}/v2
@ -373,6 +377,7 @@ class PetApi
*
* This operation contains host(s) defined in the OpenAPI spec. Use 'hostIndex' to select the host.
* if needed, use the 'variables' parameter to pass variables to the host.
* URL: http://localhost/v2
* URL: http://petstore.swagger.io/v2
* URL: http://path-server-test.petstore.local/v2
* URL: http://{server}.swagger.io:{port}/v2
@ -504,6 +509,10 @@ class PetApi
protected function getHostSettingsForaddPet(): array
{
return [
[
"url" => "http://localhost/v2",
"description" => "No description provided",
],
[
"url" => "http://petstore.swagger.io/v2",
"description" => "No description provided",
@ -1656,6 +1665,7 @@ class PetApi
*
* This operation contains host(s) defined in the OpenAPI spec. Use 'hostIndex' to select the host.
* if needed, use the 'variables' parameter to pass variables to the host.
* URL: http://localhost/v2
* URL: http://petstore.swagger.io/v2
* URL: http://path-server-test.petstore.local/v2
* URL: http://{server}.swagger.io:{port}/v2
@ -1696,6 +1706,7 @@ class PetApi
*
* This operation contains host(s) defined in the OpenAPI spec. Use 'hostIndex' to select the host.
* if needed, use the 'variables' parameter to pass variables to the host.
* URL: http://localhost/v2
* URL: http://petstore.swagger.io/v2
* URL: http://path-server-test.petstore.local/v2
* URL: http://{server}.swagger.io:{port}/v2
@ -1767,6 +1778,7 @@ class PetApi
*
* This operation contains host(s) defined in the OpenAPI spec. Use 'hostIndex' to select the host.
* if needed, use the 'variables' parameter to pass variables to the host.
* URL: http://localhost/v2
* URL: http://petstore.swagger.io/v2
* URL: http://path-server-test.petstore.local/v2
* URL: http://{server}.swagger.io:{port}/v2
@ -1811,6 +1823,7 @@ class PetApi
*
* This operation contains host(s) defined in the OpenAPI spec. Use 'hostIndex' to select the host.
* if needed, use the 'variables' parameter to pass variables to the host.
* URL: http://localhost/v2
* URL: http://petstore.swagger.io/v2
* URL: http://path-server-test.petstore.local/v2
* URL: http://{server}.swagger.io:{port}/v2
@ -1871,6 +1884,7 @@ class PetApi
*
* This operation contains host(s) defined in the OpenAPI spec. Use 'hostIndex' to select the host.
* if needed, use the 'variables' parameter to pass variables to the host.
* URL: http://localhost/v2
* URL: http://petstore.swagger.io/v2
* URL: http://path-server-test.petstore.local/v2
* URL: http://{server}.swagger.io:{port}/v2
@ -2002,6 +2016,10 @@ class PetApi
protected function getHostSettingsForupdatePet(): array
{
return [
[
"url" => "http://localhost/v2",
"description" => "No description provided",
],
[
"url" => "http://petstore.swagger.io/v2",
"description" => "No description provided",

View File

@ -23,7 +23,7 @@ All URIs are relative to http://petstore.swagger.io:80/v2, except if the operati
addPet($pet)
```
### URI(s):
- http://petstore.swagger.io/v2 - http://path-server-test.petstore.local/v2 - http://{server}.swagger.io:{port}/v2 test server with variables
- http://localhost/v2 - http://petstore.swagger.io/v2 - http://path-server-test.petstore.local/v2 - http://{server}.swagger.io:{port}/v2 test server with variables
- Variables:
- server: target server
- Allowed values:
@ -82,6 +82,7 @@ try {
### Return type
void (empty response body)
@ -348,7 +349,7 @@ try {
updatePet($pet)
```
### URI(s):
- http://petstore.swagger.io/v2 - http://path-server-test.petstore.local/v2 - http://{server}.swagger.io:{port}/v2 test server with variables
- http://localhost/v2 - http://petstore.swagger.io/v2 - http://path-server-test.petstore.local/v2 - http://{server}.swagger.io:{port}/v2 test server with variables
- Variables:
- server: target server
- Allowed values:
@ -407,6 +408,7 @@ try {
### Return type
void (empty response body)

View File

@ -164,6 +164,7 @@ class PetApi
*
* This operation contains host(s) defined in the OpenAPI spec. Use 'hostIndex' to select the host.
* if needed, use the 'variables' parameter to pass variables to the host.
* URL: http://localhost/v2
* URL: http://petstore.swagger.io/v2
* URL: http://path-server-test.petstore.local/v2
* URL: http://{server}.swagger.io:{port}/v2
@ -199,6 +200,7 @@ class PetApi
*
* This operation contains host(s) defined in the OpenAPI spec. Use 'hostIndex' to select the host.
* if needed, use the 'variables' parameter to pass variables to the host.
* URL: http://localhost/v2
* URL: http://petstore.swagger.io/v2
* URL: http://path-server-test.petstore.local/v2
* URL: http://{server}.swagger.io:{port}/v2
@ -266,6 +268,7 @@ class PetApi
*
* This operation contains host(s) defined in the OpenAPI spec. Use 'hostIndex' to select the host.
* if needed, use the 'variables' parameter to pass variables to the host.
* URL: http://localhost/v2
* URL: http://petstore.swagger.io/v2
* URL: http://path-server-test.petstore.local/v2
* URL: http://{server}.swagger.io:{port}/v2
@ -305,6 +308,7 @@ class PetApi
*
* This operation contains host(s) defined in the OpenAPI spec. Use 'hostIndex' to select the host.
* if needed, use the 'variables' parameter to pass variables to the host.
* URL: http://localhost/v2
* URL: http://petstore.swagger.io/v2
* URL: http://path-server-test.petstore.local/v2
* URL: http://{server}.swagger.io:{port}/v2
@ -360,6 +364,7 @@ class PetApi
*
* This operation contains host(s) defined in the OpenAPI spec. Use 'hostIndex' to select the host.
* if needed, use the 'variables' parameter to pass variables to the host.
* URL: http://localhost/v2
* URL: http://petstore.swagger.io/v2
* URL: http://path-server-test.petstore.local/v2
* URL: http://{server}.swagger.io:{port}/v2
@ -486,6 +491,10 @@ class PetApi
protected function getHostSettingsForaddPet(): array
{
return [
[
"url" => "http://localhost/v2",
"description" => "No description provided",
],
[
"url" => "http://petstore.swagger.io/v2",
"description" => "No description provided",
@ -1583,6 +1592,7 @@ class PetApi
*
* This operation contains host(s) defined in the OpenAPI spec. Use 'hostIndex' to select the host.
* if needed, use the 'variables' parameter to pass variables to the host.
* URL: http://localhost/v2
* URL: http://petstore.swagger.io/v2
* URL: http://path-server-test.petstore.local/v2
* URL: http://{server}.swagger.io:{port}/v2
@ -1618,6 +1628,7 @@ class PetApi
*
* This operation contains host(s) defined in the OpenAPI spec. Use 'hostIndex' to select the host.
* if needed, use the 'variables' parameter to pass variables to the host.
* URL: http://localhost/v2
* URL: http://petstore.swagger.io/v2
* URL: http://path-server-test.petstore.local/v2
* URL: http://{server}.swagger.io:{port}/v2
@ -1685,6 +1696,7 @@ class PetApi
*
* This operation contains host(s) defined in the OpenAPI spec. Use 'hostIndex' to select the host.
* if needed, use the 'variables' parameter to pass variables to the host.
* URL: http://localhost/v2
* URL: http://petstore.swagger.io/v2
* URL: http://path-server-test.petstore.local/v2
* URL: http://{server}.swagger.io:{port}/v2
@ -1724,6 +1736,7 @@ class PetApi
*
* This operation contains host(s) defined in the OpenAPI spec. Use 'hostIndex' to select the host.
* if needed, use the 'variables' parameter to pass variables to the host.
* URL: http://localhost/v2
* URL: http://petstore.swagger.io/v2
* URL: http://path-server-test.petstore.local/v2
* URL: http://{server}.swagger.io:{port}/v2
@ -1779,6 +1792,7 @@ class PetApi
*
* This operation contains host(s) defined in the OpenAPI spec. Use 'hostIndex' to select the host.
* if needed, use the 'variables' parameter to pass variables to the host.
* URL: http://localhost/v2
* URL: http://petstore.swagger.io/v2
* URL: http://path-server-test.petstore.local/v2
* URL: http://{server}.swagger.io:{port}/v2
@ -1905,6 +1919,10 @@ class PetApi
protected function getHostSettingsForupdatePet(): array
{
return [
[
"url" => "http://localhost/v2",
"description" => "No description provided",
],
[
"url" => "http://petstore.swagger.io/v2",
"description" => "No description provided",

View File

@ -16,7 +16,8 @@ class AsyncTest extends TestCase
public function setUp(): void
{
$this->api = new Api\PetApi();
$config = (new Configuration())->setHost('http://localhost/v2');
$this->api = new Api\PetApi(null, $config);
$this->petId = 10005;
$pet = new Model\Pet;

View File

@ -11,15 +11,16 @@ class DebugTest extends TestCase
parent::setUpBeforeClass();
$newPet = new Model\Pet;
$newPet->setId(1);
$newPet->setName("PHP Unit Test");
(new Api\PetApi())->addPetWithHttpInfo($newPet);
$newPet->setName("PHP Unit Test");
$config = (new Configuration())->setHost('http://localhost/v2');
(new Api\PetApi(null, $config))->addPetWithHttpInfo($newPet);
}
public function testEnableDebugOutput()
{
$this->expectOutputRegex('#GET /v2/pet/1 HTTP/1.1#');
$config = new Configuration();
$config = (new Configuration())->setHost('http://localhost/v2');
$config->setDebug(true);
$api = new Api\PetApi(null, $config);
$api->getPetById(1);
@ -29,7 +30,7 @@ class DebugTest extends TestCase
{
$this->expectOutputRegex('#GET /v2/pet/1 HTTP/1.1#');
$config = new Configuration();
$config = (new Configuration())->setHost('http://localhost/v2');
$config->setDebug(true);
$api = new Api\PetApi(null, $config);
$promise = $api->getPetByIdAsync(1);

View File

@ -41,15 +41,12 @@ class PetApiTest extends TestCase
$newPet->setTags(array($tag));
$newPet->setCategory($category);
$config = new Configuration();
$petApi = new Api\PetApi(null, $config);
}
public function setUp(): void
{
$this->api = new Api\PetApi();
$config = (new Configuration())->setHost('http://localhost/v2');
$this->api = new Api\PetApi(null, $config);
}
public function testGetPetById()

View File

@ -30,10 +30,10 @@ class ServerVariablesInOperationTest extends TestCase
$this->api->addPet($this->pet, 2);
$request = $this->fakeHttpClient->getLastRequest();
$this->assertEquals('petstore.swagger.io', $request->getUri()->getHost(), 'Server variable set to default value.');
$this->assertEquals('path-server-test.petstore.local', $request->getUri()->getHost(), 'Server variable set to default value.');
# Test variables substitution
$this->api->addPet($this->pet, 2, [ 'server' => 'qa-petstore', 'port' => '8080']);
$this->api->addPet($this->pet, 3, [ 'server' => 'qa-petstore', 'port' => '8080']);
$request = $this->fakeHttpClient->getLastRequest();
$this->assertEquals('qa-petstore.swagger.io', $request->getUri()->getHost(), 'Server set to "qa-petstore"');
@ -43,7 +43,7 @@ class ServerVariablesInOperationTest extends TestCase
public function testLegacyServerChoiceInOperation(): void
{
# Test legacy behavior (set server using api->setHostIndex()
$this->api->setHostIndex(1);
$this->api->setHostIndex(2);
$this->api->addPet($this->pet);
$request = $this->fakeHttpClient->getLastRequest();
@ -53,6 +53,6 @@ class ServerVariablesInOperationTest extends TestCase
public function testInvalidVariableValueInOperation(): void
{
$this->expectException(InvalidArgumentException::class);
$this->api->addPet($this->pet, 2,['server' => 'invalid-value']);
$this->api->addPet($this->pet, 3,['server' => 'invalid-value']);
}
}

View File

@ -180,6 +180,7 @@ class PetApi
* Add a new pet to the store
*
* This operation contains host(s) defined in the OpenAP spec. Use 'hostIndex' to select the host.
* URL: http://localhost/v2
* URL: http://petstore.swagger.io/v2
* URL: http://path-server-test.petstore.local/v2
* URL: http://{server}.swagger.io:{port}/v2
@ -201,6 +202,7 @@ class PetApi
* Add a new pet to the store
*
* This operation contains host(s) defined in the OpenAP spec. Use 'hostIndex' to select the host.
* URL: http://localhost/v2
* URL: http://petstore.swagger.io/v2
* URL: http://path-server-test.petstore.local/v2
* URL: http://{server}.swagger.io:{port}/v2
@ -258,6 +260,7 @@ class PetApi
* Add a new pet to the store
*
* This operation contains host(s) defined in the OpenAP spec. Use 'hostIndex' to select the host.
* URL: http://localhost/v2
* URL: http://petstore.swagger.io/v2
* URL: http://path-server-test.petstore.local/v2
* URL: http://{server}.swagger.io:{port}/v2
@ -283,6 +286,7 @@ class PetApi
* Add a new pet to the store
*
* This operation contains host(s) defined in the OpenAP spec. Use 'hostIndex' to select the host.
* URL: http://localhost/v2
* URL: http://petstore.swagger.io/v2
* URL: http://path-server-test.petstore.local/v2
* URL: http://{server}.swagger.io:{port}/v2
@ -323,6 +327,7 @@ class PetApi
* Create request for operation 'addPet'
*
* This operation contains host(s) defined in the OpenAP spec. Use 'hostIndex' to select the host.
* URL: http://localhost/v2
* URL: http://petstore.swagger.io/v2
* URL: http://path-server-test.petstore.local/v2
* URL: http://{server}.swagger.io:{port}/v2
@ -405,7 +410,7 @@ class PetApi
$headers
);
$operationHosts = ["http://petstore.swagger.io/v2", "http://path-server-test.petstore.local/v2", "http://{server}.swagger.io:{port}/v2"];
$operationHosts = ["http://localhost/v2", "http://petstore.swagger.io/v2", "http://path-server-test.petstore.local/v2", "http://{server}.swagger.io:{port}/v2"];
if ($this->hostIndex < 0 || $this->hostIndex >= sizeof($operationHosts)) {
throw new \InvalidArgumentException("Invalid index {$this->hostIndex} when selecting the host. Must be less than ".sizeof($operationHosts));
}
@ -1432,6 +1437,7 @@ class PetApi
* Update an existing pet
*
* This operation contains host(s) defined in the OpenAP spec. Use 'hostIndex' to select the host.
* URL: http://localhost/v2
* URL: http://petstore.swagger.io/v2
* URL: http://path-server-test.petstore.local/v2
* URL: http://{server}.swagger.io:{port}/v2
@ -1453,6 +1459,7 @@ class PetApi
* Update an existing pet
*
* This operation contains host(s) defined in the OpenAP spec. Use 'hostIndex' to select the host.
* URL: http://localhost/v2
* URL: http://petstore.swagger.io/v2
* URL: http://path-server-test.petstore.local/v2
* URL: http://{server}.swagger.io:{port}/v2
@ -1510,6 +1517,7 @@ class PetApi
* Update an existing pet
*
* This operation contains host(s) defined in the OpenAP spec. Use 'hostIndex' to select the host.
* URL: http://localhost/v2
* URL: http://petstore.swagger.io/v2
* URL: http://path-server-test.petstore.local/v2
* URL: http://{server}.swagger.io:{port}/v2
@ -1535,6 +1543,7 @@ class PetApi
* Update an existing pet
*
* This operation contains host(s) defined in the OpenAP spec. Use 'hostIndex' to select the host.
* URL: http://localhost/v2
* URL: http://petstore.swagger.io/v2
* URL: http://path-server-test.petstore.local/v2
* URL: http://{server}.swagger.io:{port}/v2
@ -1575,6 +1584,7 @@ class PetApi
* Create request for operation 'updatePet'
*
* This operation contains host(s) defined in the OpenAP spec. Use 'hostIndex' to select the host.
* URL: http://localhost/v2
* URL: http://petstore.swagger.io/v2
* URL: http://path-server-test.petstore.local/v2
* URL: http://{server}.swagger.io:{port}/v2
@ -1657,7 +1667,7 @@ class PetApi
$headers
);
$operationHosts = ["http://petstore.swagger.io/v2", "http://path-server-test.petstore.local/v2", "http://{server}.swagger.io:{port}/v2"];
$operationHosts = ["http://localhost/v2", "http://petstore.swagger.io/v2", "http://path-server-test.petstore.local/v2", "http://{server}.swagger.io:{port}/v2"];
if ($this->hostIndex < 0 || $this->hostIndex >= sizeof($operationHosts)) {
throw new \InvalidArgumentException("Invalid index {$this->hostIndex} when selecting the host. Must be less than ".sizeof($operationHosts));
}