|
18 | 18 | use Elastica\Response;
|
19 | 19 | use Elastica\Transport\NullTransport;
|
20 | 20 | use FOS\ElasticaBundle\Elastica\Client;
|
| 21 | +use FOS\ElasticaBundle\Event\ElasticaRequestExceptionEvent; |
| 22 | +use FOS\ElasticaBundle\Event\PostElasticaRequestEvent; |
| 23 | +use FOS\ElasticaBundle\Event\PreElasticaRequestEvent; |
21 | 24 | use FOS\ElasticaBundle\Logger\ElasticaLogger;
|
22 | 25 | use PHPUnit\Framework\TestCase;
|
| 26 | +use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
23 | 27 |
|
24 | 28 | /**
|
25 | 29 | * @internal
|
@@ -52,6 +56,117 @@ public function testRequestsAreLogged()
|
52 | 56 | $this->assertInstanceOf(Response::class, $response);
|
53 | 57 | }
|
54 | 58 |
|
| 59 | + public function testSendsNormalEvents(): void |
| 60 | + { |
| 61 | + $client = $this->getClientMock(); |
| 62 | + $dispatcher = $this->createMock(EventDispatcherInterface::class); |
| 63 | + $dispatcher->expects($invoke = $this->exactly(2)) |
| 64 | + ->method('dispatch') |
| 65 | + ->with($this->callback(function ($o) use ($invoke): bool { |
| 66 | + $counter = $invoke->getInvocationCount() - 1; |
| 67 | + |
| 68 | + if ($counter > 1) { |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + if (0 === $counter) { |
| 73 | + if (!($o instanceof PreElasticaRequestEvent)) { |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + $this->assertEquals('event', $o->getPath()); |
| 78 | + $this->assertEquals(Request::GET, $o->getMethod()); |
| 79 | + $this->assertEquals(['some' => 'data'], $o->getData()); |
| 80 | + $this->assertEquals(['query' => 'data'], $o->getQuery()); |
| 81 | + $this->assertEquals(Request::DEFAULT_CONTENT_TYPE, $o->getContentType()); |
| 82 | + } elseif (1 === $counter) { |
| 83 | + if (!($o instanceof PostElasticaRequestEvent)) { |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + $request = $o->getRequest(); |
| 88 | + |
| 89 | + $this->assertEquals('event', $request->getPath()); |
| 90 | + $this->assertEquals(Request::GET, $request->getMethod()); |
| 91 | + $this->assertEquals(['some' => 'data'], $request->getData()); |
| 92 | + $this->assertEquals(['query' => 'data'], $request->getQuery()); |
| 93 | + $this->assertEquals(Request::DEFAULT_CONTENT_TYPE, $request->getContentType()); |
| 94 | + |
| 95 | + $this->assertInstanceOf(Response::class, $o->getResponse()); |
| 96 | + } |
| 97 | + |
| 98 | + return true; |
| 99 | + })) |
| 100 | + ; |
| 101 | + |
| 102 | + $client->setEventDispatcher($dispatcher); |
| 103 | + $client->request('event', Request::GET, ['some' => 'data'], ['query' => 'data']); |
| 104 | + } |
| 105 | + |
| 106 | + public function testSendsExceptionEvents(): void |
| 107 | + { |
| 108 | + $httpCode = 403; |
| 109 | + $responseString = JSON::stringify(['message' => 'some AWS error']); |
| 110 | + $transferInfo = [ |
| 111 | + 'request_header' => 'bar', |
| 112 | + 'http_code' => $httpCode, |
| 113 | + 'body' => $responseString, |
| 114 | + ]; |
| 115 | + $response = new Response($responseString); |
| 116 | + $response->setTransferInfo($transferInfo); |
| 117 | + |
| 118 | + $connection = $this->getConnectionMock(); |
| 119 | + $connection->method('getTransportObject') |
| 120 | + ->willThrowException(new ClientException()) |
| 121 | + ; |
| 122 | + |
| 123 | + $client = $this->getClientMock($response, $connection); |
| 124 | + |
| 125 | + $dispatcher = $this->createMock(EventDispatcherInterface::class); |
| 126 | + $dispatcher->expects($invoke = $this->exactly(2)) |
| 127 | + ->method('dispatch') |
| 128 | + ->with($this->callback(function ($o) use ($invoke): bool { |
| 129 | + $counter = $invoke->getInvocationCount() - 1; |
| 130 | + |
| 131 | + if ($counter > 1) { |
| 132 | + return false; |
| 133 | + } |
| 134 | + |
| 135 | + if (0 === $counter) { |
| 136 | + if (!($o instanceof PreElasticaRequestEvent)) { |
| 137 | + return false; |
| 138 | + } |
| 139 | + |
| 140 | + $this->assertEquals('event', $o->getPath()); |
| 141 | + $this->assertEquals(Request::GET, $o->getMethod()); |
| 142 | + $this->assertEquals(['some' => 'data'], $o->getData()); |
| 143 | + $this->assertEquals(['query' => 'data'], $o->getQuery()); |
| 144 | + $this->assertEquals(Request::DEFAULT_CONTENT_TYPE, $o->getContentType()); |
| 145 | + } elseif (1 === $counter) { |
| 146 | + if (!($o instanceof ElasticaRequestExceptionEvent)) { |
| 147 | + return false; |
| 148 | + } |
| 149 | + |
| 150 | + $request = $o->getRequest(); |
| 151 | + |
| 152 | + $this->assertEquals('event', $request->getPath()); |
| 153 | + $this->assertEquals(Request::GET, $request->getMethod()); |
| 154 | + $this->assertEquals(['some' => 'data'], $request->getData()); |
| 155 | + $this->assertEquals(['query' => 'data'], $request->getQuery()); |
| 156 | + $this->assertEquals(Request::DEFAULT_CONTENT_TYPE, $request->getContentType()); |
| 157 | + |
| 158 | + $this->assertInstanceOf(ClientException::class, $o->getException()); |
| 159 | + } |
| 160 | + |
| 161 | + return true; |
| 162 | + })) |
| 163 | + ; |
| 164 | + |
| 165 | + $client->setEventDispatcher($dispatcher); |
| 166 | + $this->expectException(ClientException::class); |
| 167 | + $client->request('event', Request::GET, ['some' => 'data'], ['query' => 'data']); |
| 168 | + } |
| 169 | + |
55 | 170 | public function testRequestsWithTransportInfoErrorsRaiseExceptions()
|
56 | 171 | {
|
57 | 172 | $httpCode = 403;
|
|
0 commit comments