Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer committed Feb 21, 2024
1 parent b28eeab commit 6de828e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
19 changes: 19 additions & 0 deletions tests/Google/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Google\Service\Drive;
use Google\AuthHandler\AuthHandlerFactory;
use Google\Auth\FetchAuthTokenCache;
use Google\Auth\CredentialsLoader;
use Google\Auth\GCECache;
use Google\Auth\Credentials\GCECredentials;
use GuzzleHttp\Client as GuzzleClient;
Expand All @@ -38,6 +39,7 @@
use ReflectionMethod;
use InvalidArgumentException;
use Exception;
use DomainException;

class ClientTest extends BaseTest
{
Expand Down Expand Up @@ -925,4 +927,21 @@ public function testQueryParamsForAuthUrl()
]);
$this->assertStringContainsString('&enable_serial_consent=true', $authUrl1);
}
public function testUniverseDomainMismatch()
{
$this->expectException(DomainException::class);
$this->expectExceptionMessage(
'The configured universe domain (example.com) does not match the credential universe domain (foo.com)'
);

$credentials = $this->prophesize(CredentialsLoader::class);
$credentials->getUniverseDomain()
->shouldBeCalledOnce()
->willReturn('foo.com');
$client = new Client([
'universe_domain' => 'example.com',
'credentials' => $credentials->reveal(),
]);
$client->authorize();
}
}
42 changes: 37 additions & 5 deletions tests/Google/Service/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@

class TestService extends \Google\Service
{
public function __construct(Client $client)
public function __construct(Client $client, $rootUrl = null)
{
parent::__construct($client);
$this->rootUrl = "https://test.example.com";
$this->rootUrl = $rootUrl ?: "https://test.example.com";
$this->rootUrlTemplate = $rootUrl ?: "https://test.UNIVERSE_DOMAIN";
$this->servicePath = "";
$this->version = "v1beta1";
$this->serviceName = "test";
Expand All @@ -59,7 +60,7 @@ public function setUp(): void
$this->client->getLogger()->willReturn($logger->reveal());
$this->client->shouldDefer()->willReturn(true);
$this->client->getHttpClient()->willReturn(new GuzzleClient());
$this->client->getUniverseDomain()->willReturn('googleapis.com');
$this->client->getUniverseDomain()->willReturn('example.com');

$this->service = new TestService($this->client->reveal());
}
Expand Down Expand Up @@ -107,6 +108,37 @@ public function testCall()
$this->assertFalse($request->hasHeader('Content-Type'));
}

public function testCallWithUniverseDomainTemplate()
{
$client = $this->prophesize(Client::class);
$logger = $this->prophesize("Monolog\Logger");
$this->client->getLogger()->willReturn($logger->reveal());
$this->client->shouldDefer()->willReturn(true);
$this->client->getHttpClient()->willReturn(new GuzzleClient());
$this->client->getUniverseDomain()->willReturn('example-universe-domain.com');

$this->service = new TestService($this->client->reveal());

$resource = new GoogleResource(
$this->service,
"test",
"testResource",
[
"methods" => [
"testMethod" => [
"parameters" => [],
"path" => "method/path",
"httpMethod" => "POST",
]
]
]
);
$request = $resource->call("testMethod", [[]]);
$this->assertEquals("https://test.example-universe-domain.com/method/path", (string) $request->getUri());
$this->assertEquals("POST", $request->getMethod());
$this->assertFalse($request->hasHeader('Content-Type'));
}

public function testCallWithPostBody()
{
$resource = new GoogleResource(
Expand All @@ -131,9 +163,9 @@ public function testCallWithPostBody()

public function testCallServiceDefinedRoot()
{
$this->service->rootUrl = "https://sample.example.com";
$service = new TestService($this->client->reveal(), "https://sample.example.com");
$resource = new GoogleResource(
$this->service,
$service,
"test",
"testResource",
[
Expand Down

0 comments on commit 6de828e

Please sign in to comment.