Skip to content

Commit

Permalink
Rework collector
Browse files Browse the repository at this point in the history
Fix tests
  • Loading branch information
tpatartmajeur committed Aug 23, 2024
1 parent a2c9641 commit 7511e5d
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 84 deletions.
37 changes: 22 additions & 15 deletions src/Elastica/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@

namespace FOS\ElasticaBundle\Elastica;

use Elastic\Elasticsearch\Response\Elasticsearch;
use Elastica\Client as BaseClient;
use Elastica\ClientConfiguration;
use Elastica\Exception\ClientException;
use Elastica\Exception\ExceptionInterface;
use Elastica\Index as BaseIndex;
use Elastica\Request;
use Elastica\Response;
use FOS\ElasticaBundle\Logger\ElasticaLogger;
use Psr\Http\Message\RequestInterface;
use Symfony\Component\Stopwatch\Stopwatch;

/**
Expand Down Expand Up @@ -50,26 +51,32 @@ class Client extends BaseClient
*/
private $stopwatch;

/**
* @param array<mixed> $data
* @param array<mixed> $query
*/
public function request(string $path, string $method = Request::GET, $data = [], array $query = [], string $contentType = Request::DEFAULT_CONTENT_TYPE): Response
public function request(string $path, string $method = Request::GET, $data = [], string $contentType = Request::DEFAULT_CONTENT_TYPE): Response
{
$headers = [
'Content-Type' => $contentType,
'Accept' => $contentType,
];

$request = $this->createRequest($method, $path, $headers, $data);
$es = $this->sendRequest($request);

return new Response($es->asString(), $es->getStatusCode());
}

public function sendRequest(RequestInterface $request): Elasticsearch
{
if ($this->stopwatch) {
$this->stopwatch->start('es_request', 'fos_elastica');
}

$query = $request->getBody()->__toString();

try {
$headers = [
'Content-Type' => $contentType,
'Accept' => $contentType,
];
$request = $this->createRequest($method, $path, $headers, $data);
$es = parent::sendRequest($request);
$response = new Response($es->asString(), $es->getStatusCode());
} catch (ExceptionInterface $e) {
$this->logQuery($path, $method, $data, $query, 0, 0, 0);
$this->logQuery($request->getUri(), $request->getMethod(), [], $query, 0, 0, 0);
throw $e;
}

Expand All @@ -90,16 +97,16 @@ public function request(string $path, string $method = Request::GET, $data = [],
}

if (isset($responseData['took'], $responseData['hits'])) {
$this->logQuery($path, $method, $data, $query, $responseData['took'], $response->getEngineTime(), $responseData['hits']['total']['value'] ?? 0);
$this->logQuery($request->getUri(), $request->getMethod(), $query, [], $responseData['took'], $response->getEngineTime(), $responseData['hits']['total']['value'] ?? 0);
} else {
$this->logQuery($path, $method, $data, $query, $responseData['took'] ?? 0, 0, 0);
$this->logQuery($request->getUri(), $request->getMethod(), $query, [], $responseData['took'] ?? 0, 0, 0);
}

if ($this->stopwatch) {
$this->stopwatch->stop('es_request');
}

return $response;
return $es;
}

public function getIndex(string $name): BaseIndex
Expand Down
25 changes: 10 additions & 15 deletions tests/Unit/Command/CreateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,15 @@ public function testExecuteWithIndexProvidedAndWithAlias()
->expects($this->exactly(2))
->method('getOption')
->withConsecutive(['index'], ['no-alias'])
->willReturnOnConsecutiveCalls($indexName, false)
;
->willReturnOnConsecutiveCalls($indexName, false);
$output->expects($this->once())->method('writeln');
$this->configManager->expects($this->once())->method('getIndexConfiguration')->with($indexName)->willReturn($this->indexConfig);
$this->indexManager->expects($this->once())->method('getIndex')->with($indexName)->willReturn($this->index);
$this->indexConfig->expects($this->exactly(2))->method('isUseAlias')->willReturn(true);
$this->indexConfig->expects($this->once())->method('getElasticSearchName')->willReturn($indexName);
$this->aliasProcessor->expects($this->once())->method('setRootName')->with($this->indexConfig, $this->index);
$this->mappingBuilder->expects($this->once())->method('buildIndexMapping')->with($this->indexConfig)->willReturn($mapping);
$this->index->expects($this->once())->method('create')->with(['mapping'], false);
$this->index->expects($this->once())->method('create')->with(['mapping']);
$this->index->expects($this->once())->method('addAlias')->with($indexName);

