Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensures servicebuilder throws proper exceptions #682

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions src/Core/ServiceBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}


Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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
));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
));
Expand Down