2020use Google \Cloud \TestUtils \TestTrait ;
2121use PHPUnit \Framework \TestCase ;
2222
23- require_once __DIR__ . '/../src/create_recognizer.php ' ;
24- require_once __DIR__ . '/../src/delete_recognizer.php ' ;
25-
2623class 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