$this->command->run($input, $output);
Expand All @@ -120,15 +119,14 @@ public function testExecuteWithIndexProvidedAndWithAliasButDisabled()
->expects($this->exactly(2))
->method('getOption')
->withConsecutive(['index'], ['no-alias'])
->willReturnOnConsecutiveCalls($indexName, true)
;
->willReturnOnConsecutiveCalls($indexName, true);
$output->expects($this->once())->method('writeln');
$this->configManager->expects($this->once())->method('getIndexConfiguration')->with($indexName)->willReturn($this->indexConfig);
$this->indexManager->expects($this->once())->method('getIndex')->with($indexName)->willReturn($this->index);
$this->indexConfig->expects($this->exactly(2))->method('isUseAlias')->willReturn(true);
$this->aliasProcessor->expects($this->once())->method('setRootName')->with($this->indexConfig, $this->index);
$this->mappingBuilder->expects($this->once())->method('buildIndexMapping')->with($this->indexConfig)->willReturn($mapping);
$this->index->expects($this->once())->method('create')->with(['mapping'], false);
$this->index->expects($this->once())->method('create')->with(['mapping']);
$this->index->expects($this->never())->method('addAlias')->with($indexName);

$this->command->run($input, $output);
Expand All @@ -149,7 +147,7 @@ public function testExecuteWithIndexProvidedAndWithoutAlias()
$this->indexConfig->expects($this->exactly(2))->method('isUseAlias')->willReturn(false);
$this->aliasProcessor->expects($this->never())->method('setRootName');
$this->mappingBuilder->expects($this->once())->method('buildIndexMapping')->with($this->indexConfig)->willReturn($mapping);
$this->index->expects($this->once())->method('create')->with(['mapping'], false);
$this->index->expects($this->once())->method('create')->with(['mapping']);
$this->index->expects($this->never())->method('addAlias');

$this->command->run($input, $output);
Expand All @@ -174,13 +172,11 @@ public function testExecuteAllIndices()

$this->configManager->expects($this->exactly(2))->method('getIndexConfiguration')
->withConsecutive(['foo'], ['bar'])
->willReturnOnConsecutiveCalls($indexConfig1, $indexConfig2)
;
->willReturnOnConsecutiveCalls($indexConfig1, $indexConfig2);

$this->indexManager->expects($this->exactly(2))->method('getIndex')
->withConsecutive(['foo'], ['bar'])
->willReturnOnConsecutiveCalls($index1, $index2)
;
->willReturnOnConsecutiveCalls($index1, $index2);

$indexConfig1->expects($this->exactly(2))->method('isUseAlias')->willReturn(false);
$indexConfig2->expects($this->exactly(2))->method('isUseAlias')->willReturn(false);
Expand All @@ -189,12 +185,11 @@ public function testExecuteAllIndices()

$this->mappingBuilder->expects($this->exactly(2))->method('buildIndexMapping')
->withConsecutive([$indexConfig1], [$indexConfig2])
->willReturn($mapping)
;
->willReturn($mapping);

$index1->expects($this->once())->method('create')->with(['mapping'], false);
$index1->expects($this->once())->method('create')->with(['mapping']);
$index1->expects($this->never())->method('addAlias');
$index2->expects($this->once())->method('create')->with(['mapping'], false);
$index2->expects($this->once())->method('create')->with(['mapping']);
$index2->expects($this->never())->method('addAlias');

$this->command->run($input, $output);
Expand Down
Loading

0 comments on commit 7511e5d

Please sign in to comment.