Skip to content

Commit 59797d1

Browse files
committed
Integrate Client events.
1 parent 9461c7a commit 59797d1

File tree

5 files changed

+85
-50
lines changed

5 files changed

+85
-50
lines changed

CHANGELOG-7.0.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ in 7.x versions.
1717
* **[BC break]** Configuration options: `host`, `port`, `url` are no longer available and replaced with single `hosts`.
1818
* **[BC break]** Configuration options: `proxy`, `auth_type`, `aws_*`, `ssl`, `curl`, `persistent`, `compression`, `connectTimeout` are no longer available.
1919
* **[BC break]** Configuration `connectionStrategy` is renamed to `connection_strategy`.
20+
* **[BC break]** Event `PostElasticaRequestEvent` now operates with `Psr\Http\Message\RequestInterface` instead of `Elastica\Request`.
21+
* **[BC break]** Event `ElasticaRequestExceptionEvent` now operates with `Psr\Http\Message\RequestInterface` instead of `Elastica\Request`, and `Elastic\Elasticsearch\Exception\ElasticsearchException` instead of `Elastica\Exception\ExceptionInterface`.
2022

2123
Main change is the configuration of the bundle:
2224
* There are no `connections` level anymore.

src/Elastica/Client.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
use Elastic\Elasticsearch\Response\Elasticsearch;
1717
use Elastica\Client as BaseClient;
1818
use Elastica\Exception\ClientException;
19-
use FOS\ElasticaBundle\Logger\ElasticaLogger;
2019
use FOS\ElasticaBundle\Event\ElasticaRequestExceptionEvent;
2120
use FOS\ElasticaBundle\Event\PostElasticaRequestEvent;
2221
use FOS\ElasticaBundle\Event\PreElasticaRequestEvent;
22+
use FOS\ElasticaBundle\Logger\ElasticaLogger;
2323
use Psr\Http\Message\RequestInterface;
2424
use Psr\Log\LoggerInterface;
2525
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@@ -82,38 +82,39 @@ public function sendRequest(RequestInterface $request): Elasticsearch
8282
$query = [];
8383
\parse_str($request->getUri()->getQuery(), $query);
8484

