Skip to content

Commit a141430

Browse files
committed
Add tests for event dispatch
1 parent 95b915a commit a141430

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

tests/Unit/Elastica/ClientTest.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@
1818
use Elastica\Response;
1919
use Elastica\Transport\NullTransport;
2020
use FOS\ElasticaBundle\Elastica\Client;
21+
use FOS\ElasticaBundle\Event\ElasticaRequestExceptionEvent;
22+
use FOS\ElasticaBundle\Event\PostElasticaRequestEvent;
23+
use FOS\ElasticaBundle\Event\PreElasticaRequestEvent;
2124
use FOS\ElasticaBundle\Logger\ElasticaLogger;
2225
use PHPUnit\Framework\TestCase;
26+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
2327

2428
/**
2529
* @internal
@@ -52,6 +56,114 @@ public function testRequestsAreLogged()
5256
$this->assertInstanceOf(Response::class, $response);
5357
}
5458

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+
$client->setEventDispatcher($dispatcher);
102+
$client->request('event', Request::GET, ['some' => 'data'], ['query' => 'data']);
103+
}
104+
105+
public function testSendsExceptionEvents(): void
106+
{
107+
$httpCode = 403;
108+
$responseString = JSON::stringify(['message' => 'some AWS error']);
109+
$transferInfo = [
110+
'request_header' => 'bar',
111+
'http_code' => $httpCode,
112+
'body' => $responseString,
113+
];
114+
$response = new Response($responseString);
115+
$response->setTransferInfo($transferInfo);
116+
117+
$connection = $this->getConnectionMock();
118+
$connection->method('getTransportObject')
119+
->willThrowException(new ClientException());
120+
121+
$client = $this->getClientMock($response, $connection);
122+
123+
$dispatcher = $this->createMock(EventDispatcherInterface::class);
124+
$dispatcher->expects($invoke = $this->exactly(2))
125+
->method('dispatch')
126+
->with($this->callback(function ($o) use ($invoke): bool {
127+
$counter = $invoke->getInvocationCount() - 1;
128+
129+
if ($counter > 1) {
130+
return false;
131+
}
132+
133+
if (0 === $counter) {
134+
if (!($o instanceof PreElasticaRequestEvent)) {
135+
return false;
136+
}
137+
138+
$this->assertEquals('event', $o->getPath());
139+
$this->assertEquals(Request::GET, $o->getMethod());
140+
$this->assertEquals(['some' => 'data'], $o->getData());
141+
$this->assertEquals(['query' => 'data'], $o->getQuery());
142+
$this->assertEquals(Request::DEFAULT_CONTENT_TYPE, $o->getContentType());
143+
} elseif (1 === $counter) {
144+
if (!($o instanceof ElasticaRequestExceptionEvent)) {
145+
return false;
146+
}
147+
148+
$request = $o->getRequest();
149+
150+
$this->assertEquals('event', $request->getPath());
151+
$this->assertEquals(Request::GET, $request->getMethod());
152+
$this->assertEquals(['some' => 'data'], $request->getData());
153+
$this->assertEquals(['query' => 'data'], $request->getQuery());
154+
$this->assertEquals(Request::DEFAULT_CONTENT_TYPE, $request->getContentType());
155+
156+
$this->assertInstanceOf(ClientException::class, $o->getException());
157+
}
158+
159+
return true;
160+
}));
161+
162+
$client->setEventDispatcher($dispatcher);
163+
$this->expectException(ClientException::class);
164+
$client->request('event', Request::GET, ['some' => 'data'], ['query' => 'data']);
165+
}
166+
55167
public function testRequestsWithTransportInfoErrorsRaiseExceptions()
56168
{
57169
$httpCode = 403;

0 commit comments

Comments
 (0)