Skip to content

Commit

Permalink
Merge pull request #4 from dmt-software/2.0
Browse files Browse the repository at this point in the history
2.0
  • Loading branch information
proggeler authored Aug 29, 2024
2 parents 18a8f9e + d4a05b5 commit db80ccf
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 31 deletions.
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"require": {
"php": ">=8.1",
"psr/http-message": "^1.0",
"psr/http-message": "^2.0",
"psr/http-client": "^1.0",
"psr/simple-cache": "^1.0",
"psr/http-factory": "^1.0"
Expand All @@ -28,9 +28,8 @@
},
"require-dev": {
"ext-zlib": "*",
"phpunit/phpunit": "^9.5",
"guzzlehttp/guzzle": "^7.4",
"http-interop/http-factory-guzzle": "^1.2"
"phpunit/phpunit": ">=9.5",
"guzzlehttp/guzzle": "^7.9"
},
"suggest": {
"ext-zlib": "Needed to use ZlibInflateMiddleware"
Expand Down
12 changes: 6 additions & 6 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="vendor/autoload.php"
colors="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory>src/</directory>
</include>
</coverage>
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd">
<testsuites>
<testsuite name="Default">
<directory>tests/</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>src/</directory>
</include>
</source>
</phpunit>
24 changes: 10 additions & 14 deletions tests/Middleware/FollowRedirectMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,17 @@ public function testProcess(string $method, int $statusCode, string $expectedMet
{
$client = $this->getMockBuilder(Client::class)
->onlyMethods(['sendRequest'])
->getMockForAbstractClass();
->getMock();

$client
->expects($this->exactly(2))
->method('sendRequest')
->will(
$this->onConsecutiveCalls(
new Response($statusCode, ['Location' => 'https://new-location.org/path']),
$this->returnCallback(
function (RequestInterface $request) {
return new Response(200, ['RequestMethod' => $request->getMethod()]);
}
)
)
);
->willReturnCallback(function (RequestInterface $request) use ($method, $statusCode, &$response) {
if (!$response) {
return $response = new Response($statusCode, ['Location' => 'https://new-location.org/path']);
}
return new Response(200, ['RequestMethod' => $request->getMethod()]);
});

$requestHandler = new RequestHandler($client, new FollowRedirectMiddleware(new HttpFactory()));
$response = $requestHandler->handle(new Request($method, 'https://some-location.org/path'));
Expand All @@ -43,7 +39,7 @@ function (RequestInterface $request) {
$this->assertSame($expectedMethod, $response->getHeaderLine('requestMethod'));
}

public function provideFollowRedirect(): iterable
public static function provideFollowRedirect(): iterable
{
return [
['POST', 301, 'GET'],
Expand All @@ -62,7 +58,7 @@ public function testProcessNoFollow(string $method, int $statusCode, array $head
{
$client = $this->getMockBuilder(Client::class)
->onlyMethods(['sendRequest'])
->getMockForAbstractClass();
->getMock();

$client
->expects($this->exactly(1))
Expand All @@ -74,7 +70,7 @@ public function testProcessNoFollow(string $method, int $statusCode, array $head
$this->assertSame($response, $requestHandler->handle(new Request($method, 'https://some-location.org/path')));
}

public function provideNoFollow(): iterable
public static function provideNoFollow(): iterable
{
return [
['PUT', 300, ['Location' => 'https://new-location.org/path']],
Expand Down
8 changes: 4 additions & 4 deletions tests/Middleware/RateLimitMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\HttpFactory;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Http\Factory\Guzzle\ResponseFactory;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\SimpleCache\CacheInterface;
Expand All @@ -38,7 +38,7 @@ public function testExceedsLimit(): void

$handler = new RequestHandler(
$client,
new RateLimitMiddleware(3, 10, new ResponseFactory(), $cache)
new RateLimitMiddleware(3, 10, new HttpFactory(), $cache)
);

while ($response->getStatusCode() === 202) {
Expand Down Expand Up @@ -67,7 +67,7 @@ public function testNotExceedsLimit(): void

$handler = new RequestHandler(
$client,
new RateLimitMiddleware(2, 1, new ResponseFactory(), $this->getCache())
new RateLimitMiddleware(2, 1, new HttpFactory(), $this->getCache())
);

while ($response->getStatusCode() === 202) {
Expand All @@ -94,7 +94,7 @@ public function testResetCounter(): void
$cache = $this->getCache();
$handler = new RequestHandler(
$client,
new RateLimitMiddleware(10, 4, new ResponseFactory(), $cache, 'cacheKey')
new RateLimitMiddleware(10, 4, new HttpFactory(), $cache, 'cacheKey')
);

try {
Expand Down
6 changes: 3 additions & 3 deletions tests/Middleware/ZlibInflateMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\HttpFactory;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Http\Factory\Guzzle\StreamFactory;
use PHPUnit\Framework\TestCase;

class ZlibInflateMiddlewareTest extends TestCase
Expand All @@ -27,13 +27,13 @@ public function testProcess(): void
'handler' => HandlerStack::create(
new MockHandler([
(new Response(200))
->withBody((new StreamFactory())->createStreamFromResource($handle))
->withBody((new HttpFactory())->createStreamFromResource($handle))
->withHeader('Content-Type', 'application/x-gzip-compressed'),
])
)
]);

$handler = new RequestHandler($client, new ZlibInflateMiddleware(new StreamFactory()));
$handler = new RequestHandler($client, new ZlibInflateMiddleware(new HttpFactory()));

$this->assertSame($expected, $handler->handle($request)->getBody()->getContents());
}
Expand Down

0 comments on commit db80ccf

Please sign in to comment.