From 9c6cfe63b3ff938c2e42920b663c5ac7521f9835 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 23 Mar 2023 10:11:58 +0100 Subject: [PATCH 1/3] Remove redundant code in tests --- test/Tags/TagsClientTest.php | 24 ++++-------------------- test/Visits/VisitsClientTest.php | 2 +- 2 files changed, 5 insertions(+), 21 deletions(-) diff --git a/test/Tags/TagsClientTest.php b/test/Tags/TagsClientTest.php index ec85e17..9ac93c6 100644 --- a/test/Tags/TagsClientTest.php +++ b/test/Tags/TagsClientTest.php @@ -21,6 +21,8 @@ use Shlinkio\Shlink\SDK\Tags\TagsClient; use Throwable; +use function array_map; + class TagsClientTest extends TestCase { private TagsClient $tagsClient; @@ -59,16 +61,7 @@ public function listTagsWithStatsReturnsExpectedResponse(): void $this->assertListTags( ['/tags/stats', $this->isType('array')], [[], [], [], [], []], - function (): array { - $iterable = $this->tagsClient->listTagsWithStats(); - $result = []; - - foreach ($iterable as $value) { - $result[] = []; - } - - return $result; - }, + fn (): array => array_map(fn () => [], [...$this->tagsClient->listTagsWithStats()]), ); } @@ -87,16 +80,7 @@ public function listTagsWithStatsWithFilterReturnsExpectedResponse(): void return true; })], [[], [], [], [], []], - function () use ($filter): array { - $iterable = $this->tagsClient->listTagsWithStatsWithFilter($filter); - $result = []; - - foreach ($iterable as $value) { - $result[] = []; - } - - return $result; - }, + fn (): array => array_map(fn () => [], [...$this->tagsClient->listTagsWithStatsWithFilter($filter)]), ); } diff --git a/test/Visits/VisitsClientTest.php b/test/Visits/VisitsClientTest.php index 477a67d..45dcb6f 100644 --- a/test/Visits/VisitsClientTest.php +++ b/test/Visits/VisitsClientTest.php @@ -54,7 +54,7 @@ public function getVisitsSummaryPerformsExpectedCall(array $visits, callable $as $assert($this->visitsClient->getVisitsSummary()); } - public function provideVisits(): iterable + public static function provideVisits(): iterable { yield 'legacy response' => [[ 'visitsCount' => 200, From 937ba5139db911515b94c2851edcb5462d1feb00 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 23 Mar 2023 10:28:15 +0100 Subject: [PATCH 2/3] Add support for bot and non-bot visits in tags with stats --- src/Tags/Model/TagWithStats.php | 18 +++++++- test/Tags/TagsClientTest.php | 74 +++++++++++++++++++++++++++++++-- 2 files changed, 87 insertions(+), 5 deletions(-) diff --git a/src/Tags/Model/TagWithStats.php b/src/Tags/Model/TagWithStats.php index bed4afb..0578054 100644 --- a/src/Tags/Model/TagWithStats.php +++ b/src/Tags/Model/TagWithStats.php @@ -4,17 +4,31 @@ namespace Shlinkio\Shlink\SDK\Tags\Model; +use Shlinkio\Shlink\SDK\Visits\Model\VisitsCount; + final class TagWithStats { + /** @deprecated Use $visitsSummary->total instead */ + public readonly int $visitsCount; + private function __construct( public readonly string $tag, public readonly int $shortUrlsCount, - public readonly int $visitsCount, + public readonly VisitsCount $visitsSummary, + int $visitsCount, ) { + $this->visitsCount = $visitsCount; } public static function fromArray(array $payload): self { - return new self($payload['tag'] ?? '', $payload['shortUrlsCount'] ?? 0, $payload['visitsCount'] ?? 0); + $visitsCount = $payload['visitsCount'] ?? 0; + + return new self( + tag: $payload['tag'] ?? '', + shortUrlsCount: $payload['shortUrlsCount'] ?? 0, + visitsSummary: VisitsCount::fromArrayWithFallback($payload['visitsSummary'] ?? [], $visitsCount), + visitsCount: $visitsCount, + ); } } diff --git a/test/Tags/TagsClientTest.php b/test/Tags/TagsClientTest.php index 9ac93c6..ae9e478 100644 --- a/test/Tags/TagsClientTest.php +++ b/test/Tags/TagsClientTest.php @@ -4,6 +4,7 @@ namespace ShlinkioTest\Shlink\SDK\Tags; +use PHPUnit\Framework\Assert; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\MockObject\MockObject; @@ -18,6 +19,8 @@ use Shlinkio\Shlink\SDK\Tags\Model\TagRenaming; use Shlinkio\Shlink\SDK\Tags\Model\TagsFilter; use Shlinkio\Shlink\SDK\Tags\Model\TagsListOrderField; +use Shlinkio\Shlink\SDK\Tags\Model\TagsWithStatsList; +use Shlinkio\Shlink\SDK\Tags\Model\TagWithStats; use Shlinkio\Shlink\SDK\Tags\TagsClient; use Throwable; @@ -65,16 +68,81 @@ public function listTagsWithStatsReturnsExpectedResponse(): void ); } + #[Test, DataProvider('provideTagsWithStats')] + public function listTagsWithStatsReturnsExpectedVisitsCount(array $resp, callable $assert): void + { + $this->httpClient->expects($this->once())->method('getFromShlink')->willReturn([ + 'tags' => [ + 'data' => [$resp], + ], + ]); + $assert($this->tagsClient->listTagsWithStats()); + } + + public static function provideTagsWithStats(): iterable + { + yield 'legacy response' => [[ + 'tag' => 'foo', + 'shortUrlsCount' => 1, + 'visitsCount' => 3, + ], function (TagsWithStatsList $list): void { + /** @var TagWithStats $item */ + foreach ($list as $item) { + self::assertEquals(3, $item->visitsCount); + self::assertEquals(3, $item->visitsSummary->total); + self::assertNull($item->visitsSummary->nonBots); + self::assertNull($item->visitsSummary->bots); + break; + } + }]; + yield 'current response' => [[ + 'tag' => 'foo', + 'shortUrlsCount' => 1, + 'visitsCount' => 3, + 'visitsSummary' => [ + 'total' => 3, + 'nonBots' => 2, + 'bots' => 1, + ], + ], function (TagsWithStatsList $list): void { + /** @var TagWithStats $item */ + foreach ($list as $item) { + self::assertEquals(3, $item->visitsCount); + self::assertEquals(3, $item->visitsSummary->total); + self::assertEquals(2, $item->visitsSummary->nonBots); + self::assertEquals(1, $item->visitsSummary->bots); + break; + } + }]; + yield 'future response' => [[ + 'tag' => 'foo', + 'shortUrlsCount' => 1, + 'visitsSummary' => [ + 'total' => 3, + 'nonBots' => 2, + 'bots' => 1, + ], + ], function (TagsWithStatsList $list): void { + /** @var TagWithStats $item */ + foreach ($list as $item) { + self::assertEquals(0, $item->visitsCount); + self::assertEquals(3, $item->visitsSummary->total); + self::assertEquals(2, $item->visitsSummary->nonBots); + self::assertEquals(1, $item->visitsSummary->bots); + break; + } + }]; + } + #[Test] public function listTagsWithStatsWithFilterReturnsExpectedResponse(): void { $filter = TagsFilter::create()->searchingBy('foo')->orderingAscBy(TagsListOrderField::TAG); - $test = $this; $this->assertListTags( - ['/tags/stats', $this->callback(function (array $arg) use ($filter, $test) { + ['/tags/stats', $this->callback(function (array $arg) use ($filter) { $filterArray = $filter->toArray(); foreach ($filterArray as $key => $expectedValue) { - $test->assertEquals($expectedValue, $arg[$key]); + Assert::assertEquals($expectedValue, $arg[$key]); } return true; From 96f18b9c678e95385bb79c2bc21a888fc3adc4b2 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 23 Mar 2023 10:28:48 +0100 Subject: [PATCH 3/3] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 62ff5b9..d22df0a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this ## [Unreleased] ### Added * [#38](https://github.com/shlinkio/shlink-php-sdk/issues/38) Add support for bot and non-bot visits in summary. +* [#39](https://github.com/shlinkio/shlink-php-sdk/issues/39) Add support for bot and non-bot visits in tags with stats. ### Changed * Replace references to `doma.in` by `s.test`.