Skip to content

Commit c644c07

Browse files
committed
feat(PubSub): Add CreateUnwrappedPushSubscription Sample
1 parent ff3d936 commit c644c07

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2025 Google LLC.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* For instructions on how to run the full sample:
21+
*
22+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/blob/main/pubsub/api/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\PubSub;
26+
27+
# [START pubsub_create_unwrapped_push_subscription]
28+
use Google\Cloud\PubSub\PubSubClient;
29+
30+
/**
31+
* Create unwrappped push subscription.
32+
*
33+
* @param string $projectId The Google project ID.
34+
* @param string $topicName The Pub/Sub topic name.
35+
* @param string $subscriptionId The ID of the subscription.
36+
*/
37+
function create_unwrapped_push_subscription(
38+
string $projectId,
39+
string $topicName,
40+
string $subscriptionId
41+
): void {
42+
43+
$pubsub = new PubSubClient([
44+
'projectId' => $projectId,
45+
]);
46+
$pubsub->subscribe($subscriptionId, $topicName, [
47+
'pushConfig' => [
48+
'no_wrapper' => [
49+
'write_metadata' => true
50+
]
51+
]
52+
]);
53+
printf('Unwrapped push subscription created: %s' . PHP_EOL, $subscriptionId);
54+
}
55+
# [END pubsub_create_unwrapped_push_subscription]
56+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
57+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

pubsub/api/test/pubsubTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,4 +487,26 @@ public function testPublishAndSubscribeWithOrderingKeys()
487487
$this->assertMatchesRegularExpression('/Created subscription with ordering/', $output);
488488
$this->assertMatchesRegularExpression('/\"enableMessageOrdering\":true/', $output);
489489
}
490+
491+
public function testCreateAndDeleteUnwrappedSubscription()
492+
{
493+
$topic = $this->requireEnv('GOOGLE_PUBSUB_TOPIC');
494+
$subscription = 'test-subscription-' . rand();
495+
$output = $this->runFunctionSnippet('create_unwrapped_push_subscription', [
496+
self::$projectId,
497+
$topic,
498+
$subscription,
499+
]);
500+
501+
$this->assertMatchesRegularExpression('/Unwrapped push subscription created:/', $output);
502+
$this->assertMatchesRegularExpression(sprintf('/%s/', $subscription), $output);
503+
504+
$output = $this->runFunctionSnippet('delete_subscription', [
505+
self::$projectId,
506+
$subscription,
507+
]);
508+
509+
$this->assertMatchesRegularExpression('/Subscription deleted:/', $output);
510+
$this->assertMatchesRegularExpression(sprintf('/%s/', $subscription), $output);
511+
}
490512
}

0 commit comments

Comments
 (0)