Skip to content
Merged
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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
}
],
"require": {
"php": "~8.2.0 || ~8.3.0",
"php": "~8.2.0 || ~8.3.0 || ~8.4.0",
"ext-json": "*",
"azjezz/psl": "^3.0",
"azjezz/psl": "^3.1",
"cardinalby/content-disposition": "^1.1",
"league/uri": "^7.3",
"php-http/client-common": "^2.7",
Expand Down
6 changes: 3 additions & 3 deletions phive.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive">
<phar name="psalm" version="^5.15.0" installed="5.25.0" location="./tools/psalm.phar" copy="true"/>
<phar name="phpunit" version="^9.6.13" installed="9.6.20" location="./tools/phpunit.phar" copy="true"/>
<phar name="php-cs-fixer" version="^3.30.0" installed="3.64.0" location="./tools/php-cs-fixer.phar" copy="true"/>
<phar name="psalm" version="^6.3.0" installed="6.3.0" location="./tools/psalm.phar" copy="true"/>
<phar name="phpunit" version="^11.5.6" installed="11.5.6" location="./tools/phpunit.phar" copy="true"/>
<phar name="php-cs-fixer" version="^3.68.5" installed="3.68.5" location="./tools/php-cs-fixer.phar" copy="true"/>
</phive>
14 changes: 7 additions & 7 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.5/phpunit.xsd"
bootstrap="tests/bootstrap.php"
executionOrder="depends,defects"
forceCoversAnnotation="false"
beStrictAboutCoversAnnotation="false"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
failOnRisky="true"
failOnWarning="true"
verbose="true">
>
<testsuites>
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>

<coverage processUncoveredFiles="true">
<source>
<include>
<directory suffix=".php">src</directory>
<directory>./src</directory>
</include>
</source>

<coverage>
<report>
<clover outputFile="coverage/clover.xml" />
<html outputDirectory="coverage/report" lowUpperBound="99" highLowerBound="99" />
Expand Down
8 changes: 4 additions & 4 deletions src/Test/UseHttpFactories.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@

trait UseHttpFactories
{
private function createRequest(string $method, string $uri): RequestInterface
private static function createRequest(string $method, string $uri): RequestInterface
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

YES ! :) I've bumped against this as well last week.

{
return Psr17FactoryDiscovery::findRequestFactory()->createRequest($method, $uri);
}

private function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface
private static function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface
{
return Psr17FactoryDiscovery::findResponseFactory()->createResponse($code, $reasonPhrase);
}

private function createStream(string $content): StreamInterface
private static function createStream(string $content): StreamInterface
{
return Psr17FactoryDiscovery::findStreamFactory()->createStream($content);
}

private function createEmptyHttpClientException(string $message): ClientExceptionInterface&Exception
private static function createEmptyHttpClientException(string $message): ClientExceptionInterface&Exception
{
return new class($message) extends RuntimeException implements ClientExceptionInterface, Exception {
};
Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/Client/Factory/FactoriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function it_can_use_http_factories(string $factoryName, callable $factory
self::assertSame(['success' => true], Json\decode($response->getBody()->__toString(), true));
}

public function provideFactories()
public static function provideFactories()
{
yield 'autodiscover' => [
'AutoDiscoveredClientFactory',
Expand Down
10 changes: 5 additions & 5 deletions tests/Unit/Encoding/Binary/BinaryFileDecoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ public function it_can_decode_binary_files(
self::assertSame($actual->extension(), $expected->extension());
}

public function provideCases()
public static function provideCases(): iterable
{
$defaultDecoder = BinaryFileDecoder::createWithAutodiscoveredPsrFactories();

yield 'from-empty-response' => [
$defaultDecoder,
$response = $this->createResponse(),
$response = self::createResponse(),
new BinaryFile(
$response->getBody(), 0, null, null, null, md5('')
),
];
yield 'from-full-response' => [
$defaultDecoder,
$response = $this->createResponse()
->withBody($this->createStream('12345'))
$response = self::createResponse()
->withBody(self::createStream('12345'))
->withHeader('Content-Type', 'image/jpeg')
->withHeader('Content-Disposition', 'inline; filename="hello.jpg"'),
new BinaryFile(
Expand All @@ -62,7 +62,7 @@ public function provideCases()
fn () => 'jpg',
fn () => 'md5',
),
$response = $this->createResponse(),
$response = self::createResponse(),
new BinaryFile(
$response->getBody(), 5, 'image/jpeg', 'hello.jpg', 'jpg', 'md5'
),
Expand Down
12 changes: 6 additions & 6 deletions tests/Unit/Encoding/Binary/Extractor/ExtensionExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,30 @@ public function it_can_extract_extension(ResponseInterface $response, ?string $e
self::assertSame($actual, $expected);
}

public function provideCases()
public static function provideCases(): iterable
{
yield 'from-valid-content-type' => [
$this->createResponse()
self::createResponse()
->withHeader('Content-Type', 'image/jpeg'),
'jpg',
];
yield 'from-invalid-content-type' => [
$this->createResponse()
self::createResponse()
->withHeader('Content-Type', ['unknown/unkown']),
null,
];
yield 'filename-with-extension' => [
$this->createResponse()
self::createResponse()
->withHeader('Content-Disposition', 'attachment; filename="hello.jpg"'),
'jpg',
];
yield 'filename-without-extension-mime-type' => [
$this->createResponse()
self::createResponse()
->withHeader('Content-Disposition', 'attachment; filename="hello"'),
null,
];
yield 'none' => [
$this->createResponse(),
self::createResponse(),
null,
];
}
Expand Down
12 changes: 6 additions & 6 deletions tests/Unit/Encoding/Binary/Extractor/FilenameExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,33 @@ public function it_can_extract_filenames(ResponseInterface $response, ?string $e
self::assertSame($actual, $expected);
}

public function provideCases()
public static function provideCases(): iterable
{
yield 'single-content-disposition' => [
$this->createResponse()
self::createResponse()
->withHeader('Content-Disposition', 'inline; filename=hello.jpg'),
'hello.jpg',
];
yield 'multiple-content-disposition' => [
$this->createResponse()
self::createResponse()
->withHeader('Content-Disposition', [
'inline; filename=hello.jpg',
'inline; filename=goodbye.jpg',
]),
'hello.jpg',
];
yield 'filename-ext-content-disposition' => [
$this->createResponse()
self::createResponse()
->withHeader('Content-Disposition', 'attachment; filename="notthis.jpg"; filename*=UTF-8\'\'hello.jpg'),
'hello.jpg',
];
yield 'invalid-disposition' => [
$this->createResponse()
self::createResponse()
->withHeader('Content-Disposition', 'qsdfqsdfqsdf'),
null,
];
yield 'none' => [
$this->createResponse(),
self::createResponse(),
null,
];
}
Expand Down
12 changes: 6 additions & 6 deletions tests/Unit/Encoding/Binary/Extractor/HashExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,25 @@ public function it_can_extract_hash(ResponseInterface $response, string $expecte
self::assertSame($endPosition, $response->getBody()->tell());
}

public function provideCases()
public static function provideCases(): iterable
{
yield 'from-empty-stream-size' => [
$this->createResponse(),
self::createResponse(),
hash('', Algorithm::Md5),
];

yield 'from-stream-size' => [
$this->createResponse()
self::createResponse()
->withBody(
$this->createStream('12345')
self::createStream('12345')
),
hash('12345', Algorithm::Md5),
];

$stream = $this->createStream('12345');
$stream = self::createStream('12345');
$stream->seek(3);
yield 'from-partially-read-stream' => [
$this->createResponse()
self::createResponse()
->withBody($stream),
hash('12345', Algorithm::Md5),
3,
Expand Down
14 changes: 7 additions & 7 deletions tests/Unit/Encoding/Binary/Extractor/MimeTypeExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,38 @@ public function it_can_extract_mime_type(ResponseInterface $response, ?string $e
self::assertSame($actual, $expected);
}

public function provideCases()
public static function provideCases(): iterable
{
yield 'single-content-type' => [
$this->createResponse()
self::createResponse()
->withHeader('Content-Type', 'image/jpeg'),
'image/jpeg',
];
yield 'multiple-content-type' => [
$this->createResponse()
self::createResponse()
->withHeader('Content-Type', [
'image/jpeg',
'image/png',
]),
'image/jpeg',
];
yield 'filename-with-extension-mime-type' => [
$this->createResponse()
self::createResponse()
->withHeader('Content-Disposition', 'attachment; filename="hello.jpg"'),
'image/jpeg',
];
yield 'filename-without-extension-mime-type' => [
$this->createResponse()
self::createResponse()
->withHeader('Content-Disposition', 'attachment; filename="hello"'),
null,
];
yield 'filename-with-unknown-extension-mime-type' => [
$this->createResponse()
self::createResponse()
->withHeader('Content-Disposition', 'attachment; filename="hello.thisextensiondoesnotexist";'),
null,
];
yield 'none' => [
$this->createResponse(),
self::createResponse(),
null,
];
}
Expand Down
42 changes: 25 additions & 17 deletions tests/Unit/Encoding/Binary/Extractor/SizeExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Phpro\HttpTools\Encoding\Binary\Extractor\SizeExtractor;
use Phpro\HttpTools\Test\UseHttpFactories;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
Expand All @@ -17,57 +18,64 @@ final class SizeExtractorTest extends TestCase
/**
* @test
*
* @param callable<ResponseInterface> $response
*
* @dataProvider provideCases
*/
public function it_can_extract_size(ResponseInterface $response, ?int $expected): void
public function it_can_extract_size(callable $response, ?int $expected): void
{
$extractor = new SizeExtractor();
$actual = $extractor($response);
$actual = $extractor($response($this));

self::assertSame($actual, $expected);
}

public function provideCases()
public static function provideCases()
{
$notSizeableStream = $this->createMock(StreamInterface::class);
$notSizeableStream->method('getSize')->willReturn(null);

yield 'from-empty-stream-size' => [
$this->createResponse(),
static fn (self $testCase) => self::createResponse(),
0,
];

yield 'from-stream-size' => [
$this->createResponse()
->withBody($this->createStream('12345')),
static fn (self $testCase) => self::createResponse()
->withBody(self::createStream('12345')),
5,
];

yield 'from-single-content-length' => [
$this->createResponse()
->withBody($notSizeableStream)
static fn (self $testCase) => self::createResponse()
->withBody($testCase->notSizeableStreamMock())
->withHeader('Content-Length', '500'),
500,
];
yield 'from-multiple-content-length' => [
$this->createResponse()
->withBody($notSizeableStream)
static fn (self $testCase) => self::createResponse()
->withBody($testCase->notSizeableStreamMock())
->withHeader('Content-Length', [
'500',
'600',
]),
500,
];
yield 'from-invalid-content-length' => [
$this->createResponse()
->withBody($notSizeableStream)
static fn (self $testCase) => self::createResponse()
->withBody($testCase->notSizeableStreamMock())
->withHeader('Content-Length', 'thisisnotanint'),
null,
];
yield 'from-no-info-whatsoever' => [
$this->createResponse()
->withBody($notSizeableStream),
static fn (self $testCase) => self::createResponse()
->withBody($testCase->notSizeableStreamMock()),
null,
];
}

private function notSizeableStreamMock(): MockObject
{
$notSizeableStream = $this->createMock(StreamInterface::class);
$notSizeableStream->method('getSize')->willReturn(null);

return $notSizeableStream;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function it_can_remove_sensitive_keys_from_response_with_request_context_
self::assertSame($this->formatHeaders($expected), $formatted);
}

public function provideJsonExpectations(): iterable
public static function provideJsonExpectations(): iterable
{
yield 'sample1' => [
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function it_can_remove_sensitive_json_keys_from_response_with_request_con
self::assertSame($expected, Json\decode($formatted, true));
}

public function provideJsonExpectations()
public static function provideJsonExpectations(): iterable
{
yield 'sample1' => [
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function it_can_format_a_response_with_request_context_if_base_method_doe
);
}

public function provideJsonExpectations(): iterable
public static function provideJsonExpectations(): iterable
{
yield 'regular' => [
'https://testapi.com/api/v1/products?query=string',
Expand Down
Loading
Loading