85-
if ($this->dispatcher) {
86-
$this->dispatcher->dispatch(new PreElasticaRequestEvent($path, $method, $data, $query, $contentType));
85+
if (null !== $this->dispatcher) {
86+
$this->dispatcher->dispatch(new PreElasticaRequestEvent($path, $method, $data, $query, $request->getHeaderLine('Content-Type')));
8787
}
8888

8989
$start = \microtime(true);
9090
try {
9191
$elasticResponse = parent::sendRequest($request);
9292
$response = $this->toElasticaResponse($elasticResponse);
9393

94-
if ($this->dispatcher) {
94+
if (null !== $this->dispatcher) {
9595
$this->dispatcher->dispatch(new PostElasticaRequestEvent($request, $response));
9696
}
9797
} catch (ClientResponseException $responseException) {
9898
$this->logQuery($path, $method, $data, $query, 0.0, 0, 0);
9999

100100
$response = $responseException->getResponse();
101+
102+
if (null !== $this->dispatcher) {
103+
$this->dispatcher->dispatch(new ElasticaRequestExceptionEvent($request, $responseException));
104+
}
105+
101106
if (\in_array($response->getStatusCode(), $this->forbiddenCodes, true)) {
102107
$body = (string) $response->getBody();
103108
$message = \sprintf('Error in transportInfo: response code is %s, response body is %s', $response->getStatusCode(), $body);
104-
throw new ClientException($message);
105-
}
106-
107-
if ($this->dispatcher) {
108-
$this->dispatcher->dispatch(new ElasticaRequestExceptionEvent($this->getLastRequest(), $e));
109+
throw new ClientException($message, 0, $responseException);
109110
}
110111

111112
throw $responseException;
112113
} catch (ElasticsearchException $e) {
113114
$this->logQuery($path, $method, $data, $query, 0.0, 0, 0);
114115

115-
if ($this->dispatcher) {
116-
$this->dispatcher->dispatch(new ElasticaRequestExceptionEvent($this->getLastRequest(), $e));
116+
if (null !== $this->dispatcher) {
117+
$this->dispatcher->dispatch(new ElasticaRequestExceptionEvent($request, $e));
117118
}
118119

119120
throw $e;

src/Event/ElasticaRequestExceptionEvent.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,28 @@
1111

1212
namespace FOS\ElasticaBundle\Event;
1313

14-
use Elastica\Exception\ExceptionInterface;
15-
use Elastica\Request;
14+
use Elastic\Elasticsearch\Exception\ElasticsearchException;
15+
use Psr\Http\Message\RequestInterface;
1616

1717
class ElasticaRequestExceptionEvent
1818
{
19-
private Request $request;
20-
private ExceptionInterface $exception;
19+
private RequestInterface $request;
20+
private ElasticsearchException $exception;
2121

2222
public function __construct(
23-
Request $request,
24-
ExceptionInterface $exception
23+
RequestInterface $request,
24+
ElasticsearchException $exception
2525
) {
2626
$this->request = $request;
2727
$this->exception = $exception;
2828
}
2929

30-
public function getRequest(): Request
30+
public function getRequest(): RequestInterface
3131
{
3232
return $this->request;
3333
}
3434

35-
public function getException(): ExceptionInterface
35+
public function getException(): ElasticsearchException
3636
{
3737
return $this->exception;
3838
}

src/Event/PostElasticaRequestEvent.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@
1111

1212
namespace FOS\ElasticaBundle\Event;
1313

14-
use Elastica\Request;
1514
use Elastica\Response;
15+
use Psr\Http\Message\RequestInterface;
1616

1717
class PostElasticaRequestEvent
1818
{
19-
private Request $request;
19+
private RequestInterface $request;
2020
private Response $response;
2121

2222
public function __construct(
23-
Request $request,
23+
RequestInterface $request,
2424
Response $response
2525
) {
2626
$this->request = $request;
2727
$this->response = $response;
2828
}
2929

30-
public function getRequest(): Request
30+
public function getRequest(): RequestInterface
3131
{
3232
return $this->request;
3333
}

tests/Unit/Elastica/ClientTest.php

Lines changed: 59 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111

1212
namespace FOS\ElasticaBundle\Tests\Unit\Elastica;
1313

14+
use Elastic\Elasticsearch\Exception\ElasticsearchException;
1415
use Elastic\Elasticsearch\Response\Elasticsearch;
1516
use Elastica\Exception\ClientException;
1617
use Elastica\JSON;
1718
use Elastica\Request;
19+
use Elastica\Response;
1820
use FOS\ElasticaBundle\Elastica\Client;
1921
use FOS\ElasticaBundle\Event\ElasticaRequestExceptionEvent;
2022
use FOS\ElasticaBundle\Event\PostElasticaRequestEvent;
@@ -66,7 +68,6 @@ public function testRequestsAreLogged()
6668

6769
public function testSendsNormalEvents(): void
6870
{
69-
$client = $this->getClientMock();
7071
$dispatcher = $this->createMock(EventDispatcherInterface::class);
7172
$dispatcher->expects($invoke = $this->exactly(2))
7273
->method('dispatch')
@@ -94,11 +95,21 @@ public function testSendsNormalEvents(): void
9495

9596
$request = $o->getRequest();
9697

97-
$this->assertEquals('event', $request->getPath());
98-
$this->assertEquals(Request::GET, $request->getMethod());
99-
$this->assertEquals(['some' => 'data'], $request->getData());
100-
$this->assertEquals(['query' => 'data'], $request->getQuery());
101-
$this->assertEquals(Request::DEFAULT_CONTENT_TYPE, $request->getContentType());
98+
$path = \ltrim($request->getUri()->getPath(), '/'); // to have the same result as in the 6.0
99+
$method = $request->getMethod();
100+
try {
101+
$data = \json_decode((string) $request->getBody(), true, 512, \JSON_THROW_ON_ERROR);
102+
} catch (\JsonException) {
103+
$data = [];
104+
}
105+
$query = [];
106+
\parse_str($request->getUri()->getQuery(), $query);
107+
108+
$this->assertEquals('event', $path);
109+
$this->assertEquals(Request::GET, $method);
110+
$this->assertEquals(['some' => 'data'], $data);
111+
$this->assertEquals(['query' => 'data'], $query);
112+
$this->assertEquals(Request::DEFAULT_CONTENT_TYPE, $request->getHeaderLine('Content-Type'));
102113

103114
$this->assertInstanceOf(Response::class, $o->getResponse());
104115
}
@@ -107,28 +118,34 @@ public function testSendsNormalEvents(): void
107118
}))
108119
;
109120

121+
$response = new \GuzzleHttp\Psr7\Response(
122+
200,
123+
['Content-Type' => 'application/json', Elasticsearch::HEADER_CHECK => Elasticsearch::PRODUCT_NAME],
124+
\json_encode(['foo' => 'bar'], \JSON_THROW_ON_ERROR)
125+
);
126+
$logger = $this->createMock(ElasticaLogger::class);
127+
$client = $this->getClient($logger, $response);
110128
$client->setEventDispatcher($dispatcher);
111-
$client->request('event', Request::GET, ['some' => 'data'], ['query' => 'data']);
129+
130+
$client->sendRequest(new \GuzzleHttp\Psr7\Request(
131+
Request::GET,
132+
'https://some.tld/event?'.\http_build_query(['query' => 'data']),
133+
['Content-Type' => 'application/json'],
134+
\json_encode(['some' => 'data'], \JSON_THROW_ON_ERROR)
135+
));
112136
}
113137

114138
public function testSendsExceptionEvents(): void
115139
{
116140
$httpCode = 403;
117141
$responseString = JSON::stringify(['message' => 'some AWS error']);
118-
$transferInfo = [
119-
'request_header' => 'bar',
120-
'http_code' => $httpCode,
121-
'body' => $responseString,
122-
];
123-
$response = new Response($responseString);
124-
$response->setTransferInfo($transferInfo);
125-
126-
$connection = $this->getConnectionMock();
127-
$connection->method('getTransportObject')
128-
->willThrowException(new ClientException())
129-
;
142+
$response = new \GuzzleHttp\Psr7\Response(
143+
$httpCode,
144+
['Content-Type' => 'application/json', Elasticsearch::HEADER_CHECK => Elasticsearch::PRODUCT_NAME],
145+
$responseString
146+
);
130147

131-
$client = $this->getClientMock($response, $connection);
148+
$client = $this->getClient($this->createMock(LoggerInterface::class), $response);
132149

133150
$dispatcher = $this->createMock(EventDispatcherInterface::class);
134151
$dispatcher->expects($invoke = $this->exactly(2))
@@ -157,13 +174,23 @@ public function testSendsExceptionEvents(): void
157174

158175
$request = $o->getRequest();
159176

160-
$this->assertEquals('event', $request->getPath());
161-
$this->assertEquals(Request::GET, $request->getMethod());
162-
$this->assertEquals(['some' => 'data'], $request->getData());
163-
$this->assertEquals(['query' => 'data'], $request->getQuery());
164-
$this->assertEquals(Request::DEFAULT_CONTENT_TYPE, $request->getContentType());
177+
$path = \ltrim($request->getUri()->getPath(), '/'); // to have the same result as in the 6.0
178+
$method = $request->getMethod();
179+
try {
180+
$data = \json_decode((string) $request->getBody(), true, 512, \JSON_THROW_ON_ERROR);
181+
} catch (\JsonException) {
182+
$data = [];
183+
}
184+
$query = [];
185+
\parse_str($request->getUri()->getQuery(), $query);
186+
187+
$this->assertEquals('event', $path);
188+
$this->assertEquals(Request::GET, $method);
189+
$this->assertEquals(['some' => 'data'], $data);
190+
$this->assertEquals(['query' => 'data'], $query);
191+
$this->assertEquals(Request::DEFAULT_CONTENT_TYPE, $request->getHeaderLine('Content-Type'));
165192

166-
$this->assertInstanceOf(ClientException::class, $o->getException());
193+
$this->assertInstanceOf(ElasticsearchException::class, $o->getException());
167194
}
168195

169196
return true;
@@ -172,7 +199,12 @@ public function testSendsExceptionEvents(): void
172199

173200
$client->setEventDispatcher($dispatcher);
174201
$this->expectException(ClientException::class);
175-
$client->request('event', Request::GET, ['some' => 'data'], ['query' => 'data']);
202+
$client->sendRequest(new \GuzzleHttp\Psr7\Request(
203+
Request::GET,
204+
'https://some.tld/event?'.\http_build_query(['query' => 'data']),
205+
['Content-Type' => 'application/json'],
206+
\json_encode(['some' => 'data'], \JSON_THROW_ON_ERROR)
207+
));
176208
}
177209

178210
public function testRequestsWithTransportInfoErrorsRaiseExceptions()

0 commit comments

Comments
 (0)