exceptionIsThrownWhenUrlIsInvalid()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 0
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 GuzzleHttp\RequestOptions;
11
use Laminas\Diactoros\Response;
12
use PHPUnit\Framework\TestCase;
13
use Prophecy\Argument;
14
use Prophecy\PhpUnit\ProphecyTrait;
15
use Prophecy\Prophecy\ObjectProphecy;
16
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
17
use Shlinkio\Shlink\Core\Options\UrlShortenerOptions;
18
use Shlinkio\Shlink\Core\Util\UrlValidator;
19
20
class UrlValidatorTest extends TestCase
21
{
22
    use ProphecyTrait;
23
24
    private UrlValidator $urlValidator;
25
    private ObjectProphecy $httpClient;
26
    private UrlShortenerOptions $options;
27
28
    public function setUp(): void
29
    {
30
        $this->httpClient = $this->prophesize(ClientInterface::class);
31
        $this->options = new UrlShortenerOptions(['validate_url' => true]);
32
        $this->urlValidator = new UrlValidator($this->httpClient->reveal(), $this->options);
33
    }
34
35
    /** @test */
36
    public function exceptionIsThrownWhenUrlIsInvalid(): void
37
    {
38
        $request = $this->httpClient->request(Argument::cetera())->willThrow(ClientException::class);
39
40
        $request->shouldBeCalledOnce();
41
        $this->expectException(InvalidUrlException::class);
42
43
        $this->urlValidator->validateUrl('http://foobar.com/12345/hello?foo=bar', null);
44
    }
45
46
    /** @test */
47
    public function expectedUrlIsCalledWhenTryingToVerify(): void
48
    {
49
        $expectedUrl = 'http://foobar.com';
50
51
        $request = $this->httpClient->request(
52
            RequestMethodInterface::METHOD_GET,
53
            $expectedUrl,
54
            [
55
                RequestOptions::ALLOW_REDIRECTS => ['max' => 15],
56
                RequestOptions::IDN_CONVERSION => true,
57
            ],
58
        )->willReturn(new Response());
59
60
        $this->urlValidator->validateUrl($expectedUrl, null);
61
62
        $request->shouldHaveBeenCalledOnce();
63
    }
64
65
    /**
66
     * @test
67
     * @dataProvider provideDisabledCombinations
68
     */
69
    public function noCheckIsPerformedWhenUrlValidationIsDisabled(?bool $doValidate, bool $validateUrl): void
70
    {
71
        $request = $this->httpClient->request(Argument::cetera())->willReturn(new Response());
72
        $this->options->validateUrl = $validateUrl;
0 ignored issues
show
Bug Best Practice introduced by
The property $validateUrl is declared private in Shlinkio\Shlink\Core\Options\UrlShortenerOptions. Since you implement __set, consider adding a @property or @property-write.
Loading history...
73
74
        $this->urlValidator->validateUrl('', $doValidate);
75
76
        $request->shouldNotHaveBeenCalled();
77
    }
78
79
    public function provideDisabledCombinations(): iterable
80
    {
81
        yield 'config is disabled and no runtime option is provided' => [null, false];
82
        yield 'config is enabled but runtime option is disabled' => [false, true];
83
        yield 'both config and runtime option are disabled' => [false, false];
84
    }
85
}
86