Skip to content

Commit

Permalink
Bump phpunit to next major and fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
christeredvartsen committed Mar 28, 2023
1 parent d59769e commit fc39eda
Show file tree
Hide file tree
Showing 10 changed files with 748 additions and 781 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ test.php
docs/_build
.vscode
.php-cs-fixer.cache
.phpunit.result.cache
.phpunit.cache
.psalm
6 changes: 2 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,13 @@
},
"require-dev": {
"imbo/imbo-coding-standard": "^2.0",
"phpunit/phpunit": "^9.5",
"phpunit/phpunit": "^10.0",
"psalm/plugin-phpunit": "^0.18.4",
"symfony/var-dumper": "^6.2",
"vimeo/psalm": "^5.5"
},
"scripts": {
"docs": [
"cd docs; make html"
],
"docs": "cd docs; make html",
"test": "vendor/bin/phpunit --verbose",
"test:coverage": "vendor/bin/phpunit --verbose --coverage-html build/coverage --coverage-text",
"sa": "vendor/bin/psalm",
Expand Down
807 changes: 386 additions & 421 deletions composer.lock

Large diffs are not rendered by default.

13 changes: 2 additions & 11 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
<?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"
bootstrap="vendor/autoload.php"
executionOrder="depends,defects"
forceCoversAnnotation="true"
beStrictAboutCoversAnnotation="false"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
verbose="true">
<coverage processUncoveredFiles="false">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" bootstrap="vendor/autoload.php" executionOrder="depends,defects" beStrictAboutOutputDuringTests="true" cacheDirectory=".phpunit.cache" requireCoverageMetadata="true" beStrictAboutCoverageMetadata="false">
<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>

<testsuites>
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
Expand Down
2 changes: 2 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
findUnusedCode="false"
findUnusedBaselineEntry="true"
>
<projectFiles>
<directory name="src"/>
Expand Down
283 changes: 147 additions & 136 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,68 +33,6 @@ protected function setUp(): void
$this->historyContainer = [];
}

/**
* @param array<int,ResponseInterface> $responses
* @return GuzzleHttpClient
*/
private function getMockGuzzleHttpClient(array $responses): GuzzleHttpClient
{
$handler = HandlerStack::create(new MockHandler($responses));
$handler->push(Middleware::history($this->historyContainer));
return new GuzzleHttpClient(['handler' => $handler]);
}

/**
* @param array<int,ResponseInterface> $responses
*/
private function getClient(array $responses = []): Client
{
return new Client(
$this->imboUrl,
$this->user,
$this->publicKey,
$this->privateKey,
$this->getMockGuzzleHttpClient($responses),
);
}

private function getPreviousRequest(): Request
{
return $this->getPreviousTransaction()['request'];
}

/**
* @return array<int,Request>
*/
private function getPreviousRequests(int $num): array
{
return array_map(
fn (array $transaction): Request => $transaction['request'],
$this->getPreviousTransactions($num),
);
}

/**
* @return array{request:Request,response:Response}
*/
private function getPreviousTransaction(): array
{
return $this->getPreviousTransactions(1)[0];
}

/**
* @return array<int,array{request:Request,response:Response}>
*/
private function getPreviousTransactions(int $num): array
{
if ($num > count($this->historyContainer)) {
$this->fail('Not enough transactions in the Guzzle history');
}

/** @var array<int,array{request:Request,response:Response}> */
return array_slice($this->historyContainer, -$num);
}

/**
* @covers ::getServerStatus
* @covers ::getUrlForPath
Expand Down Expand Up @@ -149,24 +87,6 @@ public function testGetUserInfo(): void
$this->assertInstanceOf(AccessTokenUrl::class, $uri);
}

/**
* @return array<string,array{query:?ImagesQuery,expectedQueryString:string}>
*/
public function getImagesQuery(): array
{
return [
'no query' => [
'query' => null,
'expectedQueryString' => 'page=1&limit=20&metadata=0',
],

'custom query' => [
'query' => (new ImagesQuery())->withLimit(10)->withIds(['id1', 'id2']),
'expectedQueryString' => 'page=1&limit=10&metadata=0&ids%5B0%5D=id1&ids%5B1%5D=id2',
],
];
}

/**
* @dataProvider getImagesQuery
* @covers ::getImages
Expand Down Expand Up @@ -263,8 +183,7 @@ public function testAddImageFromUrlThrowsExceptionWhenUnableToFetchImage(): void
}

/**
* @testWith ["http://example.com/image.jpg"]
* ["https://example.com/image.jpg"]
* @dataProvider getUrlsForAddImage
* @covers ::addImage
*/
public function testGenericAddImageWithUrl(string $url): void
Expand Down Expand Up @@ -385,65 +304,14 @@ public function testDeleteMetadata(): void
}

