Skip to content

Commit 3adc584

Browse files
Merge pull request #35 from dapr/fix/uncomment-cloudevent-tests
Uncomment empty subject check
2 parents c7e3287 + cee0b54 commit 3adc584

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/index.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,15 +420,16 @@ function test_pubsub(): void
420420
unlink('/tmp/sub-received');
421421
//unset($event->time);
422422
$event->topic = "test";
423-
$event->pubsubname = "pubsub";
423+
$event->pubsub_name = "pubsub";
424424
echo "Expecting this data:\n";
425425
echo json_encode(json_decode($event->to_json()), JSON_PRETTY_PRINT)."\n";
426426
$received = CloudEvent::parse($raw_event);
427+
unset($received->trace_id);
427428
echo json_encode(json_decode($received->to_json()), JSON_PRETTY_PRINT)."\n";
428429
assert_equals(
429430
$event->to_json(),
430431
$received->to_json(),
431-
'Event should be the same event we sent.'
432+
'Event should be the same event we sent, minus the trace id.'
432433
);
433434

434435
echo "\n\nPublishing raw event";

src/lib/PubSub/CloudEvent.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,21 @@ class CloudEvent
9696
*/
9797
public $data;
9898

99+
/**
100+
* @var string|null The trace id
101+
*/
102+
public ?string $trace_id;
103+
104+
/**
105+
* @var string|null The topic
106+
*/
107+
public ?string $topic;
108+
109+
/**
110+
* @var string|null The name of the pubsub
111+
*/
112+
public ?string $pubsub_name;
113+
99114
public function __construct()
100115
{
101116
}
@@ -116,6 +131,9 @@ public static function parse(string $json): CloudEvent
116131
$event->type = (string)$raw['type'];
117132
$event->data_content_type = $raw['datacontenttype'] ?? null;
118133
$event->subject = $raw['subject'] ?? null;
134+
$event->pubsub_name = $raw['pubsubname'] ?? null;
135+
$event->topic = $raw['topic'] ?? null;
136+
$event->trace_id = $raw['traceid'] ?? null;
119137
$time = $raw['time'] ?? null;
120138
if ( ! empty($time)) {
121139
$event->time = new \DateTime($time);
@@ -131,6 +149,7 @@ public static function parse(string $json): CloudEvent
131149
public function to_json(): string|bool
132150
{
133151
Runtime::$logger?->debug('Serializing cloud event');
152+
134153
return json_encode($this->to_array());
135154
}
136155

@@ -158,6 +177,9 @@ public function to_array(): array
158177
if (isset($this->data)) {
159178
$json['data'] = $this->data;
160179
}
180+
if (isset($this->trace_id)) {
181+
$json['traceid'] = $this->trace_id;
182+
}
161183

162184
return $json;
163185
}
@@ -184,11 +206,9 @@ public function validate(): bool
184206
return false;
185207
}
186208

187-
// for non-custom events, the subject is an empty string
188-
/*
189209
if (isset($this->subject) && empty($this->subject)) {
190210
return false;
191-
}*/
211+
}
192212

193213
return true;
194214
}

0 commit comments

Comments
 (0)