Skip to content

Commit 11f5b95

Browse files
author
Denis Smetannikov
committed
Fixes tests
1 parent cefe2c8 commit 11f5b95

File tree

7 files changed

+94
-69
lines changed

7 files changed

+94
-69
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# JBZoo - Http-Client
1+
# JBZoo / Http-Client
22

3-
[![Build Status](https://travis-ci.org/JBZoo/Http-Client.svg?branch=master)](https://travis-ci.org/JBZoo/Http-Client) [![Coverage Status](https://coveralls.io/repos/JBZoo/Http-Client/badge.svg)](https://coveralls.io/github/JBZoo/Http-Client?branch=master) [![Psalm Coverage](https://shepherd.dev/github/JBZoo/Http-Client/coverage.svg)](https://shepherd.dev/github/JBZoo/Http-Client) [![Codacy Badge](https://app.codacy.com/project/badge/Grade/68dfbc5c587743ac82f0f8c75f46279d)](https://www.codacy.com/gh/JBZoo/Http-Client)
3+
[![Build Status](https://travis-ci.org/JBZoo/Http-Client.svg?branch=master)](https://travis-ci.org/JBZoo/Http-Client) [![Coverage Status](https://coveralls.io/repos/JBZoo/Http-Client/badge.svg)](https://coveralls.io/github/JBZoo/Http-Client?branch=master) [![Psalm Coverage](https://shepherd.dev/github/JBZoo/Http-Client/coverage.svg)](https://shepherd.dev/github/JBZoo/Http-Client)
44
[![Latest Stable Version](https://poser.pugx.org/JBZoo/Http-Client/v)](https://packagist.org/packages/JBZoo/Http-Client) [![Latest Unstable Version](https://poser.pugx.org/JBZoo/Http-Client/v/unstable)](https://packagist.org/packages/JBZoo/Http-Client) [![Dependents](https://poser.pugx.org/JBZoo/Http-Client/dependents)](https://packagist.org/packages/JBZoo/Http-Client/dependents?order_by=downloads) [![GitHub Issues](https://img.shields.io/github/issues/JBZoo/Http-Client)](https://github.com/JBZoo/Http-Client/issues) [![Total Downloads](https://poser.pugx.org/JBZoo/Http-Client/downloads)](https://packagist.org/packages/JBZoo/Http-Client/stats) [![GitHub License](https://img.shields.io/github/license/JBZoo/Http-Client)](https://github.com/JBZoo/Http-Client/blob/master/LICENSE)
55

66

src/Driver/Guzzle.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
namespace JBZoo\HttpClient\Driver;
1717

1818
use GuzzleHttp\Client;
19+
use JBZoo\Data\Data;
1920
use JBZoo\HttpClient\Options;
2021
use JBZoo\Utils\Url;
2122
use Throwable;
@@ -62,16 +63,17 @@ public function multiRequest(array $urls)
6263
$urlData = [$urlData, []];
6364
}
6465

65-
$urlOptions = new Options($urlData[1]);
66+
$requestOptions = new Options($urlData[1]);
67+
$urlParams = new Data($urlData[1]);
6668

67-
$method = $urlOptions->get('method', 'GET', 'up');
68-
$args = $urlOptions->get('args');
69+
$method = $urlParams->get('method', 'GET', 'up');
70+
$args = $urlParams->get('args');
6971
$url = 'GET' === $method ? Url::addArg((array)$args, $urlData[0]) : $urlData[0];
7072

7173
$promises[$urlName] = $client->requestAsync(
7274
$method,
7375
$url,
74-
$this->getClientOptions($urlOptions, $method, $args)
76+
$this->getClientOptions($requestOptions, $method, $args)
7577
);
7678
}
7779

@@ -117,7 +119,7 @@ protected function getClientOptions(Options $options, $method, $args)
117119
'connect_timeout' => $options->getTimeout(),
118120
'timeout' => $options->getTimeout(),
119121
'verify' => $options->isVerify(),
120-
'exceptions' => $options->showException(),
122+
'exceptions' => $options->allowException(),
121123
'auth' => $options->getAuth(),
122124
'allow_redirects' => $this->getAllowRedirects($options)
123125
];

src/Driver/Rmccue.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
namespace JBZoo\HttpClient\Driver;
1717

18+
use JBZoo\Data\Data;
1819
use JBZoo\HttpClient\Exception;
1920
use JBZoo\HttpClient\Options;
2021
use JBZoo\Utils\Filter;
@@ -34,7 +35,7 @@ class Rmccue extends AbstractDriver
3435
*/
3536
public function request(string $url, $args, string $method, Options $options)
3637
{
37-
$headers = $options->get('headers', []);
38+
$headers = $options->getHeaders();
3839
$method = Filter::up($method);
3940
$args = 'GET' !== $method ? $args : [];
4041

@@ -44,14 +45,15 @@ public function request(string $url, $args, string $method, Options $options)
4445
*/
4546
$httpResult = Requests::request(
4647
$url,
47-
(array)$headers,
48+
$headers,
4849
/** @phan-suppress-next-line PhanPartialTypeMismatchArgument */
4950
$args,
5051
$method,
5152
$this->getClientOptions($options)
5253
);
5354

54-
if ($httpResult->status_code >= self::INVALID_CODE_LINE && $options->showException()) {
55+
56+
if ($httpResult->status_code >= self::INVALID_CODE_LINE && $options->allowException()) {
5557
throw new Exception($httpResult->body, (int)$httpResult->status_code);
5658
}
5759

@@ -69,33 +71,38 @@ public function request(string $url, $args, string $method, Options $options)
6971
public function multiRequest(array $urls)
7072
{
7173
$requests = [];
74+
75+
/** @var array|string $urlData */
7276
foreach ($urls as $urlName => $urlData) {
7377
if (is_string($urlData)) {
7478
$urlData = [$urlData, []];
7579
}
7680

77-
$urlOptions = new Options($urlData[1]);
81+
$requestOptions = new Options((array)$urlData[1]);
82+
$urlParams = new Data((array)$urlData[1]);
7883

79-
$method = $urlOptions->get('method', 'GET', 'up');
80-
$args = $urlOptions->get('args');
81-
$url = 'GET' === $method ? Url::addArg((array)$args, $urlData[0]) : $urlData[0];
84+
$method = (string)$urlParams->get('method', 'GET', 'up');
85+
$args = (array)$urlParams->get('args');
86+
87+
$url = 'GET' === $method ? Url::addArg($args, (string)$urlData[0]) : (string)$urlData[0];
8288
$args = 'GET' !== $method ? $args : [];
8389

8490
$requests[$urlName] = [
8591
'url' => $url,
86-
'headers' => $urlOptions->getHeaders(),
92+
'headers' => $requestOptions->getHeaders(),
8793
'data' => $args,
8894
'type' => $method,
89-
'options' => $this->getClientOptions($urlOptions),
95+
'options' => $this->getClientOptions($requestOptions),
9096
];
9197
}
9298

9399
$httpResults = Requests::request_multiple($requests);
94100

95101
$result = [];
102+
96103
foreach ($httpResults as $resName => $httpResult) {
97104
$result[$resName] = [
98-
$httpResult->status_code,
105+
(int)$httpResult->status_code,
99106
$httpResult->headers->getAll(),
100107
$httpResult->body
101108
];
@@ -116,7 +123,7 @@ protected function getClientOptions(Options $options)
116123
'follow_redirects' => $options->isAllowRedirects(),
117124
'redirects' => $options->getMaxRedirects(),
118125
'useragent' => $options->getUserAgent('Rmccue'),
119-
'auth' => $options->getAuth(),
126+
'auth' => $options->getAuth() ?: false,
120127
];
121128
}
122129
}

src/HttpClient.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,29 @@ public function __construct(array $options = [])
4747
* @return Response
4848
* @throws Exception
4949
*/
50-
public function request($url, $args = null, $method = Options::DEFAULT_METHOD, array $options = []): Response
51-
{
50+
public function request(
51+
string $url,
52+
$args = null,
53+
string $method = Options::DEFAULT_METHOD,
54+
array $options = []
55+
): Response {
5256
$method = Filter::up($method);
5357
$url = 'GET' === $method ? Url::addArg((array)$args, $url) : $url;
5458

5559
/** @noinspection CallableParameterUseCaseInTypeContextInspection */
56-
$options = new Options(array_merge($this->options->getArrayCopy(), $options));
60+
$options = new Options(array_merge($this->options->toArray(), $options));
5761

5862
$client = $this->getClient($options);
5963
$response = new Response();
6064

6165
try {
6266
[$code, $headers, $body] = $client->request($url, $args, $method, $options);
6367

64-
$response->setCode($code);
65-
$response->setHeaders($headers);
66-
$response->setBody($body);
68+
$response->setCode((int)$code);
69+
$response->setHeaders((array)$headers);
70+
$response->setBody((string)$body);
6771
} catch (\Exception $exception) {
68-
if ($options->showException()) {
72+
if ($options->allowException()) {
6973
throw new Exception($exception->getMessage(), (int)$exception->getCode(), $exception);
7074
}
7175

@@ -86,19 +90,21 @@ public function request($url, $args = null, $method = Options::DEFAULT_METHOD, a
8690
public function multiRequest(array $urls, array $options = [])
8791
{
8892
/** @noinspection CallableParameterUseCaseInTypeContextInspection */
89-
$options = new Options(array_merge($this->options->getArrayCopy(), $options));
93+
$options = new Options(array_merge($this->options->toArray(), $options));
9094
$client = $this->getClient($options);
9195

9296
$httpResults = $client->multiRequest($urls);
9397

9498
$results = [];
99+
100+
/** @var array $resultRow */
95101
foreach ($httpResults as $resKey => $resultRow) {
96102
[$code, $headers, $body] = $resultRow;
97103

98104
$results[$resKey] = new Response();
99-
$results[$resKey]->setCode($code);
100-
$results[$resKey]->setHeaders($headers);
101-
$results[$resKey]->setBody($body);
105+
$results[$resKey]->setCode((int)$code);
106+
$results[$resKey]->setHeaders((array)$headers);
107+
$results[$resKey]->setBody((string)$body);
102108
}
103109

104110
return $results;
@@ -109,6 +115,7 @@ public function multiRequest(array $urls, array $options = [])
109115
* @return AbstractDriver
110116
* @throws Exception
111117
* @psalm-suppress MoreSpecificReturnType
118+
* @psalm-suppress LessSpecificReturnStatement
112119
*/
113120
protected function getClient(Options $options): AbstractDriver
114121
{
@@ -117,8 +124,7 @@ protected function getClient(Options $options): AbstractDriver
117124
$className = __NAMESPACE__ . "\\Driver\\{$driverName}";
118125

119126
if (class_exists($className)) {
120-
/** @psalm-suppress LessSpecificReturnStatement */
121-
return new $className($options);
127+
return new $className($options);
122128
}
123129

124130
throw new Exception("Driver '{$driverName}' not found");

src/Options.php

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@
1515

1616
namespace JBZoo\HttpClient;
1717

18-
use JBZoo\Data\JSON;
18+
use JBZoo\Data\Data;
19+
20+
use function JBZoo\Utils\bool;
21+
use function JBZoo\Utils\int;
1922

2023
/**
2124
* Class Options
2225
* @package JBZoo\HttpClient
2326
*/
24-
class Options extends JSON
27+
class Options
2528
{
2629
public const DEFAULT_METHOD = 'GET';
2730
public const DEFAULT_DRIVER = 'Guzzle';
@@ -36,7 +39,7 @@ class Options extends JSON
3639
* @var array
3740
*/
3841
protected $default = [
39-
'auth' => false,
42+
'auth' => [],
4043
'headers' => [],
4144
'driver' => self::DEFAULT_DRIVER,
4245
'timeout' => self::DEFAULT_TIMEOUT,
@@ -47,86 +50,99 @@ class Options extends JSON
4750
'user_agent' => self::DEFAULT_USER_AGENT,
4851
];
4952

53+
/**
54+
* @var Data
55+
*/
56+
protected $data;
57+
5058
/**
5159
* Response constructor.
5260
* @param array $data
5361
*/
54-
public function __construct($data = [])
62+
public function __construct(array $data = [])
5563
{
56-
$data = array_merge($this->default, $data);
57-
parent::__construct($data);
64+
$this->data = new Data(array_merge($this->default, $data));
5865
}
5966

6067
/**
61-
* @return array
68+
* @return array|null
6269
*/
63-
public function getAuth()
70+
public function getAuth(): ?array
6471
{
65-
return $this->get('auth', false);
72+
return (array)$this->data->get('auth', []) ?: null;
6673
}
6774

6875
/**
6976
* @return array
7077
*/
71-
public function getHeaders()
78+
public function getHeaders(): array
7279
{
73-
return $this->get('headers', []);
80+
return (array)$this->data->get('headers', []);
7481
}
7582

7683
/**
7784
* @return string
7885
*/
79-
public function getDriver()
86+
public function getDriver(): string
8087
{
81-
return $this->get('driver', self::DEFAULT_DRIVER, 'ucfirst');
88+
return (string)$this->data->get('driver', self::DEFAULT_DRIVER, 'ucfirst');
8289
}
8390

8491
/**
8592
* @return int
8693
*/
87-
public function getTimeout()
94+
public function getTimeout(): int
8895
{
89-
return $this->get('timeout', self::DEFAULT_TIMEOUT, 'int');
96+
return int($this->data->get('timeout', self::DEFAULT_TIMEOUT, 'int'));
9097
}
9198

9299
/**
93100
* @return bool
94101
*/
95-
public function isVerify()
102+
public function isVerify(): bool
96103
{
97-
return $this->get('verify', self::DEFAULT_VERIFY, 'bool');
104+
return bool($this->data->get('verify', self::DEFAULT_VERIFY, 'bool'));
98105
}
99106

100107
/**
101108
* @return bool
102109
*/
103-
public function showException()
110+
public function allowException(): bool
104111
{
105-
return $this->get('exceptions', self::DEFAULT_EXCEPTIONS, 'bool');
112+
return bool($this->data->get('exceptions', self::DEFAULT_EXCEPTIONS, 'bool'));
106113
}
107114

108115
/**
109116
* @return bool
110117
*/
111-
public function isAllowRedirects()
118+
public function isAllowRedirects(): bool
112119
{
113-
return $this->get('allow_redirects', self::DEFAULT_ALLOW_REDIRECTS, 'bool');
120+
return bool($this->data->get('allow_redirects', self::DEFAULT_ALLOW_REDIRECTS, 'bool'));
114121
}
115122

116123
/**
117124
* @return int
118125
*/
119-
public function getMaxRedirects()
126+
public function getMaxRedirects(): int
120127
{
121-
return $this->get('max_redirects', self::DEFAULT_MAX_REDIRECTS, 'int');
128+
return int($this->data->get('max_redirects', self::DEFAULT_MAX_REDIRECTS, 'int'));
122129
}
123130

124131
/**
125132
* @param string $suffix
126133
* @return string
127134
*/
128-
public function getUserAgent($suffix)
135+
public function getUserAgent(string $suffix): string
136+
{
137+
$packageName = (string)$this->data->get('user_agent', self::DEFAULT_USER_AGENT);
138+
return "{$packageName} ({$suffix})";
139+
}
140+
141+
/**
142+
* @return array
143+
*/
144+
public function toArray(): array
129145
{
130-
return $this->get('user_agent', self::DEFAULT_USER_AGENT) . " ({$suffix})";
146+
return $this->data->getArrayCopy();
131147
}
132148
}

tests/AbstractDriverTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ abstract class AbstractDriverTest extends PHPUnit
3030
/**
3131
* @var string
3232
*/
33-
protected $driver = 'Undefined'; // For quick toggling tests (Auto|Guzzle|Rmccue)
33+
protected $driver = 'Auto';
3434

3535
/**
3636
* @var array
@@ -74,7 +74,7 @@ public function testPOSTPayload()
7474
$payload = json_encode(['key' => $uniq]);
7575

7676
$url = 'http://mockbin.org/request?key=value';
77-
$result = $this->getClient()->request($url, $payload, 'post');
77+
$result = $this->getClient(['exceptions' => true])->request($url, $payload, 'post');
7878
$body = $result->getJSON();
7979

8080
isSame($payload, $body->find('postData.text'));

0 commit comments

Comments
 (0)