/**
* @return array<int,array{serverUrls:array<string>|string,imageIdentifier:string,expectedHost:string}>
*/
public function getHostsForImageUrl(): array
{
$serverUrls = [
'https://imbo1',
'https://imbo2',
'https://imbo3',
'https://imbo4',
'https://imbo5',
];

return [
[
'serverUrls' => 'https://imbo',
'imageIdentifier' => 'id-1',
'expectedHost' => 'imbo',
],
[
'serverUrls' => $serverUrls,
'imageIdentifier' => 'id-1',
'expectedHost' => 'imbo5',
],
[
'serverUrls' => $serverUrls,
'imageIdentifier' => 'id-2',
'expectedHost' => 'imbo1',
],
[
'serverUrls' => $serverUrls,
'imageIdentifier' => 'id-3',
'expectedHost' => 'imbo2',
],
[
'serverUrls' => $serverUrls,
'imageIdentifier' => 'id-4',
'expectedHost' => 'imbo3',
],
[
'serverUrls' => $serverUrls,
'imageIdentifier' => 'id-5',
'expectedHost' => 'imbo4',
],
[
'serverUrls' => $serverUrls,
'imageIdentifier' => 'id-6',
'expectedHost' => 'imbo5',
],
];
}

/**
* @param array<string>|string $serverUrls
* @dataProvider getHostsForImageUrl
* @covers ::__construct
* @covers ::getImageUrl
* @covers ::getHostForImageIdentifier
*
* @param array<string>|string $serverUrls
*/
public function testGetImageUrl($serverUrls, string $imageIdentifier, string $expectedHost): void
public function testGetImageUrl(array|string $serverUrls, string $imageIdentifier, string $expectedHost): void
{
$url = (new Client($serverUrls, 'user', 'pub', 'priv'))->getImageUrl($imageIdentifier);
$this->assertSame('/users/user/images/' . $imageIdentifier, $url->getPath());
Expand Down Expand Up @@ -1035,4 +903,147 @@ public function testDeleteAccessControlRule(): void
$this->assertSame('DELETE', $request->getMethod());
$this->assertSame('/keys/public/access/id-1.json', $request->getUri()->getPath());
}

/**
* @return array<string,array{query:?ImagesQuery,expectedQueryString:string}>
*/
public static function getImagesQuery(): array
{
return [
'no query' => [
'query' => null,
'expectedQueryString' => 'page=1&limit=20&metadata=0',
],

'custom query' => [
'query' => (new ImagesQuery())->withLimit(10)->withIds(['id1', 'id2']),
'expectedQueryString' => 'page=1&limit=10&metadata=0&ids%5B0%5D=id1&ids%5B1%5D=id2',
],
];
}

/**
* @return array<int,array{serverUrls:array<string>|string,imageIdentifier:string,expectedHost:string}>
*/
public static function getHostsForImageUrl(): array
{
$serverUrls = [
'https://imbo1',
'https://imbo2',
'https://imbo3',
'https://imbo4',
'https://imbo5',
];

return [
[
'serverUrls' => 'https://imbo',
'imageIdentifier' => 'id-1',
'expectedHost' => 'imbo',
],
[
'serverUrls' => $serverUrls,
'imageIdentifier' => 'id-1',
'expectedHost' => 'imbo5',
],
[
'serverUrls' => $serverUrls,
'imageIdentifier' => 'id-2',
'expectedHost' => 'imbo1',
],
[
'serverUrls' => $serverUrls,
'imageIdentifier' => 'id-3',
'expectedHost' => 'imbo2',
],
[
'serverUrls' => $serverUrls,
'imageIdentifier' => 'id-4',
'expectedHost' => 'imbo3',
],
[
'serverUrls' => $serverUrls,
'imageIdentifier' => 'id-5',
'expectedHost' => 'imbo4',
],
[
'serverUrls' => $serverUrls,
'imageIdentifier' => 'id-6',
'expectedHost' => 'imbo5',
],
];
}

/**
* @return array<array{url:string}>
*/
public static function getUrlsForAddImage(): array
{
return [
['url' => 'http://example.com/image.jpg'],
['url' => 'https://example.com/image.jpg'],
];
}

/**
* @param array<int,ResponseInterface> $responses
* @return GuzzleHttpClient
*/
private function getMockGuzzleHttpClient(array $responses): GuzzleHttpClient
{
$handler = HandlerStack::create(new MockHandler($responses));
$handler->push(Middleware::history($this->historyContainer));
return new GuzzleHttpClient(['handler' => $handler]);
}

/**
* @param array<int,ResponseInterface> $responses
*/
private function getClient(array $responses = []): Client
{
return new Client(
$this->imboUrl,
$this->user,
$this->publicKey,
$this->privateKey,
$this->getMockGuzzleHttpClient($responses),
);
}

private function getPreviousRequest(): Request
{
return $this->getPreviousTransaction()['request'];
}

/**
* @return array<int,Request>
*/
private function getPreviousRequests(int $num): array
{
return array_map(
fn (array $transaction): Request => $transaction['request'],
$this->getPreviousTransactions($num),
);
}

/**
* @return array{request:Request,response:Response}
*/
private function getPreviousTransaction(): array
{
return $this->getPreviousTransactions(1)[0];
}

/**
* @return array<int,array{request:Request,response:Response}>
*/
private function getPreviousTransactions(int $num): array
{
if ($num > count($this->historyContainer)) {
$this->fail('Not enough transactions in the Guzzle history');
}

/** @var array<int,array{request:Request,response:Response}> */
return array_slice($this->historyContainer, -$num);
}
}

0 comments on commit fc39eda

Please sign in to comment.