Skip to content

Commit 57f7e42

Browse files
feat(PubSub): Add samples to create topics and subscriptions with SMT support (#2179)
1 parent 337644a commit 57f7e42

File tree

3 files changed

+180
-0
lines changed

3 files changed

+180
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Copyright 2026 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/blob/main/pubsub/api/README.md
22+
*/
23+
namespace Google\Cloud\Samples\PubSub;
24+
25+
# [START pubsub_create_subscription_with_smt]
26+
use Google\Cloud\PubSub\PubSubClient;
27+
use Google\Cloud\PubSub\Topic;
28+
29+
/**
30+
* Create a topic with a Single Message Transform function
31+
*
32+
* @param string $projectId
33+
* @param string $topicId
34+
* @param string $subscriptionName
35+
*/
36+
function create_subscription_with_smt(
37+
string $projectId,
38+
string $topicId,
39+
string $subscriptionName
40+
) {
41+
$pubsub = new PubSubClient([
42+
'projectId' => $projectId
43+
]);
44+
45+
$functionName = 'toUpper';
46+
$code = 'function toUpper(message, metadata){
47+
message.data = message.data.toUpperCase();
48+
return message;
49+
}';
50+
51+
$smtConfig = [
52+
'javascriptUdf' => [
53+
'functionName' => $functionName,
54+
'code' => $code,
55+
]
56+
];
57+
58+
$subscriptionConfig = [
59+
'messageTransforms' => [
60+
$smtConfig
61+
]
62+
];
63+
64+
$subscription = $pubsub->subscribe($subscriptionName, $topicId, $subscriptionConfig);
65+
66+
printf('Subscription with SMT %s created', $subscription->name());
67+
}
68+
# [END pubsub_create_subscription_with_smt]
69+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
70+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Copyright 2026 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/blob/main/pubsub/api/README.md
22+
*/
23+
namespace Google\Cloud\Samples\PubSub;
24+
25+
# [START pubsub_create_topic_with_smt]
26+
use Google\Cloud\PubSub\PubSubClient;
27+
use Google\Cloud\PubSub\Topic;
28+
29+
/**
30+
* Create a topic with a Single Message Transform function
31+
*
32+
* @param string $projectId
33+
* @param string $topicId
34+
*/
35+
function create_topic_with_smt(
36+
string $projectId,
37+
string $topicId
38+
) {
39+
$pubsub = new PubSubClient([
40+
'projectId' => $projectId
41+
]);
42+
43+
$functionName = 'toUpper';
44+
$code = 'function toUpper(message, metadata){
45+
message.data = message.data.toUpperCase();
46+
return message;
47+
}';
48+
49+
$smtConfig = [
50+
'javascriptUdf' => [
51+
'functionName' => $functionName,
52+
'code' => $code
53+
]
54+
];
55+
56+
$topicConfig = [
57+
'messageTransforms' => [
58+
$smtConfig
59+
]
60+
];
61+
62+
$topic = $pubsub->createTopic($topicId, $topicConfig);
63+
64+
printf('Topic with SMT %s created', $topic->name());
65+
}
66+
# [END pubsub_create_topic_with_smt]
67+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
68+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

pubsub/api/test/pubsubTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,26 @@ public function testCreateAndDeleteTopic()
159159
$this->assertMatchesRegularExpression(sprintf('/%s/', $topic), $output);
160160
}
161161

162+
public function testCreateAndDeleteTopicWithSMT()
163+
{
164+
$topic = 'test-topic-smt-' . rand();
165+
$output = $this->runFunctionSnippet('create_topic_with_smt', [
166+
self::$projectId,
167+
$topic,
168+
]);
169+
170+
$this->assertMatchesRegularExpression('/Topic with SMT/', $output);
171+
$this->assertMatchesRegularExpression(sprintf('/%s/', $topic), $output);
172+
173+
$output = $this->runFunctionSnippet('delete_topic', [
174+
self::$projectId,
175+
$topic,
176+
]);
177+
178+
$this->assertMatchesRegularExpression('/Topic deleted:/', $output);
179+
$this->assertMatchesRegularExpression(sprintf('/%s/', $topic), $output);
180+
}
181+
162182
public function testTopicMessage()
163183
{
164184
$topic = $this->requireEnv('GOOGLE_PUBSUB_TOPIC');
@@ -234,6 +254,28 @@ public function testCreateAndDeleteSubscription()
234254
$this->assertMatchesRegularExpression(sprintf('/%s/', $subscription), $output);
235255
}
236256

257+
public function testCreateAndDeleteSubscriptionWithSMT()
258+
{
259+
$topic = $this->requireEnv('GOOGLE_PUBSUB_TOPIC');
260+
$subscription = 'test-subscription-' . rand();
261+
$output = $this->runFunctionSnippet('create_subscription_with_SMT', [
262+
self::$projectId,
263+
$topic,
264+
$subscription
265+
]);
266+
267+
$this->assertMatchesRegularExpression('/Subscription with SMT/', $output);
268+
$this->assertMatchesRegularExpression(sprintf('/%s/', $subscription), $output);
269+
270+
$output = $this->runFunctionSnippet('delete_subscription', [
271+
self::$projectId,
272+
$subscription,
273+
]);
274+
275+
$this->assertMatchesRegularExpression('/Subscription deleted:/', $output);
276+
$this->assertMatchesRegularExpression(sprintf('/%s/', $subscription), $output);
277+
}
278+
237279
public function testCreateAndDeleteSubscriptionWithFilter()
238280
{
239281
$topic = $this->requireEnv('GOOGLE_PUBSUB_TOPIC');

0 commit comments

Comments
 (0)