Skip to content

Commit 3e22be6

Browse files
committed
fix tests
1 parent 80898b2 commit 3e22be6

File tree

8 files changed

+94
-51
lines changed

8 files changed

+94
-51
lines changed

speech/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ API using the transcribe command:
4444

4545
```sh
4646
php src/transcribe_sync.php test/data/audio32KHz.raw
47-
php src/transcribe_async.php test/data/audio32KHz.raw
4847
php src/transcribe_async_words.php test/data/audio32KHz.raw
4948
```
5049
## Troubleshooting

speech/quickstart.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@
6060
print_r($operation->getError());
6161
}
6262

63-
// Can also use {@see Google\Cloud\Speech\V2\AutoDetectDecodingConfig}
6463
$config = (new RecognitionConfig())
64+
// Can also use {@see Google\Cloud\Speech\V2\AutoDetectDecodingConfig}
65+
// ->setAutoDecodingConfig(new AutoDetectDecodingConfig());
66+
6567
->setExplicitDecodingConfig(new ExplicitDecodingConfig([
6668
'encoding' => AudioEncoding::LINEAR16,
6769
'sample_rate_hertz' => 16000,

speech/src/streaming_recognize.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ function streaming_recognize(string $projectId, string $location, string $recogn
8383
// process the results
8484
foreach ($response->getResults() as $result) {
8585
printf(
86-
'Transcription: "%s"' . PHP_EOL,
86+
'Transcript: "%s"' . PHP_EOL,
8787
$result->getAlternatives()[0]->getTranscript()
8888
);
8989
}

speech/src/transcribe_async_gcs.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function transcribe_async_gcs(string $projectId, string $location, string $recog
7070
->setRecognitionOutputConfig($outputConfig);
7171

7272
try {
73-
$operation = $client->batchRecognize($request);
73+
$operation = $speech->batchRecognize($request);
7474
$operation->pollUntilComplete();
7575

7676
if ($operation->operationSucceeded()) {
@@ -94,7 +94,7 @@ function transcribe_async_gcs(string $projectId, string $location, string $recog
9494
print_r($operation->getError());
9595
}
9696
} finally {
97-
$client->close();
97+
$speech->close();
9898
}
9999
}
100100
# [END speech_transcribe_async_gcs]

speech/src/transcribe_async_words.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,14 @@ function transcribe_async_words(string $projectId, string $location, string $rec
5555
'diarization_config' => new SpeakerDiarizationConfig(),
5656
]);
5757

58-
// Can also use {@see Google\Cloud\Speech\V2\AutoDetectDecodingConfig}
5958
$config = (new RecognitionConfig())
6059
->setFeatures($features)
6160
// When running outside the "global" location, you can set the model to "chirp_3" in
6261
// RecognitionConfig instead of on the recognizer.
6362
// ->setModel('chirp_3')
6463

65-
// Can also use {@see Google\Cloud\Speech\V2\ExplicitDecodingConfig}
66-
// ->setExplicitDecodingConfig(new ExplicitDecodingConfig([...]);
64+
// Can also use {@see Google\Cloud\Speech\V2\AutoDetectDecodingConfig}
65+
// ->setAutoDecodingConfig(new AutoDetectDecodingConfig());
6766

6867
->setExplicitDecodingConfig(new ExplicitDecodingConfig([
6968
// change these variables if necessary
@@ -99,14 +98,14 @@ function transcribe_async_words(string $projectId, string $location, string $rec
9998
foreach ($transcript->getResults() as $transacriptResult) {
10099
$alternatives = $transacriptResult->getAlternatives();
101100
$mostLikely = $alternatives[0];
102-
var_dump($alternatives, $mostLikely);exit;
103101
foreach ($mostLikely->getWords() as $wordInfo) {
104102
$startTime = $wordInfo->getStartOffset();
105103
$endTime = $wordInfo->getEndOffset();
106104
printf(' Word: %s (start: %s, end: %s)' . PHP_EOL,
107-
$wordInfo->getWord(),
108-
$startTime->serializeToJsonString(),
109-
$endTime->serializeToJsonString());
105+
$wordInfo->getWord(),
106+
$startTime?->serializeToJsonString(),
107+
$endTime?->serializeToJsonString()
108+
);
110109
}
111110
}
112111
}

speech/src/transcribe_sync_gcs.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ function transcribe_sync_gcs(string $projectId, string $location, string $recogn
4545
$recognizerName = SpeechClient::recognizerName($projectId, $location, $recognizerId);
4646

4747
$config = (new RecognitionConfig())
48-
4948
// Can also use {@see Google\Cloud\Speech\V2\AutoDetectDecodingConfig}
5049
// ->setAutoDecodingConfig(new AutoDetectDecodingConfig());
5150

speech/test/quickstartTest.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,25 @@ class quickstartTest extends TestCase
2222
use TestTrait;
2323
public function testQuickstart()
2424
{
25-
$this->expectOutputRegex('/Created Recognizer: projects\/.+\/locations\/global\/recognizers\/quickstart-recognizer-.+/');
26-
$this->expectOutputRegex('/Transcript: how old is the Brooklyn Bridge/');
27-
$this->expectOutputRegex('/Deleted Recognizer: projects\/.+\/locations\/global\/recognizers\/quickstart-recognizer-.+/');
28-
// Invoke quickstart.php
29-
include __DIR__ . '/../quickstart.php';
25+
if (!$projectId = getenv('GOOGLE_PROJECT_ID')) {
26+
$this->markTestSkipped('GOOGLE_PROJECT_ID must be set.');
27+
}
28+
29+
$file = sys_get_temp_dir() . '/speech_quickstart.php';
30+
$contents = file_get_contents(__DIR__ . '/../quickstart.php');
31+
$contents = str_replace(
32+
['YOUR_PROJECT_ID', '__DIR__'],
33+
[$projectId, sprintf('"%s/.."', __DIR__)],
34+
$contents
35+
);
36+
file_put_contents($file, $contents);
37+
38+
// Invoke quickstart.php and capture output
39+
ob_start();
40+
include $file;
41+
$result = ob_get_clean();
42+
43+
// Make sure it looks correct
44+
$this->assertStringContainsString('Transcript: how old is the Brooklyn Bridge', $result);
3045
}
3146
}

speech/test/speechTest.php

Lines changed: 62 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,27 @@
2020
use Google\Cloud\TestUtils\TestTrait;
2121
use PHPUnit\Framework\TestCase;
2222

23-
require_once __DIR__ . '/../src/create_recognizer.php';
24-
require_once __DIR__ . '/../src/delete_recognizer.php';
25-
2623
class speechTest extends TestCase
2724
{
25+
const GLOBAL = 'global';
2826
use TestTrait;
2927

30-
private $recognizerId;
31-
private $projectId;
28+
private static string $recognizerId;
3229

33-
protected function setUp(): void
30+
public static function setUpBeforeClass(): void
3431
{
35-
$this->projectId = $this->requireEnv('GOOGLE_CLOUD_PROJECT');
36-
$this->recognizerId = 'test-recognizer-' . uniqid();
37-
create_recognizer($this->projectId, 'global', $this->recognizerId);
32+
self::$projectId = self::requireEnv('GOOGLE_CLOUD_PROJECT');
33+
self::$recognizerId = 'test-recognizer-' . uniqid();
3834
}
3935

40-
protected function tearDown(): void
36+
public static function tearDownAfterClass(): void
4137
{
42-
delete_recognizer($this->projectId, 'global', $this->recognizerId);
38+
self::runFunctionSnippet('delete_recognizer', [self::$projectId, self::GLOBAL, self::$recognizerId]);
4339
}
4440

4541
public function testBase64Audio()
4642
{
47-
$audioFile = __DIR__ . '/data/audio32KHz.raw';
43+
$audioFile = __DIR__ . '/data/audio32KHz.flac';
4844

4945
$output = $this->runFunctionSnippet('base64_encode_audio', [$audioFile]);
5046

@@ -55,66 +51,99 @@ public function testBase64Audio()
5551
);
5652
}
5753

54+
public function testCreateRecognizer()
55+
{
56+
$output = $this->runFunctionSnippet('create_recognizer', [self::$projectId, self::GLOBAL, self::$recognizerId]);
57+
$this->assertStringContainsString('Created Recognizer:', $output);
58+
}
59+
60+
/** @depends testCreateRecognizer */
5861
public function testTranscribeEnhanced()
5962
{
6063
$path = __DIR__ . '/data/commercial_mono.wav';
61-
$output = $this->runFunctionSnippet('transcribe_enhanced_model', [$this->projectId, 'global', $this->recognizerId, $path]);
64+
$output = $this->runFunctionSnippet('transcribe_enhanced_model', [self::$projectId, self::GLOBAL, self::$recognizerId, $path]);
6265
$this->assertStringContainsString('Chrome', $output);
6366
}
6467

68+
/** @depends testCreateRecognizer */
6569
public function testTranscribeModel()
6670
{
67-
$path = __DIR__ . '/data/audio32KHz.raw';
71+
$path = __DIR__ . '/data/audio32KHz.flac';
6872
$output = $this->runFunctionSnippet(
6973
'transcribe_model_selection',
70-
[$this->projectId, 'global', $this->recognizerId, $path, 'video']
74+
[self::$projectId, self::GLOBAL, self::$recognizerId, $path, 'telephony']
7175
);
7276
$this->assertStringContainsStringIgnoringCase(
7377
'how old is the Brooklyn Bridge',
7478
$output
7579
);
7680
}
7781

82+
/** @depends testCreateRecognizer */
7883
public function testTranscribePunctuation()
7984
{
80-
$path = __DIR__ . '/data/audio32KHz.raw';
81-
$output = $this->runFunctionSnippet('transcribe_auto_punctuation', [$this->projectId, 'global', $this->recognizerId, $path]);
85+
$path = __DIR__ . '/data/audio32KHz.flac';
86+
$output = $this->runFunctionSnippet('transcribe_auto_punctuation', [self::$projectId, self::GLOBAL, self::$recognizerId, $path]);
8287
$this->assertStringContainsStringIgnoringCase(
8388
'How old is the Brooklyn Bridge',
8489
$output
8590
);
8691
}
8792

88-
/** @dataProvider provideTranscribe */
93+
public function testTranscribeWords()
94+
{
95+
$recognizerId = self::$recognizerId . '-chirp3';
96+
$audioFile = 'gs://cloud-samples-data/speech/brooklyn_bridge.raw';
97+
$location = 'eu';
98+
99+
$output = $this->runFunctionSnippet('create_recognizer', [self::$projectId, $location, $recognizerId, 'chirp_3']);
100+
$this->assertStringContainsString('Created Recognizer:', $output);
101+
102+
$output = $this->runFunctionSnippet('transcribe_async_words', [self::$projectId, $location, $recognizerId, $audioFile]);
103+
104+
// Check for the word time offsets
105+
$this->assertStringContainsString('Word: How (start: ', $output);
106+
}
107+
108+
public function testTranscribeMultRegion()
109+
{
110+
$recognizerId = self::$recognizerId . '-eu';
111+
$audioFile = 'gs://cloud-samples-data/speech/brooklyn_bridge.raw';
112+
$location = 'eu';
113+
114+
$output = $this->runFunctionSnippet('create_recognizer', [self::$projectId, $location, $recognizerId]);
115+
$this->assertStringContainsString('Created Recognizer:', $output);
116+
117+
$output = $this->runFunctionSnippet('multi_region_gcs', [self::$projectId, $location, $recognizerId, $audioFile]);
118+
119+
$this->assertStringContainsString('how old is the Brooklyn Bridge', $output);
120+
}
121+
122+
/**
123+
* @dataProvider provideTranscribe
124+
*
125+
* @depends testCreateRecognizer
126+
*/
89127
public function testTranscribe($command, $audioFile, $requireGrpc = false)
90128
{
91129
if ($requireGrpc && !extension_loaded('grpc')) {
92130
self::markTestSkipped('Must enable grpc extension.');
93131
}
94132

95-
$output = $this->runFunctionSnippet($command, [$this->projectId, 'global', $this->recognizerId, $audioFile]);
96-
97-
$this->assertStringContainsString('how old is the Brooklyn Bridge', $output);
133+
$output = $this->runFunctionSnippet($command, [self::$projectId, self::GLOBAL, self::$recognizerId, $audioFile]);
98134

99-
// Check for the word time offsets
100-
if (in_array($command, ['transcribe_async_words'])) {
101-
$this->assertMatchesRegularExpression('/start_offset {\\s*seconds: \\d+\\s*}/', $output);
102-
$this->assertMatchesRegularExpression('/end_offset {\\s*seconds: \\d+\\s*}/', $output);
103-
}
135+
$this->assertStringContainsString('old is the Brooklyn Bridge', $output);
104136
}
105137

106138
public function provideTranscribe()
107139
{
108140
return [
109-
['transcribe_sync', __DIR__ . '/data/audio32KHz.raw'],
141+
['transcribe_sync', __DIR__ . '/data/audio32KHz.flac'],
110142
['transcribe_sync_gcs', 'gs://cloud-samples-data/speech/audio.raw'],
111-
['transcribe_async', __DIR__ . '/data/audio32KHz.raw'],
112143
['transcribe_async_gcs', 'gs://cloud-samples-data/speech/audio.raw'],
113-
['transcribe_async_words', __DIR__ . '/data/audio32KHz.raw'],
114-
['profanity_filter_gcs', 'gs://cloud-samples-data/speech/profanity.raw'],
115-
['multi_region_gcs', 'gs://cloud-samples-data/speech/brooklyn_bridge.raw' ],
116-
['profanity_filter', __DIR__ . '/data/profanity.raw'],
117-
['streaming_recognize', __DIR__ . '/data/audio32KHz.raw', true],
144+
['profanity_filter_gcs', 'gs://cloud-samples-data/speech/brooklyn_bridge.raw'],
145+
['profanity_filter', __DIR__ . '/data/audio32KHz.flac'],
146+
['streaming_recognize', __DIR__ . '/data/audio32KHz.flac', true],
118147
];
119148
}
120149
}

0 commit comments

Comments
 (0)