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

ShortUrlRepositoryTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 71
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
B findOneByShortCodeReturnsProperData() 0 33 2
A countListReturnsProperNumberOfResults() 0 13 2
1
<?php
2
declare(strict_types=1);
3
4
namespace ShlinkioTest\Shlink\Core\Repository;
5
6
use Doctrine\Common\Collections\ArrayCollection;
7
use Shlinkio\Shlink\Core\Entity\ShortUrl;
8
use Shlinkio\Shlink\Core\Entity\Visit;
9
use Shlinkio\Shlink\Core\Repository\ShortUrlRepository;
10
use ShlinkioTest\Shlink\Common\DbUnit\DatabaseTestCase;
11
12
class ShortUrlRepositoryTest extends DatabaseTestCase
13
{
14
    const ENTITIES_TO_EMPTY = [
15
        ShortUrl::class,
16
        Visit::class,
17
    ];
18
19
    /**
20
     * @var ShortUrlRepository
21
     */
22
    private $repo;
23
24
    public function setUp()
25
    {
26
        $this->repo = $this->getEntityManager()->getRepository(ShortUrl::class);
27
    }
28
29
    /**
30
     * @test
31
     */
32
    public function findOneByShortCodeReturnsProperData()
33
    {
34
        $foo = new ShortUrl();
35
        $foo->setOriginalUrl('foo')
36
            ->setShortCode('foo');
37
        $this->getEntityManager()->persist($foo);
38
39
        $bar = new ShortUrl();
40
        $bar->setOriginalUrl('bar')
41
            ->setShortCode('bar')
42
            ->setValidSince((new \DateTime())->add(new \DateInterval('P1M')));
43
        $this->getEntityManager()->persist($bar);
44
45
        $visits = [];
46
        for ($i = 0; $i < 3; $i++) {
47
            $visit = new Visit();
48
            $this->getEntityManager()->persist($visit);
49
            $visits[] = $visit;
50
        }
51
        $baz = new ShortUrl();
52
        $baz->setOriginalUrl('baz')
53
            ->setShortCode('baz')
54
            ->setVisits(new ArrayCollection($visits))
55
            ->setMaxVisits(3);
56
        $this->getEntityManager()->persist($baz);
57
58
        $this->getEntityManager()->flush();
59
60
        $this->assertSame($foo, $this->repo->findOneByShortCode($foo->getShortCode()));
61
        $this->assertNull($this->repo->findOneByShortCode('invalid'));
62
        $this->assertNull($this->repo->findOneByShortCode($bar->getShortCode()));
63
        $this->assertNull($this->repo->findOneByShortCode($baz->getShortCode()));
64
    }
65
66
    /**
67
     * @test
68
     */
69
    public function countListReturnsProperNumberOfResults()
70
    {
71
        $count = 5;
72
        for ($i = 0; $i < $count; $i++) {
73
            $this->getEntityManager()->persist(
74
                (new ShortUrl())->setOriginalUrl((string) $i)
75
                                ->setShortCode((string) $i)
76
            );
77
        }
78
        $this->getEntityManager()->flush();
79
80
        $this->assertEquals($count, $this->repo->countList());
81
    }
82
}
83