Skip to content

Commit fbe8122

Browse files
committed
1 parent 4610ab7 commit fbe8122

13 files changed

+208
-199
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* Class CurlClientFactory
4+
*
5+
* @created 14.03.2024
6+
* @author smiley <[email protected]>
7+
* @copyright 2024 smiley
8+
* @license MIT
9+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace chillerlan\HTTPTest\ClientFactories;
14+
15+
use chillerlan\HTTP\{CurlClient, HTTPOptions};
16+
use chillerlan\HTTPTest\HTTPClientTestAbstract;
17+
use chillerlan\PHPUnitHttp\HttpClientFactoryInterface;
18+
use Psr\Http\Client\ClientInterface;
19+
use Psr\Http\Message\ResponseFactoryInterface;
20+
21+
final class CurlClientFactory implements HttpClientFactoryInterface{
22+
23+
public function getClient(string $cacert, ResponseFactoryInterface $responseFactory):ClientInterface{
24+
$options = new HTTPOptions;
25+
$options->ca_info = $cacert;
26+
$options->user_agent = HTTPClientTestAbstract::USER_AGENT;
27+
28+
return new CurlClient($responseFactory, $options);
29+
}
30+
31+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Class CurlClientNoCAFactory
4+
*
5+
* @created 14.03.2024
6+
* @author smiley <[email protected]>
7+
* @copyright 2024 smiley
8+
* @license MIT
9+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace chillerlan\HTTPTest\ClientFactories;
14+
15+
use chillerlan\HTTP\{CurlClient, HTTPOptions};
16+
use chillerlan\HTTPTest\HTTPClientTestAbstract;
17+
use chillerlan\PHPUnitHttp\HttpClientFactoryInterface;
18+
use Psr\Http\Client\ClientInterface;
19+
use Psr\Http\Message\ResponseFactoryInterface;
20+
21+
final class CurlClientNoCAFactory implements HttpClientFactoryInterface{
22+
23+
public function getClient(string $cacert, ResponseFactoryInterface $responseFactory):ClientInterface{
24+
$options = new HTTPOptions;
25+
$options->ca_info = null;
26+
$options->ssl_verifypeer = false;
27+
$options->user_agent = HTTPClientTestAbstract::USER_AGENT;
28+
29+
return new CurlClient($responseFactory, $options);
30+
}
31+
32+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* Class StreamClientFactory
4+
*
5+
* @created 14.03.2024
6+
* @author smiley <[email protected]>
7+
* @copyright 2024 smiley
8+
* @license MIT
9+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace chillerlan\HTTPTest\ClientFactories;
14+
15+
use chillerlan\HTTP\{HTTPOptions, StreamClient};
16+
use chillerlan\HTTPTest\HTTPClientTestAbstract;
17+
use chillerlan\PHPUnitHttp\HttpClientFactoryInterface;
18+
use Psr\Http\Client\ClientInterface;
19+
use Psr\Http\Message\ResponseFactoryInterface;
20+
21+
final class StreamClientFactory implements HttpClientFactoryInterface{
22+
23+
public function getClient(string $cacert, ResponseFactoryInterface $responseFactory):ClientInterface{
24+
$options = new HTTPOptions;
25+
$options->ca_info = $cacert;
26+
$options->user_agent = HTTPClientTestAbstract::USER_AGENT;
27+
28+
return new StreamClient($responseFactory, $options);
29+
}
30+
31+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Class StreamClientNoCAFactory
4+
*
5+
* @created 14.03.2024
6+
* @author smiley <[email protected]>
7+
* @copyright 2024 smiley
8+
* @license MIT
9+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace chillerlan\HTTPTest\ClientFactories;
14+
15+
use chillerlan\HTTP\{HTTPOptions, StreamClient};
16+
use chillerlan\HTTPTest\HTTPClientTestAbstract;
17+
use chillerlan\PHPUnitHttp\HttpClientFactoryInterface;
18+
use Psr\Http\Client\ClientInterface;
19+
use Psr\Http\Message\ResponseFactoryInterface;
20+
21+
final class StreamClientNoCAFactory implements HttpClientFactoryInterface{
22+
23+
public function getClient(string $cacert, ResponseFactoryInterface $responseFactory):ClientInterface{
24+
$options = new HTTPOptions;
25+
$options->ca_info = null;
26+
$options->ssl_verifypeer = false;
27+
$options->user_agent = HTTPClientTestAbstract::USER_AGENT;
28+
29+
return new StreamClient($responseFactory, $options);
30+
}
31+
32+
}

tests/CurlClientNoCATest.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,15 @@
1212

1313
namespace chillerlan\HTTPTest;
1414

15-
use chillerlan\HTTP\CurlClient;
15+
use chillerlan\HTTPTest\ClientFactories\CurlClientNoCAFactory;
1616
use PHPUnit\Framework\Attributes\Group;
17-
use Psr\Http\Client\ClientInterface;
1817

1918
/**
2019
*
2120
*/
2221
#[Group('slow')]
23-
class CurlClientNoCATest extends HTTPClientTestAbstract{
22+
final class CurlClientNoCATest extends HTTPClientTestAbstract{
2423

25-
protected function initClient():ClientInterface{
26-
$this->options->ca_info = null;
27-
$this->options->ssl_verifypeer = false;
28-
29-
return new CurlClient($this->responseFactory, $this->options);
30-
}
24+
protected string $HTTP_CLIENT_FACTORY = CurlClientNoCAFactory::class;
3125

3226
}

tests/CurlClientTest.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,22 @@
1212

1313
namespace chillerlan\HTTPTest;
1414

15-
use chillerlan\HTTP\{CurlClient, RequestException};
15+
use chillerlan\HTTP\RequestException;
16+
use chillerlan\HTTPTest\ClientFactories\CurlClientFactory;
1617
use PHPUnit\Framework\Attributes\Group;
17-
use Psr\Http\Client\ClientInterface;
1818

1919
/**
2020
*
2121
*/
2222
#[Group('slow')]
23-
class CurlClientTest extends HTTPClientTestAbstract{
23+
final class CurlClientTest extends HTTPClientTestAbstract{
2424

25-
protected function initClient():ClientInterface{
26-
return new CurlClient($this->responseFactory, $this->options);
27-
}
25+
protected string $HTTP_CLIENT_FACTORY = CurlClientFactory::class;
2826

2927
public function testRequestError():void{
3028
$this->expectException(RequestException::class);
3129

32-
$this->http->sendRequest($this->requestFactory->createRequest('GET', ''));
30+
$this->httpClient->sendRequest($this->requestFactory->createRequest('GET', ''));
3331
}
3432

3533
}

tests/CurlHandleTest.php

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212

1313
namespace chillerlan\HTTPTest;
1414

15-
use chillerlan\HTTP\{CurlClient, CurlHandle, HTTPOptions};
15+
use chillerlan\HTTP\{CurlHandle, HTTPOptions};
1616
use chillerlan\HTTP\Utils\MessageUtil;
17-
use PHPUnit\Framework\Attributes\DataProvider;
18-
use PHPUnit\Framework\Attributes\Group;
17+
use chillerlan\HTTPTest\ClientFactories\CurlClientFactory;
18+
use chillerlan\PHPUnitHttp\HttpFactoryTrait;
19+
use PHPUnit\Framework\Attributes\{DataProvider, Group};
1920
use PHPUnit\Framework\TestCase;
20-
use Psr\Http\Client\{ClientExceptionInterface, ClientInterface};
21+
use Psr\Http\Client\ClientExceptionInterface;
2122
use Exception, Throwable;
2223
use function str_repeat, strlen, strtolower, realpath;
2324
use const CURLOPT_CAINFO, CURLOPT_CAPATH, CURLOPT_SSL_VERIFYHOST, CURLOPT_SSL_VERIFYPEER;
@@ -27,19 +28,17 @@
2728
*/
2829
#[Group('slow')]
2930
class CurlHandleTest extends TestCase{
30-
use FactoryTrait;
31+
use HttpFactoryTrait;
3132

32-
protected ClientInterface $http;
33+
protected string $HTTP_CLIENT_FACTORY = CurlClientFactory::class;
3334

34-
// called from FactoryTrait
3535
protected function setUp():void{
36-
$this->initFactories();
37-
38-
$options = new HTTPOptions([
39-
'ca_info' => __DIR__.'/cacert.pem',
40-
]);
41-
42-
$this->http = new CurlClient($this->responseFactory, $options);
36+
try{
37+
$this->initFactories(realpath(__DIR__.'/cacert.pem'));
38+
}
39+
catch(Throwable $e){
40+
$this->markTestSkipped('unable to init http factories: '.$e->getMessage());
41+
}
4342
}
4443

4544
protected function createHandle(HTTPOptions $options, string $method = 'GET'):CurlHandle{
@@ -95,14 +94,13 @@ public function testCaInfoFile(string $option, mixed $value, string $expectedPat
9594
}
9695

9796
public static function requestMethodProvider():array{
97+
// head and options are not supported by httpbin
9898
return [
99-
'delete' => ['DELETE'],
100-
'get' => ['GET'],
101-
# 'head' => ['HEAD'],
102-
# 'options' => ['OPTIONS'],
103-
'patch' => ['PATCH'],
104-
'post' => ['POST'],
105-
'put' => ['PUT'],
99+
'delete' => ['DELETE'],
100+
'get' => ['GET'],
101+
'patch' => ['PATCH'],
102+
'post' => ['POST'],
103+
'put' => ['PUT'],
106104
];
107105
}
108106

@@ -112,7 +110,7 @@ public function testRequestMethods(string $method):void{
112110
$request = $this->requestFactory->createRequest($method, $url);
113111

114112
try{
115-
$response = $this->http->sendRequest($request);
113+
$response = $this->httpClient->sendRequest($request);
116114
$status = $response->getStatusCode();
117115

118116
if($status !== 200){
@@ -149,7 +147,7 @@ public function testRequestMethodsWithFormBody(string $method):void{
149147
;
150148

151149
try{
152-
$response = $this->http->sendRequest($request);
150+
$response = $this->httpClient->sendRequest($request);
153151
$status = $response->getStatusCode();
154152

155153
if($status !== 200){
@@ -178,7 +176,7 @@ public function testRequestMethodsWithJsonBody(string $method):void{
178176
;
179177

180178
try{
181-
$response = $this->http->sendRequest($request);
179+
$response = $this->httpClient->sendRequest($request);
182180
$status = $response->getStatusCode();
183181

184182
if($status !== 200){
@@ -207,7 +205,7 @@ public function testLargeBody():void{
207205
;
208206

209207
try{
210-
$response = $this->http->sendRequest($request);
208+
$response = $this->httpClient->sendRequest($request);
211209
$status = $response->getStatusCode();
212210

213211
if($status !== 200){

tests/CurlMultiClientTest.php

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use chillerlan\HTTP\{CurlMultiClient, HTTPOptions, MultiResponseHandlerInterface};
1616
use chillerlan\HTTP\Utils\QueryUtil;
17+
use chillerlan\PHPUnitHttp\HttpFactoryTrait;
1718
use PHPUnit\Framework\Attributes\Group;
1819
use PHPUnit\Framework\TestCase;
1920
use Psr\Http\Client\ClientExceptionInterface;
@@ -25,11 +26,11 @@
2526
*
2627
*/
2728
#[Group('slow')]
28-
class CurlMultiClientTest extends TestCase{
29-
use FactoryTrait;
29+
final class CurlMultiClientTest extends TestCase{
30+
use HttpFactoryTrait;
3031

31-
protected CurlMultiClient $http;
32-
protected MultiResponseHandlerInterface $multiResponseHandler;
32+
private CurlMultiClient $http;
33+
private MultiResponseHandlerInterface $multiResponseHandler;
3334

3435
protected function setUp():void{
3536
$this->initFactories();
@@ -45,32 +46,11 @@ protected function setUp():void{
4546
$this->http = new CurlMultiClient($this->multiResponseHandler, $this->responseFactory, $options);
4647
}
4748

48-
protected function getRequests():array{
49-
50-
$ids = [
51-
[1, 2, 6, 11, 15, 23, 24, 56, 57, 58, 59, 60, 61, 62, 63, 64, 68, 69, 70, 71, 72, 73, 74, 75, 76],
52-
[77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101],
53-
];
54-
55-
$requests = [];
56-
57-
foreach($ids as $chunk){
58-
foreach(['de', 'en', 'es', 'fr', 'zh'] as $lang){
59-
$requests[] = $this->requestFactory->createRequest(
60-
'GET',
61-
'https://api.guildwars2.com/v2/items?'.QueryUtil::build(['lang' => $lang, 'ids' => implode(',', $chunk)])
62-
);
63-
}
64-
}
65-
66-
return $requests;
67-
}
68-
69-
protected function getTestResponseHandler():MultiResponseHandlerInterface{
49+
private function getTestResponseHandler():MultiResponseHandlerInterface{
7050

7151
return new class () implements MultiResponseHandlerInterface{
7252

73-
protected array $responses = [];
53+
private array $responses = [];
7454

7555
public function handleResponse(
7656
ResponseInterface $response,
@@ -100,9 +80,27 @@ public function getResponses():array{
10080

10181
}
10282

103-
/**
104-
* @todo
105-
*/
83+
private function getRequests():array{
84+
85+
$ids = [
86+
[1, 2, 6, 11, 15, 23, 24, 56, 57, 58, 59, 60, 61, 62, 63, 64, 68, 69, 70, 71, 72, 73, 74, 75, 76],
87+
[77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101],
88+
];
89+
90+
$requests = [];
91+
92+
foreach($ids as $chunk){
93+
foreach(['de', 'en', 'es', 'fr', 'zh'] as $lang){
94+
$requests[] = $this->requestFactory->createRequest(
95+
'GET',
96+
'https://api.guildwars2.com/v2/items?'.QueryUtil::build(['lang' => $lang, 'ids' => implode(',', $chunk)])
97+
);
98+
}
99+
}
100+
101+
return $requests;
102+
}
103+
106104
public function testMultiRequest():void{
107105
$requests = $this->getRequests();
108106

0 commit comments

Comments
 (0)