Skip to content

Commit ab96eb6

Browse files
committed
Do deeper tests
1 parent ba2e001 commit ab96eb6

File tree

5 files changed

+122
-38
lines changed

5 files changed

+122
-38
lines changed

src/lib/Client/HttpPubSubTrait.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function publishEvent(
3030
array $metadata = [],
3131
string $contentType = 'application/json'
3232
): void {
33-
$this->publishEventAsync($pubsubName, $topicName, $data, $metadata, $contentType)->wait();
33+
$this->publishEventAsync($pubsubName, $topicName, $data, $metadata, $contentType)->wait(true);
3434
}
3535

3636
public function publishEventAsync(
@@ -45,7 +45,9 @@ public function publishEventAsync(
4545
...array_map(fn($key, $value) => ["metadata.$key" => $value], array_keys($metadata), $metadata)
4646
),
4747
'body' => $this->serializer->as_json($data),
48-
'header' => []
48+
'headers' => [
49+
'Content-Type' => $contentType,
50+
]
4951
];
5052
$pubsubName = rawurlencode($pubsubName);
5153
$topicName = rawurlencode($topicName);

src/lib/Client/PromiseHandlingTrait.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ private function handlePromise(
2626
}
2727
if (empty($errorTransformer)) {
2828
$errorTransformer = fn(\Throwable $exception) => match ($exception::class) {
29-
ServerException::class, ClientException::class => new DaprException(
29+
ServerException::class, ClientException::class => throw new DaprException(
3030
$exception->hasResponse()
3131
? $exception->getResponse()->getBody()->getContents()
3232
: $exception->getMessage(),
3333
$exception->getCode(),
3434
$exception
3535
),
36-
default => $exception
36+
default => throw $exception
3737
};
3838
}
3939
return $closure->then(

tests/DaprTests.php

+59
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
<?php
22

33
require_once __DIR__ . '/Mocks/DaprClient.php';
4+
require_once __DIR__ . '/Mocks/MockedHttpClientContainer.php';
45
require_once __DIR__ . '/../vendor/autoload.php';
56

67
use Dapr\Client\DaprClient as NewClient;
78
use Dapr\DaprClient;
9+
use Dapr\Mocks\MockedHttpClientContainer;
810
use Dapr\Mocks\TestClient;
911
use DI\Container;
1012
use DI\ContainerBuilder;
1113
use DI\DependencyException;
1214
use DI\NotFoundException;
15+
use GuzzleHttp\Handler\MockHandler;
16+
use GuzzleHttp\HandlerStack;
17+
use GuzzleHttp\Middleware;
1318
use PHPUnit\Framework\MockObject\MockObject;
1419
use PHPUnit\Framework\TestCase;
1520
use Psr\Log\LogLevel;
@@ -68,6 +73,35 @@ protected function get_client(): TestClient
6873
return $this->container->get(DaprClient::class);
6974
}
7075

76+
public function assertRequestMethod(string $expectedMethod, \GuzzleHttp\Psr7\Request $request): void
77+
{
78+
$this->assertSame($expectedMethod, $request->getMethod());
79+
}
80+
81+
public function assertRequestUri(string $expectedUri, \GuzzleHttp\Psr7\Request $request): void
82+
{
83+
$this->assertSame($expectedUri, $request->getUri()->getPath());
84+
}
85+
86+
public function assertRequestQueryString(string $expectedQuery, \GuzzleHttp\Psr7\Request $request): void
87+
{
88+
$this->assertSame($expectedQuery, $request->getUri()->getQuery());
89+
}
90+
91+
public function assertRequestHasHeaders(array $expectedHeaders, \GuzzleHttp\Psr7\Request $request): void
92+
{
93+
foreach ($expectedHeaders as $name => $header) {
94+
$this->assertTrue($request->hasHeader($name), 'Request does not have ' . $name);
95+
$actual = $request->getHeader($name);
96+
$this->assertTrue(in_array($header, $actual), "Request missing '$name: $header', found '$name: {$actual[0]}'");
97+
}
98+
}
99+
100+
public function assertRequestBody(string $body, \GuzzleHttp\Psr7\Request $request): void
101+
{
102+
$this->assertSame($body, $request->getBody()->getContents());
103+
}
104+
71105
protected function get_new_client(): NewClient|MockObject
72106
{
73107
$client = $this->createMock(NewClient::class);
@@ -78,6 +112,31 @@ protected function get_new_client(): NewClient|MockObject
78112
return $client;
79113
}
80114

115+
protected function get_new_client_with_http(\GuzzleHttp\Client|MockObject $mock): NewClient
116+
{
117+
$reflection = new ReflectionClass(\Dapr\Client\DaprHttpClient::class);
118+
$property = $reflection->getProperty('httpClient');
119+
$property->setAccessible(true);
120+
121+
$client = \Dapr\Client\DaprClient::clientBuilder()->build();
122+
$property->setValue($client, $mock);
123+
return $client;
124+
}
125+
126+
protected function get_http_client_stack(
127+
array $responseQueue = [],
128+
array|null &$history = null
129+
): MockedHttpClientContainer {
130+
$container = new MockedHttpClientContainer();
131+
$container->mock = new MockHandler($responseQueue);
132+
$history = Middleware::history($container->history);
133+
$container->handlerStack = HandlerStack::create($container->mock);
134+
$container->handlerStack->push($history);
135+
$container->client = new \GuzzleHttp\Client(['handler' => $container->handlerStack]);
136+
$history = &$container->history;
137+
return $container;
138+
}
139+
81140
protected function deserialize(string $json)
82141
{
83142
return json_decode($json, true);
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Dapr\Mocks;
4+
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\Handler\MockHandler;
7+
use GuzzleHttp\HandlerStack;
8+
9+
/**
10+
* Class MockedHttpClientContainer
11+
* @package Dapr\Mocks
12+
*/
13+
class MockedHttpClientContainer
14+
{
15+
public array $history = [];
16+
public HandlerStack $handlerStack;
17+
public MockHandler $mock;
18+
public Client $client;
19+
}

tests/PublishTest.php

+38-34
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,40 @@ class PublishTest extends DaprTests
1919
*/
2020
public function testSimplePublish()
2121
{
22-
$client = $this->get_new_client();
23-
$client->expects($this->once())->method('publishEvent')->with(
24-
$this->equalTo('pubsub'),
25-
$this->equalTo('topic'),
26-
$this->equalTo(['my' => 'event']),
27-
$this->equalTo([]),
28-
$this->equalTo('application/json')
22+
$container = $this->get_http_client_stack(
23+
[
24+
new \GuzzleHttp\Psr7\Response(204)
25+
]
2926
);
27+
$client = $this->get_new_client_with_http($container->client);
3028
$topic = new Topic('pubsub', 'topic', $client);
31-
$topic->publish(['my' => 'event']);
29+
$topic->publish(['my' => 'event'], ['test' => 'meta']);
30+
31+
$request = $container->history[0]['request'];
32+
$this->assertRequestMethod('POST', $request);
33+
$this->assertRequestUri('/v1.0/publish/pubsub/topic', $request);
34+
$this->assertRequestQueryString('metadata.test=meta', $request);
35+
$this->assertRequestHasHeaders(['Content-Type' => 'application/json'], $request);
36+
$this->assertRequestBody(json_encode(['my' => 'event']), $request);
3237
}
3338

3439
public function testBinaryPublish()
3540
{
36-
$client = $this->get_new_client();
37-
$client->expects($this->once())->method('publishEvent')->with(
38-
$this->equalTo('pubsub'),
39-
$this->equalTo('test'),
40-
$this->equalTo('data'),
41-
$this->equalTo([]),
42-
$this->equalTo('application/octet-stream')
41+
$container = $this->get_http_client_stack(
42+
[
43+
new \GuzzleHttp\Psr7\Response(204),
44+
]
4345
);
46+
$client = $this->get_new_client_with_http($container->client);
4447
$topic = new Topic('pubsub', 'test', $client);
4548
$topic->publish('data', content_type: 'application/octet-stream');
49+
50+
$request = $container->history[0]['request'];
51+
$this->assertRequestMethod('POST', $request);
52+
$this->assertRequestUri('/v1.0/publish/pubsub/test', $request);
53+
$this->assertRequestQueryString('', $request);
54+
$this->assertRequestHasHeaders(['Content-Type' => 'application/octet-stream'], $request);
55+
$this->assertRequestBody('"data"', $request);
4656
}
4757

4858
/**
@@ -51,6 +61,9 @@ public function testBinaryPublish()
5161
*/
5262
public function testCloudEventPublish()
5363
{
64+
$container = $this->get_http_client_stack([new \GuzzleHttp\Psr7\Response(204)]);
65+
$client = $this->get_new_client_with_http($container->client);
66+
5467
$event = new CloudEvent();
5568
$event->data = ['my' => 'event'];
5669
$event->type = 'type';
@@ -60,25 +73,6 @@ public function testCloudEventPublish()
6073
$event->source = 'source';
6174
$event->time = new DateTime('2020-12-12T20:47:00+00:00Z');
6275

63-
$client = $this->get_new_client();
64-
$client->expects($this->once())->method('publishEvent')->with(
65-
$this->equalTo('pubsub'),
66-
$this->equalTo('test'),
67-
$this->equalTo([
68-
'id' => 'id',
69-
'source' => 'source',
70-
'specversion' => '1.0',
71-
'type' => 'type',
72-
'datacontenttype' => 'application/json',
73-
'subject' => 'subject',
74-
'time' => '2020-12-12T20:47:00+00:00Z',
75-
'data' => [
76-
'my' => 'event',
77-
],
78-
]),
79-
$this->equalTo([]),
80-
$this->equalTo('application/cloudevents+json')
81-
);
8276
$topic = new Topic('pubsub', 'test', $client);
8377
$topic->publish($event);
8478

@@ -101,6 +95,16 @@ public function testCloudEventPublish()
10195
]
10296
);
10397
$publisher->topic('topic')->publish($event);
98+
99+
$request = $container->history[0]['request'];
100+
$this->assertRequestMethod('POST', $request);
101+
$this->assertRequestUri('/v1.0/publish/pubsub/test', $request);
102+
$this->assertRequestQueryString('', $request);
103+
$this->assertRequestHasHeaders(['Content-Type' => 'application/cloudevents+json'], $request);
104+
$this->assertRequestBody(
105+
'{"id":"id","source":"source","specversion":"1.0","type":"type","datacontenttype":"application\/json","subject":"subject","time":"2020-12-12T20:47:00+00:00Z","data":{"my":"event"}}',
106+
$request
107+
);
104108
}
105109

106110
/**

0 commit comments

Comments
 (0)