Skip to content

Commit

Permalink
fix: reusage of parameters
Browse files Browse the repository at this point in the history
Signed-off-by: Soner Sayakci <s.sayakci@gmail.com>
  • Loading branch information
shyim committed Feb 10, 2025
1 parent 23fd245 commit a975abc
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### Added
- Added Guzzle and Symfony client factories ([#287](https://github.com/opensearch-project/opensearch-php/pull/287))
### Changed
- Changed EndpointFactory to return new objects on each call to fix issues with parameter reusage ([#292](https://github.com/opensearch-project/opensearch-php/pull/292))
### Deprecated
### Removed
### Fixed
Expand Down
11 changes: 1 addition & 10 deletions src/OpenSearch/EndpointFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
*/
class EndpointFactory implements EndpointFactoryInterface
{
/**
* @var array<string, AbstractEndpoint>
*/
private array $endpoints = [];

private ?SerializerInterface $serializer;

public function __construct(?SerializerInterface $serializer = null)
Expand All @@ -29,11 +24,7 @@ public function __construct(?SerializerInterface $serializer = null)
*/
public function getEndpoint(string $class): AbstractEndpoint
{
if (!isset($this->endpoints[$class])) {
$this->endpoints[$class] = $this->createEndpoint($class);
}

return $this->endpoints[$class];
return $this->createEndpoint($class);
}

private function getSerializer(): SerializerInterface
Expand Down
4 changes: 4 additions & 0 deletions tests/Endpoints/MlNamespaceIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public function testRegisterModelGroup()
],
]);
$this->assertNotEmpty($modelGroupResponse['model_group_id']);

$client->ml()->deleteModelGroup([
'id' => $modelGroupResponse['model_group_id'],
]);
}

public function testgetModels()
Expand Down
50 changes: 50 additions & 0 deletions tests/Namespaces/SearchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace OpenSearch\Tests\Namespaces;

use OpenSearch\Client;
use OpenSearch\EndpointFactory;
use OpenSearch\TransportInterface;
use PHPUnit\Framework\TestCase;

class SearchTest extends TestCase
{
public function testSearchDoesNotInhereitPreviousParameters(): void
{
$transport = $this->createMock(TransportInterface::class);

$calledUrls = [];

$transport->method('sendRequest')
->willReturnCallback(function ($method, $uri, $params, $body) use (&$calledUrls) {
$calledUrls[] = $uri;
return [];
});

$client = new Client($transport, new EndpointFactory());

$client->search([
'index' => 'test',
'body' => [
'query' => [
'match_all' => new \stdClass(),
],
],
]);

$client->search([
'body' => [
'query' => [
'match_all' => new \stdClass(),
],
],
]);

static::assertSame([
'/test/_search',
'/_search',
], $calledUrls);
}
}

0 comments on commit a975abc

Please sign in to comment.