Skip to content

Commit d8237e8

Browse files
committed
Use parseName instead
1 parent b6ae184 commit d8237e8

File tree

1 file changed

+43
-82
lines changed

1 file changed

+43
-82
lines changed

secretmanager/test/secretmanagerTest.php

Lines changed: 43 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
/*
33
* Copyright 2020 Google LLC.
44
*
5-
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* Licensed under the Apache License, Version 2.0 (the 'License');
66
* you may not use this file except in compliance with the License.
77
* You may obtain a copy of the License at
88
*
99
* http://www.apache.org/licenses/LICENSE-2.0
1010
*
1111
* Unless required by applicable law or agreed to in writing, software
12-
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* distributed under the License is distributed on an 'AS IS' BASIS,
1313
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
@@ -108,133 +108,102 @@ private static function deleteSecret(string $name)
108108
}
109109
}
110110

111-
private static function explodeName(string $str): array
112-
{
113-
preg_match('/^projects\/(.+)(\/secrets\/(.+)(\/versions\/(.+))?)?$/U', $str, $matches);
114-
115-
if (count($matches) > 5) {
116-
return [$matches[1], $matches[3], $matches[5]];
117-
} elseif (count($matches) > 3) {
118-
return [$matches[1], $matches[3]];
119-
} elseif (count($matches) > 1) {
120-
return [$matches[1]];
121-
}
122-
123-
return [];
124-
}
125-
126111
public function testAccessSecretVersion()
127112
{
128-
list($projectId, $secretId, $versionId) = self::explodeName(
129-
self::$testSecretVersion->getName()
130-
);
113+
$name = self::$client->parseName(self::$testSecretVersion->getName());
131114

132115
$output = $this->runSnippet('access_secret_version', [
133-
$projectId,
134-
$secretId,
135-
$versionId,
116+
$name['project'],
117+
$name['secret'],
118+
$name['secret_version'],
136119
]);
137120

138121
$this->assertContains('my super secret data', $output);
139122
}
140123

141124
public function testAddSecretVersion()
142125
{
143-
list($projectId, $secretId) = self::explodeName(
144-
self::$testSecretWithVersions->getName()
145-
);
126+
$name = self::$client->parseName(self::$testSecretWithVersions->getName());
146127

147128
$output = $this->runSnippet('add_secret_version', [
148-
$projectId,
149-
$secretId,
129+
$name['project'],
130+
$name['secret'],
150131
]);
151132

152133
$this->assertContains('Added secret version', $output);
153134
}
154135

155136
public function testCreateSecret()
156137
{
157-
list($projectId, $secretId) = self::explodeName(
158-
self::$testSecretToCreateName
159-
);
138+
$name = self::$client->parseName(self::$testSecretToCreateName);
160139

161140
$output = $this->runSnippet('create_secret', [
162-
$projectId,
163-
$secretId,
141+
$name['project'],
142+
$name['secret'],
164143
]);
165144

166145
$this->assertContains('Created secret', $output);
167146
}
168147

169148
public function testDeleteSecret()
170149
{
171-
list($projectId, $secretId) = self::explodeName(
172-
self::$testSecretToDelete->getName()
173-
);
150+
$name = self::$client->parseName(self::$testSecretToDelete->getName());
174151

175152
$output = $this->runSnippet('delete_secret', [
176-
$projectId,
177-
$secretId,
153+
$name['project'],
154+
$name['secret'],
178155
]);
179156

180157
$this->assertContains('Deleted secret', $output);
181158
}
182159

183160
public function testDestroySecretVersion()
184161
{
185-
list($projectId, $secretId, $versionId) = self::explodeName(
186-
self::$testSecretVersionToDestroy->getName()
187-
);
162+
$name = self::$client->parseName(self::$testSecretVersionToDestroy->getName());
188163

189164
$output = $this->runSnippet('destroy_secret_version', [
190-
$projectId,
191-
$secretId,
192-
$versionId,
165+
$name['project'],
166+
$name['secret'],
167+
$name['secret_version'],
193168
]);
194169

195170
$this->assertContains('Destroyed secret version', $output);
196171
}
197172

198173
public function testDisableSecretVersion()
199174
{
200-
list($projectId, $secretId, $versionId) = self::explodeName(
201-
self::$testSecretVersionToDisable->getName()
202-
);
175+
$name = self::$client->parseName(self::$testSecretVersionToDisable->getName());
203176

204177
$output = $this->runSnippet('disable_secret_version', [
205-
$projectId,
206-
$secretId,
207-
$versionId,
178+
$name['project'],
179+
$name['secret'],
180+
$name['secret_version'],
208181
]);
209182

210183
$this->assertContains('Disabled secret version', $output);
211184
}
212185

213186
public function testEnableSecretVersion()
214187
{
215-
list($projectId, $secretId, $versionId) = self::explodeName(
216-
self::$testSecretVersionToEnable->getName()
217-
);
188+
$name = self::$client->parseName(self::$testSecretVersionToEnable->getName());
218189

219190
$output = $this->runSnippet('enable_secret_version', [
220-
$projectId,
221-
$secretId,
222-
$versionId,
191+
$name['project'],
192+
$name['secret'],
193+
$name['secret_version'],
223194
]);
224195

225196
$this->assertContains('Enabled secret version', $output);
226197
}
227198

228199
public function testGetSecretVersion()
229200
{
230-
list($projectId, $secretId, $versionId) = self::explodeName(
231-
self::$testSecretVersion->getName()
232-
);
201+
$name = self::$client->parseName(self::$testSecretVersion->getName());
233202

234203
$output = $this->runSnippet('get_secret_version', [
235-
$projectId,
236-
$secretId,
237-
$versionId,
204+
$name['project'],
205+
$name['secret'],
206+
$name['secret_version'],
238207
]);
239208

240209
$this->assertContains('Got secret version', $output);
@@ -243,13 +212,11 @@ public function testGetSecretVersion()
243212

244213
public function testGetSecret()
245214
{
246-
list($projectId, $secretId) = self::explodeName(
247-
self::$testSecret->getName()
248-
);
215+
$name = self::$client->parseName(self::$testSecret->getName());
249216

250217
$output = $this->runSnippet('get_secret', [
251-
$projectId,
252-
$secretId,
218+
$name['project'],
219+
$name['secret'],
253220
]);
254221

255222
$this->assertContains('secret', $output);
@@ -258,41 +225,35 @@ public function testGetSecret()
258225

259226
public function testListSecretVersions()
260227
{
261-
list($projectId, $secretId) = self::explodeName(
262-
self::$testSecretWithVersions->getName()
263-
);
228+
$name = self::$client->parseName(self::$testSecretWithVersions->getName());
264229

265230
$output = $this->runSnippet('list_secret_versions', [
266-
$projectId,
267-
$secretId,
231+
$name['project'],
232+
$name['secret'],
268233
]);
269234

270235
$this->assertContains('secret version', $output);
271236
}
272237

273238
public function testListSecrets()
274239
{
275-
list($projectId, $secretId) = self::explodeName(
276-
self::$testSecret->getName()
277-
);
240+
$name = self::$client->parseName(self::$testSecret->getName());
278241

279242
$output = $this->runSnippet('list_secrets', [
280-
$projectId,
243+
$name['project'],
281244
]);
282245

283246
$this->assertContains('secret', $output);
284-
$this->assertContains($secretId, $output);
247+
$this->assertContains($name['secret'], $output);
285248
}
286249

287250
public function testUpdateSecret()
288251
{
289-
list($projectId, $secretId) = self::explodeName(
290-
self::$testSecret->getName()
291-
);
252+
$name = self::$client->parseName(self::$testSecret->getName());
292253

293254
$output = $this->runSnippet('update_secret', [
294-
$projectId,
295-
$secretId,
255+
$name['project'],
256+
$name['secret'],
296257
]);
297258

298259
$this->assertContains('Updated secret', $output);

0 commit comments

Comments
 (0)