Passed
Push — master ( 4437d5...b3ea29 )
by Alejandro
04:00 queued 13s
created

UrlValidatorTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 48
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A exceptionIsThrownWhenUrlIsInvalid() 0 10 1
A provideUrls() 0 4 1
A expectedUrlIsCalledInOrderToVerifyProvidedUrl() 0 12 1
A setUp() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ShlinkioTest\Shlink\Core\Util;
6
7
use Fig\Http\Message\RequestMethodInterface;
8
use GuzzleHttp\ClientInterface;
9
use GuzzleHttp\Exception\ClientException;
10
use PHPUnit\Framework\TestCase;
11
use Prophecy\Argument;
12
use Prophecy\Prophecy\ObjectProphecy;
13
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
14
use Shlinkio\Shlink\Core\Util\UrlValidator;
15
use Zend\Diactoros\Request;
16
17
class UrlValidatorTest extends TestCase
18
{
19
    /** @var UrlValidator */
20
    private $urlValidator;
21
    /** @var ObjectProphecy */
22
    private $httpClient;
23
24
    public function setUp(): void
25
    {
26
        $this->httpClient = $this->prophesize(ClientInterface::class);
27
        $this->urlValidator = new UrlValidator($this->httpClient->reveal());
28
    }
29
30
    /** @test */
31
    public function exceptionIsThrownWhenUrlIsInvalid(): void
32
    {
33
        $request = $this->httpClient->request(Argument::cetera())->willThrow(
34
            new ClientException('', $this->prophesize(Request::class)->reveal())
35
        );
36
37
        $this->expectException(InvalidUrlException::class);
38
        $request->shouldBeCalledOnce();
39
40
        $this->urlValidator->validateUrl('http://foobar.com/12345/hello?foo=bar');
41
    }
42
43
    /**
44
     * @test
45
     * @dataProvider provideUrls
46
     */
47
    public function expectedUrlIsCalledInOrderToVerifyProvidedUrl(string $providedUrl, string $expectedUrl): void
48
    {
49
        $request = $this->httpClient->request(
50
            RequestMethodInterface::METHOD_GET,
51
            $expectedUrl,
52
            Argument::cetera()
53
        )->will(function () {
54
        });
55
56
        $this->urlValidator->validateUrl($providedUrl);
57
58
        $request->shouldHaveBeenCalledOnce();
59
    }
60
61
    public function provideUrls(): iterable
62
    {
63
        yield 'regular domain' => ['http://foobar.com', 'http://foobar.com'];
64
        yield 'IDN' => ['https://cédric.laubacher.io/', 'https://xn--cdric-bsa.laubacher.io/'];
65
    }
66
}
67