Skip to content

Commit

Permalink
#6 Added possibility to provide custom guzzle options (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
duxthefux authored and rccrdpccl committed Feb 7, 2018
1 parent d916a41 commit 992eb59
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/ClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

class ClientBuilder
{
public static function build(string $endpoint)
public static function build(string $endpoint, array $guzzleOptions = []): Client
{
$guzzleOptions = array_merge(['base_uri' => $endpoint], $guzzleOptions);

return new \Softonic\GraphQL\Client(
new \GuzzleHttp\Client(['base_uri' => $endpoint]),
new \GuzzleHttp\Client($guzzleOptions),
new \Softonic\GraphQL\ResponseBuilder()
);
}
Expand All @@ -19,11 +21,11 @@ public static function buildWithOAuth2Provider(
string $endpoint,
OAuth2Provider $oauthProvider,
array $tokenOptions,
Cache $cache
Cache $cache,
array $guzzleOptions = []
): Client {
$guzzleOptions = [
'base_uri' => $endpoint,
];
$guzzleOptions = array_merge(['base_uri' => $endpoint], $guzzleOptions);


return new \Softonic\GraphQL\Client(
\Softonic\OAuth2\Guzzle\Middleware\ClientBuilder::build(
Expand Down
34 changes: 34 additions & 0 deletions tests/ClientBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Softonic\GraphQL\Test;

use GuzzleHttp\Cookie\CookieJar;
use PHPUnit\Framework\TestCase;
use Softonic\GraphQL\ClientBuilder;

Expand All @@ -13,6 +14,16 @@ public function testBuild()
$this->assertInstanceOf(\Softonic\GraphQL\Client::class, $client);
}

public function testBuildWithGuzzleOptions()
{
$guzzleOptions = [
'cookies' => new CookieJar(),
];

$client = ClientBuilder::build('http://foo.bar/qux', $guzzleOptions);
$this->assertInstanceOf(\Softonic\GraphQL\Client::class, $client);
}

public function testBuildWithOAuth2Provider()
{
$mockCache = $this->createMock(\Psr\Cache\CacheItemPoolInterface::class);
Expand All @@ -30,4 +41,27 @@ public function testBuildWithOAuth2Provider()
);
$this->assertInstanceOf(\Softonic\GraphQL\Client::class, $client);
}

public function testBuildWithOAuth2ProviderAndGuzzleOptions()
{
$mockCache = $this->createMock(\Psr\Cache\CacheItemPoolInterface::class);
$mockProvider = $this->createMock(\League\OAuth2\Client\Provider\AbstractProvider::class);
$mockTokenOptions = [
'grant_type' => 'client_credentials',
'scope' => 'myscope',
];

$guzzleOptions = [
'cookies' => new CookieJar(),
];

$client = ClientBuilder::buildWithOAuth2Provider(
'http://foo.bar/qux',
$mockProvider,
$mockTokenOptions,
$mockCache,
$guzzleOptions
);
$this->assertInstanceOf(\Softonic\GraphQL\Client::class, $client);
}
}

0 comments on commit 992eb59

Please sign in to comment.