Completed
Push — master ( a30f79...cb6756 )
by Alejandro
27s queued 13s
created

trackedIpAddressGetsObfuscated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 16
rs 9.9332
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ShlinkioTest\Shlink\Core\Service;
6
7
use Doctrine\ORM\EntityManager;
8
use Laminas\Stdlib\ArrayUtils;
9
use PHPUnit\Framework\TestCase;
10
use Prophecy\Argument;
11
use Prophecy\Prophecy\ObjectProphecy;
12
use Psr\EventDispatcher\EventDispatcherInterface;
13
use Shlinkio\Shlink\Common\Util\DateRange;
14
use Shlinkio\Shlink\Core\Entity\ShortUrl;
15
use Shlinkio\Shlink\Core\Entity\Tag;
16
use Shlinkio\Shlink\Core\Entity\Visit;
17
use Shlinkio\Shlink\Core\EventDispatcher\ShortUrlVisited;
18
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
19
use Shlinkio\Shlink\Core\Exception\TagNotFoundException;
20
use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;
21
use Shlinkio\Shlink\Core\Model\Visitor;
22
use Shlinkio\Shlink\Core\Model\VisitsParams;
23
use Shlinkio\Shlink\Core\Repository\ShortUrlRepositoryInterface;
24
use Shlinkio\Shlink\Core\Repository\TagRepository;
25
use Shlinkio\Shlink\Core\Repository\VisitRepository;
26
use Shlinkio\Shlink\Core\Service\VisitsTracker;
27
28
use function Functional\map;
29
use function range;
30
31
class VisitsTrackerTest extends TestCase
32
{
33
    private VisitsTracker $visitsTracker;
34
    private ObjectProphecy $em;
35
    private ObjectProphecy $eventDispatcher;
36
37
    public function setUp(): void
38
    {
39
        $this->em = $this->prophesize(EntityManager::class);
40
        $this->eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
41
42
        $this->visitsTracker  = new VisitsTracker($this->em->reveal(), $this->eventDispatcher->reveal(), true);
43
    }
44
45
    /** @test */
46
    public function trackPersistsVisit(): void
47
    {
48
        $shortCode = '123ABC';
49
50
        $this->em->persist(Argument::that(fn (Visit $visit) => $visit->setId('1')))->shouldBeCalledOnce();
51
        $this->em->flush()->shouldBeCalledOnce();
52
53
        $this->visitsTracker->track(new ShortUrl($shortCode), Visitor::emptyInstance());
54
55
        $this->eventDispatcher->dispatch(Argument::type(ShortUrlVisited::class))->shouldHaveBeenCalled();
56
    }
57
58
    /** @test */
59
    public function infoReturnsVisitsForCertainShortCode(): void
60
    {
61
        $shortCode = '123ABC';
62
        $repo = $this->prophesize(ShortUrlRepositoryInterface::class);
63
        $count = $repo->shortCodeIsInUse($shortCode, null)->willReturn(true);
64
        $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal())->shouldBeCalledOnce();
65
66
        $list = map(range(0, 1), fn () => new Visit(new ShortUrl(''), Visitor::emptyInstance()));
67
        $repo2 = $this->prophesize(VisitRepository::class);
68
        $repo2->findVisitsByShortCode($shortCode, null, Argument::type(DateRange::class), 1, 0)->willReturn($list);
69
        $repo2->countVisitsByShortCode($shortCode, null, Argument::type(DateRange::class))->willReturn(1);
70
        $this->em->getRepository(Visit::class)->willReturn($repo2->reveal())->shouldBeCalledOnce();
71
72
        $paginator = $this->visitsTracker->info(new ShortUrlIdentifier($shortCode), new VisitsParams());
73
74
        $this->assertEquals($list, ArrayUtils::iteratorToArray($paginator->getCurrentItems()));
75
        $count->shouldHaveBeenCalledOnce();
76
    }
77
78
    /** @test */
79
    public function throwsExceptionWhenRequestingVisitsForInvalidShortCode(): void
80
    {
81
        $shortCode = '123ABC';
82
        $repo = $this->prophesize(ShortUrlRepositoryInterface::class);
83
        $count = $repo->shortCodeIsInUse($shortCode, null)->willReturn(false);
84
        $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal())->shouldBeCalledOnce();
85
86
        $this->expectException(ShortUrlNotFoundException::class);
87
        $count->shouldBeCalledOnce();
88
89
        $this->visitsTracker->info(new ShortUrlIdentifier($shortCode), new VisitsParams());
90
    }
91
92
    /** @test */
93
    public function throwsExceptionWhenRequestingVisitsForInvalidTag(): void
94
    {
95
        $tag = 'foo';
96
        $repo = $this->prophesize(TagRepository::class);
97
        $count = $repo->count(['name' => $tag])->willReturn(0);
98
        $getRepo = $this->em->getRepository(Tag::class)->willReturn($repo->reveal());
99
100
        $this->expectException(TagNotFoundException::class);
101
        $count->shouldBeCalledOnce();
102
        $getRepo->shouldBeCalledOnce();
103
104
        $this->visitsTracker->visitsForTag($tag, new VisitsParams());
105
    }
106
107
    /** @test */
108
    public function visitsForTagAreReturnedAsExpected(): void
109
    {
110
        $tag = 'foo';
111
        $repo = $this->prophesize(TagRepository::class);
112
        $count = $repo->count(['name' => $tag])->willReturn(1);
113
        $getRepo = $this->em->getRepository(Tag::class)->willReturn($repo->reveal());
114
115
        $list = map(range(0, 1), fn () => new Visit(new ShortUrl(''), Visitor::emptyInstance()));
116
        $repo2 = $this->prophesize(VisitRepository::class);
117
        $repo2->findVisitsByTag($tag, Argument::type(DateRange::class), 1, 0)->willReturn($list);
118
        $repo2->countVisitsByTag($tag, Argument::type(DateRange::class))->willReturn(1);
119
        $this->em->getRepository(Visit::class)->willReturn($repo2->reveal())->shouldBeCalledOnce();
120
121
        $paginator = $this->visitsTracker->visitsForTag($tag, new VisitsParams());
122
123
        $this->assertEquals($list, ArrayUtils::iteratorToArray($paginator->getCurrentItems()));
124
        $count->shouldHaveBeenCalledOnce();
125
        $getRepo->shouldHaveBeenCalledOnce();
126
    }
127
}
128