diff --git a/README.md b/README.md index ac6a4ce..a6cf77b 100644 --- a/README.md +++ b/README.md @@ -650,9 +650,6 @@ use Http\Client\Common\Plugin; $this->getClientBuilder()->addPlugin(Plugin $plugin, int $priority): self; ``` -> [!NOTE] -> A `PluginException` will be thrown if there is a plugin with the same `priority` level. - It is important to know that this library already uses various plugins with different priorities. The following list has all the implemented plugins with the respective priority in descending order (remember that order matters): @@ -664,6 +661,10 @@ The following list has all the implemented plugins with the respective priority | [`CachePlugin`](https://docs.php-http.org/en/latest/plugins/cache.html) | 16 | only if cache is enabled | | [`LoggerPlugin`](https://docs.php-http.org/en/latest/plugins/logger.html) | 8 | only if logger is enabled | +> [!IMPORTANT] +> The plugin priority in the list above is reserved. +> This means that if you try to add any plugin with the same priority, it will be overwritten. + For example, if you wanted the client to automatically attempt to re-send a request that failed (due to unreliable connections and servers, for example), you can add the [RetryPlugin](https://docs.php-http.org/en/latest/plugins/retry.html): diff --git a/src/Builder/ClientBuilder.php b/src/Builder/ClientBuilder.php index 2a636f3..9855ffd 100644 --- a/src/Builder/ClientBuilder.php +++ b/src/Builder/ClientBuilder.php @@ -73,12 +73,6 @@ public function setStreamFactory(StreamFactoryInterface $streamFactory): self public function addPlugin(Plugin $plugin, int $priority): self { - if (isset($this->plugins[$priority])) { - throw new PluginException( - sprintf('A plugin with priority %d already exists.', $priority) - ); - } - $this->plugins[$priority] = $plugin; // sort plugins by priority (key) in descending order krsort($this->plugins); diff --git a/tests/Integration/ApiTest.php b/tests/Integration/ApiTest.php index 5859e85..5d8bcaf 100644 --- a/tests/Integration/ApiTest.php +++ b/tests/Integration/ApiTest.php @@ -50,6 +50,17 @@ public function testRequest() $this->assertSame(MockResponse::SUCCESS, $response); } + public function testMultipleRequests() + { + $this->mockClient->addResponse(new Response(body: MockResponse::SUCCESS)); + $this->mockClient->addResponse(new Response(body: MockResponse::SUCCESS)); + + $this->api->request(method: 'GET', path: '/path-1'); + $this->api->request(method: 'GET', path: '/path-2'); + + $this->assertTrue(true); + } + public function testBaseUrl() { $this->assertNull($this->api->getBaseUrl()); diff --git a/tests/Unit/Builder/ClientBuilderTest.php b/tests/Unit/Builder/ClientBuilderTest.php index 9a3ca0c..8f0b438 100644 --- a/tests/Unit/Builder/ClientBuilderTest.php +++ b/tests/Unit/Builder/ClientBuilderTest.php @@ -77,10 +77,9 @@ public function testAddPluginWithSamePriority() $plugin = $this->createMock(Plugin::class); $clientBuilder = new ClientBuilder(); - $this->expectException(PluginException::class); - $this->expectExceptionMessage('A plugin with priority 1 already exists.'); - $clientBuilder->addPlugin($plugin, 1); $clientBuilder->addPlugin($plugin, 1); + + $this->assertCount(1, $clientBuilder->getPlugins()); } } \ No newline at end of file