Skip to content

Commit f45f710

Browse files
authored
test(dlp): Implemented mocking approach in an existing unit test case (GoogleCloudPlatform#1906)
* Implemented mocking approach in existing unit test case
1 parent fe53920 commit f45f710

File tree

10 files changed

+754
-133
lines changed

10 files changed

+754
-133
lines changed

dlp/src/categorical_stats.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
/**
43
* Copyright 2018 Google Inc.
54
*
@@ -57,12 +56,8 @@ function categorical_stats(
5756
string $columnName
5857
): void {
5958
// Instantiate a client.
60-
$dlp = new DlpServiceClient([
61-
'projectId' => $callingProjectId,
62-
]);
63-
$pubsub = new PubSubClient([
64-
'projectId' => $callingProjectId,
65-
]);
59+
$dlp = new DlpServiceClient();
60+
$pubsub = new PubSubClient();
6661
$topic = $pubsub->topic($topicId);
6762

6863
// Construct risk analysis config
@@ -109,8 +104,10 @@ function categorical_stats(
109104
$startTime = time();
110105
do {
111106
foreach ($subscription->pull() as $message) {
112-
if (isset($message->attributes()['DlpJobName']) &&
113-
$message->attributes()['DlpJobName'] === $job->getName()) {
107+
if (
108+
isset($message->attributes()['DlpJobName']) &&
109+
$message->attributes()['DlpJobName'] === $job->getName()
110+
) {
114111
$subscription->acknowledge($message);
115112
// Get the updated job. Loop to avoid race condition with DLP API.
116113
do {
@@ -119,7 +116,7 @@ function categorical_stats(
119116
break 2; // break from parent do while
120117
}
121118
}
122-
printf('Waiting for job to complete' . PHP_EOL);
119+
print('Waiting for job to complete' . PHP_EOL);
123120
// Exponential backoff with max delay of 60 seconds
124121
sleep(min(60, pow(2, ++$attempt)));
125122
} while (time() - $startTime < 600); // 10 minute timeout
@@ -156,10 +153,10 @@ function categorical_stats(
156153
}
157154
break;
158155
case JobState::PENDING:
159-
printf('Job has not completed. Consider a longer timeout or an asynchronous execution model' . PHP_EOL);
156+
print('Job has not completed. Consider a longer timeout or an asynchronous execution model' . PHP_EOL);
160157
break;
161158
default:
162-
printf('Unexpected job state.');
159+
print('Unexpected job state.');
163160
}
164161
}
165162
# [END dlp_categorical_stats]

dlp/src/inspect_bigquery.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
/**
43
* Copyright 2016 Google Inc.
54
*
@@ -134,7 +133,7 @@ function inspect_bigquery(
134133
break 2; // break from parent do while
135134
}
136135
}
137-
printf('Waiting for job to complete' . PHP_EOL);
136+
print('Waiting for job to complete' . PHP_EOL);
138137
// Exponential backoff with max delay of 60 seconds
139138
sleep(min(60, pow(2, ++$attempt)));
140139
} while (time() - $startTime < 600); // 10 minute timeout
@@ -164,10 +163,10 @@ function inspect_bigquery(
164163
}
165164
break;
166165
case JobState::PENDING:
167-
printf('Job has not completed. Consider a longer timeout or an asynchronous execution model' . PHP_EOL);
166+
print('Job has not completed. Consider a longer timeout or an asynchronous execution model' . PHP_EOL);
168167
break;
169168
default:
170-
printf('Unexpected job state. Most likely, the job is either running or has not yet started.');
169+
print('Unexpected job state. Most likely, the job is either running or has not yet started.');
171170
}
172171
}
173172
# [END dlp_inspect_bigquery]

dlp/src/inspect_datastore.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
/**
43
* Copyright 2016 Google Inc.
54
*
@@ -129,8 +128,10 @@ function inspect_datastore(
129128
$startTime = time();
130129
do {
131130
foreach ($subscription->pull() as $message) {
132-
if (isset($message->attributes()['DlpJobName']) &&
133-
$message->attributes()['DlpJobName'] === $job->getName()) {
131+
if (
132+
isset($message->attributes()['DlpJobName']) &&
133+
$message->attributes()['DlpJobName'] === $job->getName()
134+
) {
134135
$subscription->acknowledge($message);
135136
// Get the updated job. Loop to avoid race condition with DLP API.
136137
do {
@@ -139,7 +140,7 @@ function inspect_datastore(
139140
break 2; // break from parent do while
140141
}
141142
}
142-
printf('Waiting for job to complete' . PHP_EOL);
143+
print('Waiting for job to complete' . PHP_EOL);
143144
// Exponential backoff with max delay of 60 seconds
144145
sleep(min(60, pow(2, ++$attempt)));
145146
} while (time() - $startTime < 600); // 10 minute timeout
@@ -165,7 +166,7 @@ function inspect_datastore(
165166
}
166167
break;
167168
case JobState::PENDING:
168-
printf('Job has not completed. Consider a longer timeout or an asynchronous execution model' . PHP_EOL);
169+
print('Job has not completed. Consider a longer timeout or an asynchronous execution model' . PHP_EOL);
169170
break;
170171
default:
171172
print('Unexpected job state.');

dlp/src/inspect_gcs.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,10 @@ function inspect_gcs(
119119
$startTime = time();
120120
do {
121121
foreach ($subscription->pull() as $message) {
122-
if (isset($message->attributes()['DlpJobName']) &&
123-
$message->attributes()['DlpJobName'] === $job->getName()) {
122+
if (
123+
isset($message->attributes()['DlpJobName']) &&
124+
$message->attributes()['DlpJobName'] === $job->getName()
125+
) {
124126
$subscription->acknowledge($message);
125127
// Get the updated job. Loop to avoid race condition with DLP API.
126128
do {
@@ -129,7 +131,7 @@ function inspect_gcs(
129131
break 2; // break from parent do while
130132
}
131133
}
132-
printf('Waiting for job to complete' . PHP_EOL);
134+
print('Waiting for job to complete' . PHP_EOL);
133135
// Exponential backoff with max delay of 60 seconds
134136
sleep(min(60, pow(2, ++$attempt)));
135137
} while (time() - $startTime < 600); // 10 minute timeout
@@ -155,7 +157,7 @@ function inspect_gcs(
155157
}
156158
break;
157159
case JobState::PENDING:
158-
printf('Job has not completed. Consider a longer timeout or an asynchronous execution model' . PHP_EOL);
160+
print('Job has not completed. Consider a longer timeout or an asynchronous execution model' . PHP_EOL);
159161
break;
160162
default:
161163
print('Unexpected job state. Most likely, the job is either running or has not yet started.');

dlp/src/k_anonymity.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
/**
43
* Copyright 2018 Google Inc.
54
*
@@ -57,12 +56,8 @@ function k_anonymity(
5756
array $quasiIdNames
5857
): void {
5958
// Instantiate a client.
60-
$dlp = new DlpServiceClient([
61-
'projectId' => $callingProjectId,
62-
]);
63-
$pubsub = new PubSubClient([
64-
'projectId' => $callingProjectId,
65-
]);
59+
$dlp = new DlpServiceClient();
60+
$pubsub = new PubSubClient();
6661
$topic = $pubsub->topic($topicId);
6762

6863
// Construct risk analysis config
@@ -113,8 +108,10 @@ function ($id) {
113108
$startTime = time();
114109
do {
115110
foreach ($subscription->pull() as $message) {
116-
if (isset($message->attributes()['DlpJobName']) &&
117-
$message->attributes()['DlpJobName'] === $job->getName()) {
111+
if (
112+
isset($message->attributes()['DlpJobName']) &&
113+
$message->attributes()['DlpJobName'] === $job->getName()
114+
) {
118115
$subscription->acknowledge($message);
119116
// Get the updated job. Loop to avoid race condition with DLP API.
120117
do {
@@ -123,7 +120,7 @@ function ($id) {
123120
break 2; // break from parent do while
124121
}
125122
}
126-
printf('Waiting for job to complete' . PHP_EOL);
123+
print('Waiting for job to complete' . PHP_EOL);
127124
// Exponential backoff with max delay of 60 seconds
128125
sleep(min(60, pow(2, ++$attempt)));
129126
} while (time() - $startTime < 600); // 10 minute timeout
@@ -166,10 +163,10 @@ function ($id) {
166163
}
167164
break;
168165
case JobState::PENDING:
169-
printf('Job has not completed. Consider a longer timeout or an asynchronous execution model' . PHP_EOL);
166+
print('Job has not completed. Consider a longer timeout or an asynchronous execution model' . PHP_EOL);
170167
break;
171168
default:
172-
printf('Unexpected job state. Most likely, the job is either running or has not yet started.');
169+
print('Unexpected job state. Most likely, the job is either running or has not yet started.');
173170
}
174171
}
175172
# [END dlp_k_anomymity]

dlp/src/k_map.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
/**
43
* Copyright 2018 Google Inc.
54
*
@@ -64,12 +63,8 @@ function k_map(
6463
array $infoTypes
6564
): void {
6665
// Instantiate a client.
67-
$dlp = new DlpServiceClient([
68-
'projectId' => $callingProjectId,
69-
]);
70-
$pubsub = new PubSubClient([
71-
'projectId' => $callingProjectId,
72-
]);
66+
$dlp = new DlpServiceClient();
67+
$pubsub = new PubSubClient();
7368
$topic = $pubsub->topic($topicId);
7469

7570
// Verify input
@@ -134,8 +129,10 @@ function k_map(
134129
$startTime = time();
135130
do {
136131
foreach ($subscription->pull() as $message) {
137-
if (isset($message->attributes()['DlpJobName']) &&
138-
$message->attributes()['DlpJobName'] === $job->getName()) {
132+
if (
133+
isset($message->attributes()['DlpJobName']) &&
134+
$message->attributes()['DlpJobName'] === $job->getName()
135+
) {
139136
$subscription->acknowledge($message);
140137
// Get the updated job. Loop to avoid race condition with DLP API.
141138
do {
@@ -144,7 +141,7 @@ function k_map(
144141
break 2; // break from parent do while
145142
}
146143
}
147-
printf('Waiting for job to complete' . PHP_EOL);
144+
print('Waiting for job to complete' . PHP_EOL);
148145
// Exponential backoff with max delay of 60 seconds
149146
sleep(min(60, pow(2, ++$attempt)));
150147
} while (time() - $startTime < 600); // 10 minute timeout
@@ -188,7 +185,7 @@ function k_map(
188185
}
189186
break;
190187
case JobState::PENDING:
191-
printf('Job has not completed. Consider a longer timeout or an asynchronous execution model' . PHP_EOL);
188+
print('Job has not completed. Consider a longer timeout or an asynchronous execution model' . PHP_EOL);
192189
break;
193190
default:
194191
print('Unexpected job state. Most likely, the job is either running or has not yet started.');

dlp/src/l_diversity.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
/**
43
* Copyright 2018 Google Inc.
54
*
@@ -56,15 +55,11 @@ function l_diversity(
5655
string $datasetId,
5756
string $tableId,
5857
string $sensitiveAttribute,
59-
array $quasiIdNames
58+
array $quasiIdNames
6059
): void {
6160
// Instantiate a client.
62-
$dlp = new DlpServiceClient([
63-
'projectId' => $callingProjectId,
64-
]);
65-
$pubsub = new PubSubClient([
66-
'projectId' => $callingProjectId,
67-
]);
61+
$dlp = new DlpServiceClient();
62+
$pubsub = new PubSubClient();
6863
$topic = $pubsub->topic($topicId);
6964

7065
// Construct risk analysis config
@@ -119,8 +114,10 @@ function ($id) {
119114
$startTime = time();
120115
do {
121116
foreach ($subscription->pull() as $message) {
122-
if (isset($message->attributes()['DlpJobName']) &&
123-
$message->attributes()['DlpJobName'] === $job->getName()) {
117+
if (
118+
isset($message->attributes()['DlpJobName']) &&
119+
$message->attributes()['DlpJobName'] === $job->getName()
120+
) {
124121
$subscription->acknowledge($message);
125122
// Get the updated job. Loop to avoid race condition with DLP API.
126123
do {
@@ -129,7 +126,7 @@ function ($id) {
129126
break 2; // break from parent do while
130127
}
131128
}
132-
printf('Waiting for job to complete' . PHP_EOL);
129+
print('Waiting for job to complete' . PHP_EOL);
133130
// Exponential backoff with max delay of 60 seconds
134131
sleep(min(60, pow(2, ++$attempt)));
135132
} while (time() - $startTime < 600); // 10 minute timeout
@@ -182,10 +179,10 @@ function ($id) {
182179
}
183180
break;
184181
case JobState::PENDING:
185-
printf('Job has not completed. Consider a longer timeout or an asynchronous execution model' . PHP_EOL);
182+
print('Job has not completed. Consider a longer timeout or an asynchronous execution model' . PHP_EOL);
186183
break;
187184
default:
188-
printf('Unexpected job state. Most likely, the job is either running or has not yet started.');
185+
print('Unexpected job state. Most likely, the job is either running or has not yet started.');
189186
}
190187
}
191188
# [END dlp_l_diversity]

dlp/src/numerical_stats.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
/**
43
* Copyright 2018 Google Inc.
54
*
@@ -57,12 +56,8 @@ function numerical_stats(
5756
string $columnName
5857
): void {
5958
// Instantiate a client.
60-
$dlp = new DlpServiceClient([
61-
'projectId' => $callingProjectId
62-
]);
63-
$pubsub = new PubSubClient([
64-
'projectId' => $callingProjectId
65-
]);
59+
$dlp = new DlpServiceClient();
60+
$pubsub = new PubSubClient();
6661
$topic = $pubsub->topic($topicId);
6762

6863
// Construct risk analysis config
@@ -109,8 +104,10 @@ function numerical_stats(
109104
$startTime = time();
110105
do {
111106
foreach ($subscription->pull() as $message) {
112-
if (isset($message->attributes()['DlpJobName']) &&
113-
$message->attributes()['DlpJobName'] === $job->getName()) {
107+
if (
108+
isset($message->attributes()['DlpJobName']) &&
109+
$message->attributes()['DlpJobName'] === $job->getName()
110+
) {
114111
$subscription->acknowledge($message);
115112
// Get the updated job. Loop to avoid race condition with DLP API.
116113
do {
@@ -119,7 +116,7 @@ function numerical_stats(
119116
break 2; // break from parent do while
120117
}
121118
}
122-
printf('Waiting for job to complete' . PHP_EOL);
119+
print('Waiting for job to complete' . PHP_EOL);
123120
// Exponential backoff with max delay of 60 seconds
124121
sleep(min(60, pow(2, ++$attempt)));
125122
} while (time() - $startTime < 600); // 10 minute timeout
@@ -160,7 +157,7 @@ function numerical_stats(
160157
}
161158
break;
162159
case JobState::PENDING:
163-
printf('Job has not completed. Consider a longer timeout or an asynchronous execution model' . PHP_EOL);
160+
print('Job has not completed. Consider a longer timeout or an asynchronous execution model' . PHP_EOL);
164161
break;
165162
default:
166163
print('Unexpected job state. Most likely, the job is either running or has not yet started.');

0 commit comments

Comments
 (0)