1111
1212namespace FOS \ElasticaBundle \Tests \Unit \Elastica ;
1313
14+ use Elastic \Elasticsearch \Exception \ElasticsearchException ;
1415use Elastic \Elasticsearch \Response \Elasticsearch ;
1516use Elastica \Exception \ClientException ;
1617use Elastica \JSON ;
1718use Elastica \Request ;
19+ use Elastica \Response ;
1820use FOS \ElasticaBundle \Elastica \Client ;
1921use FOS \ElasticaBundle \Event \ElasticaRequestExceptionEvent ;
2022use 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