Completed
Pull Request — develop (#584)
by Alejandro
05:15
created

UrlValidatorTest::provideAttemptThatThrows()   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
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 PHPUnit\Framework\TestCase;
12
use Prophecy\Argument;
13
use Prophecy\Prophecy\ObjectProphecy;
14
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
15
use Shlinkio\Shlink\Core\Util\UrlValidator;
16
use Zend\Diactoros\Response;
17
18
class UrlValidatorTest extends TestCase
19
{
20
    /** @var UrlValidator */
21
    private $urlValidator;
22
    /** @var ObjectProphecy */
23
    private $httpClient;
24
25
    public function setUp(): void
26
    {
27
        $this->httpClient = $this->prophesize(ClientInterface::class);
28
        $this->urlValidator = new UrlValidator($this->httpClient->reveal());
29
    }
30
31
    /** @test */
32
    public function exceptionIsThrownWhenUrlIsInvalid(): void
33
    {
34
        $request = $this->httpClient->request(Argument::cetera())->willThrow(ClientException::class);
35
36
        $request->shouldBeCalledOnce();
37
        $this->expectException(InvalidUrlException::class);
38
39
        $this->urlValidator->validateUrl('http://foobar.com/12345/hello?foo=bar');
40
    }
41
42
    /** @test */
43
    public function expectedUrlIsCalledWhenTryingToVerify(): void
44
    {
45
        $expectedUrl = 'http://foobar.com';
46
47
        $request = $this->httpClient->request(
48
            RequestMethodInterface::METHOD_GET,
49
            $expectedUrl,
50
            [RequestOptions::ALLOW_REDIRECTS => ['max' => 15]]
51
        )->willReturn(new Response());
52
53
        $this->urlValidator->validateUrl($expectedUrl);
54
55
        $request->shouldHaveBeenCalledOnce();
56
    }
57
}
58