Added validateUrl optional flag for create/edit short URLs

This commit is contained in:
Alejandro Celaya
2020-09-23 19:19:17 +02:00
parent 1f78f5266a
commit d5eac3b1c3
11 changed files with 71 additions and 29 deletions

View File

@@ -104,7 +104,10 @@ class ShortUrlServiceTest extends TestCase
$this->assertEquals($shortUrlEdit->longUrl() ?? $originalLongUrl, $shortUrl->getLongUrl());
$findShortUrl->shouldHaveBeenCalled();
$flush->shouldHaveBeenCalled();
$this->urlValidator->validateUrl($shortUrlEdit->longUrl())->shouldHaveBeenCalledTimes($expectedValidateCalls);
$this->urlValidator->validateUrl(
$shortUrlEdit->longUrl(),
$shortUrlEdit->doValidateUrl(),
)->shouldHaveBeenCalledTimes($expectedValidateCalls);
}
public function provideShortUrlEdits(): iterable
@@ -123,5 +126,11 @@ class ShortUrlServiceTest extends TestCase
'longUrl' => 'modifiedLongUrl',
],
)];
yield 'long URL with validation' => [1, ShortUrlEdit::fromRawData(
[
'longUrl' => 'modifiedLongUrl',
'validateUrl' => true,
],
)];
}
}

View File

@@ -30,7 +30,7 @@ class UrlShortenerTest extends TestCase
public function setUp(): void
{
$this->urlValidator = $this->prophesize(UrlValidatorInterface::class);
$this->urlValidator->validateUrl('http://foobar.com/12345/hello?foo=bar')->will(
$this->urlValidator->validateUrl('http://foobar.com/12345/hello?foo=bar', null)->will(
function (): void {
},
);

View File

@@ -37,7 +37,7 @@ class UrlValidatorTest extends TestCase
$request->shouldBeCalledOnce();
$this->expectException(InvalidUrlException::class);
$this->urlValidator->validateUrl('http://foobar.com/12345/hello?foo=bar');
$this->urlValidator->validateUrl('http://foobar.com/12345/hello?foo=bar', null);
}
/** @test */
@@ -54,19 +54,29 @@ class UrlValidatorTest extends TestCase
],
)->willReturn(new Response());
$this->urlValidator->validateUrl($expectedUrl);
$this->urlValidator->validateUrl($expectedUrl, null);
$request->shouldHaveBeenCalledOnce();
}
/** @test */
public function noCheckIsPerformedWhenUrlValidationIsDisabled(): void
/**
* @test
* @dataProvider provideDisabledCombinations
*/
public function noCheckIsPerformedWhenUrlValidationIsDisabled(?bool $doValidate, bool $validateUrl): void
{
$request = $this->httpClient->request(Argument::cetera())->willReturn(new Response());
$this->options->validateUrl = false;
$this->options->validateUrl = $validateUrl;
$this->urlValidator->validateUrl('');
$this->urlValidator->validateUrl('', $doValidate);
$request->shouldNotHaveBeenCalled();
}
public function provideDisabledCombinations(): iterable
{
yield 'config is disabled and no runtime option is provided' => [null, false];
yield 'config is enabled but runtime option is disabled' => [false, true];
yield 'both config and runtime option are disabled' => [false, false];
}
}