Completed
Push — master ( 9ab4b9...065cdd )
by Alejandro
09:44
created

UrlShortenerTest::invalidCharSetThrowsException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
namespace ShlinkioTest\Shlink\Core\Service;
3
4
use Doctrine\Common\Cache\ArrayCache;
5
use Doctrine\Common\Cache\Cache;
6
use Doctrine\Common\Persistence\ObjectRepository;
7
use Doctrine\DBAL\Connection;
8
use Doctrine\ORM\EntityManagerInterface;
9
use Doctrine\ORM\ORMException;
10
use GuzzleHttp\ClientInterface;
11
use GuzzleHttp\Exception\ClientException;
12
use GuzzleHttp\Psr7\Request;
13
use PHPUnit_Framework_TestCase as TestCase;
14
use Prophecy\Argument;
15
use Prophecy\Prophecy\ObjectProphecy;
16
use Shlinkio\Shlink\Core\Entity\ShortUrl;
17
use Shlinkio\Shlink\Core\Service\UrlShortener;
18
use Zend\Diactoros\Uri;
19
20
class UrlShortenerTest extends TestCase
21
{
22
    /**
23
     * @var UrlShortener
24
     */
25
    protected $urlShortener;
26
    /**
27
     * @var ObjectProphecy
28
     */
29
    protected $em;
30
    /**
31
     * @var ObjectProphecy
32
     */
33
    protected $httpClient;
34
    /**
35
     * @var Cache
36
     */
37
    protected $cache;
38
39
    public function setUp()
40
    {
41
        $this->httpClient = $this->prophesize(ClientInterface::class);
42
43
        $this->em = $this->prophesize(EntityManagerInterface::class);
44
        $conn = $this->prophesize(Connection::class);
45
        $conn->isTransactionActive()->willReturn(false);
46
        $this->em->getConnection()->willReturn($conn->reveal());
47
        $this->em->flush()->willReturn(null);
48
        $this->em->commit()->willReturn(null);
49
        $this->em->beginTransaction()->willReturn(null);
50
        $this->em->persist(Argument::any())->will(function ($arguments) {
51
            /** @var ShortUrl $shortUrl */
52
            $shortUrl = $arguments[0];
53
            $shortUrl->setId(10);
54
        });
55
        $repo = $this->prophesize(ObjectRepository::class);
56
        $repo->findOneBy(Argument::any())->willReturn(null);
57
        $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
58
59
        $this->cache = new ArrayCache();
60
61
        $this->urlShortener = new UrlShortener($this->httpClient->reveal(), $this->em->reveal(), $this->cache);
62
    }
63
64
    /**
65
     * @test
66
     */
67
    public function urlIsProperlyShortened()
68
    {
69
        // 10 -> 12C1c
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
70
        $shortCode = $this->urlShortener->urlToShortCode(new Uri('http://foobar.com/12345/hello?foo=bar'));
71
        $this->assertEquals('12C1c', $shortCode);
72
    }
73
74
    /**
75
     * @test
76
     * @expectedException \Shlinkio\Shlink\Common\Exception\RuntimeException
77
     */
78
    public function exceptionIsThrownWhenOrmThrowsException()
79
    {
80
        $conn = $this->prophesize(Connection::class);
81
        $conn->isTransactionActive()->willReturn(true);
82
        $this->em->getConnection()->willReturn($conn->reveal());
83
        $this->em->rollback()->shouldBeCalledTimes(1);
84
        $this->em->close()->shouldBeCalledTimes(1);
85
86
        $this->em->flush()->willThrow(new ORMException());
87
        $this->urlShortener->urlToShortCode(new Uri('http://foobar.com/12345/hello?foo=bar'));
88
    }
89
90
    /**
91
     * @test
92
     * @expectedException \Shlinkio\Shlink\Core\Exception\InvalidUrlException
93
     */
94
    public function exceptionIsThrownWhenUrlDoesNotExist()
95
    {
96
        $this->httpClient->request(Argument::cetera())->willThrow(
97
            new ClientException('', $this->prophesize(Request::class)->reveal())
98
        );
99
        $this->urlShortener->urlToShortCode(new Uri('http://foobar.com/12345/hello?foo=bar'));
100
    }
101
102
    /**
103
     * @test
104
     */
105
    public function whenShortUrlExistsItsShortcodeIsReturned()
106
    {
107
        $shortUrl = new ShortUrl();
108
        $shortUrl->setShortCode('expected_shortcode');
109
        $repo = $this->prophesize(ObjectRepository::class);
110
        $repo->findOneBy(Argument::any())->willReturn($shortUrl);
111
        $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
112
113
        $shortCode = $this->urlShortener->urlToShortCode(new Uri('http://foobar.com/12345/hello?foo=bar'));
114
        $this->assertEquals($shortUrl->getShortCode(), $shortCode);
115
    }
116
117
    /**
118
     * @test
119
     */
120
    public function shortCodeIsProperlyParsed()
121
    {
122
        // 12C1c -> 10
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
123
        $shortCode = '12C1c';
124
        $shortUrl = new ShortUrl();
125
        $shortUrl->setShortCode($shortCode)
126
                 ->setOriginalUrl('expected_url');
127
128
        $repo = $this->prophesize(ObjectRepository::class);
129
        $repo->findOneBy(['shortCode' => $shortCode])->willReturn($shortUrl);
130
        $this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal());
131
132
        $this->assertFalse($this->cache->contains($shortCode . '_longUrl'));
133
        $url = $this->urlShortener->shortCodeToUrl($shortCode);
134
        $this->assertEquals($shortUrl->getOriginalUrl(), $url);
135
        $this->assertTrue($this->cache->contains($shortCode . '_longUrl'));
136
    }
137
138
    /**
139
     * @test
140
     * @expectedException \Shlinkio\Shlink\Core\Exception\InvalidShortCodeException
141
     */
142
    public function invalidCharSetThrowsException()
143
    {
144
        $this->urlShortener->shortCodeToUrl('&/(');
145
    }
146
147
    /**
148
     * @test
149
     */
150
    public function cachedShortCodeDoesNotHitDatabase()
151
    {
152
        $shortCode = '12C1c';
153
        $expectedUrl = 'expected_url';
154
        $this->cache->save($shortCode . '_longUrl', $expectedUrl);
155
        $this->em->getRepository(ShortUrl::class)->willReturn(null)->shouldBeCalledTimes(0);
156
157
        $url = $this->urlShortener->shortCodeToUrl($shortCode);
158
        $this->assertEquals($expectedUrl, $url);
159
    }
160
}
161