Skip to content

Commit 2ed6bcb

Browse files
committed
Work on tests
Signed-off-by: Hendrik Heil <[email protected]>
1 parent 41a28c0 commit 2ed6bcb

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

src/lib/PubSub/Topic.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function publish(mixed $event, ?array $metadata = null, string $content_t
4848

4949
$event = match(get_class($event)) {
5050
'Dapr\PubSub\CloudEvent' => $event->to_array(),
51-
'CloudEvents\V1\CloudEvent' => JsonSerializer::create()->serializeStructured($event),
51+
'CloudEvents\V1\CloudEvent' => json_decode(JsonSerializer::create()->serializeStructured($event), true),
5252
};
5353
}
5454

tests/PublishTest.php

+55
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use Dapr\PubSub\CloudEvent;
44
use Dapr\PubSub\Publish;
55
use Dapr\PubSub\Topic;
6+
use CloudEvents\V1\CloudEvent as NewCloudEvent;
67
use DI\DependencyException;
78
use DI\NotFoundException;
89

@@ -107,6 +108,60 @@ public function testCloudEventPublish()
107108
);
108109
}
109110

111+
/**
112+
* @throws DependencyException
113+
* @throws NotFoundException
114+
*/
115+
public function testNewCloudEventPublish()
116+
{
117+
$container = $this->get_http_client_stack([new \GuzzleHttp\Psr7\Response(204)]);
118+
$client = $this->get_new_client_with_http($container->client);
119+
120+
$event = new NewCloudEvent(
121+
'id',
122+
'source',
123+
'type',
124+
['my' => 'event'],
125+
'application/json',
126+
null,
127+
'subject',
128+
new DateTimeImmutable('2020-12-12T20:47:00+00:00Z'),
129+
);
130+
131+
$topic = new Topic('pubsub', 'test', $client);
132+
$topic->publish($event);
133+
134+
$publisher = $this->container->make(Publish::class, ['pubsub' => 'pubsub']);
135+
$this->get_client()->register_post(
136+
'/publish/pubsub/topic',
137+
200,
138+
null,
139+
[
140+
'id' => 'id',
141+
'source' => 'source',
142+
'specversion' => '1.0',
143+
'type' => 'type',
144+
'datacontenttype' => 'application/json',
145+
'subject' => 'subject',
146+
'time' => '2020-12-12T20:47:00+00:00Z',
147+
'data' => [
148+
'my' => 'event',
149+
],
150+
]
151+
);
152+
$publisher->topic('topic')->publish($event);
153+
154+
$request = $container->history[0]['request'];
155+
$this->assertRequestMethod('POST', $request);
156+
$this->assertRequestUri('/v1.0/publish/pubsub/test', $request);
157+
$this->assertRequestQueryString('', $request);
158+
$this->assertRequestHasHeaders(['Content-Type' => 'application/cloudevents+json'], $request);
159+
$this->assertRequestBody(
160+
'{"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"}}',
161+
$request
162+
);
163+
}
164+
110165
/**
111166
* @throws Exception
112167
*/

0 commit comments

Comments
 (0)