Skip to content

Commit

Permalink
Merge pull request #41 from acelaya-forks/feature/tags-bot-visits
Browse files Browse the repository at this point in the history
Feature/tags bot visits
  • Loading branch information
acelaya authored Mar 23, 2023
2 parents 422a44d + 96f18b9 commit 4288294
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
18 changes: 16 additions & 2 deletions src/Tags/Model/TagWithStats.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
}
96 changes: 74 additions & 22 deletions test/Tags/TagsClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,9 +19,13 @@
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;

use function array_map;

class TagsClientTest extends TestCase
{
private TagsClient $tagsClient;
Expand Down Expand Up @@ -59,44 +64,91 @@ public function listTagsWithStatsReturnsExpectedResponse(): void
$this->assertListTags(
['/tags/stats', $this->isType('array')],
[[], [], [], [], []],
function (): array {
$iterable = $this->tagsClient->listTagsWithStats();
$result = [];
fn (): array => array_map(fn () => [], [...$this->tagsClient->listTagsWithStats()]),
);
}

foreach ($iterable as $value) {
$result[] = [];
}
#[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());
}

return $result;
},
);
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;
})],
[[], [], [], [], []],
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)]),
);
}

Expand Down
2 changes: 1 addition & 1 deletion test/Visits/VisitsClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 4288294

Please sign in to comment.