Completed
Push — master ( ccb9d5...d7e68d )
by Alejandro
09:04
created

VisitRepositoryTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace ShlinkioTest\Shlink\Core\Repository;
5
6
use Shlinkio\Shlink\Common\Util\DateRange;
7
use Shlinkio\Shlink\Core\Entity\ShortUrl;
8
use Shlinkio\Shlink\Core\Entity\Visit;
9
use Shlinkio\Shlink\Core\Entity\VisitLocation;
10
use Shlinkio\Shlink\Core\Repository\VisitRepository;
11
use ShlinkioTest\Shlink\Common\DbUnit\DatabaseTestCase;
12
13
class VisitRepositoryTest extends DatabaseTestCase
14
{
15
    const ENTITIES_TO_EMPTY = [
16
        VisitLocation::class,
17
        Visit::class,
18
        ShortUrl::class,
19
    ];
20
21
    /**
22
     * @var VisitRepository
23
     */
24
    private $repo;
25
26
    protected function setUp()
27
    {
28
        $this->repo = $this->getEntityManager()->getRepository(Visit::class);
29
    }
30
31
    /**
32
     * @test
33
     */
34
    public function findUnlocatedVisitsReturnsProperVisits()
35
    {
36
        for ($i = 0; $i < 6; $i++) {
37
            $visit = new Visit();
38
39
            if ($i % 2 === 0) {
40
                $location = new VisitLocation();
41
                $this->getEntityManager()->persist($location);
42
                $visit->setVisitLocation($location);
43
            }
44
45
            $this->getEntityManager()->persist($visit);
46
        }
47
        $this->getEntityManager()->flush();
48
49
        $this->assertCount(3, $this->repo->findUnlocatedVisits());
50
    }
51
52
    /**
53
     * @test
54
     */
55
    public function findVisitsByShortUrlReturnsProperData()
56
    {
57
        $shortUrl = new ShortUrl();
58
        $shortUrl->setOriginalUrl('');
59
        $this->getEntityManager()->persist($shortUrl);
60
61
        for ($i = 0; $i < 6; $i++) {
62
            $visit = new Visit();
63
            $visit->setShortUrl($shortUrl)
64
                  ->setDate(new \DateTime('2016-01-0' . ($i + 1)));
65
66
            $this->getEntityManager()->persist($visit);
67
        }
68
        $this->getEntityManager()->flush();
69
70
        $this->assertCount(0, $this->repo->findVisitsByShortUrl('invalid'));
71
        $this->assertCount(6, $this->repo->findVisitsByShortUrl($shortUrl->getId()));
72
        $this->assertCount(2, $this->repo->findVisitsByShortUrl($shortUrl->getId(), new DateRange(
73
            new \DateTime('2016-01-02'),
74
            new \DateTime('2016-01-03')
75
        )));
76
        $this->assertCount(4, $this->repo->findVisitsByShortUrl($shortUrl->getId(), new DateRange(
77
            new \DateTime('2016-01-03')
78
        )));
79
    }
80
}
81