diff --git a/src/Core/ServiceBuilder.php b/src/Core/ServiceBuilder.php index 05d5641abe52..653e055a0e99 100644 --- a/src/Core/ServiceBuilder.php +++ b/src/Core/ServiceBuilder.php @@ -118,7 +118,7 @@ public function __construct(array $config = []) */ public function bigQuery(array $config = []) { - return new BigQueryClient($config ? $this->resolveConfig($config) : $this->config); + return $this->createClient(BigQueryClient::class, 'bigquery', $config); } /** @@ -142,7 +142,7 @@ public function bigQuery(array $config = []) */ public function datastore(array $config = []) { - return new DatastoreClient($config ? $this->resolveConfig($config) : $this->config); + return $this->createClient(DatastoreClient::class, 'datastore', $config); } /** @@ -162,7 +162,7 @@ public function datastore(array $config = []) */ public function logging(array $config = []) { - return new LoggingClient($config ? $this->resolveConfig($config) : $this->config); + return $this->createClient(LoggingClient::class, 'logging', $config); } /** @@ -183,7 +183,7 @@ public function logging(array $config = []) */ public function language(array $config = []) { - return new LanguageClient($config ? $this->resolveConfig($config) : $this->config); + return $this->createClient(LanguageClient::class, 'language', $config); } /** @@ -207,7 +207,7 @@ public function language(array $config = []) */ public function pubsub(array $config = []) { - return new PubSubClient($config ? $this->resolveConfig($config) : $this->config); + return $this->createClient(PubSubClient::class, 'pubsub', $config); } /** @@ -232,7 +232,7 @@ public function pubsub(array $config = []) */ public function spanner(array $config = []) { - return new SpannerClient($config ? $this->resolveConfig($config) : $this->config); + return $this->createClient(SpannerClient::class, 'spanner', $config); } /** @@ -261,7 +261,7 @@ public function spanner(array $config = []) */ public function speech(array $config = []) { - return new SpeechClient($config ? $this->resolveConfig($config) : $this->config); + return $this->createClient(SpeechClient::class, 'speech', $config); } /** @@ -280,7 +280,7 @@ public function speech(array $config = []) */ public function storage(array $config = []) { - return new StorageClient($config ? $this->resolveConfig($config) : $this->config); + return $this->createClient(StorageClient::class, 'storage', $config); } @@ -300,7 +300,7 @@ public function storage(array $config = []) */ public function trace(array $config = []) { - return new TraceClient($config ? $this->resolveConfig($config) : $this->config); + return $this->createClient(TraceClient::class, 'trace', $config); } /** @@ -320,7 +320,7 @@ public function trace(array $config = []) */ public function vision(array $config = []) { - return new VisionClient($config ? $this->resolveConfig($config) : $this->config); + return $this->createClient(VisionClient::class, 'vision', $config); } /** @@ -368,7 +368,18 @@ public function vision(array $config = []) */ public function translate(array $config = []) { - return new TranslateClient($config ? $this->resolveConfig($config) : $this->config); + return $this->createClient(TranslateClient::class, 'translate', $config); + } + + private function createClient($class, $packageName, array $config = []) + { + if (class_exists($class)) { + return new $class($config ? $this->resolveConfig($config) : $this->config); + } + throw new \Exception(sprintf( + 'The google/cloud-%s package is missing and must be installed.', + $packageName + )); } /** diff --git a/tests/system/Core/ServicesNotFoundTest.php b/tests/unit/Core/ServicesNotFoundTest.php similarity index 65% rename from tests/system/Core/ServicesNotFoundTest.php rename to tests/unit/Core/ServicesNotFoundTest.php index 7623c2d5632a..017b63175ac3 100644 --- a/tests/system/Core/ServicesNotFoundTest.php +++ b/tests/unit/Core/ServicesNotFoundTest.php @@ -15,32 +15,37 @@ * limitations under the License. */ -namespace Google\Cloud\Tests\System\Core; +namespace Google\Cloud\Tests\Unit\Core; use Google\Cloud\Core\ServiceBuilder; +use Composer\Autoload\ClassLoader; /** * @group core */ class ServicesNotFoundTest extends \PHPUnit_Framework_TestCase { - private static $autoloaders; + private static $previousAutoloadFunc; + private static $newAutoloadFunc; private static $cloud; public static function setUpBeforeClass() { self::$cloud = new ServiceBuilder; - self::$autoloaders = spl_autoload_functions(); - foreach (self::$autoloaders as $function) { - spl_autoload_unregister($function); + foreach (spl_autoload_functions() as $function) { + if ($function[0] instanceof ClassLoader) { + $newAutoloader = clone $function[0]; + $newAutoloader->setPsr4('Google\Cloud\\', '/tmp'); + spl_autoload_register(self::$newAutoloadFunc = [$newAutoloader, 'loadClass']); + spl_autoload_unregister(self::$previousAutoloadFunc = $function); + } } } public static function tearDownAfterClass() { - foreach (self::$autoloaders as $function) { - spl_autoload_register($function); - } + spl_autoload_register(self::$previousAutoloadFunc); + spl_autoload_unregister(self::$newAutoloadFunc); } public function serviceBuilderMethods() @@ -61,11 +66,13 @@ public function serviceBuilderMethods() } /** + * @runInSeparateProcess + * @preserveGlobalState disabled * @dataProvider serviceBuilderMethods */ public function testServicesNotFound($method) { - $this->setExpectedException('Exception', sprintf( + $this->setExpectedException(\Exception::class, sprintf( 'The google/cloud-%s package is missing and must be installed.', strtolower($method) ));