Skip to content

Commit 36124dd

Browse files
bshafferdwsupplee
authored andcommitted
ensures servicebuilder throws proper exceptions (#682)
* ensures servicebuilder throws proper exceptions * moves servicebuilder tests to unit tests
1 parent fa7977f commit 36124dd

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

src/Core/ServiceBuilder.php

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public function __construct(array $config = [])
118118
*/
119119
public function bigQuery(array $config = [])
120120
{
121-
return new BigQueryClient($config ? $this->resolveConfig($config) : $this->config);
121+
return $this->createClient(BigQueryClient::class, 'bigquery', $config);
122122
}
123123

124124
/**
@@ -142,7 +142,7 @@ public function bigQuery(array $config = [])
142142
*/
143143
public function datastore(array $config = [])
144144
{
145-
return new DatastoreClient($config ? $this->resolveConfig($config) : $this->config);
145+
return $this->createClient(DatastoreClient::class, 'datastore', $config);
146146
}
147147

148148
/**
@@ -162,7 +162,7 @@ public function datastore(array $config = [])
162162
*/
163163
public function logging(array $config = [])
164164
{
165-
return new LoggingClient($config ? $this->resolveConfig($config) : $this->config);
165+
return $this->createClient(LoggingClient::class, 'logging', $config);
166166
}
167167

168168
/**
@@ -183,7 +183,7 @@ public function logging(array $config = [])
183183
*/
184184
public function language(array $config = [])
185185
{
186-
return new LanguageClient($config ? $this->resolveConfig($config) : $this->config);
186+
return $this->createClient(LanguageClient::class, 'language', $config);
187187
}
188188

189189
/**
@@ -207,7 +207,7 @@ public function language(array $config = [])
207207
*/
208208
public function pubsub(array $config = [])
209209
{
210-
return new PubSubClient($config ? $this->resolveConfig($config) : $this->config);
210+
return $this->createClient(PubSubClient::class, 'pubsub', $config);
211211
}
212212

213213
/**
@@ -232,7 +232,7 @@ public function pubsub(array $config = [])
232232
*/
233233
public function spanner(array $config = [])
234234
{
235-
return new SpannerClient($config ? $this->resolveConfig($config) : $this->config);
235+
return $this->createClient(SpannerClient::class, 'spanner', $config);
236236
}
237237

238238
/**
@@ -261,7 +261,7 @@ public function spanner(array $config = [])
261261
*/
262262
public function speech(array $config = [])
263263
{
264-
return new SpeechClient($config ? $this->resolveConfig($config) : $this->config);
264+
return $this->createClient(SpeechClient::class, 'speech', $config);
265265
}
266266

267267
/**
@@ -280,7 +280,7 @@ public function speech(array $config = [])
280280
*/
281281
public function storage(array $config = [])
282282
{
283-
return new StorageClient($config ? $this->resolveConfig($config) : $this->config);
283+
return $this->createClient(StorageClient::class, 'storage', $config);
284284
}
285285

286286

@@ -300,7 +300,7 @@ public function storage(array $config = [])
300300
*/
301301
public function trace(array $config = [])
302302
{
303-
return new TraceClient($config ? $this->resolveConfig($config) : $this->config);
303+
return $this->createClient(TraceClient::class, 'trace', $config);
304304
}
305305

306306
/**
@@ -320,7 +320,7 @@ public function trace(array $config = [])
320320
*/
321321
public function vision(array $config = [])
322322
{
323-
return new VisionClient($config ? $this->resolveConfig($config) : $this->config);
323+
return $this->createClient(VisionClient::class, 'vision', $config);
324324
}
325325

326326
/**
@@ -368,7 +368,18 @@ public function vision(array $config = [])
368368
*/
369369
public function translate(array $config = [])
370370
{
371-
return new TranslateClient($config ? $this->resolveConfig($config) : $this->config);
371+
return $this->createClient(TranslateClient::class, 'translate', $config);
372+
}
373+
374+
private function createClient($class, $packageName, array $config = [])
375+
{
376+
if (class_exists($class)) {
377+
return new $class($config ? $this->resolveConfig($config) : $this->config);
378+
}
379+
throw new \Exception(sprintf(
380+
'The google/cloud-%s package is missing and must be installed.',
381+
$packageName
382+
));
372383
}
373384

374385
/**

tests/system/Core/ServicesNotFoundTest.php renamed to tests/unit/Core/ServicesNotFoundTest.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,37 @@
1515
* limitations under the License.
1616
*/
1717

18-
namespace Google\Cloud\Tests\System\Core;
18+
namespace Google\Cloud\Tests\Unit\Core;
1919

2020
use Google\Cloud\Core\ServiceBuilder;
21+
use Composer\Autoload\ClassLoader;
2122

2223
/**
2324
* @group core
2425
*/
2526
class ServicesNotFoundTest extends \PHPUnit_Framework_TestCase
2627
{
27-
private static $autoloaders;
28+
private static $previousAutoloadFunc;
29+
private static $newAutoloadFunc;
2830
private static $cloud;
2931

3032
public static function setUpBeforeClass()
3133
{
3234
self::$cloud = new ServiceBuilder;
33-
self::$autoloaders = spl_autoload_functions();
34-
foreach (self::$autoloaders as $function) {
35-
spl_autoload_unregister($function);
35+
foreach (spl_autoload_functions() as $function) {
36+
if ($function[0] instanceof ClassLoader) {
37+
$newAutoloader = clone $function[0];
38+
$newAutoloader->setPsr4('Google\Cloud\\', '/tmp');
39+
spl_autoload_register(self::$newAutoloadFunc = [$newAutoloader, 'loadClass']);
40+
spl_autoload_unregister(self::$previousAutoloadFunc = $function);
41+
}
3642
}
3743
}
3844

3945
public static function tearDownAfterClass()
4046
{
41-
foreach (self::$autoloaders as $function) {
42-
spl_autoload_register($function);
43-
}
47+
spl_autoload_register(self::$previousAutoloadFunc);
48+
spl_autoload_unregister(self::$newAutoloadFunc);
4449
}
4550

4651
public function serviceBuilderMethods()
@@ -61,11 +66,13 @@ public function serviceBuilderMethods()
6166
}
6267

6368
/**
69+
* @runInSeparateProcess
70+
* @preserveGlobalState disabled
6471
* @dataProvider serviceBuilderMethods
6572
*/
6673
public function testServicesNotFound($method)
6774
{
68-
$this->setExpectedException('Exception', sprintf(
75+
$this->setExpectedException(\Exception::class, sprintf(
6976
'The google/cloud-%s package is missing and must be installed.',
7077
strtolower($method)
7178
));

0 commit comments

Comments
 (0)