Skip to content

Commit 4dcc8ce

Browse files
committed
Fix tests and refactor to CloudEventInterface
Signed-off-by: Hendrik Heil <[email protected]>
1 parent bc43418 commit 4dcc8ce

File tree

3 files changed

+18
-21
lines changed

3 files changed

+18
-21
lines changed

daprdocs/content/en/php-sdk-docs/php-pubsub/_index.md

+7-11
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,25 @@ For more information about publish/subscribe, check out [the howto]({{< ref howt
2121

2222
## Data content type
2323

24-
The PHP SDK allows setting the data content type either when constructing a custom cloud event, or when publishing raw
25-
data.
24+
The PHP SDK allows setting the data content type either when constructing a custom cloud event, or when publishing raw data.
2625

2726
{{< tabs CloudEvent "Raw" >}}
2827

29-
{{% codetab %}}
28+
To create a custom CloudEvent please take a look at the official documentation of [`cloudevents/sdk-php`](https://github.com/cloudevents/sdk-php/blob/602cd26557e5522060531b3103450b34b678be1c/README.md).
3029

31-
```php
32-
<?php
33-
$event = new \Dapr\PubSub\CloudEvent();
34-
$event->data = $xml;
35-
$event->data_content_type = 'application/xml';
36-
```
30+
To publish a CloudEvent:
3731

38-
{{% /codetab %}}
3932
{{% codetab %}}
4033

4134
```php
4235
<?php
4336
/**
4437
* @var \Dapr\Client\DaprClient $daprClient
38+
* @var \Dapr\PubSub\Topic $daprTopic
39+
* @var \CloudEvents\V1\CloudEventInterface $cloudEvent
4540
*/
46-
$daprClient->publishEvent(pubsubName: 'pubsub', topicName: 'my-topic', data: $raw_data, contentType: 'application/octet-stream');
41+
$daprTopic = new Topic(pubsub: 'pubsub', topic: 'my-topic', $daprClient);
42+
$daprTopic->publish($cloudEvent);
4743
```
4844

4945
{{% alert title="Binary data" color="warning" %}}

src/lib/PubSub/Topic.php

+8-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Dapr\exceptions\DaprException;
88
use JetBrains\PhpStorm\Deprecated;
99
use Psr\Log\LoggerInterface;
10-
use CloudEvents\V1\CloudEvent as NewCloudEvent;
10+
use CloudEvents\V1\CloudEventInterface;
1111
use CloudEvents\Serializers\JsonSerializer;
1212

1313
/**
@@ -27,7 +27,7 @@ public function __construct(
2727
/**
2828
* Publish an event to the topic
2929
*
30-
* @param CloudEvent|mixed $event The event to publish
30+
* @param CloudEventInterface|CloudEvent|mixed $event The event to publish
3131
* @param array|null $metadata Additional metadata to pass to the component
3232
* @param string $content_type The header to include in the publish request. Ignored when $event is a CloudEvent
3333
*
@@ -40,16 +40,17 @@ public function publish(mixed $event, ?array $metadata = null, string $content_t
4040
} elseif ($this->client instanceof NewClient) {
4141
$this->client->logger->debug('Sending {event} to {topic}', ['event' => $event, 'topic' => $this->topic]);
4242
}
43-
if ($event instanceof CloudEvent || $event instanceof NewCloudEvent) {
43+
if ($event instanceof CloudEvent || $event instanceof CloudEventInterface) {
4444
$content_type = 'application/cloudevents+json';
4545
$this->client->extra_headers = [
4646
'Content-Type: application/cloudevents+json',
4747
];
4848

49-
$event = match(get_class($event)) {
50-
'Dapr\PubSub\CloudEvent' => $event->to_array(),
51-
'CloudEvents\V1\CloudEvent' => json_decode(JsonSerializer::create()->serializeStructured($event), true),
52-
};
49+
if ($event instanceof CloudEvent) {
50+
$event = $event->to_array();
51+
} elseif ($event instanceof CloudEventInterface) {
52+
$event = json_decode(JsonSerializer::create()->serializeStructured($event), true);
53+
}
5354
}
5455

5556
if ($this->client instanceof DaprClient) {

tests/PublishTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,13 @@ public function testNewCloudEventPublish()
137137
200,
138138
null,
139139
[
140+
'specversion' => '1.0',
140141
'id' => 'id',
141142
'source' => 'source',
142-
'specversion' => '1.0',
143143
'type' => 'type',
144144
'datacontenttype' => 'application/json',
145145
'subject' => 'subject',
146-
'time' => '2020-12-12T20:47:00+00:00Z',
146+
'time' => '2020-12-12T20:47:00Z',
147147
'data' => [
148148
'my' => 'event',
149149
],
@@ -157,7 +157,7 @@ public function testNewCloudEventPublish()
157157
$this->assertRequestQueryString('', $request);
158158
$this->assertRequestHasHeaders(['Content-Type' => 'application/cloudevents+json'], $request);
159159
$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"}}',
160+
'{"specversion":"1.0","id":"id","source":"source","type":"type","datacontenttype":"application\/json","subject":"subject","time":"2020-12-12T20:47:00Z","data":{"my":"event"}}',
161161
$request
162162
);
163163
}

0 commit comments

Comments
 (0)