File tree Expand file tree Collapse file tree 2 files changed +46
-4
lines changed Expand file tree Collapse file tree 2 files changed +46
-4
lines changed Original file line number Diff line number Diff line change @@ -47,7 +47,7 @@ public function __construct(
47
47
?string $ datacontenttype ,
48
48
?string $ dataschema ,
49
49
?string $ subject ,
50
- ? string $ time ,
50
+ $ time ,
51
51
$ data
52
52
) {
53
53
$ this ->id = $ id ;
@@ -154,10 +154,16 @@ public function __toString()
154
154
return $ output ;
155
155
}
156
156
157
- private static function timeAsDateTime (? string $ time ): ?DateTime
157
+ private static function timeAsDateTime ($ time ): ?DateTime
158
158
{
159
- if (is_null ($ time )) {
160
- return null ;
159
+ if ($ time instanceof DateTime || is_null ($ time )) {
160
+ return $ time ;
161
+ }
162
+
163
+ if (!is_string ($ time )) {
164
+ throw new \UnexpectedValueException (
165
+ 'time must be a string, DateTime, or null '
166
+ );
161
167
}
162
168
163
169
return new DateTime ($ time );
Original file line number Diff line number Diff line change @@ -68,4 +68,40 @@ public function testJsonSerialize()
68
68
69
69
$ this ->assertEquals (json_encode ($ event , JSON_PRETTY_PRINT ), $ want );
70
70
}
71
+
72
+ public function testTimeCanBeDateTime ()
73
+ {
74
+ $ time = new \DateTime ();
75
+ $ event = new CloudEvent (
76
+ '1413058901901494 ' ,
77
+ '//pubsub.googleapis.com/projects/MY-PROJECT/topics/MY-TOPIC ' ,
78
+ '1.0 ' ,
79
+ 'com.google.cloud.pubsub.topic.publish ' ,
80
+ 'application/json ' ,
81
+ 'type.googleapis.com/google.logging.v2.LogEntry ' ,
82
+ 'My Subject ' ,
83
+ $ time , // can be a datetime
84
+ null
85
+ );
86
+
87
+ $ this ->assertEquals ($ time , $ event ->getTime ());
88
+ }
89
+
90
+ public function testInvalidTime ()
91
+ {
92
+ $ this ->expectException (\UnexpectedValueException::class);
93
+ $ this ->expectExceptionMessage ('time must be a string, DateTime, or null ' );
94
+
95
+ $ event = new CloudEvent (
96
+ '1413058901901494 ' ,
97
+ '//pubsub.googleapis.com/projects/MY-PROJECT/topics/MY-TOPIC ' ,
98
+ '1.0 ' ,
99
+ 'com.google.cloud.pubsub.topic.publish ' ,
100
+ 'application/json ' ,
101
+ 'type.googleapis.com/google.logging.v2.LogEntry ' ,
102
+ 'My Subject ' ,
103
+ 123456 , // not a string, datetime, or null
104
+ null
105
+ );
106
+ }
71
107
}
You can’t perform that action at this time.
0 commit comments