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

extend provider options with httpClientConfig key #771

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 10 additions & 4 deletions src/Provider/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,17 @@ public function __construct(array $options = [], array $collaborators = [])
$this->setRequestFactory($collaborators['requestFactory']);

if (empty($collaborators['httpClient'])) {
$client_config = [];
$client_options = $this->getAllowedClientOptions($options);

$collaborators['httpClient'] = new HttpClient(
array_intersect_key($options, array_flip($client_options))
);
if ($client_options) {
$client_config = array_intersect_key($options, array_flip($client_options));
}
// if set httpClientConfig, override the previous options
if (isset($options['httpClientConfig'])) {
$client_options = $this->getAllowedClientOptions($options['httpClientConfig']);
$client_config = array_intersect_key($options['httpClientConfig'], array_flip($client_options));
}
$collaborators['httpClient'] = new HttpClient($client_config);
}
$this->setHttpClient($collaborators['httpClient']);

Expand Down
61 changes: 61 additions & 0 deletions test/src/Provider/AbstractProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,67 @@ public function testCanDisableVerificationIfThereIsAProxy()
$this->assertFalse($config['verify']);
}

public function testConstructorSetsHttpClientConfig()
{
$timeout = rand(100, 900);

$httpClientConfig = [
'timeout' => $timeout,
];
$mockProvider = new MockProvider(['httpClientConfig' => $httpClientConfig]);

$config = $mockProvider->getHttpClient()->getConfig();

$this->assertContains('timeout', $config);
$this->assertEquals($timeout, $config['timeout']);
}

public function testCanSetAProxyInHttpClientConfig()
{
$proxy = '192.168.0.1:8888';
$httpClientConfig = [
'proxy' => $proxy,
];
$mockProvider = new MockProvider(['httpClientConfig' => $httpClientConfig]);

$config = $mockProvider->getHttpClient()->getConfig();

$this->assertContains('proxy', $config);
$this->assertEquals($proxy, $config['proxy']);
}

public function testCannotDisableVerifyIfNoProxyInHttpClientConfig()
{
$httpClientConfig = [
'verify' => false,
];
$mockProvider = new MockProvider([$httpClientConfig]);

$config = $mockProvider->getHttpClient()->getConfig();

$this->assertContains('verify', $config);
$this->assertTrue($config['verify']);
}

public function testOptionsOverrideByHttpClientConfig()
{
$configTimeout = 20;
$optionsTimeout = 10;
$httpClientConfig = [
'timeout' => $configTimeout,
];
$options = [
'timeout' => $optionsTimeout,
'httpClientConfig' => $httpClientConfig,
];
$mockProvider = new MockProvider($options);

$config = $mockProvider->getHttpClient()->getConfig();

$this->assertContains('timeout', $config);
$this->assertEquals($configTimeout, $config['timeout']);
}

public function testConstructorSetsGrantFactory()
{
$mockAdapter = Mockery::mock(GrantFactory::class);
Expand Down