Reduced amount of duplicated code in API tests

This commit is contained in:
Alejandro Celaya 2022-01-09 17:47:19 +01:00
parent acfc5a4676
commit 3b359cfc4f
2 changed files with 98 additions and 183 deletions

View file

@ -23,7 +23,7 @@ class ListTagsTest extends ApiTestCase
public function provideQueries(): iterable
{
yield 'admin API key without stats' => ['valid_api_key', [], [
yield 'admin API key' => ['valid_api_key', [], [
'data' => ['bar', 'baz', 'foo'],
'pagination' => [
'currentPage' => 1,
@ -43,61 +43,7 @@ class ListTagsTest extends ApiTestCase
'totalItems' => 3,
],
]];
yield 'admin API key with stats' => ['valid_api_key', ['withStats' => 'true'], [
'data' => ['bar', 'baz', 'foo'],
'stats' => [
[
'tag' => 'bar',
'shortUrlsCount' => 1,
'visitsCount' => 2,
],
[
'tag' => 'baz',
'shortUrlsCount' => 0,
'visitsCount' => 0,
],
[
'tag' => 'foo',
'shortUrlsCount' => 3,
'visitsCount' => 5,
],
],
'pagination' => [
'currentPage' => 1,
'pagesCount' => 1,
'itemsPerPage' => 3,
'itemsInCurrentPage' => 3,
'totalItems' => 3,
],
]];
yield 'admin API key with pagination and stats' => ['valid_api_key', [
'withStats' => 'true',
'page' => 1,
'itemsPerPage' => 2,
], [
'data' => ['bar', 'baz'],
'stats' => [
[
'tag' => 'bar',
'shortUrlsCount' => 1,
'visitsCount' => 2,
],
[
'tag' => 'baz',
'shortUrlsCount' => 0,
'visitsCount' => 0,
],
],
'pagination' => [
'currentPage' => 1,
'pagesCount' => 2,
'itemsPerPage' => 2,
'itemsInCurrentPage' => 2,
'totalItems' => 3,
],
]];
yield 'author API key without stats' => ['author_api_key', [], [
yield 'author API key' => ['author_api_key', [], [
'data' => ['bar', 'foo'],
'pagination' => [
'currentPage' => 1,
@ -107,30 +53,7 @@ class ListTagsTest extends ApiTestCase
'totalItems' => 2,
],
]];
yield 'author API key with stats' => ['author_api_key', ['withStats' => 'true'], [
'data' => ['bar', 'foo'],
'stats' => [
[
'tag' => 'bar',
'shortUrlsCount' => 1,
'visitsCount' => 2,
],
[
'tag' => 'foo',
'shortUrlsCount' => 2,
'visitsCount' => 5,
],
],
'pagination' => [
'currentPage' => 1,
'pagesCount' => 1,
'itemsPerPage' => 2,
'itemsInCurrentPage' => 2,
'totalItems' => 2,
],
]];
yield 'domain API key without stats' => ['domain_api_key', [], [
yield 'domain API key' => ['domain_api_key', [], [
'data' => ['foo'],
'pagination' => [
'currentPage' => 1,
@ -140,22 +63,5 @@ class ListTagsTest extends ApiTestCase
'totalItems' => 1,
],
]];
yield 'domain API key with stats' => ['domain_api_key', ['withStats' => 'true'], [
'data' => ['foo'],
'stats' => [
[
'tag' => 'foo',
'shortUrlsCount' => 1,
'visitsCount' => 0,
],
],
'pagination' => [
'currentPage' => 1,
'pagesCount' => 1,
'itemsPerPage' => 1,
'itemsInCurrentPage' => 1,
'totalItems' => 1,
],
]];
}
}

View file

@ -13,115 +13,124 @@ class TagsStatsTest extends ApiTestCase
* @test
* @dataProvider provideQueries
*/
public function expectedListOfTagsIsReturned(string $apiKey, array $query, array $expectedTags): void
{
public function expectedListOfTagsIsReturned(
string $apiKey,
array $query,
array $expectedStats,
array $expectedPagination,
): void {
$resp = $this->callApiWithKey(self::METHOD_GET, '/tags/stats', [RequestOptions::QUERY => $query], $apiKey);
$payload = $this->getJsonResponsePayload($resp);
['tags' => $tags] = $this->getJsonResponsePayload($resp);
self::assertEquals(['tags' => $expectedTags], $payload);
self::assertEquals($expectedStats, $tags['data']);
self::assertEquals($expectedPagination, $tags['pagination']);
}
/**
* @test
* @dataProvider provideQueries
*/
public function expectedListOfTagsIsReturnedForDeprecatedApproach(
string $apiKey,
array $query,
array $expectedStats,
array $expectedPagination,
): void {
$query['withStats'] = 'true';
$resp = $this->callApiWithKey(self::METHOD_GET, '/tags', [RequestOptions::QUERY => $query], $apiKey);
['tags' => $tags] = $this->getJsonResponsePayload($resp);
self::assertEquals($expectedStats, $tags['stats']);
self::assertEquals($expectedPagination, $tags['pagination']);
self::assertArrayHasKey('data', $tags);
}
public function provideQueries(): iterable
{
yield 'admin API key' => ['valid_api_key', [], [
'data' => [
[
'tag' => 'bar',
'shortUrlsCount' => 1,
'visitsCount' => 2,
],
[
'tag' => 'baz',
'shortUrlsCount' => 0,
'visitsCount' => 0,
],
[
'tag' => 'foo',
'shortUrlsCount' => 3,
'visitsCount' => 5,
],
[
'tag' => 'bar',
'shortUrlsCount' => 1,
'visitsCount' => 2,
],
'pagination' => [
'currentPage' => 1,
'pagesCount' => 1,
'itemsPerPage' => 3,
'itemsInCurrentPage' => 3,
'totalItems' => 3,
[
'tag' => 'baz',
'shortUrlsCount' => 0,
'visitsCount' => 0,
],
[
'tag' => 'foo',
'shortUrlsCount' => 3,
'visitsCount' => 5,
],
], [
'currentPage' => 1,
'pagesCount' => 1,
'itemsPerPage' => 3,
'itemsInCurrentPage' => 3,
'totalItems' => 3,
]];
yield 'admin API key with pagination' => ['valid_api_key', ['page' => 1, 'itemsPerPage' => 2], [
'data' => [
[
'tag' => 'bar',
'shortUrlsCount' => 1,
'visitsCount' => 2,
],
[
'tag' => 'baz',
'shortUrlsCount' => 0,
'visitsCount' => 0,
],
[
'tag' => 'bar',
'shortUrlsCount' => 1,
'visitsCount' => 2,
],
'pagination' => [
'currentPage' => 1,
'pagesCount' => 2,
'itemsPerPage' => 2,
'itemsInCurrentPage' => 2,
'totalItems' => 3,
[
'tag' => 'baz',
'shortUrlsCount' => 0,
'visitsCount' => 0,
],
], [
'currentPage' => 1,
'pagesCount' => 2,
'itemsPerPage' => 2,
'itemsInCurrentPage' => 2,
'totalItems' => 3,
]];
yield 'author API key' => ['author_api_key', [], [
'data' => [
[
'tag' => 'bar',
'shortUrlsCount' => 1,
'visitsCount' => 2,
],
[
'tag' => 'foo',
'shortUrlsCount' => 2,
'visitsCount' => 5,
],
[
'tag' => 'bar',
'shortUrlsCount' => 1,
'visitsCount' => 2,
],
'pagination' => [
'currentPage' => 1,
'pagesCount' => 1,
'itemsPerPage' => 2,
'itemsInCurrentPage' => 2,
'totalItems' => 2,
[
'tag' => 'foo',
'shortUrlsCount' => 2,
'visitsCount' => 5,
],
], [
'currentPage' => 1,
'pagesCount' => 1,
'itemsPerPage' => 2,
'itemsInCurrentPage' => 2,
'totalItems' => 2,
]];
yield 'author API key with pagination' => ['author_api_key', ['page' => 2, 'itemsPerPage' => 1], [
'data' => [
[
'tag' => 'foo',
'shortUrlsCount' => 2,
'visitsCount' => 5,
],
],
'pagination' => [
'currentPage' => 2,
'pagesCount' => 2,
'itemsPerPage' => 1,
'itemsInCurrentPage' => 1,
'totalItems' => 2,
[
'tag' => 'foo',
'shortUrlsCount' => 2,
'visitsCount' => 5,
],
], [
'currentPage' => 2,
'pagesCount' => 2,
'itemsPerPage' => 1,
'itemsInCurrentPage' => 1,
'totalItems' => 2,
]];
yield 'domain API key' => ['domain_api_key', [], [
'data' => [
[
'tag' => 'foo',
'shortUrlsCount' => 1,
'visitsCount' => 0,
],
],
'pagination' => [
'currentPage' => 1,
'pagesCount' => 1,
'itemsPerPage' => 1,
'itemsInCurrentPage' => 1,
'totalItems' => 1,
[
'tag' => 'foo',
'shortUrlsCount' => 1,
'visitsCount' => 0,
],
], [
'currentPage' => 1,
'pagesCount' => 1,
'itemsPerPage' => 1,
'itemsInCurrentPage' => 1,
'totalItems' => 1,
]];
}
}