From 8329e622f6f143c379569f1e29e370fa4fd933fa Mon Sep 17 00:00:00 2001 From: Csaba Maulis Date: Wed, 21 Dec 2022 14:13:12 +0800 Subject: [PATCH] Release version 1.3.0 --- .github/workflows/ci.yml | 14 ++---- CHANGELOG.md | 13 +++++ README.md | 10 ++++ composer.json | 9 +++- manifest.json | 4 +- src/Client.php | 3 +- src/Manager.php | 1 + tests/MockHttpClient.php | 47 ------------------- tests/NullAuth.php | 24 ++++++++++ tests/Unit/ClientTest.php | 32 +++++++------ tests/Unit/Handlers/CompletionsTest.php | 6 +-- tests/Unit/Handlers/EditsTest.php | 6 +-- tests/Unit/Handlers/EmbeddingsTest.php | 6 +-- tests/Unit/Handlers/FilesContentTest.php | 6 +-- tests/Unit/Handlers/FilesTest.php | 6 +-- tests/Unit/Handlers/FineTunesCancelTest.php | 6 +-- tests/Unit/Handlers/FineTunesEventsTest.php | 6 +-- tests/Unit/Handlers/FineTunesTest.php | 6 +-- tests/Unit/Handlers/ImagesEditsTest.php | 6 +-- tests/Unit/Handlers/ImagesGenerationsTest.php | 6 +-- tests/Unit/Handlers/ImagesVariationsTest.php | 6 +-- tests/Unit/Handlers/ModelsTest.php | 6 +-- tests/Unit/Handlers/ModerationsTest.php | 6 +-- 23 files changed, 122 insertions(+), 113 deletions(-) delete mode 100644 tests/MockHttpClient.php create mode 100644 tests/NullAuth.php diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ba53109..d0df459 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -34,7 +34,7 @@ jobs: steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Setup PHP uses: shivammathur/setup-php@v2 @@ -86,13 +86,11 @@ jobs: steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Setup node if: matrix.test-type == 'integration' - uses: actions/setup-node@v2 - with: - node-version: '16' + uses: actions/setup-node@v3 - name: Install and run @stoplight/prism if: matrix.test-type == 'integration' @@ -141,12 +139,10 @@ jobs: steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Setup node - uses: actions/setup-node@v2 - with: - node-version: '16' + uses: actions/setup-node@v3 - name: Install and run @stoplight/prism run: | diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a56e2e..2190069 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,19 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## 1.3.0 - 2022-12-21 + +### Added +- Use parameters defined outside endpoint methods. + +### Changed +- Encourage the use of `php-http/mock-client` for testing and mocking API responses. +- Remove the `Tests\MockHttpClient` class, and use the `php-http/mock-client` package instead. +- Make Handler and Model class names more readable. + +### Fixed +- Use correct model type for nested models. + ## 1.2.0 - 2022-11-07 ### Added diff --git a/README.md b/README.md index e539a22..141703c 100644 --- a/README.md +++ b/README.md @@ -357,6 +357,16 @@ composer test:integration We do not recommend running integration tests against the live OpenAI API endpoints. This is because the tests will send example data to all endpoints, which can result in new data being created, or existing data being deleted. +### Writing Your Own Tests + +If you are writing your own tests, you will likely need to mock the responses from the **OpenAI API**. + +One way of doing this is to install the `php-http/mock-client` package into your project, and then use the `\Http\Mock\Client` class (instead of a real PSR-18 client) when instantiating the **Tectalic OpenAI REST API Client**. + +This allows you to mock the responses from the **OpenAI API**, rather than performing real requests. + +Please see the [Mock Client documentation](https://docs.php-http.org/en/latest/clients/mock-client.html#mock-client) for details. + ## Support If you have any questions or feedback, please use the [discussion board](https://github.com/tectalichq/public-openai-client-php/discussions). diff --git a/composer.json b/composer.json index bd751d0..11f8df0 100644 --- a/composer.json +++ b/composer.json @@ -42,6 +42,7 @@ "require-dev": { "league/openapi-psr7-validator": "^0.17.0", "mikey179/vfsstream": "^1.6.10", + "php-http/mock-client": "^1.5", "phpstan/phpstan": "^1.4", "phpunit/phpunit": "^8.5.14 || ^9.5", "squizlabs/php_codesniffer": "^3.6", @@ -54,7 +55,10 @@ "@test:unit", "@test:integration" ], - "test:coverage": "XDEBUG_MODE=coverage phpunit --verbose --coverage-html=coverage", + "test:coverage": [ + "Composer\\Config::disableProcessTimeout", + "XDEBUG_MODE=coverage phpunit --verbose --coverage-html=coverage" + ], "test:integration": "phpunit --verbose --testsuite=integration", "test:phpcs": "phpcs -q -s", "test:phpstan": "phpstan analyse --memory-limit=1G", @@ -67,5 +71,8 @@ "test:phpstan": "Static test via phpstan.", "test:phpcs": "Style test via phpcs.", "test:unit": "Unit test via phpunit." + }, + "suggest": { + "php-http/mock-client": "Simplify testing by using a mock HTTP client" } } diff --git a/manifest.json b/manifest.json index 4c4792d..c72f094 100644 --- a/manifest.json +++ b/manifest.json @@ -1,5 +1,5 @@ { - "libraryVersion": "1.2.0", + "libraryVersion": "1.3.0", "apiVersion": "1.1.0", - "buildVersion": "1.1.5" + "buildVersion": "1.2.0" } \ No newline at end of file diff --git a/src/Client.php b/src/Client.php index 8918573..3e34df2 100644 --- a/src/Client.php +++ b/src/Client.php @@ -12,6 +12,7 @@ namespace Tectalic\OpenAi; +use Http\Message\Authentication; use Http\Message\MultipartStream\MultipartStreamBuilder; use Nyholm\Psr7\Factory\Psr17Factory; use Psr\Http\Client\ClientExceptionInterface; @@ -369,7 +370,7 @@ private function mergeRequestParts( $request = $request->withHeader( 'User-Agent', - 'Tectalic OpenAI REST API Client/1.2.0' + 'Tectalic OpenAI REST API Client/1.3.0' ); // Merge Headers. diff --git a/src/Manager.php b/src/Manager.php index e1bf21a..4ed4212 100644 --- a/src/Manager.php +++ b/src/Manager.php @@ -12,6 +12,7 @@ namespace Tectalic\OpenAi; +use Http\Message\Authentication; use LogicException; use Psr\Http\Client\ClientInterface; diff --git a/tests/MockHttpClient.php b/tests/MockHttpClient.php deleted file mode 100644 index b2649fd..0000000 --- a/tests/MockHttpClient.php +++ /dev/null @@ -1,47 +0,0 @@ -exception = $exception; - } - - public function makeResponse(ResponseInterface $response): void - { - $this->response = $response; - } - - public function sendRequest(RequestInterface $request): ResponseInterface - { - if (!\is_null($this->exception)) { - throw $this->exception; - } - - return $this->response ?? (new Psr17Factory())->createResponse(); - } -} diff --git a/tests/NullAuth.php b/tests/NullAuth.php new file mode 100644 index 0000000..b09caff --- /dev/null +++ b/tests/NullAuth.php @@ -0,0 +1,24 @@ +assertEquals($client, Manager::access()); @@ -64,8 +64,8 @@ public function testDoubleBuild(): void $this->expectException(LogicException::class); $this->expectExceptionMessage('Client already built.'); Manager::build( - new MockHttpClient(), - new Authentication('token') + new Client(), + new NullAuth() ); } @@ -385,15 +385,19 @@ public function invalidResponse(): array /** * @dataProvider invalidResponse - * @param class-string<\Throwable> $exception + * @param class-string<\Exception> $exception */ public function testInvalidResponse(string $method, string $exception, string $message): void { $this->expectException(ClientException::class); $this->expectExceptionMessage($message); - $client = new Client( - new MockHttpClient(new $exception()), - new Authentication('token'), + + $mockClient = new Client(); + $mockClient->addException(new $exception()); + + $client = new OpenAiClient( + $mockClient, + new NullAuth(), Manager::BASE_URI ); $client->sendRequest($client->$method('/')); diff --git a/tests/Unit/Handlers/CompletionsTest.php b/tests/Unit/Handlers/CompletionsTest.php index 7d20bb8..34760ef 100644 --- a/tests/Unit/Handlers/CompletionsTest.php +++ b/tests/Unit/Handlers/CompletionsTest.php @@ -12,6 +12,7 @@ namespace Tests\Unit\Handlers; +use Http\Mock\Client; use Nyholm\Psr7\Response; use PHPUnit\Framework\TestCase; use ReflectionClass; @@ -22,18 +23,17 @@ use Tectalic\OpenAi\Manager; use Tectalic\OpenAi\Models\Completions\CreateRequest; use Tests\AssertValidateTrait; -use Tests\MockHttpClient; final class CompletionsTest extends TestCase { use AssertValidateTrait; - /** @var MockHttpClient */ + /** @var Client */ private $mockClient; protected function setUp(): void { - $this->mockClient = new MockHttpClient(); + $this->mockClient = new Client(); Manager::build( $this->mockClient, new Authentication('token') diff --git a/tests/Unit/Handlers/EditsTest.php b/tests/Unit/Handlers/EditsTest.php index d0d6fb0..816dc0e 100644 --- a/tests/Unit/Handlers/EditsTest.php +++ b/tests/Unit/Handlers/EditsTest.php @@ -12,6 +12,7 @@ namespace Tests\Unit\Handlers; +use Http\Mock\Client; use Nyholm\Psr7\Response; use PHPUnit\Framework\TestCase; use ReflectionClass; @@ -22,18 +23,17 @@ use Tectalic\OpenAi\Manager; use Tectalic\OpenAi\Models\Edits\CreateRequest; use Tests\AssertValidateTrait; -use Tests\MockHttpClient; final class EditsTest extends TestCase { use AssertValidateTrait; - /** @var MockHttpClient */ + /** @var Client */ private $mockClient; protected function setUp(): void { - $this->mockClient = new MockHttpClient(); + $this->mockClient = new Client(); Manager::build( $this->mockClient, new Authentication('token') diff --git a/tests/Unit/Handlers/EmbeddingsTest.php b/tests/Unit/Handlers/EmbeddingsTest.php index 61b47ff..6dc8ac1 100644 --- a/tests/Unit/Handlers/EmbeddingsTest.php +++ b/tests/Unit/Handlers/EmbeddingsTest.php @@ -12,6 +12,7 @@ namespace Tests\Unit\Handlers; +use Http\Mock\Client; use Nyholm\Psr7\Response; use PHPUnit\Framework\TestCase; use ReflectionClass; @@ -22,18 +23,17 @@ use Tectalic\OpenAi\Manager; use Tectalic\OpenAi\Models\Embeddings\CreateRequest; use Tests\AssertValidateTrait; -use Tests\MockHttpClient; final class EmbeddingsTest extends TestCase { use AssertValidateTrait; - /** @var MockHttpClient */ + /** @var Client */ private $mockClient; protected function setUp(): void { - $this->mockClient = new MockHttpClient(); + $this->mockClient = new Client(); Manager::build( $this->mockClient, new Authentication('token') diff --git a/tests/Unit/Handlers/FilesContentTest.php b/tests/Unit/Handlers/FilesContentTest.php index 0ecb221..d8bb775 100644 --- a/tests/Unit/Handlers/FilesContentTest.php +++ b/tests/Unit/Handlers/FilesContentTest.php @@ -12,6 +12,7 @@ namespace Tests\Unit\Handlers; +use Http\Mock\Client; use Nyholm\Psr7\Response; use PHPUnit\Framework\TestCase; use ReflectionClass; @@ -21,18 +22,17 @@ use Tectalic\OpenAi\Handlers\FilesContent; use Tectalic\OpenAi\Manager; use Tests\AssertValidateTrait; -use Tests\MockHttpClient; final class FilesContentTest extends TestCase { use AssertValidateTrait; - /** @var MockHttpClient */ + /** @var Client */ private $mockClient; protected function setUp(): void { - $this->mockClient = new MockHttpClient(); + $this->mockClient = new Client(); Manager::build( $this->mockClient, new Authentication('token') diff --git a/tests/Unit/Handlers/FilesTest.php b/tests/Unit/Handlers/FilesTest.php index 6ce8e1c..634116c 100644 --- a/tests/Unit/Handlers/FilesTest.php +++ b/tests/Unit/Handlers/FilesTest.php @@ -12,6 +12,7 @@ namespace Tests\Unit\Handlers; +use Http\Mock\Client; use Nyholm\Psr7\Response; use PHPUnit\Framework\TestCase; use ReflectionClass; @@ -22,7 +23,6 @@ use Tectalic\OpenAi\Manager; use Tectalic\OpenAi\Models\Files\CreateRequest; use Tests\AssertValidateTrait; -use Tests\MockHttpClient; use org\bovigo\vfs\content\LargeFileContent; use org\bovigo\vfs\vfsStream; @@ -30,12 +30,12 @@ final class FilesTest extends TestCase { use AssertValidateTrait; - /** @var MockHttpClient */ + /** @var Client */ private $mockClient; protected function setUp(): void { - $this->mockClient = new MockHttpClient(); + $this->mockClient = new Client(); Manager::build( $this->mockClient, new Authentication('token') diff --git a/tests/Unit/Handlers/FineTunesCancelTest.php b/tests/Unit/Handlers/FineTunesCancelTest.php index 06191f5..a744224 100644 --- a/tests/Unit/Handlers/FineTunesCancelTest.php +++ b/tests/Unit/Handlers/FineTunesCancelTest.php @@ -12,6 +12,7 @@ namespace Tests\Unit\Handlers; +use Http\Mock\Client; use Nyholm\Psr7\Response; use PHPUnit\Framework\TestCase; use ReflectionClass; @@ -21,18 +22,17 @@ use Tectalic\OpenAi\Handlers\FineTunesCancel; use Tectalic\OpenAi\Manager; use Tests\AssertValidateTrait; -use Tests\MockHttpClient; final class FineTunesCancelTest extends TestCase { use AssertValidateTrait; - /** @var MockHttpClient */ + /** @var Client */ private $mockClient; protected function setUp(): void { - $this->mockClient = new MockHttpClient(); + $this->mockClient = new Client(); Manager::build( $this->mockClient, new Authentication('token') diff --git a/tests/Unit/Handlers/FineTunesEventsTest.php b/tests/Unit/Handlers/FineTunesEventsTest.php index 27649b5..9e32913 100644 --- a/tests/Unit/Handlers/FineTunesEventsTest.php +++ b/tests/Unit/Handlers/FineTunesEventsTest.php @@ -12,6 +12,7 @@ namespace Tests\Unit\Handlers; +use Http\Mock\Client; use Nyholm\Psr7\Response; use PHPUnit\Framework\TestCase; use ReflectionClass; @@ -21,18 +22,17 @@ use Tectalic\OpenAi\Handlers\FineTunesEvents; use Tectalic\OpenAi\Manager; use Tests\AssertValidateTrait; -use Tests\MockHttpClient; final class FineTunesEventsTest extends TestCase { use AssertValidateTrait; - /** @var MockHttpClient */ + /** @var Client */ private $mockClient; protected function setUp(): void { - $this->mockClient = new MockHttpClient(); + $this->mockClient = new Client(); Manager::build( $this->mockClient, new Authentication('token') diff --git a/tests/Unit/Handlers/FineTunesTest.php b/tests/Unit/Handlers/FineTunesTest.php index 29bc959..dcf63a0 100644 --- a/tests/Unit/Handlers/FineTunesTest.php +++ b/tests/Unit/Handlers/FineTunesTest.php @@ -12,6 +12,7 @@ namespace Tests\Unit\Handlers; +use Http\Mock\Client; use Nyholm\Psr7\Response; use PHPUnit\Framework\TestCase; use ReflectionClass; @@ -22,18 +23,17 @@ use Tectalic\OpenAi\Manager; use Tectalic\OpenAi\Models\FineTunes\CreateRequest; use Tests\AssertValidateTrait; -use Tests\MockHttpClient; final class FineTunesTest extends TestCase { use AssertValidateTrait; - /** @var MockHttpClient */ + /** @var Client */ private $mockClient; protected function setUp(): void { - $this->mockClient = new MockHttpClient(); + $this->mockClient = new Client(); Manager::build( $this->mockClient, new Authentication('token') diff --git a/tests/Unit/Handlers/ImagesEditsTest.php b/tests/Unit/Handlers/ImagesEditsTest.php index 20d9a68..82ad973 100644 --- a/tests/Unit/Handlers/ImagesEditsTest.php +++ b/tests/Unit/Handlers/ImagesEditsTest.php @@ -12,6 +12,7 @@ namespace Tests\Unit\Handlers; +use Http\Mock\Client; use Nyholm\Psr7\Response; use PHPUnit\Framework\TestCase; use ReflectionClass; @@ -22,7 +23,6 @@ use Tectalic\OpenAi\Manager; use Tectalic\OpenAi\Models\ImagesEdits\CreateImageRequest; use Tests\AssertValidateTrait; -use Tests\MockHttpClient; use org\bovigo\vfs\content\LargeFileContent; use org\bovigo\vfs\vfsStream; @@ -30,12 +30,12 @@ final class ImagesEditsTest extends TestCase { use AssertValidateTrait; - /** @var MockHttpClient */ + /** @var Client */ private $mockClient; protected function setUp(): void { - $this->mockClient = new MockHttpClient(); + $this->mockClient = new Client(); Manager::build( $this->mockClient, new Authentication('token') diff --git a/tests/Unit/Handlers/ImagesGenerationsTest.php b/tests/Unit/Handlers/ImagesGenerationsTest.php index f04e620..c42aab1 100644 --- a/tests/Unit/Handlers/ImagesGenerationsTest.php +++ b/tests/Unit/Handlers/ImagesGenerationsTest.php @@ -12,6 +12,7 @@ namespace Tests\Unit\Handlers; +use Http\Mock\Client; use Nyholm\Psr7\Response; use PHPUnit\Framework\TestCase; use ReflectionClass; @@ -22,18 +23,17 @@ use Tectalic\OpenAi\Manager; use Tectalic\OpenAi\Models\ImagesGenerations\CreateRequest; use Tests\AssertValidateTrait; -use Tests\MockHttpClient; final class ImagesGenerationsTest extends TestCase { use AssertValidateTrait; - /** @var MockHttpClient */ + /** @var Client */ private $mockClient; protected function setUp(): void { - $this->mockClient = new MockHttpClient(); + $this->mockClient = new Client(); Manager::build( $this->mockClient, new Authentication('token') diff --git a/tests/Unit/Handlers/ImagesVariationsTest.php b/tests/Unit/Handlers/ImagesVariationsTest.php index 74c9d0e..15fa1bd 100644 --- a/tests/Unit/Handlers/ImagesVariationsTest.php +++ b/tests/Unit/Handlers/ImagesVariationsTest.php @@ -12,6 +12,7 @@ namespace Tests\Unit\Handlers; +use Http\Mock\Client; use Nyholm\Psr7\Response; use PHPUnit\Framework\TestCase; use ReflectionClass; @@ -22,7 +23,6 @@ use Tectalic\OpenAi\Manager; use Tectalic\OpenAi\Models\ImagesVariations\CreateImageRequest; use Tests\AssertValidateTrait; -use Tests\MockHttpClient; use org\bovigo\vfs\content\LargeFileContent; use org\bovigo\vfs\vfsStream; @@ -30,12 +30,12 @@ final class ImagesVariationsTest extends TestCase { use AssertValidateTrait; - /** @var MockHttpClient */ + /** @var Client */ private $mockClient; protected function setUp(): void { - $this->mockClient = new MockHttpClient(); + $this->mockClient = new Client(); Manager::build( $this->mockClient, new Authentication('token') diff --git a/tests/Unit/Handlers/ModelsTest.php b/tests/Unit/Handlers/ModelsTest.php index faad78a..13f5b24 100644 --- a/tests/Unit/Handlers/ModelsTest.php +++ b/tests/Unit/Handlers/ModelsTest.php @@ -12,6 +12,7 @@ namespace Tests\Unit\Handlers; +use Http\Mock\Client; use Nyholm\Psr7\Response; use PHPUnit\Framework\TestCase; use ReflectionClass; @@ -21,18 +22,17 @@ use Tectalic\OpenAi\Handlers\Models; use Tectalic\OpenAi\Manager; use Tests\AssertValidateTrait; -use Tests\MockHttpClient; final class ModelsTest extends TestCase { use AssertValidateTrait; - /** @var MockHttpClient */ + /** @var Client */ private $mockClient; protected function setUp(): void { - $this->mockClient = new MockHttpClient(); + $this->mockClient = new Client(); Manager::build( $this->mockClient, new Authentication('token') diff --git a/tests/Unit/Handlers/ModerationsTest.php b/tests/Unit/Handlers/ModerationsTest.php index d739b38..0d17d48 100644 --- a/tests/Unit/Handlers/ModerationsTest.php +++ b/tests/Unit/Handlers/ModerationsTest.php @@ -12,6 +12,7 @@ namespace Tests\Unit\Handlers; +use Http\Mock\Client; use Nyholm\Psr7\Response; use PHPUnit\Framework\TestCase; use ReflectionClass; @@ -22,18 +23,17 @@ use Tectalic\OpenAi\Manager; use Tectalic\OpenAi\Models\Moderations\CreateRequest; use Tests\AssertValidateTrait; -use Tests\MockHttpClient; final class ModerationsTest extends TestCase { use AssertValidateTrait; - /** @var MockHttpClient */ + /** @var Client */ private $mockClient; protected function setUp(): void { - $this->mockClient = new MockHttpClient(); + $this->mockClient = new Client(); Manager::build( $this->mockClient, new Authentication('token')