Skip to content

Commit

Permalink
#184 - Client builder with two mandatory options (timeout, connect_ti…
Browse files Browse the repository at this point in the history
…meout)
  • Loading branch information
AloisJasa committed Apr 6, 2023
1 parent 6f61f73 commit b7656ac
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 0 deletions.
90 changes: 90 additions & 0 deletions app/Check/Consumers/Client/ClientBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php declare(strict_types = 1);

namespace Pd\Monitoring\Check\Consumers\Client;

class ClientBuilder
{

private ?int $connectTimeout;

private ?int $timeout;

/**
* @see https://docs.guzzlephp.org/en/stable/request-options.html#verify
*/
private bool $verify = TRUE;

private ?\Pd\Monitoring\Check\Consumers\Client\Conf\AllowRedirectsInterface $allowRedirects = NULL;


private function __construct(
?int $connectTimeout,
?int $timeout
)
{
$this->connectTimeout = $connectTimeout;
$this->timeout = $timeout;
}


public function withVerify(bool $verify): self
{
$factory = clone $this;
$factory->verify = $verify;

return $factory;
}


public function withAllowRedirects(\Pd\Monitoring\Check\Consumers\Client\Conf\AllowRedirectsInterface $allowRedirects): self
{
$factory = clone $this;
$factory->allowRedirects = $allowRedirects;

return $factory;
}


public static function create(
int $connectTimeout,
int $timeout
): self
{
return new self($connectTimeout, $timeout);
}


/**
* @return array<string, string|array<string, string>>
*/
private function config(): array
{
$config = [
'verify' => $this->verify,
'connect_timeout' => $this->connectTimeout,
'timeout' => $this->timeout,
];

$headers = [
'headers' => [
'User-Agent' => 'PeckaMonitoringBot/1.0',
],
];

return \array_filter(
\array_merge(
$config,
$headers,
$this->allowRedirects !== NULL ? $this->allowRedirects->toArray() : []
),
static fn ($value) => ! \is_null($value)
);
}


public function build(): \GuzzleHttp\Client
{
return new \GuzzleHttp\Client($this->config());
}

}
67 changes: 67 additions & 0 deletions tests/phpunit/Check/Consumers/Client/ClientBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Tests\Pd\Monitoring\Check\Consumers\Client;

class ClientBuilderTest extends \PHPUnit\Framework\TestCase
{

public function testDefault(): void
{
$client = \Pd\Monitoring\Check\Consumers\Client\ClientBuilder::create(10, 20);
$guzzle = $client->build();

$this->assertSame(['User-Agent' => 'PeckaMonitoringBot/1.0',], $guzzle->getConfig('headers'));
$this->assertSame(TRUE, $guzzle->getConfig('verify'));
$this->assertSame(10, $guzzle->getConfig('connect_timeout'));
$this->assertSame(20, $guzzle->getConfig('timeout'));
}


/**
* @dataProvider dataProviderWithVerify
*/
public function testWithAll(int $timeout, int $connectionTimeout, bool $verify, array $allowRedirectsOptions): void
{
$allowRedirects = new \Pd\Monitoring\Check\Consumers\Client\Conf\AllowRedirectsArray($allowRedirectsOptions);

$client =
\Pd\Monitoring\Check\Consumers\Client\ClientBuilder::create($connectionTimeout, $timeout)
->withVerify($verify)
->withAllowRedirects($allowRedirects)
;

$guzzle = $client->build();

$this->assertSame(['User-Agent' => 'PeckaMonitoringBot/1.0',], $guzzle->getConfig('headers'));
$this->assertSame($allowRedirectsOptions, $guzzle->getConfig('allow_redirects'));
$this->assertSame($verify, $guzzle->getConfig('verify'));
$this->assertSame($connectionTimeout, $guzzle->getConfig('connect_timeout'));
$this->assertSame($timeout, $guzzle->getConfig('timeout'));
}


public function dataProviderWithVerify(): array
{
return [
[
10,
20,
TRUE,
[
'max' => 5,
'protocols' => ['http', 'https',],
'strict' => FALSE,
'referer' => FALSE,
'track_redirects' => FALSE,
],
],
[
10,
20,
FALSE,
[],
],
];
}

}

0 comments on commit b7656ac

Please sign in to comment.