Completed
Push — master ( 1de050...162d05 )
by Alejandro
46:23 queued 09:21
created

trackedIpAddressGetsObfuscated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace ShlinkioTest\Shlink\Core\Service;
5
6
use Doctrine\ORM\EntityManager;
7
use Doctrine\ORM\EntityRepository;
8
use PHPUnit\Framework\TestCase;
9
use Prophecy\Argument;
10
use Prophecy\Prophecy\ObjectProphecy;
11
use Shlinkio\Shlink\Core\Entity\ShortUrl;
12
use Shlinkio\Shlink\Core\Entity\Visit;
13
use Shlinkio\Shlink\Core\Model\Visitor;
14
use Shlinkio\Shlink\Core\Repository\VisitRepository;
15
use Shlinkio\Shlink\Core\Service\VisitsTracker;
16
17
class VisitsTrackerTest extends TestCase
18
{
19
    /**
20
     * @var VisitsTracker
21
     */
22
    protected $visitsTracker;
23
    /**
24
     * @var ObjectProphecy
25
     */
26
    protected $em;
27
28
    public function setUp()
29
    {
30
        $this->em = $this->prophesize(EntityManager::class);
31
        $this->visitsTracker  = new VisitsTracker($this->em->reveal());
32
    }
33
34
    /**
35
     * @test
36
     */
37
    public function trackPersistsVisit()
38
    {
39
        $shortCode = '123ABC';
40
        $repo = $this->prophesize(EntityRepository::class);
41
        $repo->findOneBy(['shortCode' => $shortCode])->willReturn(new ShortUrl());
42
43
        $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal())->shouldBeCalledTimes(1);
44
        $this->em->persist(Argument::any())->shouldBeCalledTimes(1);
45
        $this->em->flush(Argument::type(Visit::class))->shouldBeCalledTimes(1);
46
47
        $this->visitsTracker->track($shortCode, Visitor::emptyInstance());
48
    }
49
50
    /**
51
     * @test
52
     */
53
    public function trackedIpAddressGetsObfuscated()
54
    {
55
        $shortCode = '123ABC';
56
        $test = $this;
57
        $repo = $this->prophesize(EntityRepository::class);
58
        $repo->findOneBy(['shortCode' => $shortCode])->willReturn(new ShortUrl());
59
60
        $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal())->shouldBeCalledTimes(1);
61
        $this->em->persist(Argument::any())->will(function ($args) use ($test) {
62
            /** @var Visit $visit */
63
            $visit = $args[0];
64
            $test->assertEquals('4.3.2.0', $visit->getRemoteAddr());
65
        })->shouldBeCalledTimes(1);
66
        $this->em->flush(Argument::type(Visit::class))->shouldBeCalledTimes(1);
67
68
        $this->visitsTracker->track($shortCode, new Visitor('', '', '4.3.2.1'));
69
    }
70
71
    /**
72
     * @test
73
     */
74
    public function infoReturnsVisistForCertainShortCode()
75
    {
76
        $shortCode = '123ABC';
77
        $shortUrl = (new ShortUrl())->setOriginalUrl('http://domain.com/foo/bar');
0 ignored issues
show
Deprecated Code introduced by
The method Shlinkio\Shlink\Core\Ent...rtUrl::setOriginalUrl() has been deprecated with message: Use setLongUrl() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
78
        $repo = $this->prophesize(EntityRepository::class);
79
        $repo->findOneBy(['shortCode' => $shortCode])->willReturn($shortUrl);
80
        $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal())->shouldBeCalledTimes(1);
81
82
        $list = [
83
            new Visit(),
84
            new Visit(),
85
        ];
86
        $repo2 = $this->prophesize(VisitRepository::class);
87
        $repo2->findVisitsByShortUrl($shortUrl, null)->willReturn($list);
88
        $this->em->getRepository(Visit::class)->willReturn($repo2->reveal())->shouldBeCalledTimes(1);
89
90
        $this->assertEquals($list, $this->visitsTracker->info($shortCode));
91
    }
92
}
93